62 lines
2.6 KiB
Python
62 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Run HTTP(S) and origin-separated read-only rsync daemons in the server namespace."""
|
|
import argparse
|
|
from pathlib import Path
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
|
|
from common import file_sha256, read_json
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--manifest", type=Path, required=True)
|
|
parser.add_argument("--log", type=Path, required=True)
|
|
parser.add_argument("--ready", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
if args.ready.exists():
|
|
raise ValueError("stale/live readiness file exists; inspect the old server first")
|
|
manifest = read_json(args.manifest)
|
|
command = [sys.executable, str(Path(__file__).with_name("serve.py")), "--archive", manifest["archive"],
|
|
"--epoch", manifest["epoch_path"], "--bind", "0.0.0.0", "--cert", manifest["tls"]["cert"],
|
|
"--key", manifest["tls"]["key"], "--log", str(args.log), "--ready", str(args.ready)]
|
|
if manifest.get("scope_path"):
|
|
if file_sha256(Path(manifest["scope_path"])) != manifest["scope_sha256"]:
|
|
raise ValueError("replay scope changed since asset preparation")
|
|
command += ["--scope", manifest["scope_path"]]
|
|
if manifest.get("bundle_manifest"):
|
|
if file_sha256(Path(manifest["bundle_manifest"])) != manifest["bundle_sha256"]:
|
|
raise ValueError("consumed bundle manifest changed since preparation")
|
|
command += ["--bundle-manifest", manifest["bundle_manifest"], "--bundle-sha256", manifest["bundle_sha256"]]
|
|
for scheme in ("http", "https"):
|
|
for port in manifest[scheme + "_ports"]:
|
|
command += ["--" + scheme + "-port", str(port)]
|
|
children = []
|
|
stopped = threading.Event()
|
|
signal.signal(signal.SIGTERM, lambda *_: stopped.set())
|
|
signal.signal(signal.SIGINT, lambda *_: stopped.set())
|
|
try:
|
|
for config in manifest["rsync_configs"]:
|
|
children.append(subprocess.Popen(["rsync", "--daemon", "--no-detach", "--config", config]))
|
|
children.append(subprocess.Popen(command))
|
|
while not stopped.wait(0.2):
|
|
if any(child.poll() is not None for child in children):
|
|
raise RuntimeError("a replay child exited unexpectedly")
|
|
finally:
|
|
for child in reversed(children):
|
|
if child.poll() is None:
|
|
child.terminate()
|
|
for child in children:
|
|
try:
|
|
child.wait(timeout=10)
|
|
except subprocess.TimeoutExpired:
|
|
child.kill()
|
|
child.wait(timeout=5)
|
|
args.ready.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|