fix: update remote LXD scripts to use OVN backed NAT-ed networks.

This commit is contained in:
slawek committed 2026-11-22 19:27:53 +01:00
1 parent da889b4865
commit 5fff009320
9 files changed
+500 -19

No files matched your search

+221
View File
@@ -0,0 +1,221 @@
#!/usr/bin/env python3
"""Create or remove an LXD project for manual tests: lxd-project.py create|remove|uplink setup|remove.
uplink setup creates the physical uplink network once per remote, uplink remove deletes it (see
uplink).
create makes, on the remote (--remote or LXD_REMOTE), the project (--project or LXD_PROJECT,
lm-dev by default) with its root disks on --pool (LXD_POOL) and networks of its own, OVN networks
on the uplink --uplink (LXD_UPLINK, uplink by default): wan, with NAT and DHCP on a subnet LXD
picks, and lana and lanb, on subnets LXD picks without DHCP. The project's default profile puts
eth0 on wan. The container repo serves a signed apt repository of the dev builds over HTTP; dev-build.py publishes into it and vm.py installs from it. What they need is
written to tmp/lxd-project-PROJECT.json, with the repository's key in tmp/lxd-project-PROJECT.gpg.
remove deletes the project's instances, its networks, the project and those files.
"""
import argparse
import json
import os
import pathlib
import re
import subprocess
import sys
import time
HERE = pathlib.Path(__file__).resolve().parent
ROOT = HERE.parent.parent
NETWORKS = ('wan', 'lana', 'lanb')
def lxc(*args, project, check=True, input=None):
"""Run lxc with the arguments in the project; raise CalledProcessError when it fails, unless check is false."""
return subprocess.run(['lxc', *args, '--project', project], input=input, capture_output=True, text=True,
check=check)
def create(args, state, key):
"""Create the project with its networks and default profile, and the repository container."""
subprocess.run(['lxc', 'project', 'create', f'{args.remote}:{args.project}', '-s', args.pool,
'-c', 'features.images=false', '-c', 'features.networks=true'],
capture_output=True, text=True, check=True)
print(f'project {args.project} created; creating its OVN networks on {args.uplink}')
lxc('network', 'create', f'{args.remote}:wan', '--type=ovn', f'network={args.uplink}', 'ipv4.address=auto',
'ipv4.nat=true', 'ipv4.dhcp=true', 'ipv6.address=none', project=args.project)
# OVN gives every network's router an address; without DHCP the LAN hosts keep their own gateways.
for name in ('lana', 'lanb'):
lxc('network', 'create', f'{args.remote}:{name}', '--type=ovn', f'network={args.uplink}',
'ipv4.address=auto', 'ipv4.nat=true', 'ipv4.dhcp=false', 'ipv6.address=none', project=args.project)
# The project's default profile has the root disk on the pool of project create's -s.
lxc('profile', 'device', 'add', f'{args.remote}:default', 'eth0', 'nic', 'network=wan', 'name=eth0',
project=args.project)
print(f'launching the repository container {args.remote}:repo (project {args.project})')
lxc('launch', args.image, f'{args.remote}:repo',
'-c', 'cloud-init.user-data=' + (HERE / 'cloud-init' / 'repo.yaml').read_text(encoding='utf-8'),
project=args.project)
def inside(*command):
return subprocess.run(['lxc', 'exec', f'{args.remote}:repo', '--project', args.project, '-T', '--', *command],
capture_output=True, text=True)
# Wait for cloud-init (status 2 is done with recoverable errors), then for the address of eth0.
for _ in range(120):
if inside('cloud-init', 'status', '--wait').returncode in (0, 2):
break
time.sleep(5)
else:
raise RuntimeError('the repository container did not boot')
for _ in range(24):
match = re.search(r' inet (\d+\.\d+\.\d+\.\d+)/', inside('ip', '-4', '-o', 'addr', 'show', 'dev', 'eth0').stdout)
if match:
break
time.sleep(5)
else:
raise RuntimeError('the repository container has no address on eth0')
address = match.group(1)
lxc('config', 'device', 'override', f'{args.remote}:repo', 'eth0', f'ipv4.address={address}',
project=args.project)
# The signing key, the publishing script and the empty indexes.
for step, command in (('key generation', ('gpg', '--batch', '--pinentry-mode', 'loopback', '--passphrase', '',
'--quick-generate-key', 'Linux Manager test repository', 'default',
'default', 'never')),
('key export', ('gpg', '--batch', '--yes', '--export', '-o', '/root/linux-manager-test.gpg'))):
result = inside(*command)
if result.returncode != 0:
raise RuntimeError(f'the {step} failed in repo: {result.stderr.strip()}')
lxc('file', 'push', str(HERE / 'repo' / 'publish.sh'), f'{args.remote}:repo/usr/local/sbin/linux-manager-repo-publish',
'--mode', '0755', project=args.project)
result = inside('linux-manager-repo-publish')
if result.returncode != 0:
raise RuntimeError(f'the repository was not published: {result.stderr.strip()}')
lxc('file', 'pull', f'{args.remote}:repo/root/linux-manager-test.gpg', str(key), project=args.project)
state.write_text(json.dumps({'remote': args.remote, 'project': args.project, 'networks': list(NETWORKS),
'repository': f'http://{address}/', 'key': str(key)}, indent=2) + '\n',
encoding='utf-8')
print(f'project {args.project} is ready on {args.remote}: networks {", ".join(NETWORKS)}, '
f'repository http://{address}/')
print(f'state: {state}')
def remove(args, state, key):
"""Delete the project's instances, the networks, the project and the local files; True when all went."""
ok = True
listing = lxc('list', f'{args.remote}:', '-c', 'n', '-f', 'csv', project=args.project, check=False)
for name in listing.stdout.split():
result = lxc('delete', '--force', f'{args.remote}:{name}', project=args.project, check=False)
if result.returncode != 0:
ok = False
print(f'could not delete {name}: {result.stderr.strip()}', file=sys.stderr)
# The default profile's eth0 keeps wan in use.
lxc('profile', 'device', 'remove', f'{args.remote}:default', 'eth0', project=args.project, check=False)
for name in NETWORKS:
if lxc('network', 'show', f'{args.remote}:{name}', project=args.project, check=False).returncode != 0:
continue
result = lxc('network', 'delete', f'{args.remote}:{name}', project=args.project, check=False)
if result.returncode != 0:
ok = False
print(f'could not delete network {name}: {result.stderr.strip()}', file=sys.stderr)
result = subprocess.run(['lxc', 'project', 'delete', f'{args.remote}:{args.project}'], capture_output=True,
text=True)
if result.returncode != 0:
ok = False
print(f'could not delete project {args.project}: {result.stderr.strip()}', file=sys.stderr)
if ok:
state.unlink(missing_ok=True)
key.unlink(missing_ok=True)
print(f'removed project {args.project} and its networks from {args.remote}')
return ok
def uplink(args):
"""Create the physical uplink network of the OVN networks on a host bridge of the LAN, or delete it.
setup creates --uplink in the default project with parent --parent, the LAN's gateway
--gateway (address/prefix), its name servers --dns, the LAN addresses OVN routers take
(--ovn-ranges, FIRST-LAST) and the LAN subnets for network forwards (--routes, CIDR); OVN
answers ARP for those on the LAN (ovn.ingress_mode l2proxy, the default). It leaves an
existing network of that name as it is. remove deletes the network.
"""
network = f'{args.remote}:{args.uplink}'
exists = lxc('network', 'show', network, project='default', check=False).returncode == 0
if args.operation == 'remove':
if exists:
lxc('network', 'delete', network, project='default')
print(f'{args.remote} has no network {args.uplink}')
return
if exists:
print(f'{args.remote} has the network {args.uplink} already')
return
missing = [option for option, value in (('--parent', args.parent), ('--gateway', args.gateway), ('--dns', args.dns),
('--ovn-ranges', args.ovn_ranges), ('--routes', args.routes)) if not value]
if missing:
sys.exit(f'uplink setup needs {", ".join(missing)}')
lxc('network', 'create', network, '--type=physical', f'parent={args.parent}', f'ipv4.gateway={args.gateway}',
f'dns.nameservers={args.dns}', f'ipv4.ovn.ranges={args.ovn_ranges}', f'ipv4.routes={args.routes}',
project='default')
print(f'{args.uplink} on {args.remote}: physical on {args.parent}, gateway {args.gateway}, OVN routers '
f'{args.ovn_ranges}, forwards {args.routes}')
def main():
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument('action', choices=('create', 'remove', 'uplink'))
parser.add_argument('operation', nargs='?', choices=('setup', 'remove'), help='with uplink: setup or remove')
parser.add_argument('--remote', default=os.environ.get('LXD_REMOTE'), help='the lxc remote (LXD_REMOTE)')
parser.add_argument('--pool', default=os.environ.get('LXD_POOL'),
help='the storage pool of the project, for create (LXD_POOL)')
parser.add_argument('--project', default=os.environ.get('LXD_PROJECT', 'lm-dev'),
help='the project (LXD_PROJECT, default lm-dev)')
parser.add_argument('--image', default=os.environ.get('LXD_IMAGE', 'ubuntu:26.04'),
help='the image of the repository container (LXD_IMAGE, default ubuntu:26.04)')
parser.add_argument('--uplink', default=os.environ.get('LXD_UPLINK', 'uplink'),
help='the physical uplink network of the OVN networks (LXD_UPLINK, default uplink)')
parser.add_argument('--parent', default=os.environ.get('LXD_UPLINK_PARENT'),
help='uplink setup: the host bridge on the LAN (LXD_UPLINK_PARENT)')
parser.add_argument('--gateway', default=os.environ.get('LXD_UPLINK_GATEWAY'),
help='uplink setup: the LAN gateway as address/prefix (LXD_UPLINK_GATEWAY)')
parser.add_argument('--dns', default=os.environ.get('LXD_UPLINK_DNS'),
help='uplink setup: the LAN name servers, comma-separated (LXD_UPLINK_DNS)')
parser.add_argument('--ovn-ranges', default=os.environ.get('LXD_UPLINK_OVN_RANGES'),
help='uplink setup: LAN addresses for OVN routers, FIRST-LAST (LXD_UPLINK_OVN_RANGES)')
parser.add_argument('--routes', default=os.environ.get('LXD_UPLINK_ROUTES'),
help='uplink setup: LAN subnets for network forwards, CIDR (LXD_UPLINK_ROUTES)')
args = parser.parse_args()
if not args.remote:
parser.error('--remote or LXD_REMOTE is required')
if (args.action == 'uplink') != (args.operation is not None):
parser.error('setup or remove follows uplink, and only uplink')
if args.action == 'uplink':
try:
uplink(args)
except subprocess.CalledProcessError as error:
sys.exit(f'{" ".join(error.cmd[:4])} failed: {error.stderr.strip()}')
return
state = ROOT / 'tmp' / f'lxd-project-{args.project}.json'
key = ROOT / 'tmp' / f'lxd-project-{args.project}.gpg'
if args.action == 'remove':
sys.exit(0 if remove(args, state, key) else 1)
if not args.pool:
parser.error('--pool or LXD_POOL is required')
if subprocess.run(['lxc', 'project', 'show', f'{args.remote}:{args.project}'], capture_output=True).returncode == 0:
sys.exit(f'project {args.project} exists on {args.remote}; remove it first or choose another --project')
state.parent.mkdir(parents=True, exist_ok=True)
try:
create(args, state, key)
except subprocess.CalledProcessError as error:
sys.exit(f'{" ".join(error.cmd[:4])} failed: {error.stderr.strip()}\n'
f'remove what was made with: {sys.argv[0]} remove --remote {args.remote} --project {args.project}')
except RuntimeError as error:
sys.exit(f'{error}\nremove what was made with: {sys.argv[0]} remove --remote {args.remote} '
f'--project {args.project}')
if __name__ == '__main__':
main()