"""Single-file headless installer entry point, using the same plan/apply engine.""" import argparse import fcntl import importlib.util import json import os import sys import tempfile import zipfile from pathlib import Path def main(): parser = argparse.ArgumentParser( description="Install VPS Host ARNavi on Ubuntu Server 24.04 amd64" ) parser.add_argument("--host", help="Public DNS name or IPv4 of this VPS") actions = parser.add_mutually_exclusive_group() actions.add_argument( "--fresh", action="store_true", help="Back up and replace this product installation" ) actions.add_argument("--restore", help="Restore the private backup created by --fresh") actions.add_argument("--export-state", help="Stop and fence this VPS; export private state") actions.add_argument("--import-state", help="Restore exported state on an empty Ubuntu VPS") actions.add_argument( "--resume-source", action="store_true", help="Cancel migration fencing; target must already be stopped", ) parser.add_argument("--sha256", help="Required SHA-256 of --import-state archive") args = parser.parse_args() if os.geteuid() != 0: parser.error("Run: sudo python3 " + Path(sys.argv[0]).name) # Serialize all owner installer actions, including crash recovery/migration. with open("/run/lock/ndc-mission-vps-installer.lock", "a") as lock: try: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: parser.error("Another VPS installer operation is running") with tempfile.TemporaryDirectory(prefix="ndc-vps-install-") as directory: with zipfile.ZipFile(sys.argv[0]) as archive: for name in archive.namelist(): if name.startswith("payload/"): relative = Path(name).relative_to("payload") if ".." in relative.parts: raise ValueError("Invalid installer payload") target = Path(directory) / relative target.parent.mkdir(parents=True, exist_ok=True) target.write_bytes(archive.read(name)) sys.path.insert(0, directory) spec = importlib.util.spec_from_file_location( "installer", Path(directory) / "install.py" ) engine = importlib.util.module_from_spec(spec) spec.loader.exec_module(engine) if args.export_state or args.import_state or args.resume_source: import portable_state if args.export_state: portable_state.export_state(engine, Path(args.export_state)) elif args.import_state: if args.host or not args.sha256: parser.error("Import preserves the endpoint; provide --sha256, not --host") portable_state.import_state(engine, Path(args.import_state), args.sha256) else: portable_state.resume_source(engine) return if args.restore: engine.restore(Path(args.restore)) return host = args.host or input("Public DNS name or IPv4 of this VPS: ").strip() options = argparse.Namespace(host=host, api_port=19443, tracker_port=9922, expect=None) proposal = engine.plan(options) print( json.dumps( {k: proposal[k] for k in ("environment", "configuration", "artifact")}, indent=2 ) ) options.expect = proposal["plan_sha256"] if args.fresh: engine.fresh(options, proposal) else: engine.apply(options, proposal) print( "VPS Host ARNavi ready. Copy the key from " "/opt/nodedc-mission-vps/connect.key into Mission Core." ) if __name__ == "__main__": main()