84 lines
3.6 KiB
Python
84 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only preparation endpoint for one fully verified current-main release."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
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, runtime_compatibility_reason
|
|
from release_state import read_json, validate_state
|
|
|
|
POLICY_FILE = Path("/etc/akyldash/release-policy.json")
|
|
STATUS_MAX_BYTES = 1024
|
|
|
|
|
|
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) -> 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)
|
|
api_args = {"api": api} if api is not None else {}
|
|
reason = runtime_compatibility_reason(
|
|
current_main, checkout=checkout, policy_file=policy_file, **api_args,
|
|
)
|
|
if reason is not None:
|
|
return {"schema_version": 1, "state": "not_ready", "reason": reason, "release": None}
|
|
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": "prepared",
|
|
"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())
|