"""Deep-merge overrides (a JSON file, path in argv[1]) into config.yaml. Run inside the hermes container as the hermes user. Only keys present in the overrides are touched. Prints CHANGED when the file was rewritten. """ import json import os import sys import yaml PATH = "/opt/data/config.yaml" with open(sys.argv[1]) as fh: overrides = json.load(fh) if os.path.exists(PATH): with open(PATH) as fh: current = yaml.safe_load(fh) or {} else: current = {} def merge(dst, src): changed = False for key, val in src.items(): if isinstance(val, dict) and isinstance(dst.get(key), dict): changed |= merge(dst[key], val) elif dst.get(key) != val: dst[key] = val changed = True return changed if merge(current, overrides): with open(PATH, "w") as fh: yaml.safe_dump(current, fh, sort_keys=False) print("CHANGED") else: print("UNCHANGED") sys.exit(0)