Files
rust-discovery/deploy/cloud_apt_recovery.py
T

179 lines
12 KiB
Python

#!/usr/bin/env python3
"""Exact configure-only recovery followed by separate fixture upgrade/noops."""
import argparse
import datetime as dt
import hashlib
import json
import os
import time
import uuid
import cloud_apt_retry as prior
import cloud_root_evidence
import cloud_root_policy
cloud=prior.cloud
require=cloud.require
BASE=cloud.ROOT/'devstate/cloud-apt-recovery'
STATE=BASE/'cases'
CASES=('apt_configure_recovery','apt_native_update','ensure_package','apply_updates')
def runtime_session():
configured=BASE/'configured-reconcile-runtime.json'
if configured.exists():
report=cloud.contract.protected_json(configured)
require(report['completed'] and report['preserved_state'] and report['readiness_only'] and
not report['native_acceptance'] and report['unknown_grant']=='33775478-15b0-46ec-b059-44f500dbb0b6',
'qualified preserving configured-reconciliation runtime')
return report,BASE/'configured-reconcile-runtime'
latest=BASE/'journal-capacity-runtime.json'
if latest.exists():
report=cloud.contract.protected_json(latest)
require(report['completed'] and report['preserved_state'],'qualified preserved capacity runtime')
return report,BASE/'capacity-recovery'
return cloud.contract.protected_json(BASE/'configure-runtime.json'),BASE
def recovery_request(device,target):
m=cloud.contract;data=prior.adapters.planning_from_device(device,recovery=True)
closed=m.protected_json(BASE/'readonly-central-result.json')['result']
reference=dict(schema_version=1,previous_grant_id=closed['result']['grant_id'],previous_attempt_id=closed['attempt_id'],
partial_proof_sha256=closed['result']['evidence']['apt']['reconciliation_proof_sha256'])
require(closed['state']=='failed' and closed['result']['error_code']=='APT_PARTIAL_UNCONFIGURED_VERIFIED' and
data['native_recovery']==reference,'exact previously closed partial reference')
pin=prior.adapters.PINS['1.0'];start=m.now();receipt=data['prefetch'][pin]
require(receipt['schema_version']==1 and receipt['sha256']==pin and receipt['size']==20672 and
receipt['sealed_at_ms']<=int(start.timestamp()*1000),'actual pregrant archive receipt')
tx=dict(schema_version=2,operation='configure_only',recipe_id='rmm-apt-fixture-install-1.0',recipe_version='1',
platform='ubuntu-24.04-amd64',resolver_config_version='rmm-apt-local-v1',no_reboot=True,
status_fingerprint=data['status_fingerprint'],baseline_sha256=data['baseline_sha256'],packages=[
dict(name='rmm-mvp-test',architecture='all',old_version='1.0',new_version='1.0',origin='rmm-test',sha256=pin,
size=20672,archive_path='/var/lib/rmm-apt-transaction/archives/'+pin+'.deb')])
raw=prior.adapters.canonical(tx)
manifest=dict(type='rmm.apt.helper-action',schema_version=4,action='apt-native-acceptance',execution_profile='dev-test',
transaction_sha256=hashlib.sha256(raw).hexdigest(),resolved_apt_transaction=tx,transaction_bytes=list(raw),
native_recovery=reference,test_result_id='configure-recovery-'+uuid.uuid4().hex,
**{k:data[k] for k in ('provider_sha256','hook_sha256','apt_config_sha256','runtime_sha256')})
m.validate_apt_manifest(manifest)
return dict(tenant_id=target['tenant_id'],action='apt_native_acceptance',targets=dict(asset_ids=[target['asset_id']]),
parameters=dict(execution_identity='root',timeout_seconds=300,max_output_bytes=65536,
recipe_id=tx['recipe_id'],recipe_version='1',apt_manifest=manifest),
schedule=m.schedule(start,start+dt.timedelta(minutes=15)),rollout=dict(max_parallel=1,halt_after_failures=1),
reason='Configure ONLY the exactly proven unpacked fixture1.0; new attempt and separate recovery record; no unpack, pending packages, reinstall or reboot')
def enable():
m=cloud.contract;require(prior.prior.evidence('gate')['unresolved']==0,'no unresolved attempt')
ready,session=runtime_session();session.mkdir(mode=0o700,exist_ok=True)
require(ready['completed'] and ready['readiness_only'] and ready['preserved_state'] and not ready['native_acceptance'],
'qualified native probe, no invented package acceptance')
target=prior.prior.identity();pin=ready['apt_pin'];identity=pin['identity']
require(all(identity[k]==target[k] for k in target),'same permanent Cloud VM')
endpoint=cloud.bootstrap.rpc('linux-vm.conf',prior.switch_endpoint,dict(id=str(uuid.uuid4()),enabled=True,
agent_id=target['agent_id'],binary_sha256=ready['helper_sha256']))
m.write_private(session/'recovery-endpoint-enabled.json',endpoint)
peers=cloud.bootstrap.rpc('linux-vm.conf',cloud_root_evidence.inspect_helpers,target)
require(peers['ledger']['faults']==0 and peers['apt']['provider_stack_sha256']==pin['provider_stack_sha256'],
'actual fixed helper peer and stack')
scope={k:identity[k] for k in ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version')}
scope.update(principals=['simulated-testprincipal-a'],helper_policy_sha256=pin['provider_stack_sha256'],
acceptance_evidence_id='cloud-configure-recovery-probe:'+ready['readiness_sha256'],allowed_services=[],allow_root_scripts=False,allow_reboot=False,
apt=dict(allow_native_acceptance=True,allow_execute=True,runtime_sha256=ready['runtime_sha256'],
apt_config_sha256=ready['apt_config_sha256'],provider_sha256=ready['provider_sha256'],
hook_sha256=hashlib.sha256((cloud.ROOT/'tools/apt-transaction/hook').read_bytes()).hexdigest()))
policy=dict(schema_version=1,profile='development',expires_at=(m.now()+dt.timedelta(hours=1)).isoformat(),scopes=[scope])
if session==BASE/'configured-reconcile-runtime':
old=m.protected_json(BASE/'capacity-recovery/recovery-policy-closed-unknown.json')
prior_checkpoint=m.protected_json(BASE/'capacity-recovery/cases/apt_configure_recovery/checkpoint.json')
closed=cloud.Backend().call('jobs.get',dict(tenant_id=cloud.TENANT,job_id=prior_checkpoint['plan']['jobs'][0]['job_id']))
require(closed['state']=='failed' and closed['result']['error_code']=='APT_RECOVERY_CONFIGURED_ACCEPTANCE_REBOOT_FLAG_VERIFIED' and
closed['result']['grant_id']=='33775478-15b0-46ec-b059-44f500dbb0b6',
'ordinary signed Sync must first close the exact historical configured effect as failed')
else:
old=m.protected_json(BASE/'recovery-policy-closed.json' if session!=BASE else cloud.ROOT/'devstate/cloud-logs/policy-closed.json')
record=cloud.bootstrap.rpc('server.conf',prior.replace_policy,dict(id=str(uuid.uuid4()),previous_sha256=old['policy_sha256'],policy=policy))
m.write_private(session/'recovery-policy.json',dict(record,policy=policy))
print('Fixed APT runtime endpoint and expiring limits enabled; no job or approval created')
def unsubmitted_preparation(payload):
import re
root=Path('/opt/rust-discovery');ident=payload['test_result_id']
require(isinstance(ident,str) and re.fullmatch('[A-Za-z0-9_-]{1,128}',ident),'fixed preparation test ID')
query="SELECT count(*) FROM operation_plans WHERE tenant_id='11111111-1111-4111-8111-111111111111' AND position('"+ident+"' in convert_from(payload,'UTF8'))>0;"
cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),
'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm']
count=int(subprocess.run(cmd,input=query.encode(),capture_output=True,check=True,timeout=15).stdout.strip())
return dict(persisted_plans=count)
def accept(case,resume_unsubmitted=False):
m=cloud.contract;target=prior.prior.identity()
require(prior.prior.evidence('gate')['unresolved']==0,'no unresolved attempt')
ready,session=runtime_session();cases=session/'cases';cases.mkdir(mode=0o700,exist_ok=True)
require(session!=BASE/'configured-reconcile-runtime' or case!='apt_configure_recovery',
'configured historical1.0 is never executed again; next action is separate native1.1 upgrade')
if session!=BASE and case=='apt_configure_recovery':
original=m.protected_json(STATE/case/'checkpoint-capacity-terminal.json')
old_job=original['plan']['jobs'][0]
old_result=cloud.Backend().call('jobs.get',dict(tenant_id=cloud.TENANT,job_id=old_job['job_id']))
counts=prior.prior.evidence('gate',job_id=old_job['job_id'])
require(original['not_started_capacity_block'] and old_result['state'] in ('expired','cancelled') and counts['permits']==0,
'preserve original approved no-start; new attempt requires fresh UI approval')
folder=cases/case
if resume_unsubmitted:
require(folder.is_dir() and {p.name for p in folder.iterdir()}=={'prepare.json'},'only an unsubmitted UI preparation may be archived')
old=m.protected_json(folder/'prepare.json')
require(old['case']==case and old['phase']=='prepare' and all(old[k]==target[k] for k in target),'same fixed prior preparation target')
untouched=cloud.bootstrap.rpc('server.conf',unsubmitted_preparation,dict(test_result_id=old['request']['parameters']['apt_manifest']['test_result_id']))
require(untouched==dict(persisted_plans=0),'existing plan must be reviewed, never silently replaced')
archived=cases/(case+'.unsubmitted-ui-'+uuid.uuid4().hex)
os.rename(folder,archived);m.write_private(archived/'not-submitted.json',untouched)
# A maintenance restart only refreshes passive inventory from the preserved
# agent; it never issues a package diagnostic or executes a job.
deadline=time.monotonic()+90
while True:
device=cloud.Backend().call('inventory.get_device',{k:target[k] for k in ('tenant_id','asset_id')})
try:
data=prior.adapters.planning_from_device(device,recovery=case=='apt_configure_recovery')
break
except m.GuardError:
require(time.monotonic()<deadline,'fresh stored exact planning unavailable; no job submitted');time.sleep(3)
request=recovery_request(device,target) if case=='apt_configure_recovery' else m.case_input(case,target,planning=data)
if data['host_reboot_required']:
request['reason']+='; Host reboot already required; flag unchanged; no reboot in this job'
if request is not None and session==BASE/'capacity-recovery':
request['reason']+='; prior fixed plan expired with zero grants at the old journal capacity; preserve it and authorize this fresh plan separately'
prior.accept(case,request=request,folder_root=cases)
def close():
m=cloud.contract;require(prior.prior.evidence('gate')['unresolved']==0,'no unresolved attempt')
ready,session=runtime_session();record=m.protected_json(session/'recovery-policy.json')
closed=cloud.bootstrap.rpc('server.conf',cloud_root_policy.close_policy,dict(expected_sha256=record['policy_sha256'],backup=record['backup']))
m.write_private(session/'recovery-policy-closed.json',closed)
endpoint=cloud.bootstrap.rpc('linux-vm.conf',prior.switch_endpoint,dict(id=str(uuid.uuid4()),enabled=False,
agent_id=prior.prior.identity()['agent_id'],binary_sha256=ready['helper_sha256']))
m.write_private(session/'recovery-endpoint-disabled.json',endpoint)
print('APT test limits closed and endpoint stopped; original histories retained')
def main():
p=argparse.ArgumentParser(description=__doc__);p.add_argument('action',choices=('enable','accept','finish','close'));p.add_argument('--case',choices=CASES)
p.add_argument('--resume-unsubmitted-ui',action='store_true',help='Preserve a failed preparation only after proving no plan was persisted')
args=p.parse_args();os.umask(0o077);STATE.mkdir(mode=0o700,exist_ok=True)
if args.action in ('accept','finish'):
require(args.case is not None,'one fixed case required')
if args.action=='accept':accept(args.case,args.resume_unsubmitted_ui)
else:prior.finish(args.case,folder_root=runtime_session()[1]/'cases')
else:(enable if args.action=='enable' else close)()
if __name__=='__main__':
try:main()
except Exception as error:
message=str(error)
print(message if message.startswith(('remote bootstrap incomplete: ','APT_UI_INCOMPLETE:source_lines=')) else 'CLOUD_APT_RECOVERY_INCOMPLETE:'+type(error).__name__)
raise SystemExit(2) from None