Files
rust-discovery/tools/dev_opnsense_bundle.py
T
kevin_janssenandClaude Opus 5.5 93429e4394 Keep OPNsense telemetry when the script worker cannot start and build it for FreeBSD 14.1
A worker linked on the FreeBSD 15 test guest needs FBSD_1.9 and does not load on
OPNsense 24.7; the agent then retried the job session every 30 s and never sent a
heartbeat or inventory. The agent now pauses script capabilities for an hour and
reconnects without them. tools/dev_opnsense_worker.py links the worker against the
official FreeBSD 14.1 pkgbase libraries. The installer checks that the worker loads
before binding it and reports success only after a heartbeat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 12:17:34 +01:00

108 lines
5.0 KiB
Python

#!/usr/bin/env python3
"""Build the OPNsense agent bundle and place it on the dev server for the HTTPS installer.
The bundle is `agent.pyz`, `collect.pyz`, the rc.d script and optionally the native
script worker `rmm-opnsense-jobs` (FreeBSD amd64, built with tools/dev_opnsense_worker.py
against FreeBSD 14.1 so that it also loads on OPNsense 24.x).
It lands in `devstate/mvp-releases/config/opnsense/` next to the Linux bootstrap
configuration and is served only against a valid one-time enrollment code.
"""
import argparse
import base64
import hashlib
import json
from pathlib import Path
import re
import shlex
import subprocess
import sys
import tempfile
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tools'))
def activate(payload):
"""Runs on the dev server as root."""
import os
target = Path('/opt/rust-discovery/devstate/mvp-releases/config/opnsense')
owner = os.stat(target.parent)
target.mkdir(mode=0o700, exist_ok=True)
os.chown(target, owner.st_uid, owner.st_gid)
os.chmod(target, 0o700)
wanted = set(payload['files']) | {'manifest.json'}
for name, data in [*payload['files'].items(), ('manifest.json', payload['manifest'])]:
raw = base64.b64decode(data, validate=True)
tmp = target / ('.' + name + '.new')
with open(tmp, 'wb') as f:
os.fchmod(f.fileno(), 0o600)
f.write(raw)
f.flush()
os.fsync(f.fileno())
os.chown(tmp, owner.st_uid, owner.st_gid)
os.replace(tmp, target / name)
for old in target.iterdir():
if old.name not in wanted:
old.unlink()
print(json.dumps({'directory': str(target), 'files': sorted(wanted)}))
def build_zipapps(out):
python = ROOT / 'devstate/opnsense-vm/venv/bin/python'
if not python.is_file():
raise SystemExit('Build-venv fehlt: devstate/opnsense-vm/venv mit grpcio-tools==1.76.0 und protobuf==6.33.5')
subprocess.run([str(python), str(ROOT / 'deploy/opnsense/build.py'), str(out)], check=True)
def worker_from_guest():
credentials = ROOT / 'credentials'
ssh = ['ssh', '-F', '/dev/null', '-T', '-i', str(credentials / 'opnsense-dev-key'),
'-o', 'UserKnownHostsFile=' + str(credentials / 'opnsense-dev.known_hosts'),
'-o', 'GlobalKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes',
'-o', 'IdentitiesOnly=yes', '-o', 'ConnectTimeout=10', 'root@192.168.247.1']
return subprocess.run([*ssh, 'cat /usr/local/libexec/discovery-agent/rmm-opnsense-jobs'],
check=True, capture_output=True).stdout
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
worker = parser.add_mutually_exclusive_group()
worker.add_argument('--worker', type=Path, help='Native rmm-opnsense-jobs (FreeBSD amd64) to include')
worker.add_argument('--worker-from-guest', action='store_true',
help='Take the installed rmm-opnsense-jobs from the OPNsense test guest (loads only on the guest release)')
parser.add_argument('--prepare-only', type=Path, metavar='DIR', help='Write the bundle locally, do not upload')
args = parser.parse_args()
version = re.search(r"^VERSION = '([A-Za-z0-9._-]+)'$", (ROOT / 'deploy/opnsense/agent.py').read_text(), re.M)[1]
with tempfile.TemporaryDirectory() as tmp:
out = Path(tmp)
build_zipapps(out)
files = {name: (out / name).read_bytes() for name in ('agent.pyz', 'collect.pyz')}
files['discovery_agent'] = (ROOT / 'deploy/opnsense/discovery_agent').read_bytes()
if args.worker:
files['rmm-opnsense-jobs'] = args.worker.read_bytes()
elif args.worker_from_guest:
files['rmm-opnsense-jobs'] = worker_from_guest()
if 'rmm-opnsense-jobs' in files and not files['rmm-opnsense-jobs'].startswith(b'\x7fELF'):
raise SystemExit('rmm-opnsense-jobs ist keine ELF-Datei')
manifest = json.dumps({'schema_version': 1, 'version': version, 'files': {
name: {'sha256': hashlib.sha256(data).hexdigest(), 'bytes': len(data)} for name, data in sorted(files.items())
}}, indent=1).encode()
if args.prepare_only:
args.prepare_only.mkdir(parents=True, exist_ok=True, mode=0o700)
for name, data in [*files.items(), ('manifest.json', manifest)]:
(args.prepare_only / name).write_bytes(data)
print(manifest.decode())
return
from dev_stage import ssh_prefix
import inspect
payload = json.dumps({'manifest': base64.b64encode(manifest).decode(),
'files': {n: base64.b64encode(d).decode() for n, d in files.items()}})
source = 'import base64, json, os\nfrom pathlib import Path\n' + inspect.getsource(activate)
subprocess.run(ssh_prefix('server') + [shlex.join(['python3', '-I', '-'])],
input=source + '\nactivate(json.loads(' + repr(payload) + '))\n', text=True, check=True)
print(manifest.decode())
if __name__ == '__main__':
main()