Register lossless Device B2 shared contour schema transition

This commit is contained in:
Codex
2026-10-01 14:38:35 +03:00
parent 39a2239563
commit 27c155276a
5 changed files with 453 additions and 4 deletions
+202 -4
View File
@@ -2962,6 +2962,9 @@ GITEA_SALVAGE_UNSUPPORTED_SCHEMA_TABLES = tuple(
)
COMPONENTS = {
"device-b2-shared-contours": {
"payload_root": DEVICE_PLANE_ROOT, "artifact_only": True, "services": (),
},
"device-b2-foundation-reconciliation": {
"payload_root": DEVICE_PLANE_ROOT, "artifact_only": True, "services": (),
},
@@ -4991,6 +4994,10 @@ def denied_payload_path(component, rel):
def allowed_payload_path(component, rel):
validate_posix_path(rel)
if component == "device-b2-shared-contours":
if rel in ("deployment", *DEVICE_B2_SHARED_ENTRIES):
return True
die("Device B2 shared contours accepts only its three fixed files")
if component == "device-b2-foundation-reconciliation":
if rel in ("deployment", DEVICE_B2_RECON_ENTRY):
return True
@@ -11631,6 +11638,8 @@ def load_artifact(artifact, work_dir):
die(f"files.txt entry missing in payload: {rel}")
validate_payload_tree(manifest["component"], payload_dir, entries)
if manifest["component"] == "device-b2-shared-contours":
validate_device_b2_shared_payload(payload_dir, entries, manifest["id"])
if manifest["component"] == "device-b2-foundation-reconciliation":
validate_device_b2_reconciliation_payload(payload_dir, entries, manifest["id"])
if manifest["component"] == "device-b2-foundation":
@@ -22493,6 +22502,10 @@ def is_platform_provider_catalog_only(entries):
def component_services(component, entries=None):
if component == "device-b2-shared-contours":
if tuple(entries or ()) != DEVICE_B2_SHARED_ENTRIES:
die("Device B2 shared contours requires all three exact entries")
return ()
if component == "device-b2-foundation-reconciliation":
if tuple(entries or ()) != (DEVICE_B2_RECON_ENTRY,):
die("Device B2 reconciliation requires the exact descriptor entry")
@@ -33334,6 +33347,11 @@ def plan_artifact(artifact):
gitea_salvage_preflight = None
with tempfile.TemporaryDirectory(prefix="plan-", dir=TMP_DIR) as tmp:
manifest, entries, payload_dir = load_artifact(artifact, Path(tmp))
if manifest["component"] == "device-b2-shared-contours":
preflight_device_b2_shared(payload_dir, entries, manifest["id"])
print("device_b2_shared_scope=one-table-primary-key;services=none;build=none;ports=preserved")
print("device_b2_shared_preserved=all-links,applications,keys,volumes,containers,compose")
print("device_b2_shared_backup=private-table-dump;rollback=lossless-schema-only;multi-links-refused")
if manifest["component"] == "device-b2-foundation-reconciliation":
preflight_device_b2_reconciliation(payload_dir, entries, manifest["id"])
if manifest["component"] == "device-b2-foundation":
@@ -39532,6 +39550,10 @@ 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 == "device-b2-shared-contours":
if tuple(entries or ()) != DEVICE_B2_SHARED_ENTRIES or tuple(services or ()) != ():
die("Device B2 shared contours health scope mismatch")
return # Exact schema/runtime/product checks run in accept_device_b2_shared.
if component == "device-b2-foundation-reconciliation":
if tuple(services or ()) != ():
die("Device B2 reconciliation cannot select services")
@@ -40676,6 +40698,7 @@ def apply_artifact(artifact):
device_autodeploy_before = None
device_priority_before = None
device_b2_before = None
device_b2_shared_before = None
current_stamp = stamp()
with DeployLock():
@@ -40720,6 +40743,8 @@ def apply_artifact(artifact):
compose_root = component_compose_root(component)
bootstrap_root = bool(COMPONENTS[component].get("bootstrap_root"))
services = component_services(component, entries)
if component == "device-b2-shared-contours":
device_b2_shared_before = preflight_device_b2_shared(payload_dir, entries, patch_id)
if component == "device-b2-foundation-reconciliation":
preflight_device_b2_reconciliation(payload_dir, entries, patch_id)
if component == "device-b2-foundation":
@@ -41263,6 +41288,8 @@ def apply_artifact(artifact):
include_nginx_html = component == "engine" and component_publish_dist(component, entries)
create_backup(root, backup_dir, entries, include_nginx_html)
if component == "device-b2-shared-contours":
backup_device_b2_shared(backup_dir, device_b2_shared_before)
if component == "device-b2-foundation":
proof = backup_dir / "device-b2-before.json"
proof.write_text(json.dumps(device_b2_before, sort_keys=True) + "\n")
@@ -41373,6 +41400,8 @@ def apply_artifact(artifact):
elif component == "device-core-autodeploy":
runtime_started = True
apply_device_autodeploy(device_autodeploy_before)
elif component == "device-b2-shared-contours":
device_b2_database((DEVICE_PLANE_ROOT / DEVICE_B2_SHARED_SQL).read_text())
elif component == "device-b2-foundation":
def mark_device_b2_runtime_started():
nonlocal runtime_started
@@ -41420,6 +41449,8 @@ def apply_artifact(artifact):
):
die("Engine L2 closed-loop app generation was not recreated")
run_healthchecks(component, entries, services)
if component == "device-b2-shared-contours":
accept_device_b2_shared(device_b2_shared_before, backup_dir)
if component == "device-b2-foundation":
accept_device_b2(device_b2_before, backup_dir)
if component == "device-core-network-priority":
@@ -41502,7 +41533,13 @@ def apply_artifact(artifact):
and backup_dir is not None
and root is not None
):
if component == "device-b2-foundation":
if component == "device-b2-shared-contours":
try:
rollback_device_b2_shared(root, backup_dir, entries, current_stamp, device_b2_shared_before)
rollback_status = "ok:device-b2-shared-contours"
except Exception as rollback_exc:
rollback_status = "failed:" + type(rollback_exc).__name__
elif component == "device-b2-foundation":
try:
rollback_device_b2(root, backup_dir, entries, current_stamp, device_b2_before, runtime_started)
rollback_status = "ok:device-b2-foundation"
@@ -42884,6 +42921,7 @@ def device_b2_failed_barrier():
def device_b2_compose_files(files):
device_b2_shared_failed_barrier()
# A failed foundation rollback blocks ordinary releases too, even when the
# old source files were already restored and no overlay remains.
device_b2_failed_barrier()
@@ -42931,8 +42969,8 @@ def device_b2_schema():
) x;"""))
def validate_device_b2_schema(schema):
if schema != DEVICE_B2_EXPECTED_SCHEMA:
def validate_device_b2_schema(schema, *, expected=None):
if schema != (DEVICE_B2_EXPECTED_SCHEMA if expected is None else expected):
die('Device B2 foundation exact schema fields/constraints mismatch')
return device_release_drift_digest(schema)
@@ -43081,7 +43119,8 @@ def validate_installed_device_b2():
if not device_b2_compose_files(()):
return
device_b2_private_directory()
validate_device_b2_schema(device_b2_schema())
expected = device_b2_shared_schema() if device_b2_shared_receipt() else DEVICE_B2_EXPECTED_SCHEMA
validate_device_b2_schema(device_b2_schema(), expected=expected)
device_b2_strip_runtime(device_release_drift_sample()['device-control-core']['fields'])
@@ -43140,6 +43179,165 @@ def rollback_device_b2(root, backup_dir, entries, stamp, before, runtime_started
accept_device_b2(before,backup_dir,rollback=True)
# Additive schema transition. SQL is fixed by reviewed hashes, never commands
# selected by a manifest. All data and running service generations are preserved.
DEVICE_B2_SHARED_ID = 'device-b2-shared-contours-v1-20261001-001'
DEVICE_B2_SHARED_DESCRIPTOR = 'deployment/device-b2-shared-contours-v1.json'
DEVICE_B2_SHARED_SQL = 'deployment/device-b2-shared-contours-v1.sql'
DEVICE_B2_SHARED_REVERSE = 'deployment/device-b2-shared-contours-v1.rollback.sql'
DEVICE_B2_SHARED_ENTRIES = (DEVICE_B2_SHARED_DESCRIPTOR, DEVICE_B2_SHARED_SQL, DEVICE_B2_SHARED_REVERSE)
DEVICE_B2_SHARED_HASHES = {
DEVICE_B2_SHARED_SQL: 'caff93f717bbd61ebaf8ffec0b9b2ad04885da22c4e332c1a4586e149faab57a',
DEVICE_B2_SHARED_REVERSE: 'f862da8316695d162167bd1fa854edcbf10d82f57462e3021cf199e72ef16b4f',
}
DEVICE_B2_SHARED_BASELINE = 'device-core-release-v1-r37-1b3001dfa12598af28882232336d837514502718'
DEVICE_B2_SHARED_BASELINE_SHA = 'fca735b4177ba5397e2af31520682aeb17670a8d38a19176be2c08f07f37dffe'
def device_b2_shared_descriptor():
return {'schemaVersion': 'nodedc.device-b2-shared-contours.v1', 'releaseId': DEVICE_B2_SHARED_ID,
'baselineReleaseId': DEVICE_B2_SHARED_BASELINE, 'baselineSha256': DEVICE_B2_SHARED_BASELINE_SHA,
'files': DEVICE_B2_SHARED_HASHES, 'transition': 'one-contour-many-projects',
'services': [], 'rollback': 'refuse-multiple-projects;preserve-all-rows'}
def validate_device_b2_shared_payload(root, entries, identity):
if tuple(entries or ()) != DEVICE_B2_SHARED_ENTRIES or identity != DEVICE_B2_SHARED_ID:
die('Device B2 shared contours exact identity/entries mismatch')
for rel in DEVICE_B2_SHARED_ENTRIES:
path = root / rel
if path.is_symlink() or not path.is_file():
die('Device B2 shared contours missing/unsafe payload')
if read_strict_json(root / DEVICE_B2_SHARED_DESCRIPTOR, 'Device B2 shared contours') != device_b2_shared_descriptor():
die('Device B2 shared contours descriptor mismatch')
if any(sha256_file(root / rel) != digest for rel, digest in DEVICE_B2_SHARED_HASHES.items()):
die('Device B2 shared contours SQL digest mismatch')
def device_b2_shared_schema():
result = json.loads(json.dumps(DEVICE_B2_EXPECTED_SCHEMA))
links = result[0]
assert links['name'] == 'device_b2_contour_links'
links['marker'] = 'nodedc.device-b2-shared-contours/v1'
links['definitions'] = [
'PRIMARY KEY (source_id, contour_id, project_id)' if v == 'PRIMARY KEY (source_id, contour_id)' else v
for v in links['definitions']]
return result
def device_b2_shared_failed_barrier():
if any(r.get('component') == 'device-b2-shared-contours' and r.get('started_apply')
and r.get('rollback_status') != 'ok:device-b2-shared-contours' for r in load_state(FAILED_STATE_FILE)):
die('Device B2 shared contours requires failed-state reconciliation')
def device_b2_shared_receipt():
device_b2_shared_failed_barrier()
present = [(DEVICE_PLANE_ROOT / p).exists() or (DEVICE_PLANE_ROOT / p).is_symlink()
for p in DEVICE_B2_SHARED_ENTRIES]
rows = [r for r in load_state(STATE_FILE) if r.get('component') == 'device-b2-shared-contours']
if not any(present) and not rows:
return False
if not all(present) or len(rows) != 1 or rows[0].get('id') != DEVICE_B2_SHARED_ID or rows[0].get('status') != 'ok':
die('Device B2 shared contours has no unique complete applied receipt')
validate_device_b2_shared_payload(DEVICE_PLANE_ROOT, DEVICE_B2_SHARED_ENTRIES, DEVICE_B2_SHARED_ID)
with tempfile.TemporaryDirectory(prefix='device-b2-shared-receipt-', dir=TMP_DIR) as tmp:
manifest, entries, payload = load_artifact(device_release_applied_archive(rows[0]), Path(tmp))
if manifest['component'] != 'device-b2-shared-contours':
die('Device B2 shared contours archive owner mismatch')
validate_device_b2_shared_payload(payload, entries, manifest['id'])
return True
def preflight_device_b2_shared(payload, entries, identity):
validate_device_b2_shared_payload(payload, entries, identity)
device_b2_shared_failed_barrier()
if any((DEVICE_PLANE_ROOT / p).exists() or (DEVICE_PLANE_ROOT / p).is_symlink() for p in entries):
die('Device B2 shared contours is initial-only')
if any(r.get('component') == 'device-b2-shared-contours' for r in load_state(STATE_FILE)):
die('Device B2 shared contours already has an applied receipt')
rows = [r for r in load_state(STATE_FILE) if r.get('component') == 'device-core-release']
if not rows or rows[-1].get('id') != DEVICE_B2_SHARED_BASELINE or rows[-1].get('sha256') != DEVICE_B2_SHARED_BASELINE_SHA:
die('Device B2 shared contours requires exact accepted r37 baseline')
with tempfile.TemporaryDirectory(prefix='device-b2-shared-baseline-', dir=TMP_DIR) as tmp:
manifest, release_entries, source = load_artifact(device_release_applied_archive(rows[-1]), Path(tmp))
if manifest['component'] != 'device-core-release' or tuple(release_entries) != DEVICE_RELEASE_ENTRIES:
die('Device B2 shared contours baseline owner/entries mismatch')
expected = collect_exact_files(source, release_entries, 'Device B2 shared contours baseline')
if collect_exact_files(DEVICE_PLANE_ROOT, DEVICE_RELEASE_ENTRIES, 'Device B2 shared contours installed source') != expected:
die('Device B2 shared contours installed source drift')
validate_installed_device_b2()
if not device_b2_compose_files(()):
die('Device B2 shared contours requires accepted B2 foundation')
validate_device_b2_schema(device_b2_schema())
run_healthchecks('device-core-release', DEVICE_RELEASE_ENTRIES, DEVICE_RELEASE_SERVICES)
return {'runtime': device_release_runtime_snapshot_v2(),
'otherComposeServices': device_release_other_compose_services(), 'installedFiles': expected,
'compose': {str(p): sha256_file(p) for p in component_compose_files('device-core-release')}}
def backup_device_b2_shared(backup_dir, before):
proof = backup_dir / 'device-b2-shared-before.json'
with proof.open('x') as output:
os.fchmod(output.fileno(), 0o600)
output.write(json.dumps(before, sort_keys=True) + '\n')
output.flush(); os.fsync(output.fileno())
ids = device_plane_service_container_ids('device-postgres')
if len(ids) != 1:
die('Device B2 shared contours requires the existing PostgreSQL container')
path = backup_dir / 'device-b2-contour-links.dump'
with path.open('xb') as output:
os.fchmod(output.fileno(), 0o600)
result = subprocess.run([str(DOCKER), 'exec', ids[0], 'pg_dump', '-U', 'device_plane', '-d', 'device_plane',
'-Fc', '--strict-names', '-t', 'public.device_b2_contour_links'],
stdout=output, stderr=subprocess.PIPE, timeout=45, check=False)
output.flush(); os.fsync(output.fileno())
with path.open('rb') as saved:
signature = saved.read(5)
if result.returncode or result.stderr or signature != b'PGDMP':
die('Device B2 shared contours private table backup failed')
digest = backup_dir / 'device-b2-contour-links.dump.sha256'
with digest.open('x') as output:
os.fchmod(output.fileno(), 0o600)
output.write(sha256_file(path) + '\n')
output.flush(); os.fsync(output.fileno())
def accept_device_b2_shared(before, backup_dir, rollback=False):
dump = backup_dir / 'device-b2-contour-links.dump'
if dump.is_symlink() or not dump.is_file() or stat.S_IMODE(dump.stat().st_mode) != 0o600:
die('Device B2 shared contours backup is missing or unsafe')
if sha256_file(dump) != (backup_dir / 'device-b2-contour-links.dump.sha256').read_text().strip():
die('Device B2 shared contours backup changed')
if not rollback:
validate_device_b2_shared_payload(DEVICE_PLANE_ROOT, DEVICE_B2_SHARED_ENTRIES, DEVICE_B2_SHARED_ID)
expected = DEVICE_B2_EXPECTED_SCHEMA if rollback else device_b2_shared_schema()
validate_device_b2_schema(device_b2_schema(), expected=expected)
device_b2_private_directory()
run_healthchecks('device-core-release', DEVICE_RELEASE_ENTRIES, DEVICE_RELEASE_SERVICES)
if device_release_runtime_snapshot_v2() != before['runtime']:
die('Device B2 shared contours changed a preserved service generation/runtime')
if device_release_other_compose_services() != before['otherComposeServices']:
die('Device B2 shared contours changed unrelated services')
if any(sha256_file(Path(p)) != digest for p, digest in before['compose'].items()):
die('Device B2 shared contours changed existing Compose files')
if collect_exact_files(DEVICE_PLANE_ROOT, DEVICE_RELEASE_ENTRIES, 'Device B2 shared contours source') != before['installedFiles']:
die('Device B2 shared contours changed application source')
print('device_b2_shared_accept=' + ('rollback' if rollback else 'shared-projects')
+ ';rows=preserved;runtime=unchanged;backup=verified')
def rollback_device_b2_shared(root, backup_dir, entries, stamp, before):
schema = device_b2_schema()
if schema == device_b2_shared_schema():
validate_device_b2_shared_payload(root, entries, DEVICE_B2_SHARED_ID)
device_b2_database((root / DEVICE_B2_SHARED_REVERSE).read_text())
elif schema != DEVICE_B2_EXPECTED_SCHEMA:
die('Device B2 shared contours rollback retains unexpected schema for reconciliation')
restore_platform_overlay(root, backup_dir, entries, stamp)
accept_device_b2_shared(before, backup_dir, rollback=True)
DEVICE_B2_RECON_ID = 'device-b2-foundation-reconciliation-v1-20260930-001'
DEVICE_B2_RECON_ENTRY = 'deployment/device-b2-foundation-reconciliation-v1.json'
DEVICE_B2_002_ID = 'device-b2-foundation-v1-20260930-002'