121 lines
7.5 KiB
Python
121 lines
7.5 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 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=m.protected_json(BASE/'configure-runtime.json')
|
|
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(BASE/'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])
|
|
old=m.protected_json(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(BASE/'recovery-policy.json',dict(record,policy=policy))
|
|
print('Fixed APT runtime endpoint and expiring limits enabled; no job or approval created')
|
|
|
|
|
|
def accept(case):
|
|
m=cloud.contract;target=prior.prior.identity()
|
|
require(prior.prior.evidence('gate')['unresolved']==0,'no unresolved attempt')
|
|
# 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 None
|
|
prior.accept(case,request=request,folder_root=STATE)
|
|
|
|
|
|
def close():
|
|
m=cloud.contract;require(prior.prior.evidence('gate')['unresolved']==0,'no unresolved attempt')
|
|
record=m.protected_json(BASE/'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(BASE/'recovery-policy-closed.json',closed)
|
|
ready=m.protected_json(BASE/'configure-runtime.json')
|
|
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(BASE/'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)
|
|
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)
|
|
else:prior.finish(args.case,folder_root=STATE)
|
|
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: ') else 'CLOUD_APT_RECOVERY_INCOMPLETE:'+type(error).__name__)
|
|
raise SystemExit(2) from None
|