Files
akyldash/infra/deploy/external_status.py
T

97 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""Read-only selector for one fully verified current-main release candidate."""
from __future__ import annotations
import json
import os
import re
import subprocess
import sys
from pathlib import Path
from deploy_release import JOURNAL_FILE, ROOT, STATE_FILE
from release_handoff import latest_main_sha
from release_source import fetch_release, latest_release_request
from release_state import read_json, validate_state
POLICY_FILE = Path("/etc/akyldash/release-policy.json")
STATUS_MAX_BYTES = 1024
SHA_RE = re.compile(r"[0-9a-f]{40}\Z")
def checkout_sha(checkout: Path) -> str:
result = subprocess.run(
["git", "-C", str(checkout), "rev-parse", "HEAD"],
check=True, text=True, capture_output=True, timeout=10,
)
value = result.stdout.strip()
if not SHA_RE.fullmatch(value):
raise ValueError("production checkout revision is invalid")
return value
def status_document(*, api=None, checkout: Path = ROOT, journal: Path = JOURNAL_FILE,
policy_file: Path = POLICY_FILE, state_file: Path = STATE_FILE,
main_lookup=latest_main_sha,
checkout_lookup=checkout_sha) -> dict:
if os.geteuid() != 0:
raise ValueError("release status command must run as root")
if journal.exists() or journal.is_symlink():
return {"schema_version": 1, "state": "not_ready", "reason": "recovery_journal", "release": None}
current_main = main_lookup(checkout)
if checkout_lookup(checkout) != current_main:
return {"schema_version": 1, "state": "not_ready", "reason": "host_checkout_stale", "release": None}
api_args = {"api": api} if api is not None else {}
request = latest_release_request(current_main=current_main, checkout=checkout, **api_args)
if request is None:
return {"schema_version": 1, "state": "not_ready", "reason": "no_current_main_release", "release": None}
if state_file.exists() or state_file.is_symlink():
state = validate_state(read_json(state_file))
if (state["commit"] == request["commit"]
and state["manifest_sha256"] == request["descriptor_sha256"]):
return {"schema_version": 1, "state": "not_ready", "reason": "already_applied", "release": None}
manifest, _raw = fetch_release(
request, policy=read_json(policy_file), current_main=current_main, checkout=checkout,
**api_args,
)
if manifest["commit"] != current_main or manifest["run_id"] != request["run_id"]:
raise ValueError("verified release descriptor does not match the selected source")
if main_lookup(checkout) != current_main:
return {"schema_version": 1, "state": "not_ready", "reason": "main_advanced", "release": None}
if journal.exists() or journal.is_symlink():
return {"schema_version": 1, "state": "not_ready", "reason": "recovery_journal", "release": None}
if state_file.exists() or state_file.is_symlink():
state = validate_state(read_json(state_file))
if (state["commit"] == request["commit"]
and state["manifest_sha256"] == request["descriptor_sha256"]):
return {"schema_version": 1, "state": "not_ready", "reason": "already_applied", "release": None}
return {
"schema_version": 1,
"state": "candidate",
"reason": None,
"release": {key: request[key] for key in ("run_id", "commit", "descriptor_sha256")},
}
def write_status(value: dict, stream=None) -> None:
payload = json.dumps(value, sort_keys=True, separators=(",", ":")).encode("utf-8") + b"\n"
if len(payload) > STATUS_MAX_BYTES:
raise ValueError("release status response exceeds its size limit")
(stream or sys.stdout.buffer).write(payload)
def main() -> int:
try:
write_status(status_document())
except Exception as error:
print(f"release status unavailable ({type(error).__name__})", file=sys.stderr)
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())