Sync servers.json and certificates.json on every change

SQLite DB in the K8s pod is ephemeral (no PV). On pod restart,
migrate_from_json() re-reads the remote JSON files. Without syncing
back, any server/cert changes after migration would be lost.

Now every server/cert write to SQLite also writes the corresponding
JSON file to the remote host, keeping them in sync.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
kappa
2026-02-08 11:32:29 +09:00
parent 0803d7db92
commit b86ba5d994
2 changed files with 40 additions and 6 deletions

View File

@@ -580,3 +580,31 @@ def sync_map_files() -> None:
logger.debug("Map files synced: %d exact, %d wildcard entries",
len(exact_entries), len(wildcard_entries))
def sync_servers_json() -> None:
"""Write servers configuration back to servers.json for persistence.
Ensures the remote JSON file stays in sync with SQLite so that
pod restarts can re-migrate without data loss.
"""
from .file_ops import atomic_write_file
config = db_load_servers_config()
content = json.dumps(config, indent=2)
atomic_write_file(SERVERS_FILE, content)
logger.debug("Synced servers.json: %d domains", len(config))
def sync_certs_json() -> None:
"""Write certificates list back to certificates.json for persistence.
Ensures the remote JSON file stays in sync with SQLite so that
pod restarts can re-migrate without data loss.
"""
from .file_ops import atomic_write_file
domains = db_load_certs()
content = json.dumps({"domains": domains}, indent=2)
atomic_write_file(CERTS_FILE, content)
logger.debug("Synced certificates.json: %d domains", len(domains))