feat: deploy platform inventory and signed APT retry client compatibility
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -25,6 +25,39 @@ STATE = cloud.ROOT / 'devstate/cloud-apt-retry'
|
||||
CASES = ('apt_install_retry', 'apt_native_update', 'ensure_package', 'apply_updates')
|
||||
|
||||
|
||||
def upgrade_agent(payload):
|
||||
"""Parser compatibility upgrade only; same identity/journal, no execution."""
|
||||
import sqlite3
|
||||
os.umask(0o077)
|
||||
state = Path('/var/lib/rmm-agent-remote'); binary = Path('/usr/local/libexec/rmm-agent-remote')
|
||||
require(not binary.is_symlink() and binary.stat().st_uid == 0 and binary.stat().st_mode&0o022 == 0 and
|
||||
hashlib.sha256(binary.read_bytes()).hexdigest() == payload['previous_sha256'], 'reviewed prior agent required')
|
||||
require(json.loads((state/'identity.json').read_text())['agent_id'] == payload['agent_id'], 'fixed identity 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,
|
||||
'unresolved native job')
|
||||
backup = Path('/var/lib/rmm-bootstrap-history')/('cloud-agent-schema3-'+payload['id']); backup.mkdir(mode=0o700)
|
||||
raw = base64.b64decode(payload['binary'], validate=True)
|
||||
require(hashlib.sha256(raw).hexdigest() == payload['sha256'] and raw[:6] == b'\x7fELF\x02\x01' and raw[18:20] == b'\x3e\x00', 'reviewed x64 artifact')
|
||||
staged = backup/'agent-next'
|
||||
with staged.open('xb') as f: f.write(raw); f.flush(); os.fsync(f.fileno()); os.fchmod(f.fileno(), 0o755)
|
||||
subprocess.run(['/lib64/ld-linux-x86-64.so.2','--list',str(staged)], capture_output=True, check=True, timeout=15)
|
||||
subprocess.run(['systemctl','stop','rmm-agent-remote.service'], capture_output=True, check=True, timeout=20)
|
||||
def manifest():
|
||||
result = {}
|
||||
for p in state.rglob('*'):
|
||||
require(not p.is_symlink(), 'agent state symlink')
|
||||
if p.is_file(): result[str(p.relative_to(state))] = hashlib.sha256(p.read_bytes()).hexdigest()
|
||||
return result
|
||||
before = manifest()
|
||||
require(binary.stat().st_dev == backup.stat().st_dev, 'same-filesystem preserved binary')
|
||||
os.rename(binary, backup/'previous-agent'); os.rename(staged, binary)
|
||||
require(before == manifest(), 'identity/journal bytes changed during upgrade')
|
||||
subprocess.run(['systemctl','start','rmm-agent-remote.service'], capture_output=True, check=True, timeout=20)
|
||||
return dict(binary_sha256=payload['sha256'], state_preserved_before_start=True, package_effects=False, backup=str(backup))
|
||||
|
||||
|
||||
def switch_endpoint(payload):
|
||||
import sqlite3
|
||||
os.umask(0o077)
|
||||
|
||||
@@ -77,11 +77,35 @@ def status(payload):
|
||||
|
||||
|
||||
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())
|
||||
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')
|
||||
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')]
|
||||
# Existing migration image runs without rebuild; preserves PG volume/PKI.
|
||||
migrate = subprocess.run(cmd+['run','--rm','--no-deps','migrate'], capture_output=True, timeout=25)
|
||||
@@ -115,7 +139,16 @@ def main():
|
||||
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))
|
||||
payload = dict(id=ident)
|
||||
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)
|
||||
print(result)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user