390 lines
28 KiB
Python
390 lines
28 KiB
Python
#!/usr/bin/env python3
|
|
"""Preserving Linux shared-core daemon upgrade, without jobs or APT effects.
|
|
|
|
Planned, not executed: prepare; install CASE; finish CASE. Install is single-use;
|
|
an interrupted/lost reply permits finish/readback only, never an automatic retry.
|
|
The caller separately qualifies actual mTLS/Sync/MCP after the retained report.
|
|
"""
|
|
import argparse
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import uuid
|
|
|
|
import cloud_reboot_acceptance as reboot
|
|
|
|
cloud = reboot.cloud
|
|
require = cloud.contract.require
|
|
ROOT = cloud.ROOT
|
|
STATE = ROOT/'devstate/shared-core/runtime-upgrade'
|
|
ARTIFACTS = ROOT/'devstate/shared-core/durable-outcome-v1'
|
|
REMOTE = '/var/lib/rmm-bootstrap-history/shared-core-'
|
|
EXPECTED_NEW = {
|
|
'agent': 'c9c09de0ecb52b556d707cf45accb69186c73b8ec9baeeabfaeff220fafb1d4e',
|
|
'helper': '51c3142ab332216d6a94ff64ff15cbc37ee1d5a99896c14c87bb6f62e63bb946',
|
|
}
|
|
|
|
|
|
def sha(raw):
|
|
return hashlib.sha256(raw).hexdigest()
|
|
|
|
|
|
def vm(payload):
|
|
"""Fixed bootstrap operations; source is sent by the pinned existing SSH RPC."""
|
|
import contextlib
|
|
import fcntl
|
|
import sqlite3
|
|
import stat
|
|
import traceback
|
|
os.umask(0o077)
|
|
ident = str(uuid.UUID(payload['id']))
|
|
require(ident == payload['id'] and os.geteuid() == 0, 'canonical root bootstrap case')
|
|
action = payload['action']
|
|
require(action in ('prepare', 'install', 'read'), 'fixed shared-core operation')
|
|
folder = Path('/var/lib/rmm-bootstrap-history')/('shared-core-'+ident)
|
|
state = Path('/var/lib/rmm-agent-remote'); helper_state = Path('/var/lib/rmm-helper')
|
|
agent = Path('/usr/local/libexec/rmm-agent-remote'); helper = Path('/usr/local/libexec/rmm-privileged-helper')
|
|
db_paths = (state/'journal.sqlite', state/'consumption.sqlite', helper_state/'consumption.sqlite')
|
|
paths = dict(agent=agent, helper=helper)
|
|
intent = payload['intent']
|
|
def digest(raw): return hashlib.sha256(raw).hexdigest()
|
|
def encode(value): return json.dumps(value, sort_keys=True, separators=(',', ':')).encode()
|
|
def run(args, **kwargs):
|
|
return subprocess.run(args, capture_output=True, check=True, timeout=15, **kwargs).stdout
|
|
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):
|
|
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.fchmod(stream.fileno(), mode); os.fsync(stream.fileno())
|
|
fsync_dir(path.parent)
|
|
def owned(path, directory=False):
|
|
value = path.lstat()
|
|
require(not path.is_symlink() and value.st_uid == 0 and not value.st_mode&0o022
|
|
and (stat.S_ISDIR(value.st_mode) if directory else stat.S_ISREG(value.st_mode)), 'fixed root-owned bootstrap path')
|
|
return value
|
|
def file(path, secret=False):
|
|
value = path.lstat()
|
|
require(not path.is_symlink() and stat.S_ISREG(value.st_mode) and value.st_nlink == 1, 'preserved regular file')
|
|
result = dict(device=value.st_dev, inode=value.st_ino, mode=value.st_mode, uid=value.st_uid,
|
|
gid=value.st_gid, size=value.st_size, mtime_ns=value.st_mtime_ns, ctime_ns=value.st_ctime_ns)
|
|
# Device private keys are never opened or exported by this upgrader.
|
|
if not secret: result['sha256'] = digest(path.read_bytes())
|
|
return result
|
|
def tree(path, skip_databases=False, metadata_only=False):
|
|
if not path.exists(): return {'absent': True}
|
|
result = {}
|
|
for item in [path, *sorted(path.rglob('*'))]:
|
|
value = item.lstat(); require(not item.is_symlink(), 'no symlinks in preserved state')
|
|
if item in db_paths or str(item).endswith(('-journal', '-wal', '-shm')):
|
|
if skip_databases: continue
|
|
if stat.S_ISDIR(value.st_mode):
|
|
result[str(item.relative_to(path))] = dict(device=value.st_dev, inode=value.st_ino,
|
|
mode=value.st_mode, uid=value.st_uid, gid=value.st_gid)
|
|
else:
|
|
require(stat.S_ISREG(value.st_mode), 'no unexpected state object')
|
|
secret = metadata_only or item.suffix == '.key' or 'private' in item.name.lower() or 'token' in item.name.lower()
|
|
result[str(item.relative_to(path))] = file(item, secret)
|
|
return result
|
|
@contextlib.contextmanager
|
|
def database(path):
|
|
value = path.lstat()
|
|
require(not path.is_symlink() and stat.S_ISREG(value.st_mode) and value.st_nlink == 1
|
|
and value.st_uid == (993 if path.parent == state else 0), 'fixed database owner/inode')
|
|
# These qualified Linux journals use DELETE mode. Refuse WAL state rather
|
|
# than create root-owned sidecars while opening an unprivileged journal.
|
|
require(all(not Path(str(path)+suffix).exists() and not Path(str(path)+suffix).is_symlink()
|
|
for suffix in ('-wal', '-shm', '-journal')), 'settled DELETE journal required')
|
|
with contextlib.closing(sqlite3.connect(path.as_uri()+'?mode=ro', uri=True)) as db:
|
|
db.execute('PRAGMA query_only=ON'); db.execute('BEGIN')
|
|
require(db.execute('PRAGMA journal_mode').fetchone()[0] == 'delete', 'qualified DELETE journal')
|
|
require(db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok', 'journal integrity')
|
|
yield db
|
|
require((path.stat().st_dev, path.stat().st_ino) == (value.st_dev, value.st_ino), 'database inode preserved')
|
|
def cells(row):
|
|
return [dict(blob_sha256=digest(v), length=len(v)) if isinstance(v, bytes) else v for v in row]
|
|
def rows(db, table):
|
|
require(table.replace('_', '').isalnum(), 'SQLite identifier')
|
|
return {str(row[0]): digest(encode(cells(row[1:])))
|
|
for row in db.execute('SELECT rowid,* FROM "'+table+'" ORDER BY rowid')}
|
|
def sql_witness():
|
|
out = {}
|
|
mutable = {'counter', 'pending', 'reconcile_meta', 'helper_sync_meta', 'helper_sync_rows', 'reconcile_requests', 'job_events'}
|
|
for path in db_paths:
|
|
with database(path) as db:
|
|
schemas = {name: sql for name, sql in db.execute("SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name")}
|
|
all_rows = {name: rows(db, name) for name in schemas}
|
|
value = dict(schema=schemas, rows=all_rows,
|
|
immutable={name: value for name, value in all_rows.items() if name not in mutable})
|
|
if path.name == 'journal.sqlite':
|
|
require(db.execute("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')").fetchone()[0] == 0, 'no unfinished agent job')
|
|
require(db.execute('SELECT count(*) FROM reconcile_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'no agent fault')
|
|
value['sequence'] = db.execute('SELECT seq FROM counter WHERE id=1').fetchone()[0]
|
|
meta = db.execute('SELECT journal_id,checkpoint,high_sequence,generation FROM reconcile_meta WHERE id=1').fetchone()
|
|
value['meta'] = list(meta)
|
|
# Completed signed requests are immutable; a pending request
|
|
# may acquire its response, but its original bytes must remain.
|
|
value['requests'] = {r[0]: dict(request=digest(r[1]), envelope=digest(r[2]), generation=r[3],
|
|
response=digest(r[4]) if r[4] is not None else None)
|
|
for r in db.execute('SELECT id,request,envelope,generation,response FROM reconcile_requests')}
|
|
if path.parent == helper_state:
|
|
root_rows = db.execute('SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY grant_id').fetchall()
|
|
actual = {r[0]: digest(json.dumps([dict(sha256=digest(v), size=len(v)) if isinstance(v, bytes) else v for v in r], separators=(',', ':')).encode()) for r in root_rows}
|
|
require(actual == intent['root_rows'] and len(actual) == 18 and all(r[5] is not None for r in root_rows), 'exact closed original Root18')
|
|
require(db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0] == 0, 'no helper fault')
|
|
value['meta'] = list(db.execute('SELECT journal_id,checkpoint,generation,binding_sha256 FROM helper_sync_meta WHERE id=1').fetchone())
|
|
value['identity'] = [path.stat().st_dev, path.stat().st_ino]
|
|
require(value['identity'] == intent['database_inodes'][str(path)], 'closeout database identity')
|
|
out[str(path)] = value
|
|
return out
|
|
def service(name):
|
|
return dict(line.split('=', 1) for line in run(['systemctl', 'show', name, '-p', 'ActiveState', '-p', 'MainPID', '-p', 'UnitFileState']).decode().splitlines())
|
|
def controls():
|
|
fixed = (Path('/etc/rmm-remote'), Path('/etc/systemd/system/rmm-agent-remote.service.d'),
|
|
Path('/etc/systemd/system/rmm-helper-cloud.service.d'))
|
|
result = {str(path): tree(path) for path in fixed}
|
|
for path in (Path('/etc/systemd/system/rmm-agent-remote.service'), Path('/etc/systemd/system/rmm-helper-cloud.service'),
|
|
Path('/etc/rmm/apt-helper.json'), Path('/etc/rmm-remote/apt-helper-proof.json'),
|
|
Path('/usr/lib/rmm-apt-transaction/apt_transaction.py'), Path('/var/lib/dpkg/status'),
|
|
Path('/usr/share/rmm-mvp-test/version')):
|
|
result[str(path)] = file(path)
|
|
return result
|
|
def previous_backups():
|
|
# Prior bootstrap folders are never opened for content or modified.
|
|
# Bind their complete file identities/metadata, excluding only this case.
|
|
return {str(path): tree(path, metadata_only=True) for path in sorted(folder.parent.iterdir()) if path != folder}
|
|
def guard(new=False):
|
|
require(Path('/proc/sys/kernel/random/boot_id').read_text().strip() == intent['boot_id'], 'no reboot/identity transition')
|
|
identity = json.loads((state/'identity.json').read_bytes())
|
|
require(all(identity[k] == intent['target'][k] for k in ('agent_id', 'asset_id')), 'same existing enrollment')
|
|
config = Path('/etc/rmm-remote/helper-general.json'); pin = Path('/etc/rmm-remote/general-helper-proof.json')
|
|
require(digest(config.read_bytes()) == intent['config_sha256'] and digest(pin.read_bytes()) == intent['pin_sha256']
|
|
and json.loads(config.read_bytes())['allow_reboot'] is False, 'closed unchanged General policy')
|
|
require(digest(Path('/etc/systemd/system/rmm-helper-cloud.service').read_bytes()) == intent['general_unit_sha256']
|
|
and digest(Path('/etc/systemd/system/rmm-agent-remote.service').read_bytes()) == intent['agent_unit_sha256'], 'unchanged qualified service units')
|
|
binding = json.loads((state/'binding.json').read_bytes())
|
|
cached = json.loads((state/'job-signing-key.json').read_bytes())
|
|
helper_key = json.loads(Path('/etc/rmm-remote/helper-signing-key.json').read_bytes())
|
|
raw_key = bytes.fromhex(cached['public_key_hex'])
|
|
require(cached == helper_key and len(raw_key) == 32 and cached['algorithm'] == 'Ed25519'
|
|
and cached['schema_version'] == 1 and digest(raw_key) == cached['key_id'] == intent['general_pin']['identity']['signing_key_id']
|
|
and binding['public_key_hex'] == cached['public_key_hex']
|
|
and all(binding[k] == intent['general_pin']['identity'][k] for k in
|
|
('tenant_id', 'asset_id', 'agent_id', 'enrollment_generation', 'assignment_version')), 'same pinned public signing identity')
|
|
require(digest(Path('/usr/lib/rmm-apt-transaction/apt_transaction.py').read_bytes()) == intent['provider_sha256'], 'provider unchanged')
|
|
require(b'RMM_APT_HELPER_' not in Path('/etc/systemd/system/rmm-agent-remote.service.d/30-cloud-root.conf').read_bytes(), 'APT endpoint absent')
|
|
for name in ('rmm-apt-helper.service', 'rmm-agent.service', 'rmm-helper-dev-test.service'):
|
|
value = service(name); require(value['ActiveState'] == 'inactive' and value['UnitFileState'] == 'disabled', 'APT/legacy daemons remain disabled')
|
|
for name, path in paths.items():
|
|
owned(path); require(digest(path.read_bytes()) == intent[('new_' if new else 'old_')+name+'_sha256'], 'binary compare-and-swap')
|
|
require(intent['id'] == ident and len(intent['root_rows']) == 18 and intent['jobs_created'] is False, 'fixed maintenance intent')
|
|
if action == 'prepare':
|
|
guard(); sql_witness()
|
|
for name in ('rmm-agent-remote.service', 'rmm-helper-cloud.service'):
|
|
require(service(name)['ActiveState'] == 'active', 'known original daemon active')
|
|
folder.mkdir(mode=0o700); create(folder/'intent.json', encode(intent))
|
|
return dict(prepared=True, id=ident, intent_sha256=digest(encode(intent)), daemon_changes=False, jobs_created=False)
|
|
owned(folder, True)
|
|
require((folder/'intent.json').read_bytes() == encode(intent), 'same original retained intent')
|
|
if action == 'read':
|
|
if not (folder/'report.json').exists():
|
|
return dict(id=ident, completed=False, stage='prepared_without_install_report', install_entered=False)
|
|
report = json.loads((folder/'report.json').read_bytes())
|
|
if report.get('completed'):
|
|
guard(True); current = sql_witness(); before = json.loads((folder/'sql-before.json').read_bytes())
|
|
require(all(file(folder/(name+'-previous')) == report['backups'][name]
|
|
and report['backups'][name]['sha256'] == intent['old_'+name+'_sha256'] for name in paths), 'versioned original binary backups intact')
|
|
for path, previous in before.items():
|
|
after = current[path]
|
|
require(after['schema'] == previous['schema'] and after['identity'] == previous['identity'], 'unchanged schema/database inode')
|
|
for table, old_rows in previous['immutable'].items():
|
|
require(all(after['immutable'][table].get(k) == v for k, v in old_rows.items()), 'all original immutable SQL rows retained')
|
|
if 'sequence' in previous:
|
|
require(after['sequence'] >= previous['sequence'] and after['meta'][0] == previous['meta'][0]
|
|
and all(a >= b for a, b in zip(after['meta'][1:], previous['meta'][1:])), 'same journal with monotonic signed Sync/counter')
|
|
for key, old in previous['requests'].items():
|
|
value = after['requests'].get(key)
|
|
require(value is not None and all(value[k] == old[k] for k in ('request', 'envelope', 'generation'))
|
|
and (old['response'] is None or value['response'] == old['response']), 'original signed request/response bytes retained')
|
|
elif 'meta' in previous:
|
|
require(after['meta'][0] == previous['meta'][0] and after['meta'][3] == previous['meta'][3]
|
|
and all(after['meta'][i] >= previous['meta'][i] for i in (1, 2)), 'same helper identity/monotonic checkpoint')
|
|
require(controls() == json.loads((folder/'controls-before.json').read_bytes()), 'bootstrap/policy/APT/package files unchanged')
|
|
for path, old in json.loads((folder/'files-before.json').read_bytes()).items():
|
|
require(tree(Path(path), True) == old, 'all non-database state, private-key metadata and PKI unchanged')
|
|
old_backups = json.loads((folder/'backups-before.json').read_bytes())
|
|
current_backups = previous_backups()
|
|
require(all(current_backups.get(path) == value for path, value in old_backups.items()), 'all earlier bootstrap backups retained')
|
|
report['readback_verified'] = True
|
|
report['services_now'] = {name: service(name) for name in ('rmm-agent-remote.service', 'rmm-helper-cloud.service')}
|
|
return report
|
|
# Single-use installation: a retained report, even incomplete, forbids rerun.
|
|
require(not (folder/'report.json').exists(), 'installation already entered; readback only')
|
|
guard(); sql_witness()
|
|
for name in paths:
|
|
upload = folder/(name+'-next.upload'); value = owned(upload)
|
|
require(value.st_nlink == 1 and value.st_mode&0o777 == 0o600, 'private SFTP artifact')
|
|
raw = upload.read_bytes()
|
|
require(digest(raw) == intent['new_'+name+'_sha256'] and raw[:6] == b'\x7fELF\x02\x01'
|
|
and raw[18:20] == b'\x3e\x00', 'qualified x86-64 ELF')
|
|
upload.chmod(0o755); run(['/lib64/ld-linux-x86-64.so.2', '--list', str(upload)])
|
|
report = dict(id=ident, completed=False, stage='validated', jobs_created=False, grants_created=False,
|
|
package_effects=False, reboot_requested=False, apt_endpoint_enabled=False, apt_pins_stale=True,
|
|
intent_sha256=digest(encode(intent)), backups={}, old_agent_sha256=intent['old_agent_sha256'],
|
|
old_helper_sha256=intent['old_helper_sha256'], agent_sha256=intent['new_agent_sha256'],
|
|
helper_sha256=intent['new_helper_sha256'], provider_sha256=intent['provider_sha256'])
|
|
def save():
|
|
create(folder/'.report-next.json', encode(report)); os.replace(folder/'.report-next.json', folder/'report.json'); fsync_dir(folder)
|
|
locks = []
|
|
try:
|
|
save()
|
|
run(['systemctl', 'stop', 'rmm-agent-remote.service', 'rmm-helper-cloud.service'])
|
|
for path in (state/'agent.lock', helper_state/'executor.lock'):
|
|
fd = os.open(path, os.O_RDWR|os.O_NOFOLLOW); locks.append(fd); fcntl.flock(fd, fcntl.LOCK_EX|fcntl.LOCK_NB)
|
|
guard(); frozen = sql_witness()
|
|
require(all(service(name)['ActiveState'] == 'inactive' for name in ('rmm-agent-remote.service', 'rmm-helper-cloud.service')), 'both exact daemons stopped')
|
|
snapshots = {str(p): tree(p, True) for p in (state, helper_state, Path('/var/lib/rmm-agent'), Path('/var/lib/rmm-apt-transaction'))}
|
|
files_full = {str(p): tree(p) for p in (state, helper_state, Path('/var/lib/rmm-agent'), Path('/var/lib/rmm-apt-transaction'))}
|
|
control_before = controls()
|
|
backups_before = previous_backups()
|
|
create(folder/'sql-before.json', encode(frozen)); create(folder/'files-before.json', encode(snapshots)); create(folder/'controls-before.json', encode(control_before))
|
|
create(folder/'backups-before.json', encode(backups_before))
|
|
for name, path in paths.items():
|
|
require(path.stat().st_dev == folder.stat().st_dev, 'same-filesystem versioned exchange')
|
|
previous = folder/(name+'-previous'); os.rename(path, previous); fsync_dir(path.parent); fsync_dir(folder)
|
|
report['backups'][name] = file(previous)
|
|
require(report['backups'][name]['sha256'] == intent['old_'+name+'_sha256'], 'retained exact previous binary')
|
|
report['stage'] = name+'_original_retained'; save()
|
|
os.rename(folder/(name+'-next.upload'), path); fsync_dir(path.parent); fsync_dir(folder)
|
|
inspected = json.loads(run([str(helper), '--inspect-general-profile'], env=dict(os.environ, RMM_HELPER_CONFIG='/etc/rmm-remote/helper-general.json')))
|
|
require(inspected == intent['general_pin'], 'same General policy-based profile pin')
|
|
require(sql_witness() == frozen and controls() == control_before and previous_backups() == backups_before
|
|
and all(tree(Path(p)) == v for p, v in files_full.items()), 'all data bytes/rows/inodes/backups unchanged before restart')
|
|
guard(True); report.update(stage='installed_preserved', preserved_before_restart=True,
|
|
root_consumed=18, root_unresolved=0, general_pin=inspected, database_inodes=intent['database_inodes'])
|
|
save()
|
|
for fd in reversed(locks): fcntl.flock(fd, fcntl.LOCK_UN); os.close(fd)
|
|
locks.clear()
|
|
run(['systemctl', 'start', 'rmm-helper-cloud.service'])
|
|
require(service('rmm-helper-cloud.service')['ActiveState'] == 'active', 'qualified General helper starts')
|
|
run(['systemctl', 'start', 'rmm-agent-remote.service'])
|
|
require(service('rmm-agent-remote.service')['ActiveState'] == 'active', 'qualified unprivileged agent starts')
|
|
pid = int(service('rmm-agent-remote.service')['MainPID'])
|
|
process = Path('/proc/'+str(pid)+'/status').read_text().splitlines()
|
|
require(next(line for line in process if line.startswith('Uid:')).split()[1:] == ['993']*4
|
|
and next(line for line in process if line.startswith('Gid:')).split()[1:] == [str(pwd.getpwnam('rmm-agent').pw_gid)]*4,
|
|
'actual agent process remains unprivileged')
|
|
report.update(stage='qualified_daemons_started', completed=True, boot_id=intent['boot_id']); 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:
|
|
for fd in reversed(locks): fcntl.flock(fd, fcntl.LOCK_UN); os.close(fd)
|
|
return report
|
|
|
|
|
|
def qualification():
|
|
out = {}
|
|
for name, filename in (('agent', 'rmm-agent'), ('helper', 'rmm-privileged-helper')):
|
|
path = ARTIFACTS/filename; metadata = cloud.contract.protected_json(path.with_name(filename+'.json'))
|
|
require(metadata['binary_sha256'] == EXPECTED_NEW[name] == sha(path.read_bytes())
|
|
and metadata['architecture'] == 'x86_64-unknown-linux-gnu' and metadata['release_build'] is True
|
|
and metadata['qualification_profile'] == 'durable-outcome-v1' and metadata['source_before_after_equal'] is True
|
|
and metadata['glibc_max'] == '2.34', 'fixed qualified Bookworm release')
|
|
require(all(sha((ROOT/p).read_bytes()) == value for p, value in metadata['source_files'].items()), 'release source bindings unchanged')
|
|
out[name] = dict(path=str(path.relative_to(ROOT)), sha256=metadata['binary_sha256'], qualification_sha256=sha(path.with_name(filename+'.json').read_bytes()))
|
|
native = ROOT/'devstate/shared-core/acceptance/08a9be72-dd7c-46b7-a2c6-7d17424533c2/linux-passed.json'
|
|
result = cloud.contract.protected_json(native)
|
|
require(result['passed'] is True and result['qualification']['manifest_sha256'] == sha((ARTIFACTS/'manifest.json').read_bytes()), 'existing real Linux76 shared-core qualification')
|
|
return dict(artifacts=out, native_linux_evidence_sha256=sha(native.read_bytes()))
|
|
|
|
|
|
def server_gate(intent):
|
|
require(reboot.evidence(intent['target'], 'gate') == dict(jobs=30, outbox=30, permits=26, unresolved=0), 'same central30/30/26/0')
|
|
policy = cloud.bootstrap.rpc('server.conf', reboot.server_policy, dict(action='state'))
|
|
require(policy == dict(policy_sha256=intent['central_policy_sha256'], closed=True), 'same closed central Root policy')
|
|
|
|
|
|
def prepare():
|
|
metadata = qualification(); closeout = cloud.contract.protected_json(ROOT/'devstate/cloud-mvp-final-closeout.json')
|
|
require(closeout['verified'] is True and closeout['central_counts'] == dict(jobs=30, outbox=30, permits=26, unresolved=0)
|
|
and closeout['policy']['closed'] is True and closeout['native']['root_unresolved'] == 0, 'completed Linux MVP closeout')
|
|
observation = closeout['observation']; require(sha((ROOT/observation['path']).read_bytes()) == observation['sha256'], 'verified closeout source')
|
|
ident = str(uuid.uuid4()); folder = STATE/ident; folder.mkdir(mode=0o700, parents=True)
|
|
native = closeout['native']; old = closeout['vm']['artifact_sha256']
|
|
intent = dict(schema_version=1, type='rmm.linux.shared-core-upgrade', id=ident, target=closeout['target'],
|
|
boot_id=native['boot_id'], root_rows=native['root_rows'], database_inodes=native['database_inodes'],
|
|
old_agent_sha256=old['agent'], old_helper_sha256=old['helper'], new_agent_sha256=EXPECTED_NEW['agent'],
|
|
new_helper_sha256=EXPECTED_NEW['helper'], provider_sha256=old['provider'], config_sha256=native['config_sha256'],
|
|
pin_sha256=native['pin_sha256'], general_pin=native['general_pin'], central_policy_sha256=closeout['policy']['policy_sha256'],
|
|
general_unit_sha256=closeout['vm']['general_unit_sha256'],
|
|
agent_unit_sha256=sha((ROOT/'deploy/linux/rmm-agent-remote.service').read_bytes()),
|
|
closeout_sha256=sha((ROOT/'devstate/cloud-mvp-final-closeout.json').read_bytes()), qualification=metadata,
|
|
jobs_created=False, grants_created=False, reboot_requested=False)
|
|
cloud.contract.write_private(folder/'intent.json', intent); server_gate(intent)
|
|
result = cloud.bootstrap.rpc('linux-vm.conf', vm, dict(action='prepare', id=ident, intent=intent))
|
|
cloud.contract.write_private(folder/'prepared.json', result); require(result.get('prepared') is True, 'retained remote intent incomplete')
|
|
print('PREPARED '+ident+'; no daemon change or RMM job')
|
|
|
|
|
|
def existing(ident):
|
|
folder = STATE/str(uuid.UUID(ident)); intent = cloud.contract.protected_json(folder/'intent.json')
|
|
require(intent['id'] == ident, 'same retained case'); return folder, intent
|
|
|
|
|
|
def install(ident):
|
|
folder, intent = existing(ident); require(qualification() == intent['qualification'], 'original qualified source/artifacts')
|
|
require(cloud.contract.protected_json(folder/'prepared.json')['prepared'] is True, 'remote preparation first')
|
|
cloud.contract.write_private(folder/'install-started.json', dict(id=ident, intent_sha256=sha((folder/'intent.json').read_bytes())))
|
|
server_gate(intent)
|
|
cfg = cloud.bootstrap.access.load_config(ROOT/'credentials/linux-vm.conf')
|
|
args = ['sftp', '-F', '/dev/null', '-P', cfg.get('port', '22'), '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes',
|
|
'-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()),
|
|
'-b', '-', cfg['username']+'@'+cfg['address']]
|
|
for name, item in intent['qualification']['artifacts'].items():
|
|
batch = 'put '+json.dumps(str(ROOT/item['path']))+' '+json.dumps(REMOTE+ident+'/'+name+'-next.upload')+'\nchmod 600 '+json.dumps(REMOTE+ident+'/'+name+'-next.upload')+'\n'
|
|
result = subprocess.run(args, input=batch.encode(), capture_output=True, timeout=120)
|
|
cloud.contract.write_private(folder/(name+'-sftp.json'), dict(returncode=result.returncode,
|
|
stdout_b64=base64.b64encode(result.stdout).decode(), stderr_b64=base64.b64encode(result.stderr).decode()))
|
|
require(result.returncode == 0, 'private SFTP error; retain case and inspect, never repeat install')
|
|
result = cloud.bootstrap.rpc('linux-vm.conf', vm, dict(action='install', id=ident, intent=intent))
|
|
cloud.contract.write_private(folder/'install-reply.json', result)
|
|
require(result.get('completed') is True, 'retained incomplete report; readback only')
|
|
print('INSTALLED '+ident+'; finish/readback and independent native Sync qualification still required')
|
|
|
|
|
|
def finish(ident):
|
|
folder, intent = existing(ident)
|
|
cloud.contract.protected_json(folder/'install-started.json')
|
|
result = cloud.bootstrap.rpc('linux-vm.conf', vm, dict(action='read', id=ident, intent=intent))
|
|
cloud.contract.write_private(folder/('readback-'+str(uuid.uuid4())+'.json'), result)
|
|
require(result.get('completed') is True and result.get('readback_verified') is True, 'remote maintenance incomplete; do not reinstall')
|
|
require(all(v['ActiveState'] == 'active' for v in result['services_now'].values()), 'both qualified daemons active')
|
|
server_gate(intent)
|
|
cloud.contract.write_private(folder/'result.json', dict(verified_preservation=True, native_sync_accepted=False,
|
|
id=ident, intent_sha256=sha((folder/'intent.json').read_bytes()), report=result))
|
|
print('PASS '+ident+' preserving Linux daemon upgrade; APT closed/stale; native Sync/MCP acceptance remains separate')
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('action', choices=('prepare', 'install', 'finish')); parser.add_argument('id', nargs='?')
|
|
args = parser.parse_args(); os.umask(0o077)
|
|
require((args.id is None) == (args.action == 'prepare'), 'prepare creates a case; install/finish require its exact ID')
|
|
if args.action == 'prepare': prepare()
|
|
elif args.action == 'install': install(args.id)
|
|
else: finish(args.id)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try: main()
|
|
except Exception as error:
|
|
text = str(error)
|
|
print(text if text.startswith('remote bootstrap incomplete: ') else 'SHARED_CORE_UPGRADE_INCOMPLETE:'+type(error).__name__)
|
|
raise SystemExit(2) from None
|