67 lines
1.4 KiB
Python
67 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from io import TextIOWrapper
|
|
import logging
|
|
|
|
from sarge import run, shell_format, capture_both
|
|
from systemd.journal import JournalHandler
|
|
|
|
logger = logging.getLogger("letsencrypt-renew")
|
|
logger.propagate = False
|
|
logger.addHandler(JournalHandler())
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
def parse_domain_list(domainfile):
|
|
|
|
domains = list()
|
|
|
|
with open(domainfile, "r") as handle:
|
|
|
|
for row in handle:
|
|
|
|
if not row.rstrip():
|
|
continue
|
|
|
|
# Ignore everything after # (comment)
|
|
row = row.partition("#")[0]
|
|
row = row.rstrip()
|
|
|
|
if not row:
|
|
continue
|
|
|
|
domains.append(row)
|
|
|
|
if not domains:
|
|
logger.warning("No domains found in configuration.")
|
|
return
|
|
|
|
domains = [shell_format("-d {0}", domain) for domain in domains]
|
|
|
|
return domains
|
|
|
|
|
|
def renew_domains(letsencrypt_path, domains):
|
|
|
|
domains = " ".join(domains)
|
|
command = " ".join([letsencrypt_path, "certonly", domains])
|
|
|
|
logger.info("Renewing domain certificates...")
|
|
process = capture_both(command)
|
|
|
|
for stdout in TextIOWrapper(process.stdout):
|
|
logger.info(stdin)
|
|
|
|
for stderr in TextIOWrapper(process.stderr):
|
|
logger.info(stderr)
|
|
|
|
if process.returncode != 0:
|
|
logger.error("Let's Encrypt domain renewal failed.")
|
|
return
|
|
else:
|
|
logger.info("Domain renewal succeeded.")
|
|
|
|
|
|
|
|
|
|
|