Files
rust-discovery/deploy/cloud_journal_capacity_upgrade.py
2026-11-13 19:11:45 +01:00

356 lines
27 KiB
Python

#!/usr/bin/env python3
"""Preserving Cloud journal-capacity maintenance; never a job/history reset.
The copy-only mode changes only its own private disposable database copy.
Installation requires a separate explicit option and qualified artifact hashes.
"""
import argparse
import base64
import hashlib
import json
import os
from pathlib import Path
import re
import uuid
import cloud_apt_recovery_upgrade as previous
cloud = previous.cloud
require = cloud.require
STATE = previous.STATE
def copy_reproducer(payload):
import sqlite3
os.umask(0o077)
source = Path('/var/lib/rmm-helper/consumption.sqlite')
state = Path('/var/lib/rmm-agent-remote')
require(json.loads((state/'identity.json').read_text())['agent_id'] == payload['agent_id'], 'fixed permanent Cloud identity')
require(not source.is_symlink() and source.stat().st_uid == 0 and source.stat().st_mode&0o777 == 0o600, 'private original ledger')
def fingerprint(db):
h = hashlib.sha256()
tables = db.execute("SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name").fetchall()
for name,sql in tables:
require(name.replace('_','').isalnum(), 'fixed stored table name')
h.update(json.dumps([name,sql],separators=(',',':')).encode())
for row in db.execute('SELECT * FROM "'+name+'" ORDER BY rowid'):
data = [dict(blob_sha256=hashlib.sha256(v).hexdigest(),length=len(v)) if isinstance(v,bytes) else v for v in row]
h.update(json.dumps(data,separators=(',',':'),ensure_ascii=True).encode())
return h.hexdigest()
def identity():
s = source.stat()
return dict(device=s.st_dev,inode=s.st_ino,size=s.st_size,sha256=hashlib.sha256(source.read_bytes()).hexdigest())
before = identity()
with sqlite3.connect(source.as_uri()+'?mode=ro',uri=True) as original:
require(original.execute('PRAGMA integrity_check').fetchone()[0] == 'ok', 'original integrity')
require({r[0] for r in original.execute('SELECT grant_id FROM consumed')} == set(payload['expected_grants'])
and len(payload['expected_grants']) == 10
and original.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0] == 0
and original.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0] == 0,
'exact ten known closed Root consumptions')
original_fingerprint = fingerprint(original)
folder = Path('/var/lib/rmm-bootstrap-history')/('journal-capacity-copy-'+str(uuid.UUID(payload['id'])))
folder.mkdir(mode=0o700)
copy = folder/'ledger-copy.sqlite'
fd = os.open(copy,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600);os.close(fd)
with sqlite3.connect(copy) as duplicate:
original.backup(duplicate)
require(fingerprint(duplicate) == original_fingerprint, 'consistent private SQLite backup')
page_size = duplicate.execute('PRAGMA page_size').fetchone()[0]
page_count = duplicate.execute('PRAGMA page_count').fetchone()[0]
old = duplicate.execute('PRAGMA max_page_count=16384').fetchone()[0]
require(page_size == 4096 and old == 16384 and page_count <= old, 'actual bounded old capacity')
failed = None
try:
duplicate.execute('BEGIN IMMEDIATE')
duplicate.execute('CREATE TABLE journal_capacity_probe(payload BLOB NOT NULL)')
duplicate.execute('INSERT INTO journal_capacity_probe VALUES(zeroblob(524288))')
except sqlite3.DatabaseError as error:
failed = getattr(error,'sqlite_errorcode',None)
finally:
duplicate.rollback()
require(failed == sqlite3.SQLITE_FULL, 'old limit must fail with SQLITE_FULL13 on the copied actual DB')
require(fingerprint(duplicate) == original_fingerprint, 'old-capacity test rolled back every row/schema change')
bounded = duplicate.execute('PRAGMA max_page_count=65536').fetchone()[0]
require(bounded == 65536, 'bounded 256 MiB new capacity')
duplicate.execute('BEGIN IMMEDIATE')
duplicate.execute('CREATE TABLE journal_capacity_probe(payload BLOB NOT NULL)')
duplicate.execute('INSERT INTO journal_capacity_probe VALUES(zeroblob(524288))')
require(duplicate.execute('SELECT length(payload) FROM journal_capacity_probe').fetchone()[0] == 524288, 'same copied-DB insertion passes at the new finite cap')
duplicate.rollback()
require(fingerprint(duplicate) == original_fingerprint and duplicate.execute('PRAGMA integrity_check').fetchone()[0] == 'ok', 'new-capacity test rolled back every row/schema change')
after = identity()
require(after == before, 'original DB bytes, size and inode must remain unchanged')
report = dict(verified=True,backup=str(folder),old_cap_pages=16384,new_cap_pages=65536,page_size=page_size,page_count=page_count,
old_error_code=failed,probe_bytes=524288,new_insert_passed=True,copy_original_content_sha256=original_fingerprint,
original_before=before,original_after=after,original_unchanged=True,rows_and_schema_rolled_back=True,
original_package_effects=False,original_sql_mutations=False)
fd = os.open(folder/'report.json',os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
with os.fdopen(fd,'w') as stream:json.dump(report,stream,indent=2);stream.flush();os.fsync(stream.fileno())
return report
def install_capacity(payload):
import fcntl
import sqlite3
import time
import traceback
os.umask(0o077)
base = Path('/var/lib/rmm-apt-transaction'); state = Path('/var/lib/rmm-agent-remote')
ledger = Path('/var/lib/rmm-helper/consumption.sqlite')
helper = Path('/usr/local/libexec/rmm-privileged-helper'); agent = Path('/usr/local/libexec/rmm-agent-remote')
provider = Path('/usr/lib/rmm-apt-transaction/apt_transaction.py')
config = Path('/etc/rmm/apt-helper.json'); proof_file = Path('/etc/rmm-remote/apt-helper-proof.json')
metadata = base/'install-metadata.json'; readiness_file = base/'native-readiness.json'
dropin = Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf')
reduced = ('[Service]\nEnvironment=RMM_HELPER_SOCKET=/run/rmm-helper-cloud/control.sock\n'
'Environment=RMM_HELPER_POLICY_FILE=/etc/rmm-remote/general-helper-proof.json\n'
'Environment=RMM_LOG_UNITS_FILE=/etc/rmm-remote/log-units.json\n')
def run(args, **kwargs):
return subprocess.run(args, capture_output=True, check=True, timeout=90, **kwargs).stdout
def sha(raw): return hashlib.sha256(raw).hexdigest()
def canonical(value): return json.dumps(value, sort_keys=True, separators=(',', ':'), ensure_ascii=True).encode()
def fsync_dir(path):
fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try: os.fsync(fd)
finally: os.close(fd)
def create(path, raw, mode=0o600, gid=0):
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode)
with os.fdopen(fd, 'wb') as stream:
stream.write(raw); stream.flush(); os.fchown(stream.fileno(), 0, gid)
os.fchmod(stream.fileno(), mode); os.fsync(stream.fileno())
fsync_dir(path.parent)
def manifest(path):
require(not path.is_symlink(), 'preserved directory symlink')
if not path.exists(): return {'__absent__':True}
require(path.is_dir(), 'preserved directory required')
result = {}
for p in [path, *sorted(path.rglob('*'))]:
s = p.lstat(); require(not p.is_symlink() and (p.is_dir() or p.is_file()), 'preserved regular state required')
result[str(p.relative_to(path))] = dict(dev=s.st_dev, inode=s.st_ino, mode=s.st_mode,
uid=s.st_uid, gid=s.st_gid, sha256=sha(p.read_bytes()) if p.is_file() else None)
return result
def guard():
require(len(payload['expected_grants']) == 10, 'exact ten original closed Root grants')
require(payload['partial_grant'] == '31ef5e5e-5dbd-4428-a9a1-cbd15de2f5a0' and
payload['partial_proof_sha256'] == 'dc3bf3870ea77c12f9291bac712c53ae72194eaec980a8646155a7fb5c0dbe21', 'one reviewed closed partial only')
with sqlite3.connect(ledger.as_uri()+'?mode=ro', uri=True) as db:
require(db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok', 'Root ledger integrity')
require({r[0] for r in db.execute('SELECT grant_id FROM consumed')} == set(payload['expected_grants']) and
db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0] == 0 and
db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'exact known closed Root attempts')
db.row_factory = sqlite3.Row
rows = db.execute('SELECT * FROM apt_partial_reconciliations').fetchall()
require(len(rows) == 1, 'one immutable partial proof required'); row = dict(rows[0])
expected = payload['proof_row']
for key in ('grant_id', 'original_request_sha256', 'previous_outcome_sha256', 'proof_sha256', 'checked_at_ms'):
require(row[key] == expected[key], 'immutable partial binding drift')
for key in ('previous_outcome', 'proof', 'classified_outcome'):
require(sha(row[key]) == expected[key]['sha256'] and len(row[key]) == expected[key]['size'], 'immutable partial bytes drift')
require(row['grant_id'] == payload['partial_grant'] and sha(row['proof']) == payload['partial_proof_sha256'], 'closed proof hash required')
proof = json.loads(row['proof']); result = db.execute('SELECT c.result,e.outcome,e.start FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE grant_id=?', (payload['partial_grant'],)).fetchone()
classified = json.loads(row['classified_outcome'])
require(json.loads(result['result']) == json.loads(result['outcome']) == classified and
classified['state'] == 'failed' and classified['error_code'] == 'APT_PARTIAL_UNCONFIGURED_VERIFIED', 'normal signed RootSync closure required')
saved = json.loads(result['start'])
# save_start nests Request through serde_json::Value (sorted keys),
# whereas the immutable proof hashes serde's Request struct codec.
request_fields = ('job_bytes','permit_bytes','signature','plan_hash','authorization_id')
require(set(saved['request']) == set(request_fields), 'original Request fields required')
request_bytes = json.dumps({key:saved['request'][key] for key in request_fields},
separators=(',', ':'), ensure_ascii=False).encode()
require(sha(request_bytes) == row['original_request_sha256'], 'original signed request hash required')
require(proof['grant_id'] == payload['partial_grant'] and proof['attempt_id'] == payload['partial_attempt'] and
proof['snapshot']['rmm-mvp-test:all'] == {'version':'1.0','status':'install ok unpacked'}, 'exact partial fixture required')
with sqlite3.connect((state/'journal.sqlite').as_uri()+'?mode=ro', uri=True) as db:
require(db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok' and
db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0] == 0 and
db.execute('SELECT count(*) FROM reconcile_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'no unresolved or faulted agent attempt')
require(Path('/usr/share/rmm-mvp-test/version').read_bytes() == b'1.0\n', 'existing unpacked fixture payload required')
require(not list(base.glob('acceptance-*.json')) and not list(base.glob('pending-acceptance-*.json')) and
not list(base.glob('recovery-*.json')) and not list(base.glob('pending-recovery-*.json')), 'no existing acceptance or recovery attempt')
return proof
def bound_partial(proof):
keys = 'transaction_sha256 job_payload_sha256 effect_armed_ledger_id grant_id job_id attempt_id authorization_id plan_sha256'.split()
value = json.loads(run(['/usr/bin/python3','-I',str(provider),'verify-partial'], input=canonical({k:proof[k] for k in keys})))
require(value['effect_state'] == 'known_failed' and value['code'] == 'APT_PARTIAL_UNCONFIGURED_VERIFIED' and
{k:v for k,v in value['proof'].items() if k != 'checked_at_ms'} ==
{k:v for k,v in proof.items() if k != 'checked_at_ms'}, 'fresh original-bound partial state required')
require(json.loads((state/'identity.json').read_text())['agent_id'] == payload['agent_id'], 'permanent Cloud identity required')
require(not dropin.is_symlink() and dropin.read_text() == reduced, 'APT endpoint must remain absent')
require(run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','--value']).strip() == b'inactive', 'APT helper must be stopped')
for name, pin in payload['old_files'].items():
p = Path(name); require(not p.is_symlink() and p.stat().st_uid == 0 and p.stat().st_mode & 0o022 == 0 and sha(p.read_bytes()) == pin, 'installed file drift')
require(Path('/etc/systemd/system/rmm-apt-helper.service').read_text() == payload['unit'], 'exact reviewed APT service required')
markers = {base/'bootstrap-attempt-rmm-apt-fixture-install-1.0.json':'ec84aa4df8065f0b54b282bd273a6f342f07a6d47312289608389d05c7fb6053',
base/'bootstrap-retry-rmm-apt-fixture-install-1.0.json':'a118072812553dca1a8ffee84550be0b2d514d555ad7cbcfc4722675a211a0e3'}
require({p.name for p in base.glob('bootstrap-*.json')} == {p.name for p in markers} and
all(sha(p.read_bytes()) == pin for p,pin in markers.items()), 'original/retry tombstones required')
partial = guard(); bound_partial(partial)
raw = {name:base64.b64decode(payload[name], validate=True) for name in ('helper','agent')}
pins = {'helper':payload['helper_sha256'],'agent':payload['agent_sha256'],
'provider':'5ff260f75cb2959678f6be729d3a806a4ea29febd5ae4003317fed41afc25327'}
require(all(sha(raw[name]) == pins[name] for name in ('helper','agent')) and
sha(provider.read_bytes()) == pins['provider'], 'qualified exact capacity binaries and unchanged provider required')
for name in ('helper','agent'):
require(raw[name][:6] == b'\x7fELF\x02\x01' and raw[name][18:20] == b'\x3e\x00', 'Linux x64 binary required')
if payload.get('preflight_only') is True:
return dict(stage='preflight_validated', completed=False, preflight_only=True,
package_effects=False, maintenance_mutations=False, expected_grants=len(payload['expected_grants']),
partial_proof_sha256=payload['partial_proof_sha256'], apt_endpoint_enabled=False)
backup = Path('/var/lib/rmm-bootstrap-history')/('cloud-journal-capacity-'+payload['id']); backup.mkdir(mode=0o700)
report = dict(stage='validated', completed=False, backup=str(backup), package_effects=False,
apt_endpoint_enabled=False, partial_proof_sha256=payload['partial_proof_sha256'], backups={})
def save():
next_path = backup/'.report-next.json'; create(next_path,json.dumps(report,indent=2).encode())
os.replace(next_path,backup/'report.json'); fsync_dir(backup)
def replace(path, staged=None):
target = backup/str(path).lstrip('/').replace('/','__')
require(not path.is_symlink() and path.stat().st_dev == backup.stat().st_dev and not target.exists(), 'exclusive same-filesystem versioned replacement')
os.rename(path,target); fsync_dir(path.parent); fsync_dir(backup)
report['backups'][str(path)] = str(target); save()
if staged is not None:
os.rename(staged,path); fsync_dir(path.parent); fsync_dir(backup)
lock = None
try:
save()
for name in ('helper','agent'): create(backup/(name+'-next'), raw[name], 0o755)
for name in ('helper','agent'): run(['/lib64/ld-linux-x86-64.so.2','--list',str(backup/(name+'-next'))])
run(['systemctl','stop','rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'])
lock = os.open(ledger.parent/'executor.lock', os.O_RDWR | os.O_NOFOLLOW)
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
partial = guard(); bound_partial(partial)
preserved = {str(p):manifest(p) for p in (state,Path('/var/lib/rmm-agent'),ledger.parent)}
preserved_base = manifest(base)
witnesses = {}
for p in (Path('/var/lib/dpkg/status'),Path('/usr/share/rmm-mvp-test/version'),dropin,
Path('/etc/systemd/system/rmm-apt-helper.service'),Path('/usr/lib/rmm-apt-transaction/hook'),
Path('/etc/rmm-remote/general-helper-proof.json'),Path('/etc/rmm-remote/helper-general.json'),provider,metadata):
s = p.stat(); witnesses[str(p)] = (s.st_dev,s.st_ino,sha(p.read_bytes()))
general_bytes = Path('/etc/rmm-remote/general-helper-proof.json').read_bytes()
general_config = Path('/etc/rmm-remote/helper-general.json').read_bytes()
old_metadata = json.loads(metadata.read_text()); old_config = json.loads(config.read_text())
require(old_metadata['schema_version'] == 2 and old_metadata['provider_sha256'] == sha(provider.read_bytes()) and
old_metadata['hook_sha256'] == sha(Path('/usr/lib/rmm-apt-transaction/hook').read_bytes()) and
old_metadata['unit_sha256'] == sha(payload['unit'].encode()), 'old install metadata required')
for path,name in ((helper,'helper'),(agent,'agent')): replace(path,backup/(name+'-next'))
command = ['/usr/bin/python3','-I',str(provider)]
config_bytes = run(command+['print-config']); config_sha = sha(config_bytes)
require(config_sha == payload['reviewed_config_sha256'] and all(line in config_bytes for line in (
b'DPkg::ConfigurePending "false";', b'DPkg::TriggersPending "false";', b'dpkg::selection::remove::approved "false";')), 'reviewed exact configure/no-pending settings')
runtime = json.loads(run(command+['inspect-runtime'])); runtime_sha = sha(canonical(runtime))
old_config['apt_provider'].update(runtime_sha256=runtime_sha,apt_config_sha256=config_sha)
create(backup/'config-next',json.dumps(old_config).encode()); replace(config,backup/'config-next')
pin = json.loads(run([str(helper),'--inspect-apt-profile'],env=dict(os.environ,RMM_HELPER_CONFIG=str(config))))
require(pin['identity'] == json.loads(general_bytes)['identity'], 'same helper identity required')
create(backup/'proof-next',json.dumps(pin).encode(),0o640,984); replace(proof_file,backup/'proof-next')
general = json.loads(run([str(helper),'--inspect-general-profile'],env=dict(os.environ,RMM_HELPER_CONFIG='/etc/rmm-remote/helper-general.json')))
require(general == json.loads(general_bytes) and Path('/etc/rmm-remote/helper-general.json').read_bytes() == general_config, 'General helper policy unchanged')
for name,values in preserved.items(): require(manifest(Path(name)) == values, 'identity/journal/Root ledger bytes or inode changed')
def preserved_original_base():
current = manifest(base)
require(all(current.get(name) == value for name,value in preserved_base.items() if name not in ('install-metadata.json','native-readiness.json')), 'original APT state/archive/hook/probe changed')
for name,value in witnesses.items():
p=Path(name);s=p.stat();require((s.st_dev,s.st_ino,sha(p.read_bytes())) == value, 'original package/control file bytes or inode changed')
return current
preserved_original_base(); bound_partial(guard())
report.update(stage='installed_preserved',preserved_state=True,helper_sha256=pins['helper'],agent_sha256=pins['agent'],journal_page_size=4096,journal_max_page_count=65536,journal_capacity_bytes=268435456,
provider_sha256=pins['provider'],runtime_sha256=runtime_sha,apt_config_sha256=config_sha,apt_pin=pin,general_pin=general)
save()
# No client can reach this endpoint; it exists only for the service's
# ExecStartPre copied-database probe, then is stopped before restart.
run(['systemd-analyze','verify','/etc/systemd/system/rmm-apt-helper.service'])
replace(readiness_file)
# Daemon startup itself takes executor.lock to open the existing DB.
# Installation is complete; agent/General remain stopped during probe.
fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock);lock=None
run(['systemctl','start','rmm-apt-helper.service'])
for _ in range(3):
status = run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','-p','SubState','-p','NRestarts']).decode()
require('ActiveState=active\n' in status and 'SubState=running\n' in status and 'NRestarts=0\n' in status, 'stable native probe helper required')
time.sleep(1)
run(['systemctl','stop','rmm-apt-helper.service'])
lock = os.open(ledger.parent/'executor.lock', os.O_RDWR | os.O_NOFOLLOW)
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
ready = json.loads(readiness_file.read_text())
require(ready['runtime'] == runtime and ready['readiness_only'] is True and ready['native_acceptance'] is False and
ready['host_status_sha256'] == sha(Path('/var/lib/dpkg/status').read_bytes()), 'exact new copied-database probe required')
workspace = base/'probes'/str(uuid.UUID(ready['probe_id']))
require((workspace/'dpkg/status').read_bytes() == Path('/var/lib/dpkg/status').read_bytes() and
(workspace/'apt.conf').read_bytes().count(b'dpkg::selection::remove::approved "false";') == 1,
'partial copied status and configure fix required')
current = preserved_original_base()
require(all(name in preserved_base or name.startswith('probes/'+ready['probe_id']+'/') or name == 'probes/'+ready['probe_id']
for name in current), 'only generated readiness probe metadata may be added')
for name,values in preserved.items(): require(manifest(Path(name)) == values, 'journal/ledger bytes or inode changed during probe')
bound_partial(guard())
require(dropin.read_text() == reduced and Path('/etc/rmm-remote/general-helper-proof.json').read_bytes() == general_bytes, 'endpoint/General proof unchanged')
report.update(stage='native_probe_preserved',readiness_only=True,native_acceptance=False,
journals_preserved_before_restart=True,readiness_sha256=sha(readiness_file.read_bytes()),probe_id=ready['probe_id'])
save()
fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock);lock=None
run(['systemctl','start','rmm-helper-cloud.service','rmm-agent-remote.service'])
require(run(['systemctl','show','rmm-apt-helper.service','-p','ActiveState','--value']).strip() == b'inactive', 'APT endpoint must remain stopped')
report.update(stage='qualified_runtime_started',completed=True);save()
except Exception as error:
report.update(error_class=type(error).__name__,frames=[dict(function=f.name,line=f.lineno) for f in traceback.extract_tb(error.__traceback__)])
save()
finally:
if lock is not None: fcntl.flock(lock,fcntl.LOCK_UN);os.close(lock)
subprocess.run(['systemctl','stop','rmm-apt-helper.service'],capture_output=True,timeout=30)
return report
def main():
parser = argparse.ArgumentParser(description=__doc__)
mode = parser.add_mutually_exclusive_group(required=True)
mode.add_argument('--copy-only',action='store_true')
mode.add_argument('--preflight-only',action='store_true')
mode.add_argument('--install-reviewed-artifacts',action='store_true')
parser.add_argument('--agent-sha256');parser.add_argument('--helper-sha256')
args = parser.parse_args();os.umask(0o077)
require(previous.previous.apt.prior.evidence('gate')['unresolved'] == 0, 'no unsettled central job')
payload = previous.build_payload()
require(len(payload['expected_grants']) == 10, 'exact completed Root/log history')
if args.copy_only:
report = cloud.bootstrap.rpc('linux-vm.conf',copy_reproducer,dict(id=str(uuid.uuid4()),agent_id=payload['agent_id'],expected_grants=payload['expected_grants']))
cloud.contract.write_private(STATE/('journal-capacity-copy-'+Path(report['backup']).name.removeprefix('journal-capacity-copy-')+'.json'),report)
print({k:report[k] for k in ('verified','old_error_code','new_insert_passed','original_unchanged','rows_and_schema_rolled_back','original_sql_mutations')})
return
require(not (STATE/'journal-capacity-runtime.json').exists(), 'existing capacity runtime retained; no automatic maintenance repeat')
reviewed = {'agent':'2ce885cf5e6881e77383482b7d3a87a02c6847aa26b82e5fd70d2ebd8a45360d',
'helper':'332eae004d9416234eabf7ed3d5c9ece5440d1e0c764ea63357961c16b673b6b'}
for name,pin in reviewed.items():
require(getattr(args,name+'_sha256') == pin and re.fullmatch('[0-9a-f]{64}',pin), 'independently reviewed capacity artifact pin required')
raw = (STATE/(name+'-journal-capacity')).read_bytes()
require(hashlib.sha256(raw).hexdigest() == pin, 'qualified capacity artifact drift')
payload[name] = base64.b64encode(raw).decode();payload[name+'_sha256'] = pin
payload.pop('provider')
previous_runtime = cloud.contract.protected_json(STATE/'configure-runtime.json')
require(previous_runtime['completed'] and previous_runtime['helper_sha256'] == previous.PINS['helper']
and previous_runtime['agent_sha256'] == previous.PINS['agent'] and previous_runtime['provider_sha256'] == previous.PINS['provider'], 'exact independently qualified previous runtime')
installed = cloud.contract.protected_json(cloud.ROOT/'devstate/remote-vm.json')
require(installed['binary_sha256'] == previous.PINS['agent'], 'old permanent agent record required')
policy = cloud.bootstrap.rpc('server.conf',previous.closed_policy,{})
old = cloud.bootstrap.rpc('linux-vm.conf',previous.installed_metadata,previous.PINS)
payload.update(old_files=old['files'],preflight_only=args.preflight_only)
report = cloud.bootstrap.rpc('linux-vm.conf',install_capacity,payload)
if args.preflight_only:
require(report.get('preflight_only') and report.get('maintenance_mutations') is False, 'read-only maintenance preflight required')
cloud.contract.write_private(STATE/('journal-capacity-preflight-'+payload['id']+'.json'),dict(report,closed_policy=policy))
print(report);return
cloud.contract.write_private(STATE/('journal-capacity-install-'+payload['id']+'.json'),dict(report,closed_policy=policy))
require(report['completed'] and report['preserved_state'] and report['journals_preserved_before_restart'] and report['provider_sha256'] == previous.PINS['provider'], 'preserving bounded-capacity installation incomplete; retain report and do not repeat')
previous.previous.record_agent_upgrade(report,installed)
cloud.contract.write_private(STATE/'journal-capacity-runtime.json',report)
print({k:report[k] for k in ('completed','stage','preserved_state','journals_preserved_before_restart','readiness_only','apt_endpoint_enabled')})
if __name__ == '__main__':
try:main()
except Exception as error:
print('CLOUD_JOURNAL_CAPACITY_INCOMPLETE:'+type(error).__name__)
if str(error).startswith('remote bootstrap incomplete: '):print(str(error))
raise SystemExit(2) from None