fix: connectivity panel rearrangement. LXD VM for repeatable tests.
This commit is contained in:
1 parent
85a1ffa21f
commit
da889b4865
15 files changed
+363
-47
No files matched your search
Executable
+175
@@ -0,0 +1,175 @@
|
||||
#!/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, and 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 NAT proxy devices forward ports 7080 and 7081 of the host address --listen (or LXD_LISTEN,
|
||||
by default the IPv4 address of the host of the remote's URL) to the same ports of the VM, and port
|
||||
2222 to its SSH port. --remove deletes the VM 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='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')
|
||||
if 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'
|
||||
|
||||
if args.remove:
|
||||
if not exists:
|
||||
sys.exit(f'no VM {where}')
|
||||
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 debs:
|
||||
sys.exit('no out/linux-manager_*_all.deb; run ./build.sh first')
|
||||
|
||||
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')
|
||||
|
||||
print(f'launching {where} from {args.image} with {debs[-1].name}; proxies listen on {args.listen}')
|
||||
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 and proxy the host's --listen 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)
|
||||
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.
|
||||
if not push(args, debs[-1], '/tmp/linux-manager.deb', '0644'):
|
||||
sys.exit(f'could not copy {debs[-1].name} into {args.name}')
|
||||
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', '/tmp/linux-manager.deb')
|
||||
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()
|
||||
Reference in new issue
Block a user