Reconcile Gitea Actions failure with order-stable runtime checks
This commit is contained in:
@@ -8,6 +8,7 @@ import grp
|
||||
import hashlib
|
||||
import http.client
|
||||
import io
|
||||
import itertools
|
||||
import json
|
||||
import os
|
||||
import pwd
|
||||
@@ -72,6 +73,9 @@ GITEA_CI_V4_SOURCE_SHA256 = {
|
||||
}
|
||||
GITEA_ACTIONS_ROOT = Path("/volume1/docker/nodedc-gitea-actions")
|
||||
GITEA_ACTIONS_ENTRY = "deployment/actions-enable-v1.json"
|
||||
GITEA_ACTIONS_V2_ENTRY = "deployment/actions-enable-v2.json"
|
||||
GITEA_ACTIONS_FAILED_ID = "gitea-actions-enable-v1-20260929-001"
|
||||
GITEA_ACTIONS_FAILED_BACKUP = "gitea-actions-config-gitea-actions-enable-v1-20260929-001-20260929-160304"
|
||||
GITEA_ACTIONS_LEGACY = Path("/volume1/docker/gitea-clean-standalone-v1")
|
||||
GITEA_ACTIONS_CONFIG = Path("/volume1/docker/gitea-clean-20260820/config/app.ini")
|
||||
GITEA_ACTIONS_CONTAINER = "clean-gitea-20260820"
|
||||
@@ -4958,7 +4962,7 @@ def allowed_payload_path(component, rel):
|
||||
die(f"path rejected: {rel}: {reason}")
|
||||
|
||||
if component == "gitea-actions-config":
|
||||
if rel in ("deployment", GITEA_ACTIONS_ENTRY):
|
||||
if rel in ("deployment", GITEA_ACTIONS_ENTRY, GITEA_ACTIONS_V2_ENTRY):
|
||||
return True
|
||||
die("Gitea Actions accepts only its activation descriptor")
|
||||
if component == "gitea-ci":
|
||||
@@ -31431,13 +31435,18 @@ def rollback_map_access(root, backup_dir, entries, current_stamp):
|
||||
|
||||
|
||||
def validate_gitea_actions_payload(payload_dir, entries, release_id):
|
||||
if tuple(entries) != (GITEA_ACTIONS_ENTRY,) or not re.fullmatch(
|
||||
r"gitea-actions-enable-v1-[a-zA-Z0-9._-]{1,65}", release_id):
|
||||
recovery = tuple(entries) == (GITEA_ACTIONS_V2_ENTRY,)
|
||||
version = "v2" if recovery else "v1"
|
||||
entry = GITEA_ACTIONS_V2_ENTRY if recovery else GITEA_ACTIONS_ENTRY
|
||||
if tuple(entries) != (entry,) or not re.fullmatch(
|
||||
r"gitea-actions-enable-" + version + r"-[a-zA-Z0-9._-]{1,65}", release_id):
|
||||
die("Gitea Actions transition identity mismatch")
|
||||
expected = {"schemaVersion": "nodedc.gitea.actions-enable.v1", "releaseId": release_id,
|
||||
expected = {"schemaVersion": "nodedc.gitea.actions-enable." + version, "releaseId": release_id,
|
||||
"action": "enable-instance-actions", "service": "gitea",
|
||||
"productionDeployEnabled": False}
|
||||
if read_strict_json(payload_dir / GITEA_ACTIONS_ENTRY, "Gitea Actions descriptor") != expected:
|
||||
if recovery:
|
||||
expected["reconcilesFailedAttempt"] = GITEA_ACTIONS_FAILED_ID
|
||||
if read_strict_json(payload_dir / entry, "Gitea Actions descriptor") != expected:
|
||||
die("Gitea Actions descriptor mismatch")
|
||||
|
||||
|
||||
@@ -31531,7 +31540,121 @@ def gitea_actions_fingerprint(info):
|
||||
return hashlib.sha256(json.dumps(fields, sort_keys=True).encode()).hexdigest()
|
||||
|
||||
|
||||
def preflight_gitea_actions():
|
||||
def gitea_actions_ordered_fields(info):
|
||||
fields = {key: info.get(key) for key in ("Id", "Image", "Config", "HostConfig", "Mounts")}
|
||||
fields = json.loads(json.dumps(fields))
|
||||
if not isinstance(fields["HostConfig"], dict):
|
||||
die("Gitea Actions HostConfig is malformed")
|
||||
mounts = fields["Mounts"]
|
||||
limits = fields["HostConfig"].get("Ulimits")
|
||||
for values, key in ((mounts, "Destination"), (limits, "Name")):
|
||||
if values is None and key == "Name":
|
||||
continue
|
||||
if not isinstance(values, list) or any(not isinstance(x, dict) or not isinstance(x.get(key), str) for x in values):
|
||||
die("Gitea Actions unordered runtime field is malformed")
|
||||
if len({x[key] for x in values}) != len(values):
|
||||
die("Gitea Actions duplicate runtime entries rejected")
|
||||
values.sort(key=lambda x: json.dumps(x, sort_keys=True))
|
||||
fields["networks"] = sorted(info.get("NetworkSettings", {}).get("Networks", {}))
|
||||
return fields
|
||||
|
||||
|
||||
def gitea_actions_fingerprint_v2(info):
|
||||
return hashlib.sha256(json.dumps(gitea_actions_ordered_fields(info), sort_keys=True).encode()).hexdigest()
|
||||
|
||||
|
||||
def gitea_actions_context_fingerprint(info, before):
|
||||
return (gitea_actions_fingerprint_v2(info) if before.get("fingerprintVersion") == 2
|
||||
else gitea_actions_fingerprint(info))
|
||||
|
||||
|
||||
def gitea_actions_prove_legacy_order(info, expected):
|
||||
fields = gitea_actions_ordered_fields(info)
|
||||
mounts, limits = fields["Mounts"], fields["HostConfig"].get("Ulimits")
|
||||
count = 1
|
||||
for length in (len(mounts), len(limits or [])):
|
||||
for factor in range(2, length + 1):
|
||||
count *= factor
|
||||
if count > 4096:
|
||||
die("Gitea Actions order audit exceeds bounded permutation limit")
|
||||
for mount_order in itertools.permutations(mounts):
|
||||
fields["Mounts"] = list(mount_order)
|
||||
for limit_order in itertools.permutations(limits or []):
|
||||
if limits is not None:
|
||||
fields["HostConfig"]["Ulimits"] = list(limit_order)
|
||||
if hashlib.sha256(json.dumps(fields, sort_keys=True).encode()).hexdigest() == expected:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def preflight_gitea_actions_recovery():
|
||||
# Read-only reconciliation of one known failure, never a generic drift override.
|
||||
backup = BACKUPS_DIR / GITEA_ACTIONS_FAILED_BACKUP
|
||||
record = [json.loads(line) for line in FAILED_STATE_FILE.read_text().splitlines() if line.strip()]
|
||||
record = [row for row in record if row.get("id") == GITEA_ACTIONS_FAILED_ID]
|
||||
if len(record) != 1 or any(record[0].get(key) != value for key, value in {
|
||||
"component": "gitea-actions-config", "sha256": "2ae874cfc6015378246e0bb7b24d4594a749e4136fbd82dbe9db0b57356d2162",
|
||||
"status": "failed", "artifact": "nodedc-gitea-actions-enable-v1-20260929-001.tgz.20260929-160304",
|
||||
"backup_id": GITEA_ACTIONS_FAILED_BACKUP, "rollback_status": "failed:DeployError",
|
||||
"message": "Gitea Actions runtime changed before mutation", "started_apply": True}.items()):
|
||||
die("Gitea Actions recovery failed-attempt evidence mismatch")
|
||||
receipt_path = backup / "gitea-actions-before.json"
|
||||
for path, expected in ((receipt_path, "7b45f822d5afb165f49f0fd4575d92c930ed079cf286a685350f75c61364e0fe"),
|
||||
(FAILED_DIR / record[0]["artifact"], record[0]["sha256"])):
|
||||
if (any(p.is_symlink() for p in (path, *path.parents)) or not path.is_file()
|
||||
or sha256_file(path) != expected):
|
||||
die("Gitea Actions recovery archived evidence mismatch")
|
||||
old = read_strict_json(receipt_path, "Gitea Actions recovery receipt", max_bytes=131072)
|
||||
old_descriptor = GITEA_ACTIONS_ROOT / GITEA_ACTIONS_ENTRY
|
||||
if (not old_descriptor.is_file() or any(p.is_symlink() for p in (old_descriptor, *old_descriptor.parents))
|
||||
or sha256_file(old_descriptor) != "4bc516559ac7a3b62c8d90d8ebc1423ec2039e14591be62eb81fde76bb6751be"
|
||||
or (GITEA_ACTIONS_ROOT / GITEA_ACTIONS_V2_ENTRY).exists()):
|
||||
die("Gitea Actions recovery metadata predecessor mismatch")
|
||||
for rel, expected in {"compose.yaml": "c21ef87833f87623f8352477d2b541d0a0f48769d8f9f79251d58033e90197ff",
|
||||
"production.env": "9ebbf8638e035dddca25fd455b2ac4d4b6db306e1a6a4debd973b133fdeb5bb7"}.items():
|
||||
path = GITEA_ACTIONS_LEGACY / rel
|
||||
if any(p.is_symlink() for p in (path, *path.parents)) or sha256_file(path) != expected:
|
||||
die("Gitea Actions recovery Compose context mismatch")
|
||||
data, metadata = gitea_actions_read_config()
|
||||
gitea_actions_transform(data)
|
||||
if old.get("configBeforeSha256") != GITEA_ACTIONS_BEFORE or old.get("configAfterSha256") != GITEA_ACTIONS_AFTER:
|
||||
die("Gitea Actions recovery config evidence mismatch")
|
||||
samples = []
|
||||
for _ in range(3):
|
||||
info = gitea_actions_container()
|
||||
matched = (info["Id"] == old.get("containerId")
|
||||
and gitea_actions_prove_legacy_order(info, old.get("fingerprint")))
|
||||
sample = {"legacyFingerprint": gitea_actions_fingerprint(info),
|
||||
"canonicalFingerprint": gitea_actions_fingerprint_v2(info),
|
||||
"matchesSavedStateByOrderOnly": matched}
|
||||
samples.append(sample)
|
||||
if not matched:
|
||||
print("gitea_actions_readonly_audit=" + json.dumps({"samples": samples, "mutation": False}, sort_keys=True))
|
||||
die("Gitea Actions recovery cannot prove order-only difference; runtime mutation forbidden")
|
||||
if len({sample["canonicalFingerprint"] for sample in samples}) != 1:
|
||||
die("Gitea Actions recovery runtime changed during read-only audit")
|
||||
if (info["Image"] != inspect_local_image(GITEA_ACTIONS_IMAGE, "Gitea Actions recovery image")
|
||||
or info.get("State", {}).get("Health", {}).get("Status") != "healthy"):
|
||||
die("Gitea Actions recovery image or health mismatch")
|
||||
if gitea_actions_http("http://172.22.27.2:3000", "/api/v1/version") != (200, {"version": "1.27.2"}):
|
||||
die("Gitea Actions recovery version mismatch")
|
||||
status, _ = gitea_actions_http("http://172.22.27.2:3000", GITEA_ACTIONS_PING, ping=True)
|
||||
if status != 404:
|
||||
die("Gitea Actions recovery requires unchanged disabled RPC")
|
||||
inventory = gitea_ci_inventory()
|
||||
inventory.pop(info["Id"], None)
|
||||
if inventory != old.get("inventory"):
|
||||
die("Gitea Actions recovery preserved inventory mismatch")
|
||||
print("gitea_actions_readonly_audit=" + json.dumps({"samples": samples, "mutation": False,
|
||||
"configUnchanged": True, "otherContainersUnchanged": True, "healthy": True}, sort_keys=True))
|
||||
return {"config": data, "metadata": metadata, "containerId": info["Id"],
|
||||
"fingerprint": samples[0]["canonicalFingerprint"], "fingerprintVersion": 2,
|
||||
"mutationStarted": False, "inventory": inventory}
|
||||
|
||||
|
||||
def preflight_gitea_actions(entries=None):
|
||||
if tuple(entries or ()) == (GITEA_ACTIONS_V2_ENTRY,):
|
||||
return preflight_gitea_actions_recovery()
|
||||
expected = {"compose.yaml": "c21ef87833f87623f8352477d2b541d0a0f48769d8f9f79251d58033e90197ff",
|
||||
"production.env": "9ebbf8638e035dddca25fd455b2ac4d4b6db306e1a6a4debd973b133fdeb5bb7"}
|
||||
for rel, digest in expected.items():
|
||||
@@ -31601,10 +31724,14 @@ def apply_gitea_actions(before, backup_dir):
|
||||
fsync_directory(private.parent)
|
||||
fsync_directory(RUNTIME_DIR)
|
||||
receipt = {key: before[key] for key in ("containerId", "fingerprint", "inventory")}
|
||||
if before.get("fingerprintVersion") == 2:
|
||||
receipt["fingerprintVersion"] = 2
|
||||
receipt["reconcilesFailedAttempt"] = GITEA_ACTIONS_FAILED_ID
|
||||
receipt.update({"configBeforeSha256": GITEA_ACTIONS_BEFORE, "configAfterSha256": GITEA_ACTIONS_AFTER})
|
||||
(backup_dir / "gitea-actions-before.json").write_text(json.dumps(receipt, sort_keys=True) + "\n")
|
||||
if gitea_actions_fingerprint(gitea_actions_container()) != before["fingerprint"]:
|
||||
if gitea_actions_context_fingerprint(gitea_actions_container(), before) != before["fingerprint"]:
|
||||
die("Gitea Actions runtime changed before mutation")
|
||||
before["mutationStarted"] = True
|
||||
gitea_actions_replace(GITEA_ACTIONS_BEFORE, gitea_actions_transform(before["config"]), before["metadata"])
|
||||
subprocess.run([str(DOCKER), "restart", "--time", "60", before["containerId"]],
|
||||
check=True, capture_output=True, timeout=90)
|
||||
@@ -31615,7 +31742,7 @@ def accept_gitea_actions(before, enabled=True):
|
||||
while True:
|
||||
try:
|
||||
info = gitea_actions_container()
|
||||
if gitea_actions_fingerprint(info) != before["fingerprint"]:
|
||||
if gitea_actions_context_fingerprint(info, before) != before["fingerprint"]:
|
||||
die("Gitea Actions runtime configuration drift")
|
||||
expected = GITEA_ACTIONS_AFTER if enabled else GITEA_ACTIONS_BEFORE
|
||||
if sha256_file(GITEA_ACTIONS_CONFIG) != expected:
|
||||
@@ -31646,9 +31773,15 @@ def accept_gitea_actions(before, enabled=True):
|
||||
def rollback_gitea_actions(root, backup_dir, entries, current_stamp, before, runtime_started):
|
||||
if runtime_started:
|
||||
info = gitea_actions_container(require_running=False)
|
||||
if gitea_actions_fingerprint(info) != before["fingerprint"]:
|
||||
if gitea_actions_context_fingerprint(info, before) != before["fingerprint"]:
|
||||
die("Gitea Actions rollback runtime drift")
|
||||
current = sha256_file(GITEA_ACTIONS_CONFIG)
|
||||
if before.get("fingerprintVersion") == 2 and not before.get("mutationStarted"):
|
||||
if current != GITEA_ACTIONS_BEFORE:
|
||||
die("Gitea Actions pre-mutation rollback found unexpected config change")
|
||||
accept_gitea_actions(before, enabled=False)
|
||||
restore_overlay_source(root, backup_dir, entries, current_stamp)
|
||||
return
|
||||
if current == GITEA_ACTIONS_AFTER:
|
||||
gitea_actions_replace(GITEA_ACTIONS_AFTER, before["config"], before["metadata"])
|
||||
elif current != GITEA_ACTIONS_BEFORE:
|
||||
@@ -32135,7 +32268,7 @@ def plan_artifact(artifact):
|
||||
if manifest["component"] == "gitea-ci":
|
||||
preflight_gitea_ci(entries)
|
||||
if manifest["component"] == "gitea-actions-config":
|
||||
preflight_gitea_actions()
|
||||
preflight_gitea_actions(entries)
|
||||
reject_failed_artifact_replay(manifest, sha)
|
||||
reject_terminal_engine_l2_failed_artifact(manifest, sha)
|
||||
reject_terminal_device_plane_foundation_artifact(manifest, sha)
|
||||
@@ -32720,6 +32853,8 @@ def plan_artifact(artifact):
|
||||
print(f"type={manifest['type']}")
|
||||
print(f"payload_root={root}")
|
||||
if component == "gitea-actions-config":
|
||||
if tuple(entries) == (GITEA_ACTIONS_V2_ENTRY,):
|
||||
print("gitea_actions_recovery=exact-failed-attempt;read-only-order-equivalence-proven")
|
||||
print("gitea_actions_transition=disabled-to-enabled;production_deploy=false")
|
||||
print("gitea_actions_runtime=restart-existing-clean-gitea-20260820;brief-web-and-ssh-interruption")
|
||||
print("gitea_actions_config=one-boolean-only;owner-and-mode=preserved")
|
||||
@@ -38280,7 +38415,7 @@ process.stdout.write('engine-l2-closed-loop:0.7.0:cas+safe-profile+external-plan
|
||||
|
||||
def run_healthchecks(component, entries=None, services=None):
|
||||
if component == "gitea-actions-config":
|
||||
if tuple(services or ()) != ("gitea",) or tuple(entries or ()) != (GITEA_ACTIONS_ENTRY,):
|
||||
if tuple(services or ()) != ("gitea",) or tuple(entries or ()) not in ((GITEA_ACTIONS_ENTRY,), (GITEA_ACTIONS_V2_ENTRY,)):
|
||||
die("Gitea Actions service/entry set mismatch")
|
||||
# Context-aware acceptance runs in apply immediately after the restart.
|
||||
return
|
||||
@@ -39439,7 +39574,7 @@ def apply_artifact(artifact):
|
||||
if component == "gitea-ci":
|
||||
gitea_ci_before = preflight_gitea_ci(entries)
|
||||
if component == "gitea-actions-config":
|
||||
gitea_actions_before = preflight_gitea_actions()
|
||||
gitea_actions_before = preflight_gitea_actions(entries)
|
||||
artifact_only = component_artifact_only(component)
|
||||
defer_bootstrap_root = is_gitea_bootstrap_slice(
|
||||
component,
|
||||
|
||||
Reference in New Issue
Block a user