38 lines
1.8 KiB
Python
38 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Provision stopped sandbox guests through the Debian-to-Pimox SSH path."""
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def main():
|
|
scripts = Path(__file__).resolve().parent
|
|
host = os.environ.get('LAB_PIMOX_HOST', '192.168.100.80')
|
|
user = os.environ.get('LAB_PIMOX_USER', 'jv')
|
|
key = os.environ.get('LAB_PIMOX_SSH_KEY_PATH', '/home/jv/.ssh/id_ed25519')
|
|
try:
|
|
spec = importlib.util.spec_from_file_location('sandbox_config', scripts / 'network-sandbox-config.py')
|
|
config = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(config)
|
|
configs = config.build_configs()
|
|
public_key = Path(key + '.pub')
|
|
authorized = public_key.read_text(encoding='utf-8').strip() if public_key.is_file() else ''
|
|
if not authorized.startswith(('ssh-ed25519 ', 'ssh-rsa ', 'ecdsa-sha2-')):
|
|
raise ValueError('A readable SSH public key is required for the sandbox router')
|
|
source = (scripts / 'network-sandbox-guests-host.py').read_text(encoding='utf-8')
|
|
prelude = 'import json\nCONFIGS = json.loads(' + repr(json.dumps(configs)) + ')\nAUTHORIZED_KEY = ' + repr(authorized) + '\n'
|
|
return subprocess.run(['ssh', '-T', '-i', key, '-o', 'BatchMode=yes',
|
|
'-o', 'IdentitiesOnly=yes', '-o', 'ConnectTimeout=10',
|
|
'-o', 'StrictHostKeyChecking=accept-new', f'{user}@{host}',
|
|
'sudo -n python3 -'], input=(prelude + source).encode(), timeout=1800).returncode
|
|
except (OSError, ValueError, subprocess.SubprocessError) as exc:
|
|
print(f'Sandbox guest provisioning failed: {exc}', file=sys.stderr)
|
|
return 1
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|