Skip to content

Commit

Permalink
Adds support for run once mode
Browse files Browse the repository at this point in the history
  • Loading branch information
janeczku committed May 14, 2017
1 parent e44c644 commit 0e36948
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
37 changes: 27 additions & 10 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import (
)

const (
CERT_DESCRIPTION = "Created by Let's Encrypt Certificate Manager"
ISSUER_PRODUCTION = "Let's Encrypt"
ISSUER_STAGING = "fake CA"
CERT_DESCRIPTION = "Created by Let's Encrypt Certificate Manager"
ISSUER_PRODUCTION = "Let's Encrypt"
ISSUER_STAGING = "fake CA"
RENEWAL_PERIOD_DAYS = 20
)

type Context struct {
Acme *letsencrypt.Client
Rancher *rancher.Client

CertificateName string
Domains []string
RenewalTime int
CertificateName string
Domains []string
RenewalDayTime int
RenewalPeriodDays int
RunOnce bool

ExpiryDate time.Time
RancherCertId string
Expand All @@ -46,9 +49,23 @@ func (c *Context) InitContext() {
domainParam := getEnvOption("DOMAINS", true)
keyTypeParam := getEnvOption("PUBLIC_KEY_TYPE", true)
certNameParam := getEnvOption("CERT_NAME", true)
timeParam := getEnvOption("RENEWAL_TIME", true)
dayTimeParam := getEnvOption("RENEWAL_TIME", true)
providerParam := getEnvOption("PROVIDER", true)
resolversParam := getEnvOption("DNS_RESOLVERS", false)
renewalDays := getEnvOption("RENEWAL_PERIOD_DAYS", false)
runOnce := getEnvOption("RUN_ONCE", false)

if b, err := strconv.ParseBool(runOnce); err == nil {
c.RunOnce = b
} else {
c.RunOnce = false
}

if i, err := strconv.Atoi(renewalDays); err == nil {
c.RenewalPeriodDays = i
} else {
c.RenewalPeriodDays = RENEWAL_PERIOD_DAYS
}

if eulaParam != "Yes" {
logrus.Fatalf("Terms of service were not accepted")
Expand All @@ -70,9 +87,9 @@ func (c *Context) InitContext() {
}

c.CertificateName = certNameParam
c.RenewalTime, err = strconv.Atoi(timeParam)
if err != nil || c.RenewalTime < 0 || c.RenewalTime > 23 {
logrus.Fatalf("Invalid value for RENEWAL_TIME: %s", timeParam)
c.RenewalDayTime, err = strconv.Atoi(dayTimeParam)
if err != nil || c.RenewalDayTime < 0 || c.RenewalDayTime > 23 {
logrus.Fatalf("Invalid value for RENEWAL_TIME: %s", dayTimeParam)
}

apiVersion := letsencrypt.ApiVersion(apiVerParam)
Expand Down
22 changes: 16 additions & 6 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ import (
"github.com/Sirupsen/logrus"
)

const (
RENEW_BEFORE_DAYS = 20
)

func (c *Context) Run() {
c.startup()
if c.RunOnce {
// Renew certificate if it's about to expire
if time.Now().UTC().After(c.getRenewalDate()) {
c.renew()
} else {
logrus.Infof("Certificate %s expires on %s", c.CertificateName,
c.ExpiryDate.UTC().Format(time.UnixDate))
}
return
}

for {
<-c.timer()
c.renew()
Expand Down Expand Up @@ -142,7 +149,10 @@ func (c *Context) timer() <-chan time.Time {
}

func (c *Context) getRenewalDate() time.Time {
date := c.ExpiryDate.AddDate(0, 0, -RENEW_BEFORE_DAYS)
if c.ExpiryDate.IsZero() {
logrus.Fatalf("Could not determine expiry date for certificate: %s", c.CertificateName)
}
date := c.ExpiryDate.AddDate(0, 0, -c.RenewalPeriodDays)
dYear, dMonth, dDay := date.Date()
return time.Date(dYear, dMonth, dDay, c.RenewalTime, 0, 0, 0, time.UTC)
return time.Date(dYear, dMonth, dDay, c.RenewalDayTime, 0, 0, 0, time.UTC)
}

0 comments on commit 0e36948

Please sign in to comment.