39 lines
2.1 KiB
Python
39 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Explicit root deployment helper on terra; validates and backs up mirror config."""
|
|
from pathlib import Path
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
config = Path(sys.argv[1] if len(sys.argv) > 1 else '/opt/mirror/caddy/Caddyfile')
|
|
original = config.read_text()
|
|
snippet = (Path(__file__).parent / 'mirror-route.caddy').read_text()
|
|
marker = ' # Preserve the existing non-Arch mirror frontend.'
|
|
if '# BEGIN ARCHIPELAGO' in original:
|
|
raise SystemExit('Archipelago route already exists; review changes explicitly')
|
|
if original.count(marker) != 1:
|
|
raise SystemExit('Unknown mirror configuration: expected exactly one fallback marker')
|
|
candidate = original.replace(marker, snippet + '\n' + marker)
|
|
temporary = config.with_name('Caddyfile.archipelago-candidate')
|
|
temporary.write_text(candidate)
|
|
try:
|
|
# Candidate is copied into the running container only for validation; live file
|
|
# remains untouched until Caddy accepts it with the real modules/environment.
|
|
subprocess.run(['docker','cp',str(temporary),'mirror-caddy:/tmp/archipelago-Caddyfile'],check=True)
|
|
subprocess.run(['docker','exec','mirror-caddy','caddy','validate','--config','/tmp/archipelago-Caddyfile','--adapter','caddyfile'],check=True)
|
|
backup = config.with_name(f'Caddyfile.before-archipelago-{time.time_ns()}')
|
|
backup.write_bytes(config.read_bytes())
|
|
# Keep the inode: the existing Docker bind mount targets this file.
|
|
with config.open('w') as stream:
|
|
stream.write(candidate)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
result = subprocess.run(['docker','exec','mirror-caddy','caddy','reload','--config','/etc/caddy/Caddyfile','--adapter','caddyfile'])
|
|
if result.returncode:
|
|
config.write_text(original)
|
|
subprocess.run(['docker','exec','mirror-caddy','caddy','reload','--config','/etc/caddy/Caddyfile','--adapter','caddyfile'],check=True)
|
|
raise SystemExit('Reload failed; original mirror configuration restored')
|
|
print(f'Integrated /archipelago/; backup: {backup}')
|
|
finally:
|
|
temporary.unlink(missing_ok=True)
|