diff --git a/acme_nginx/Acme.py b/acme_nginx/Acme.py index b2ae6d1..eba1a75 100644 --- a/acme_nginx/Acme.py +++ b/acme_nginx/Acme.py @@ -10,6 +10,7 @@ import sys import tempfile import time +from datetime import datetime, timedelta try: from urllib.request import urlopen, Request # Python 3 @@ -32,6 +33,7 @@ def __init__( cert_path='/etc/ssl/private/letsencrypt-domain.pem', dns_provider=None, skip_nginx_reload=False, + update_date_threshold_days=None, debug=False): """ Params: @@ -60,6 +62,23 @@ def __init__( self.chain = "https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem" self.dns_provider = dns_provider self.skip_nginx_reload = skip_nginx_reload + self.update_date_threshold_days = update_date_threshold_days + + self.IsOutOfDate = True + if self.update_date_threshold_days: + try: + certTime = datetime.fromtimestamp(os.path.getmtime(self.cert_path)) + certTimeThreshold = certTime + timedelta(days=self.update_date_threshold_days) + + self.IsOutOfDate = (certTimeThreshold < datetime.now()) + self.log.info('Cert file {1} (expiration time {0})'.format( certTimeThreshold, "is out of date" if self.IsOutOfDate else "is not out of date")) + + except OSError as e: + if e.errno == 2: + self.log.info('Cert file {0} not found -> DO UPDATE CERT'.format(self.cert_path)) + except: + pass + def _reload_nginx(self): """ Reload nginx """ diff --git a/acme_nginx/client.py b/acme_nginx/client.py index 7750795..17e2aa7 100644 --- a/acme_nginx/client.py +++ b/acme_nginx/client.py @@ -74,6 +74,11 @@ def set_arguments(): dest='skip_reload', action='store_true', help="don't reload nginx after certificate signing") + parser.add_argument( + '--out-of-date-update-threshold-days', + dest='update_date_threshold_days', + type=int, + help="expiration threshold in days") return parser.parse_args() @@ -107,6 +112,8 @@ def main(): cert_path=args.cert_path, debug=args.debug, dns_provider=args.dns_provider, - skip_nginx_reload=args.skip_reload + skip_nginx_reload=args.skip_reload, + update_date_threshold_days = args.update_date_threshold_days ) - acme.get_certificate() + if acme.IsOutOfDate: + acme.get_certificate()