From 2a9875b22080da91a56a02211bd2ffaed5dfcb4a Mon Sep 17 00:00:00 2001 From: xenolf Date: Sun, 6 Dec 2015 22:35:52 +0100 Subject: [PATCH] Add a way for cronjobs to automatically renew certificates. --- cli.go | 7 +++++++ cli_handlers.go | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cli.go b/cli.go index 1074989f..7af67ccc 100644 --- a/cli.go +++ b/cli.go @@ -47,6 +47,13 @@ func main() { Name: "renew", Usage: "Renew a certificate", Action: renew, + Flags: []cli.Flag{ + cli.IntFlag{ + Name: "days", + Value: 0, + Usage: "The number of days left on a certificate to renew it.", + }, + }, }, } diff --git a/cli_handlers.go b/cli_handlers.go index dafa25c9..a129fc68 100644 --- a/cli_handlers.go +++ b/cli_handlers.go @@ -7,6 +7,7 @@ import ( "os" "path" "strings" + "time" "github.com/codegangsta/cli" "github.com/xenolf/lego/acme" @@ -181,6 +182,17 @@ func renew(c *cli.Context) { return } + if c.IsSet("days") { + expTime, err := acme.GetPEMCertExpiration(certBytes) + if err != nil { + logger().Printf("Could not get Certification expiration for domain %s", domain) + } + + if int(expTime.Sub(time.Now()).Hours() / 24.0) <= c.Int("days") { + continue; + } + } + keyBytes, err := ioutil.ReadFile(privPath) if err != nil { logger().Printf("Error while loading the private key for domain %s\n\t%v", domain, err)