207 lines
14 KiB
Python
207 lines
14 KiB
Python
#!/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'
|
|
image_tag='rust-discovery-server:upgrade-'+payload['id']
|
|
worker.write_text('''import json,os,subprocess
|
|
from pathlib import Path
|
|
os.umask(0o077)
|
|
root=Path('/opt/rust-discovery'); here=Path(__file__).parent
|
|
record=json.loads((here/'source.json').read_text())
|
|
with (here/'build.log').open('xb') as log:
|
|
result=subprocess.run(['docker','build','--file',str(root/'deploy/Dockerfile'),'--tag',record['image_tag'],str(root)],stdout=log,stderr=subprocess.STDOUT)
|
|
outcome={'exit_code':result.returncode}
|
|
if result.returncode==0:
|
|
outcome['image_id']=subprocess.run(['docker','image','inspect',record['image_tag'],'--format','{{.Id}}'],capture_output=True,check=True).stdout.decode().strip()
|
|
(here/'build-result.json').write_text(json.dumps(outcome))
|
|
'''); worker.chmod(0o600)
|
|
record = dict(id=payload['id'], source_sha256=payload['sha256'], files=manifest, previous=previous, old_image=old_image, backup=str(backup),image_tag=image_tag)
|
|
(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'
|
|
unit=subprocess.run(['systemctl','show','rmm-source-build-'+payload['id'],'-p','ActiveState','-p','SubState','-p','ExecMainStatus'],capture_output=True,check=True,timeout=10).stdout.decode()
|
|
current=subprocess.run(['docker','image','inspect','rust-discovery-server:local','--format','{{.Id}}'],capture_output=True,check=True,timeout=10).stdout.decode().strip()
|
|
log=backup/'build.log'
|
|
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()),
|
|
service=dict(line.split('=',1) for line in unit.splitlines() if '=' in line),build_log_bytes=log.stat().st_size if log.exists() else 0,
|
|
build_has_separate_image_tag='image_tag' in record,runtime_tag_unchanged=current==record['old_image'] if not (backup/'deploy-result.json').exists() else None)
|
|
|
|
|
|
def deploy(payload):
|
|
import io
|
|
import tarfile
|
|
root = Path('/opt/rust-discovery'); backup = root/'devstate'/('source-upgrade-'+payload['id'])
|
|
record = json.loads((backup/'source.json').read_text())
|
|
built=json.loads((backup/'build-result.json').read_text())
|
|
require(built['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')
|
|
if payload.get('closed_policy_sha256'):
|
|
import datetime as dt
|
|
policy = (root/'devstate/root-policy/policy.json').read_bytes()
|
|
require(hashlib.sha256(policy).hexdigest() == payload['closed_policy_sha256'] and
|
|
dt.datetime.fromisoformat(json.loads(policy)['expires_at']) < dt.datetime.now(dt.timezone.utc),
|
|
'known unknown requires closed root policy')
|
|
raw = base64.b64decode(payload['web_tar'], validate=True)
|
|
require(hashlib.sha256(raw).hexdigest() == payload['web_sha256'], 'reviewed web archive hash')
|
|
# Publish immutable hashed assets before index, keeping the bind-mounted
|
|
# dist directory inode. Retain old files; no recursive deletion.
|
|
with tarfile.open(fileobj=io.BytesIO(raw), mode='r:') as archive:
|
|
members = sorted(archive.getmembers(), key=lambda m: m.name == 'index.html')
|
|
require(any(m.name == 'index.html' for m in members), 'built UI index required')
|
|
for member in members:
|
|
name = member.name
|
|
require(member.isfile() and not Path(name).is_absolute() and '..' not in Path(name).parts and
|
|
(name == 'index.html' or name.startswith('assets/') and name.endswith(('.js','.css'))), 'built web path allowlist')
|
|
data = archive.extractfile(member).read(); require(len(data) < 2*1024*1024, 'web file limit')
|
|
dest = root/'web/dist'/name
|
|
require(not dest.is_symlink() and not dest.parent.is_symlink(), 'web symlink')
|
|
dest.parent.mkdir(mode=0o755, parents=True, exist_ok=True)
|
|
if dest.exists():
|
|
retained = backup/'web'/name; retained.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
with retained.open('xb') as f: f.write(dest.read_bytes()); f.flush(); os.fsync(f.fileno())
|
|
next_path = dest.with_name(dest.name+'.upgrade-next')
|
|
fd = os.open(next_path, os.O_WRONLY|os.O_CREAT|os.O_EXCL|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)
|
|
cmd = ['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml')]
|
|
if 'image_tag' in record:
|
|
inspected=subprocess.run(['docker','image','inspect',record['image_tag'],'--format','{{.Id}}'],capture_output=True,check=True,timeout=10).stdout.decode().strip()
|
|
require(inspected==built['image_id'] and inspected.startswith('sha256:'),'fixed qualified image required')
|
|
# Only this explicit, gated deployment may move the runtime tag. A
|
|
# concurrently finishing source build cannot alter running services.
|
|
subprocess.run(['docker','tag',inspected,'rust-discovery-server:local'],capture_output=True,check=True,timeout=10)
|
|
# 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')
|
|
parser.add_argument('--preserve-known-unknown', action='store_true', help='preserve the exact fenced APT attempt while upgrading its control path')
|
|
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'))
|
|
before = None; payload = dict(id=ident)
|
|
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']))
|
|
if args.preserve_known_unknown:
|
|
import cloud_apt_retry as apt
|
|
checkpoint = cloud.contract.protected_json(apt.STATE/'apt_install_retry/checkpoint.json')
|
|
job = checkpoint['plan']['jobs'][0]
|
|
require(checkpoint['stage'] == 'unknown' and checkpoint['audit']['simulation'] is True and
|
|
gate == dict(jobs=13, outbox=13, permits=12, unresolved=1), 'one exact known unknown required')
|
|
current = cloud.Backend().call('jobs.get', dict(tenant_id=cloud.TENANT, job_id=job['job_id']))
|
|
require(current['state'] == 'unknown' and current['attempt_id'] == job['attempt_id'], 'same unknown attempt required')
|
|
before = cloud.bootstrap.rpc('linux-vm.conf', apt.stored_attempt_evidence,
|
|
dict(job_id=job['job_id'], attempt_id=job['attempt_id']))
|
|
require(before['ledger']['unresolved'] == 1 and before['ledger']['consumed'] == 9 and
|
|
before['ledger']['result'] is None and before['ledger']['outcome']['error_code'] == 'DPKG_INCONSISTENT', 'preserve exact native unknown')
|
|
closed = cloud.contract.protected_json(apt.STATE/'policy-closed.json')
|
|
payload['closed_policy_sha256'] = closed['policy_sha256']
|
|
require((cloud.ROOT/'migrations/0013_windows_inventory_scope.sql').is_file(), 'retain existing additive schema')
|
|
else: require(gate['unresolved'] == 0, 'unresolved cloud job; no server switch')
|
|
if args.action == 'deploy':
|
|
stream = io.BytesIO()
|
|
with tarfile.open(fileobj=stream, mode='w:') as archive:
|
|
for p in sorted((cloud.ROOT/'web/dist').rglob('*')):
|
|
if p.is_file():
|
|
require(not p.is_symlink(), 'built UI symlink')
|
|
archive.add(p, arcname=str(p.relative_to(cloud.ROOT/'web/dist')), recursive=False)
|
|
data = stream.getvalue(); payload.update(web_tar=base64.b64encode(data).decode(), web_sha256=hashlib.sha256(data).hexdigest())
|
|
result = cloud.bootstrap.rpc('server.conf', status if args.action == 'status' else deploy, payload)
|
|
if before is not None:
|
|
after = cloud.bootstrap.rpc('linux-vm.conf', apt.stored_attempt_evidence,
|
|
dict(job_id=job['job_id'], attempt_id=job['attempt_id']))
|
|
gate_after = cloud.bootstrap.rpc('server.conf', cloud.server, dict(kind='gate', tenant_id=cloud.TENANT, agent_id=target['agent_id']))
|
|
current = cloud.Backend().call('jobs.get', dict(tenant_id=cloud.TENANT, job_id=job['job_id']))
|
|
require(before == after and gate_after == gate and current['state'] == 'unknown' and current['attempt_id'] == job['attempt_id'],
|
|
'unknown evidence or execution artifacts changed during control-path upgrade')
|
|
cloud.contract.write_private(STATE/(ident+'.unknown-preserved.json'), dict(evidence=after, gate=gate_after, result=current))
|
|
result['unknown_attempt_preserved'] = True
|
|
print(result)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try: main()
|
|
except Exception as error:
|
|
print('CLOUD_SERVER_UPGRADE_INCOMPLETE:'+type(error).__name__); raise SystemExit(2) from None
|