Refactor certificate removal logic and add option to clear old let's encrypt certificates

This commit is contained in:
Théophile Diot 2024-04-03 12:58:37 +01:00
parent 6dd8d2c0cd
commit 1a2f23c5ac
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
2 changed files with 18 additions and 7 deletions

View file

@ -168,13 +168,15 @@ try:
LOGGER.info(f"Certificate generation succeeded for domain(s) : {domains}")
# Remove old certificates
for elem in chain(DATA_PATH.glob("archive/*"), DATA_PATH.glob("live/*"), DATA_PATH.glob("renewal/*")):
if elem.name.replace(".conf", "") not in generated_domains:
LOGGER.debug(f"Removing old certificate {elem}")
if elem.is_dir():
rmtree(elem, ignore_errors=True)
else:
elem.unlink(missing_ok=True)
if getenv("LETS_ENCRYPT_CLEAR_OLD_CERTS", "no") == "yes":
LOGGER.info("Clear old certificates is activated, removing old / no longer used certificates...")
for elem in chain(DATA_PATH.glob("archive/*"), DATA_PATH.glob("live/*"), DATA_PATH.glob("renewal/*")):
if elem.name.replace(".conf", "") not in generated_domains:
LOGGER.warning(f"Removing old certificate {elem}")
if elem.is_dir():
rmtree(elem, ignore_errors=True)
else:
elem.unlink(missing_ok=True)
# Save Let's Encrypt data to db cache
if DATA_PATH.is_dir() and list(DATA_PATH.iterdir()):

View file

@ -31,6 +31,15 @@
"label": "Use Let's Encrypt Staging",
"regex": "^(yes|no)$",
"type": "check"
},
"LETS_ENCRYPT_CLEAR_OLD_CERTS": {
"context": "global",
"default": "no",
"help": "Clear old certificates when renewing.",
"id": "lets-encrypt-clear-old-certs",
"label": "Clear old certificates when they are no longer needed",
"regex": "^(yes|no)$",
"type": "check"
}
},
"jobs": [