153 lines
9.0 KiB
Python
153 lines
9.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline source qualification and Linux delivery pack. No SSH or deployment.
|
|
|
|
This tool never enrolls an agent, starts a daemon, edits a policy or approves a
|
|
job. Linux/Windows VM execution and full-profile activation remain separate.
|
|
"""
|
|
import datetime as dt
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tarfile
|
|
import time
|
|
import uuid
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
LINUX_IMAGE = 'sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6'
|
|
WINDOWS_IMAGE = 'sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9'
|
|
POSTGRES_IMAGE = 'sha256:567d5d704a3e5a4864393b623a0d50eb1b5a4ad134cdac7cf19555cb2d8ffe9e'
|
|
|
|
|
|
def sha(path):
|
|
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
|
|
|
|
|
def inputs():
|
|
paths = [ROOT / p for p in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml',
|
|
'tests/backend_root_logs.rs', 'tests/script_console/codec-golden.json',
|
|
'tests/apt_transaction/fixtures/planning-canonical.json',
|
|
'tools/apt-transaction/apt_transaction.py', 'tools/apt-transaction/hook',
|
|
'deploy/linux/rmm-apt-helper.service',
|
|
'deploy/linux/rmm-full-maintenance-helper.service', 'deploy/script_console_build.py')]
|
|
paths += [p for p in (ROOT / 'crates').rglob('*')
|
|
if p.is_file() and p.suffix in ('.rs', '.toml', '.c', '.h', '.idl', '.sql')]
|
|
paths += [p for p in (ROOT / 'tests').rglob('*.rs')]
|
|
paths += [p for p in (ROOT / 'migrations').rglob('*.sql')]
|
|
paths += [p for p in (ROOT / 'proto').rglob('*') if p.is_file() and p.suffix == '.proto']
|
|
for path in paths:
|
|
if path.is_symlink():
|
|
raise RuntimeError('Symlink compiler input refused: ' + str(path.relative_to(ROOT)))
|
|
return {str(p.relative_to(ROOT)): sha(p) for p in sorted(set(paths))}
|
|
|
|
|
|
def main():
|
|
os.umask(0o077)
|
|
state = ROOT / 'devstate/script-console-qualification'
|
|
state.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
out = state / ('run-' + str(uuid.uuid4()))
|
|
out.mkdir(mode=0o700)
|
|
frozen = inputs()
|
|
checks = {}
|
|
|
|
def command(label, args):
|
|
result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
|
|
log = out / (label + '.log')
|
|
log.write_bytes(result.stdout)
|
|
checks[label] = {'exit_code': result.returncode, 'log_sha256': sha(log)}
|
|
if result.returncode:
|
|
raise RuntimeError(label + ' failed; inspect private log ' + str(log))
|
|
return result.stdout
|
|
|
|
def cargo(label, image, *args):
|
|
target = ROOT / ('devstate/script-console-windows-target' if image == WINDOWS_IMAGE else 'devstate/script-console-target')
|
|
target.mkdir(mode=0o700, exist_ok=True)
|
|
return command(label, ['docker', 'run', '--rm', '--network', 'none', '--read-only',
|
|
'--tmpfs', '/tmp:rw,size=512m', '-v', str(ROOT) + ':/src:ro', '-v', str(target) + ':/target',
|
|
'-e', 'CARGO_TARGET_DIR=/target', '--entrypoint', 'cargo', image, *args])
|
|
|
|
common = ['--manifest-path', '/src/Cargo.toml', '--locked', '--offline']
|
|
cargo('format', LINUX_IMAGE, 'fmt', '--manifest-path', '/src/Cargo.toml', '--all', '--check')
|
|
cargo('linux-clippy', LINUX_IMAGE, 'clippy', *common, '--workspace', '--all-targets', '--', '-D', 'warnings')
|
|
cargo('linux-tests', LINUX_IMAGE, 'test', *common, '--workspace', '--all-targets')
|
|
# Existing ignored DB contracts use their own fresh PostgreSQL, internal
|
|
# network namespace only. No published port, production URL or real device.
|
|
pg_name='rmm-script-contract-'+uuid.uuid4().hex[:12]
|
|
command('postgres-start',['docker','run','-d','--network','none','--name',pg_name,'-e','POSTGRES_HOST_AUTH_METHOD=trust',POSTGRES_IMAGE])
|
|
try:
|
|
for _ in range(100):
|
|
ready=subprocess.run(['docker','exec',pg_name,'pg_isready','-h','127.0.0.1','-U','postgres'],capture_output=True)
|
|
if ready.returncode == 0: break
|
|
time.sleep(.2)
|
|
else: raise RuntimeError('Isolated PostgreSQL did not become ready')
|
|
sql='CREATE ROLE rmm_app LOGIN NOINHERIT NOSUPERUSER NOBYPASSRLS;\n'+''.join(p.read_text()+'\n' for p in sorted((ROOT/'migrations').glob('*.sql')))
|
|
result=subprocess.run(['docker','exec','-i',pg_name,'psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres'],input=sql.encode(),capture_output=True)
|
|
(out/'postgres-migrations.log').write_bytes(result.stdout+result.stderr)
|
|
if result.returncode: raise RuntimeError('Isolated migrations failed')
|
|
command('postgres-contracts',['docker','run','--rm','--network','container:'+pg_name,'--read-only','--tmpfs','/tmp:rw,size=512m','-v',str(ROOT)+':/src:ro','-v',str(ROOT/'devstate/script-console-target')+':/target','-e','CARGO_TARGET_DIR=/target','-e','RMM_TEST_REPAIR_ADMIN_URL=postgres://postgres@127.0.0.1/postgres','-e','RMM_TEST_REPAIR_APP_URL=postgres://rmm_app@127.0.0.1/postgres','--entrypoint','cargo',LINUX_IMAGE,'test',*common,'-p','rmm-server','--bin','rmm-server','--','--include-ignored','--test-threads=1'])
|
|
finally:
|
|
command('postgres-remove',['docker','rm','-f',pg_name])
|
|
for feature in ('default', 'system-helper'):
|
|
cargo('windows-' + feature + '-clippy', WINDOWS_IMAGE, 'clippy', *common,
|
|
'--target', 'x86_64-pc-windows-gnu', '-p', 'rmm-agent-windows', '--all-targets',
|
|
*(['--features', 'system-helper'] if feature == 'system-helper' else []), '--', '-D', 'warnings')
|
|
raw = cargo('linux-unit-link', LINUX_IMAGE, 'test', *common, '--workspace', '--all-targets',
|
|
'--target', 'x86_64-unknown-linux-gnu', '--release', '--no-run', '--message-format=json')
|
|
selected = {'rmm_domain': 'domain-tests', 'rmm_agent_linux': 'linux-runtime-tests',
|
|
'rmm-privileged-helper': 'helper-policy-tests', 'rmm-server': 'server-contract-tests'}
|
|
artifacts = {}
|
|
for line in raw.splitlines():
|
|
try:
|
|
value = json.loads(line)
|
|
except ValueError:
|
|
continue
|
|
target = value.get('target', {})
|
|
name = target.get('name')
|
|
if (value.get('reason') == 'compiler-artifact' and value.get('executable')
|
|
and value.get('profile', {}).get('test') and name in selected
|
|
and target.get('kind') in (['lib'], ['bin'])):
|
|
label = selected[name]
|
|
if label in artifacts:
|
|
raise RuntimeError('Duplicate selected test artifact: ' + label)
|
|
source = ROOT / 'devstate/script-console-target' / Path(value['executable']).relative_to('/target')
|
|
artifact = out / label
|
|
shutil.copyfile(source, artifact)
|
|
artifacts[label] = {'sha256': sha(artifact), 'bytes': artifact.stat().st_size,
|
|
'native_linux_executed': False, 'native_windows_executed': False}
|
|
if set(artifacts) != set(selected.values()):
|
|
raise RuntimeError('Incomplete Linux unit artifact set')
|
|
for label in artifacts:
|
|
command(label + '-list', ['docker', 'run', '--rm', '--network', 'none', '--read-only',
|
|
'--tmpfs', '/tmp:rw,exec,size=128m', '-v', str(out / label) + ':/unit:ro',
|
|
'--entrypoint', 'python3', LINUX_IMAGE, '-c',
|
|
'import shutil,os,subprocess;shutil.copyfile("/unit","/tmp/unit");os.chmod("/tmp/unit",0o500);subprocess.run(["/tmp/unit","--list","--format","terse"],check=True,cwd="/tmp")'])
|
|
cargo('linux-binary-link', LINUX_IMAGE, 'build', *common, '--release',
|
|
'--target', 'x86_64-unknown-linux-gnu', '-p', 'rmm-agent', '-p', 'rmm-privileged-helper', '-p', 'rmm-server', '--bins')
|
|
for name in ('rmm-agent', 'rmm-privileged-helper', 'rmm-server'):
|
|
artifact = out / name
|
|
shutil.copyfile(ROOT / 'devstate/script-console-target/x86_64-unknown-linux-gnu/release' / name, artifact)
|
|
artifacts[name] = {'sha256': sha(artifact), 'bytes': artifact.stat().st_size,
|
|
'installed': False, 'native_executed': False}
|
|
if inputs() != frozen:
|
|
raise RuntimeError('Compiler/deployment inputs changed during qualification; no deployable manifest')
|
|
archive = out / 'reviewed-sources.tar.gz'
|
|
with tarfile.open(archive, 'w:gz') as tar:
|
|
for name in frozen:
|
|
tar.add(ROOT / name, arcname=name, recursive=False)
|
|
manifest = {'schema_version': 1, 'type': 'rmm.script-console.local-source-qualification',
|
|
'created_at': dt.datetime.now(dt.timezone.utc).isoformat(), 'profile': 'full_maintenance_v1',
|
|
'images': {'linux': LINUX_IMAGE, 'windows_cross': WINDOWS_IMAGE, 'isolated_postgres': POSTGRES_IMAGE}, 'source_files': frozen,
|
|
'source_before_after_equal': True, 'checks': checks, 'artifacts': artifacts,
|
|
'source_archive_sha256': sha(archive), 'native_linux_executed': False,
|
|
'native_windows_executed': False, 'target_state_writes': False, 'rmm_jobs': False,
|
|
'capabilities_activated': False, 'approvals': False}
|
|
path = out / 'manifest.json'
|
|
path.write_text(json.dumps(manifest, indent=2) + '\n')
|
|
print(json.dumps({'manifest': str(path), 'sha256': sha(path), 'native_executed': False}))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|