Files
linux-cloud-router/tests/remote/vm.py
T

221 lines
13 KiB
Python
Executable File

#!/usr/bin/env python3
"""Launch one Linux Manager server VM on an LXD remote to work with, or remove it: vm.py [--remove].
The VM is launched on the remote (--remote or LXD_REMOTE) in --project with that project's
default profile. It gets the newest out/linux-manager_*_all.deb with the administrator admin and
a generated password; in a test project made by lxd-project.py, which tmp/lxd-project-PROJECT.json
describes, it gets the package instead from that project's repository, as its apt source, so later
dev builds reach it as updates. It also gets a configuration from configs/ (core.json by
default), whose @ETH0@ is the interface of the VM's default route, @CIDR0@ its network and @PSK@
a generated key. The password and the key are written to tmp/vm-NAME.env. The VM's leased
address is pinned on its NIC, and ports 7080 and 7081 of a listen address lead to the same ports
of the VM, and port 2222 to its SSH port. In a test project the listen address is a network
forward's on the project's WAN, allocated from its uplink's routes; elsewhere NAT proxy devices
listen on the host address --listen (or LXD_LISTEN, by default the IPv4 address of the host of the
remote's URL). --remove deletes the VM, its forward and that file.
"""
import argparse
import ipaddress
import json
import os
import pathlib
import re
import secrets
import socket
import subprocess
import sys
import time
import urllib.parse
HERE = pathlib.Path(__file__).resolve().parent
ROOT = HERE.parent.parent
# The host port and the VM port of each proxy: the Manager, the status web service and SSH.
PROXIES = ((7080, 7080), (7081, 7081), (2222, 22))
def inside(args, *command, input=None):
"""Run the command in the VM with the input, and return its CompletedProcess."""
return subprocess.run(['lxc', 'exec', f'{args.remote}:{args.name}', '--project', args.project, '-T', '--',
*command], input=input, capture_output=True, text=True)
def push(args, source, path, mode):
"""Copy the local file into the VM at the path with the mode; True when it was copied."""
return subprocess.run(['lxc', 'file', 'push', str(source), f'{args.remote}:{args.name}{path}',
'--project', args.project, '--mode', mode], capture_output=True).returncode == 0
def main():
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument('--remote', default=os.environ.get('LXD_REMOTE'), help='the lxc remote (LXD_REMOTE)')
parser.add_argument('--project', default='default', help='the project of the VM (default default)')
parser.add_argument('--name', default='lm-dev', help='the name of the VM (default lm-dev)')
parser.add_argument('--image', default=os.environ.get('LXD_IMAGE', 'ubuntu:26.04'),
help='the image of the VM (LXD_IMAGE, default ubuntu:26.04)')
parser.add_argument('--config', default='core.json', help='the configuration from configs/ (default core.json)')
parser.add_argument('--listen', default=os.environ.get('LXD_LISTEN'),
help='outside a test project, the LXD host address the proxies listen on (LXD_LISTEN, '
'default the IPv4 address of the remote URL\'s host)')
parser.add_argument('--remove', action='store_true', help='delete the VM and its env file')
args = parser.parse_args()
if not args.remote:
parser.error('--remote or LXD_REMOTE is required')
project_file = ROOT / 'tmp' / f'lxd-project-{args.project}.json'
project = json.loads(project_file.read_text(encoding='utf-8')) if project_file.is_file() else None
if project and project['remote'] != args.remote:
sys.exit(f'{project_file} is of remote {project["remote"]}, not {args.remote}')
if not project and not args.remove and not args.listen:
# The default listen address is the IPv4 address of the remote's host in its lxc URL.
remotes = json.loads(subprocess.run(['lxc', 'remote', 'list', '--format', 'json'], capture_output=True,
text=True, check=True).stdout)
host = urllib.parse.urlsplit(remotes.get(args.remote, {}).get('Addr', '')).hostname
try:
args.listen = socket.getaddrinfo(host, None, socket.AF_INET)[0][4][0] if host else None
except socket.gaierror:
pass
if not args.listen:
parser.error(f'no IPv4 address of the host of remote {args.remote}; give --listen or LXD_LISTEN')
where = f'{args.remote}:{args.name} (project {args.project})'
exists = subprocess.run(['lxc', 'info', f'{args.remote}:{args.name}', '--project', args.project],
capture_output=True).returncode == 0
env = ROOT / 'tmp' / f'vm-{args.name}.env'
# In a test project the VM is reached through a network forward on the project's WAN, tagged with user.vm.
wan = f'{args.remote}:{project["networks"][0]}' if project else None
def forward_of_vm():
listing = subprocess.run(['lxc', 'network', 'forward', 'list', wan, '-f', 'json', '--project', args.project],
capture_output=True, text=True, check=True).stdout
return next((forward['listen_address'] for forward in json.loads(listing) or []
if forward.get('config', {}).get('user.vm') == args.name), None)
if args.remove:
if not exists:
sys.exit(f'no VM {where}')
if project and (listen := forward_of_vm()):
subprocess.run(['lxc', 'network', 'forward', 'delete', wan, listen, '--project', args.project], check=True)
subprocess.run(['lxc', 'delete', '--force', f'{args.remote}:{args.name}', '--project', args.project],
check=True)
env.unlink(missing_ok=True)
print(f'removed {where}')
return
if exists:
sys.exit(f'{where} exists; remove it with --remove or choose another --name')
config = HERE / 'configs' / args.config
if not config.is_file():
parser.error(f'no configs/{args.config}')
debs = sorted((ROOT / 'out').glob('linux-manager_*_all.deb'))
if not project and not debs:
sys.exit('no out/linux-manager_*_all.deb; run ./build.sh first')
source = f'the repository {project["repository"]}' if project else debs[-1].name
password, psk = secrets.token_hex(16), secrets.token_hex(24)
env.parent.mkdir(parents=True, exist_ok=True)
env.touch(mode=0o600)
env.chmod(0o600)
env.write_text(f'ADMIN_USERNAME=admin\nADMIN_PASSWORD={password}\nPSK={psk}\n', encoding='utf-8')
reached = f'a forward on {project["networks"][0]}' if project else f'proxies on {args.listen}'
print(f'launching {where} from {args.image} with {source}; reached through {reached}')
subprocess.run(['lxc', 'launch', args.image, f'{args.remote}:{args.name}', '--vm', '-c', 'limits.cpu=2',
'-c', 'limits.memory=4GiB',
'-c', 'cloud-init.user-data=' + (HERE / 'cloud-init' / 'server.yaml').read_text(encoding='utf-8'),
'--project', args.project], check=True)
# Wait for the agent and cloud-init (status 2 is done with recoverable errors), then for the default route.
for _ in range(120):
if inside(args, 'cloud-init', 'status', '--wait').returncode in (0, 2):
break
time.sleep(5)
else:
sys.exit(f'{args.name} did not boot; remove it with --remove')
for _ in range(24):
route = re.search(r'\bdev (\S+)', inside(args, 'ip', '-4', '-o', 'route', 'show', 'default').stdout)
address = route and re.search(r' inet (\S+) ',
inside(args, 'ip', '-4', '-o', 'addr', 'show', 'dev', route.group(1)).stdout)
if address:
break
time.sleep(5)
else:
sys.exit(f'{args.name} has no default route or address; remove it with --remove')
nic, interface = route.group(1), ipaddress.IPv4Interface(address.group(1))
# Pin the leased address on the profile's NIC, then forward or proxy the ports to the VM.
mac = inside(args, 'cat', f'/sys/class/net/{nic}/address').stdout.strip()
expanded = subprocess.run(['lxc', 'config', 'show', '--expanded', f'{args.remote}:{args.name}',
'--project', args.project], capture_output=True, text=True, check=True).stdout
device = next((m.group(1) for m in re.finditer(r'^\s*volatile\.(\S+)\.hwaddr: "?([0-9a-f:]+)"?$', expanded, re.M)
if m.group(2) == mac), None)
if not device:
sys.exit(f'no LXD device of {args.name} has the MAC address {mac} of {nic}')
subprocess.run(['lxc', 'config', 'device', 'override', f'{args.remote}:{args.name}', device,
f'ipv4.address={interface.ip}', '--project', args.project], check=True)
if project:
# An OVN network refuses NAT proxies; the forward's listen address is allocated from the uplink's routes.
subprocess.run(['lxc', 'network', 'forward', 'create', wan, '--allocate=ipv4', f'user.vm={args.name}',
'--project', args.project], check=True)
args.listen = forward_of_vm()
if not args.listen:
sys.exit(f'no forward of {args.name} on {wan}; remove the VM with --remove')
for host_port, vm_port in PROXIES:
subprocess.run(['lxc', 'network', 'forward', 'port', 'add', wan, args.listen, 'tcp', str(host_port),
str(interface.ip), str(vm_port), '--project', args.project], check=True)
else:
for host_port, vm_port in PROXIES:
subprocess.run(['lxc', 'config', 'device', 'add', f'{args.remote}:{args.name}', f'proxy-{vm_port}',
'proxy', f'listen=tcp:{args.listen}:{host_port}', f'connect=tcp:{interface.ip}:{vm_port}',
'nat=true', '--project', args.project], check=True)
# Install the package with the test administrator, from the test project's repository when there is one.
if project:
sources = (f'Types: deb\nURIs: {project["repository"]}\nSuites: resolute\nComponents: main\n'
f'Signed-By: /etc/apt/keyrings/linux-manager-test.gpg\n')
if (inside(args, 'mkdir', '-p', '/etc/apt/keyrings').returncode != 0 or
not push(args, project['key'], '/etc/apt/keyrings/linux-manager-test.gpg', '0644') or
inside(args, 'tee', '/etc/apt/sources.list.d/linux-manager-test.sources', input=sources).returncode != 0):
sys.exit(f'could not add the repository of project {args.project} to {args.name}')
updated = inside(args, 'apt-get', 'update')
if updated.returncode != 0:
sys.exit(f'apt-get update failed in {args.name}: {updated.stderr.strip()}')
package = 'linux-manager'
else:
if not push(args, debs[-1], '/tmp/linux-manager.deb', '0644'):
sys.exit(f'could not copy {debs[-1].name} into {args.name}')
package = '/tmp/linux-manager.deb'
preseed = (f'linux-manager linux-manager/admin_username string admin\n'
f'linux-manager linux-manager/admin_password password {password}\n')
if inside(args, 'debconf-set-selections', input=preseed).returncode != 0:
sys.exit('debconf-set-selections failed')
result = inside(args, 'env', 'DEBIAN_FRONTEND=noninteractive', 'apt-get', 'install', '-y', package)
log = ROOT / 'tmp' / f'vm-{args.name}-install.log'
log.write_text(result.stdout + result.stderr, encoding='utf-8')
if result.returncode != 0:
sys.exit(f'the package did not install; see {log}')
# Apply the configuration twice, as the server's certificates follow the first run.
text = (config.read_text(encoding='utf-8').replace('@ETH0@', nic).replace('@CIDR0@', str(interface.network))
.replace('@PSK@', psk))
rendered = ROOT / 'tmp' / f'vm-{args.name}.json'
rendered.touch(mode=0o600)
rendered.chmod(0o600)
rendered.write_text(text, encoding='utf-8')
pushed = push(args, rendered, '/etc/linux-manager/linux-manager.json', '0600')
rendered.unlink()
if not pushed:
sys.exit(f'could not copy {args.config} into {args.name}')
for _ in range(2):
if inside(args, 'systemctl', 'restart', 'linux-manager-setup').returncode != 0:
sys.exit(f'linux-manager-setup failed with {args.config}; see journalctl -u linux-manager-setup in the VM')
print(f'{args.name} is up at {interface.ip} with {args.config}')
print(f'administrator: admin password: {password} (also in {env})')
print(f'manager: http://{args.listen}:7080/ status: http://{args.listen}:7081/ ssh: {args.listen} port 2222')
print(f'shell: lxc exec {args.remote}:{args.name} --project {args.project} -- su -l')
print(f'remove: {sys.argv[0]} --remote {args.remote} --project {args.project} --name {args.name} --remove')
if __name__ == '__main__':
main()