feat: preserve failed APT history for one signed UI-approved recovery
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Versioned cloud source/build/deploy, no DB/PKI reset or job approval."""
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import tarfile
|
||||
import uuid
|
||||
|
||||
import cloud_mvp_acceptance as cloud
|
||||
|
||||
STATE = cloud.ROOT/'devstate/server-upgrades'
|
||||
require = cloud.require
|
||||
|
||||
|
||||
def stage(payload):
|
||||
import io
|
||||
import tarfile
|
||||
root = Path('/opt/rust-discovery')
|
||||
backup = root/'devstate'/('source-upgrade-'+payload['id']); backup.mkdir(mode=0o700)
|
||||
raw = base64.b64decode(payload['tar'], validate=True)
|
||||
require(hashlib.sha256(raw).hexdigest() == payload['sha256'], 'source archive hash')
|
||||
def allowed(name):
|
||||
return name in ('Cargo.toml','Cargo.lock','rust-toolchain.toml','deploy/Dockerfile',
|
||||
'deploy/linux/rmm-apt-helper.service','tools/apt-transaction/apt_transaction.py',
|
||||
'tools/apt-transaction/hook') or name.startswith(('crates/','migrations/'))
|
||||
old_image = subprocess.run(['docker','image','inspect','rust-discovery-server:local','--format','{{.Id}}'], capture_output=True, check=True).stdout.decode().strip()
|
||||
manifest = {}; previous = {}
|
||||
with tarfile.open(fileobj=io.BytesIO(raw), mode='r:') as archive:
|
||||
for member in archive.getmembers():
|
||||
name = member.name
|
||||
require(member.isfile() and allowed(name) and not Path(name).is_absolute() and '..' not in Path(name).parts and name not in manifest, 'source path allowlist')
|
||||
dest = root/name; data = archive.extractfile(member).read()
|
||||
require(len(data) <= 2*1024*1024 and not dest.is_symlink(), 'source size/path')
|
||||
for parent in dest.parents:
|
||||
require(not parent.is_symlink(), 'source parent symlink')
|
||||
if parent == root: break
|
||||
if dest.exists():
|
||||
old = dest.read_bytes(); previous[name] = hashlib.sha256(old).hexdigest()
|
||||
retained = backup/'source'/name; retained.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
with retained.open('xb') as f: f.write(old); f.flush(); os.fsync(f.fileno())
|
||||
else: previous[name] = None
|
||||
manifest[name] = hashlib.sha256(data).hexdigest()
|
||||
dest.parent.mkdir(mode=0o755, parents=True, exist_ok=True)
|
||||
next_path = dest.with_name(dest.name+'.upgrade-next')
|
||||
fd = os.open(next_path, os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW, 0o644)
|
||||
with os.fdopen(fd, 'wb') as f: f.write(data); f.flush(); os.fsync(f.fileno()); os.fchmod(f.fileno(), 0o644)
|
||||
os.replace(next_path, dest)
|
||||
# Build asynchronously. Captured compiler output remains root-private.
|
||||
worker = backup/'build.py'
|
||||
worker.write_text('''import json,os,subprocess
|
||||
from pathlib import Path
|
||||
os.umask(0o077)
|
||||
root=Path('/opt/rust-discovery'); here=Path(__file__).parent
|
||||
with (here/'build.log').open('xb') as log:
|
||||
result=subprocess.run(['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),'build','migrate'],stdout=log,stderr=subprocess.STDOUT)
|
||||
(here/'build-result.json').write_text(json.dumps({'exit_code':result.returncode}))
|
||||
'''); worker.chmod(0o600)
|
||||
record = dict(id=payload['id'], source_sha256=payload['sha256'], files=manifest, previous=previous, old_image=old_image, backup=str(backup))
|
||||
(backup/'source.json').write_text(json.dumps(record)); (backup/'source.json').chmod(0o600)
|
||||
unit = 'rmm-source-build-'+payload['id']
|
||||
result = subprocess.run(['systemd-run','--unit='+unit,'--property=UMask=0077','--property=WorkingDirectory='+str(root),
|
||||
'/usr/bin/python3',str(worker)], capture_output=True, timeout=15)
|
||||
require(result.returncode == 0, 'source build launch incomplete')
|
||||
return dict(record, unit=unit, launched=True, deployed=False)
|
||||
|
||||
|
||||
def status(payload):
|
||||
root = Path('/opt/rust-discovery'); backup = root/'devstate'/('source-upgrade-'+payload['id'])
|
||||
record = json.loads((backup/'source.json').read_text())
|
||||
result = backup/'build-result.json'
|
||||
return dict(id=payload['id'], finished=result.exists(), exit_code=json.loads(result.read_text())['exit_code'] if result.exists() else None,
|
||||
source_unchanged=all(hashlib.sha256((root/name).read_bytes()).hexdigest() == sha for name,sha in record['files'].items()))
|
||||
|
||||
|
||||
def deploy(payload):
|
||||
root = Path('/opt/rust-discovery'); backup = root/'devstate'/('source-upgrade-'+payload['id'])
|
||||
record = json.loads((backup/'source.json').read_text())
|
||||
require(json.loads((backup/'build-result.json').read_text())['exit_code'] == 0, 'build must pass')
|
||||
require(all(hashlib.sha256((root/name).read_bytes()).hexdigest() == sha for name,sha in record['files'].items()), 'source drift')
|
||||
require(not (backup/'deploy-result.json').exists(), 'deployment already recorded')
|
||||
cmd = ['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml')]
|
||||
# Existing migration image runs without rebuild; preserves PG volume/PKI.
|
||||
migrate = subprocess.run(cmd+['run','--rm','--no-deps','migrate'], capture_output=True, timeout=25)
|
||||
require(migrate.returncode == 0, 'migration failed')
|
||||
result = subprocess.run(cmd+['up','-d','--no-deps','api','gateway'], capture_output=True, timeout=25)
|
||||
outcome = dict(deployed=result.returncode == 0, existing_state_preserved=True, approvals_created=False)
|
||||
(backup/'deploy-result.json').write_text(json.dumps(outcome)); (backup/'deploy-result.json').chmod(0o600)
|
||||
return outcome
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__); parser.add_argument('action', choices=('stage','status','deploy')); parser.add_argument('--id')
|
||||
args = parser.parse_args(); os.umask(0o077); STATE.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
if args.action == 'stage':
|
||||
names = ['Cargo.toml','Cargo.lock','rust-toolchain.toml','deploy/Dockerfile','deploy/linux/rmm-apt-helper.service',
|
||||
'tools/apt-transaction/apt_transaction.py','tools/apt-transaction/hook']
|
||||
names += [str(p.relative_to(cloud.ROOT)) for base in ('crates','migrations') for p in (cloud.ROOT/base).rglob('*')
|
||||
if p.is_file() and not p.is_symlink() and 'target' not in p.parts and '__pycache__' not in p.parts and p.suffix in ('.rs','.toml','.sql')]
|
||||
stream = io.BytesIO()
|
||||
with tarfile.open(fileobj=stream, mode='w:') as archive:
|
||||
for name in sorted(set(names)):
|
||||
path = cloud.ROOT/name; require(not path.is_symlink(), 'source symlink')
|
||||
archive.add(path, arcname=name, recursive=False)
|
||||
data = stream.getvalue(); ident = str(uuid.uuid4())
|
||||
result = cloud.bootstrap.rpc('server.conf', stage, dict(id=ident, tar=base64.b64encode(data).decode(), sha256=hashlib.sha256(data).hexdigest()))
|
||||
cloud.contract.write_private(STATE/(ident+'.json'), result)
|
||||
print(dict(id=ident, launched=result['launched'], deployed=False))
|
||||
else:
|
||||
ident = str(uuid.UUID(args.id)); cloud.contract.protected_json(STATE/(ident+'.json'))
|
||||
if args.action == 'deploy':
|
||||
target = cloud.contract.protected_json(cloud.ROOT/'devstate/remote-vm.json')['identity']
|
||||
gate = cloud.bootstrap.rpc('server.conf', cloud.server, dict(kind='gate', tenant_id=cloud.TENANT, agent_id=target['agent_id']))
|
||||
require(gate['unresolved'] == 0, 'unresolved cloud job; no server switch')
|
||||
result = cloud.bootstrap.rpc('server.conf', status if args.action == 'status' else deploy, dict(id=ident))
|
||||
print(result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try: main()
|
||||
except Exception as error:
|
||||
print('CLOUD_SERVER_UPGRADE_INCOMPLETE:'+type(error).__name__); raise SystemExit(2) from None
|
||||
Reference in New Issue
Block a user