83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Run a native agent and roll back a release that fails its startup probe."""
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
from pathlib import Path
|
||
|
|
import signal
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
import time
|
||
|
|
import urllib.error
|
||
|
|
import urllib.request
|
||
|
|
|
||
|
|
root = Path(os.environ["Agent__UpdateInstallRoot"]).resolve()
|
||
|
|
releases = root / "releases"
|
||
|
|
current = root / "current"
|
||
|
|
pending = root / "pending-update.json"
|
||
|
|
dotnet = os.environ.get("HEIMDALL_DOTNET", "/usr/bin/dotnet")
|
||
|
|
process = None
|
||
|
|
|
||
|
|
|
||
|
|
def terminate(_signum, _frame):
|
||
|
|
if process is not None and process.poll() is None:
|
||
|
|
process.terminate()
|
||
|
|
|
||
|
|
|
||
|
|
signal.signal(signal.SIGTERM, terminate)
|
||
|
|
signal.signal(signal.SIGINT, terminate)
|
||
|
|
|
||
|
|
|
||
|
|
def within_releases(path):
|
||
|
|
resolved = Path(path).resolve()
|
||
|
|
return resolved != releases and releases in resolved.parents
|
||
|
|
|
||
|
|
|
||
|
|
def restore(previous):
|
||
|
|
if not within_releases(previous):
|
||
|
|
raise RuntimeError("Rollback target is outside the release root")
|
||
|
|
replacement = root / (".rollback-" + str(os.getpid()))
|
||
|
|
replacement.symlink_to(previous, target_is_directory=True)
|
||
|
|
os.replace(replacement, current)
|
||
|
|
pending.unlink(missing_ok=True)
|
||
|
|
|
||
|
|
|
||
|
|
try:
|
||
|
|
marker = json.loads(pending.read_text()) if pending.exists() else None
|
||
|
|
if marker is not None and (not within_releases(marker["previous"]) or
|
||
|
|
not within_releases(current)):
|
||
|
|
raise RuntimeError("Invalid pending update marker")
|
||
|
|
process = subprocess.Popen([dotnet, str(current / "Heimdall.Agent.dll")], cwd=current)
|
||
|
|
if marker is not None:
|
||
|
|
wanted = str(marker["version"]).removeprefix("v")
|
||
|
|
deadline = time.monotonic() + 180
|
||
|
|
healthy_since = None
|
||
|
|
healthy = False
|
||
|
|
while time.monotonic() < deadline and process.poll() is None:
|
||
|
|
try:
|
||
|
|
with urllib.request.urlopen("http://127.0.0.1:8081/api/version", timeout=3) as response:
|
||
|
|
info = json.load(response)
|
||
|
|
if str(info.get("version", "")).split("+")[0].removeprefix("v") == wanted:
|
||
|
|
healthy_since = healthy_since or time.monotonic()
|
||
|
|
if time.monotonic() - healthy_since >= 60:
|
||
|
|
healthy = True
|
||
|
|
break
|
||
|
|
else:
|
||
|
|
healthy_since = None
|
||
|
|
except (urllib.error.URLError, TimeoutError, ValueError):
|
||
|
|
healthy_since = None
|
||
|
|
time.sleep(2)
|
||
|
|
if not healthy:
|
||
|
|
if process.poll() is None:
|
||
|
|
process.terminate()
|
||
|
|
try:
|
||
|
|
process.wait(timeout=10)
|
||
|
|
except subprocess.TimeoutExpired:
|
||
|
|
process.kill()
|
||
|
|
process.wait()
|
||
|
|
restore(marker["previous"])
|
||
|
|
raise RuntimeError("Updated agent failed startup; previous release restored")
|
||
|
|
pending.unlink(missing_ok=True)
|
||
|
|
sys.exit(process.wait())
|
||
|
|
except Exception as error:
|
||
|
|
print(f"Agent supervisor: {error}", file=sys.stderr)
|
||
|
|
sys.exit(75)
|