Skip to content

Instantly share code, notes, and snippets.

@diamondo25
Last active February 22, 2021 18:54
Show Gist options
  • Save diamondo25/a07e5b8234ecc4496062ff76963cd785 to your computer and use it in GitHub Desktop.
Save diamondo25/a07e5b8234ecc4496062ff76963cd785 to your computer and use it in GitHub Desktop.
Fix letsencrypt symlinks from /live to point to correct /archive entry
#!/usr/bin/env python3
# Just run this program. It'll find all 'archived' letsencrypt certs,
# and sets up the corresponding live/ entries.
# This solves the following error:
# CertStorageError: expected /etc/letsencrypt/live/example.com/cert.pem to be a symlink
import os
import re
maindir = "/etc/letsencrypt"
archived_entries = {}
for dirpath, dirnames, filepaths in os.walk(maindir + "/archive"):
if len(filepaths) == 0: continue
print("Found dir %s" % (dirpath))
for filepath in filepaths:
m = re.search(r'([^0-9]+)(\d+)\.pem', filepath)
if not m: continue
entry = dirpath.replace("/archive", "/live") + '/' + m.group(1) + '.pem'
idx = int(m.group(2))
if entry not in archived_entries:
archived_entries[entry] = {
'lastver': 0,
}
if archived_entries[entry]['lastver'] < idx:
archived_entries[entry]['lastver'] = idx
archived_entries[entry]['path'] = dirpath + '/' + filepath
for entry, data in archived_entries.items():
print('%s (v%d) -> %s' % (entry, data['lastver'], data['path']))
os.remove(entry)
os.symlink(data['path'], entry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment