feature: the VPN, the router, the name server and WebDAV are components switched on the System tab and installed on demand, Caddy from the Ubuntu archive is a dependency and serves every HTTP and HTTPS port, HTTPS is chosen at install, its certificate is of the Servers Issuing CA or self-signed by default, of ACME or of another CA, the issuing CAs are in one tab, a locked account is unlocked again, the name of a CA is fixed once the CA exists, and a run of setup is no longer cut short by a change.
Release / release (push) Failing after 8s
Release / release (push) Failing after 8s
This commit is contained in:
1 parent
a649331aa2
commit
a7bfabcb89
97 files changed
+4498
-1834
No files matched your search
+126
-36
@@ -1,17 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create or remove an LXD project for manual tests, or its uplink: lm-dev-lxd-project.py create|remove|uplink.
|
||||
"""Create or remove an LXD project for manual tests, its Vault, or its uplink: lm-dev-lxd-project.py
|
||||
create|remove|vault|uplink.
|
||||
|
||||
uplink setup creates the physical uplink network once per remote, and uplink remove deletes it
|
||||
(see uplink).
|
||||
(see uplink). vault create makes the project's Vault, vault unseal unseals it after a restart, and
|
||||
vault remove deletes it (see vault_create).
|
||||
|
||||
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;
|
||||
lm-dev-build.py publishes into it and lm-dev-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.
|
||||
lm-dev-build.py publishes into it and lm-dev-vm.py installs from it. The container vault is the
|
||||
project's Vault, made first, which the scripts hand secrets to the instances through; every other
|
||||
instance gets an AppRole of its own in it and /etc/vault (lm_dev.enrol_instance). What they need is written
|
||||
to .lm-dev-config/lxd-project-PROJECT.json, with the repository's key in
|
||||
.lm-dev-config/lxd-project-PROJECT.gpg. remove deletes the project's instances, its networks, the
|
||||
project and those files.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
@@ -22,7 +27,8 @@ import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
from lm_dev import PROJECT, ROOT, TESTS, lxc_exec
|
||||
from lm_dev import PROJECT, TESTS, VAULT_PORT, VaultError, config_path, enrol_instance, lxc_exec, vault_initialise, \
|
||||
vault_state, vault_unseal
|
||||
|
||||
NETWORKS = ('wan', 'lana', 'lanb')
|
||||
|
||||
@@ -33,8 +39,84 @@ def lxc(*args, project, check=True, input=None):
|
||||
check=check)
|
||||
|
||||
|
||||
def launch(args, name, user_data):
|
||||
"""Launch the container with the cloud-init user data on the project's default profile, wait until it has booted
|
||||
and has an address on eth0, and pin that address; the address."""
|
||||
lxc('launch', args.image, f'{args.remote}:{name}',
|
||||
'-c', 'cloud-init.user-data=' + (TESTS / 'cloud-init' / user_data).read_text(encoding='utf-8'),
|
||||
project=args.project)
|
||||
# cloud-init's status 2 is done with recoverable errors.
|
||||
for _ in range(120):
|
||||
if lxc_exec(args.remote, args.project, name, 'cloud-init', 'status', '--wait').returncode in (0, 2):
|
||||
break
|
||||
time.sleep(5)
|
||||
else:
|
||||
raise RuntimeError(f'the container {name} did not boot')
|
||||
for _ in range(24):
|
||||
match = re.search(r' inet (\d+\.\d+\.\d+\.\d+)/',
|
||||
lxc_exec(args.remote, args.project, name, 'ip', '-4', '-o', 'addr', 'show', 'dev',
|
||||
'eth0').stdout)
|
||||
if match:
|
||||
break
|
||||
time.sleep(5)
|
||||
else:
|
||||
raise RuntimeError(f'the container {name} has no address on eth0')
|
||||
lxc('config', 'device', 'override', f'{args.remote}:{name}', 'eth0', f'ipv4.address={match.group(1)}',
|
||||
project=args.project)
|
||||
return match.group(1)
|
||||
|
||||
|
||||
def vault_forward(args):
|
||||
"""The listen address of the forward of the project's Vault on wan, or None."""
|
||||
listing = lxc('network', 'forward', 'list', f'{args.remote}:wan', '-f', 'json', project=args.project,
|
||||
check=False)
|
||||
if listing.returncode != 0:
|
||||
return None
|
||||
return next((forward['listen_address'] for forward in json.loads(listing.stdout) or []
|
||||
if forward.get('config', {}).get('user.service') == 'vault'), None)
|
||||
|
||||
|
||||
def vault_create(args):
|
||||
"""Make the project's Vault: the container vault (cloud-init vault.yaml), a forward of its port on wan at
|
||||
--vault-address or an allocated one, which the scripts reach it on, and its initialisation with one key share,
|
||||
unsealed, with KV v2, TOTP and AppRole; its key and root token are kept in .lm-dev-config/vault-PROJECT.json;
|
||||
the state."""
|
||||
if vault_state(args.project) is not None:
|
||||
raise RuntimeError(f'project {args.project} has a Vault; remove it with vault remove first')
|
||||
print(f'launching the Vault container {args.remote}:vault (project {args.project})')
|
||||
address = launch(args, 'vault', 'vault.yaml')
|
||||
listen = [args.vault_address] if args.vault_address else ['--allocate=ipv4']
|
||||
lxc('network', 'forward', 'create', f'{args.remote}:wan', *listen, 'user.service=vault', project=args.project)
|
||||
forward = vault_forward(args)
|
||||
if not forward:
|
||||
raise RuntimeError('the forward of the Vault was not made')
|
||||
lxc('network', 'forward', 'port', 'add', f'{args.remote}:wan', forward, 'tcp', str(VAULT_PORT), address,
|
||||
str(VAULT_PORT), project=args.project)
|
||||
state = vault_initialise(args.project, f'http://{forward}:{VAULT_PORT}', f'http://{address}:{VAULT_PORT}')
|
||||
print(f'the Vault of {args.project} is at {state["url"]}, for the instances {state["internal_url"]}; '
|
||||
f'state: {config_path(f"vault-{args.project}.json")}')
|
||||
return state
|
||||
|
||||
|
||||
def vault_remove(args):
|
||||
"""Delete the project's Vault, its forward and its state; True when all went."""
|
||||
ok = True
|
||||
if lxc('info', f'{args.remote}:vault', project=args.project, check=False).returncode == 0:
|
||||
result = lxc('delete', '--force', f'{args.remote}:vault', project=args.project, check=False)
|
||||
if result.returncode != 0:
|
||||
ok = False
|
||||
print(f'could not delete vault: {result.stderr.strip()}', file=sys.stderr)
|
||||
forward = vault_forward(args)
|
||||
if forward:
|
||||
lxc('network', 'forward', 'delete', f'{args.remote}:wan', forward, project=args.project, check=False)
|
||||
if ok:
|
||||
config_path(f'vault-{args.project}.json').unlink(missing_ok=True)
|
||||
print(f'project {args.project} has no Vault')
|
||||
return ok
|
||||
|
||||
|
||||
def create(args, state, key):
|
||||
"""Create the project with its networks and default profile, and the repository container."""
|
||||
"""Create the project with its networks and default profile, the Vault 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)
|
||||
@@ -49,33 +131,16 @@ def create(args, state, key):
|
||||
lxc('profile', 'device', 'add', f'{args.remote}:default', 'eth0', 'nic', 'network=wan', 'name=eth0',
|
||||
project=args.project)
|
||||
|
||||
vault = vault_create(args)
|
||||
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=' + (TESTS / 'cloud-init' / 'repo.yaml').read_text(encoding='utf-8'),
|
||||
project=args.project)
|
||||
address = launch(args, 'repo', 'repo.yaml')
|
||||
problem = enrol_instance(args.remote, args.project, 'repo', vault)
|
||||
if problem:
|
||||
raise RuntimeError(problem)
|
||||
|
||||
def inside(*command):
|
||||
return lxc_exec(args.remote, args.project, 'repo', *command)
|
||||
|
||||
# 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.
|
||||
generate = ('gpg', '--batch', '--pinentry-mode', 'loopback', '--passphrase', '', '--quick-generate-key',
|
||||
'Linux Manager test repository', 'default', 'default', 'never')
|
||||
@@ -94,6 +159,7 @@ def create(args, state, key):
|
||||
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')
|
||||
state.chmod(0o600)
|
||||
print(f'project {args.project} is ready on {args.remote}: networks {", ".join(NETWORKS)}, '
|
||||
f'repository http://{address}/')
|
||||
print(f'state: {state}')
|
||||
@@ -125,6 +191,7 @@ def remove(args, state, key):
|
||||
if ok:
|
||||
state.unlink(missing_ok=True)
|
||||
key.unlink(missing_ok=True)
|
||||
config_path(f'vault-{args.project}.json').unlink(missing_ok=True)
|
||||
print(f'removed project {args.project} and its networks from {args.remote}')
|
||||
return ok
|
||||
|
||||
@@ -163,8 +230,9 @@ def uplink(args):
|
||||
|
||||
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='after uplink: setup or remove')
|
||||
parser.add_argument('action', choices=('create', 'remove', 'vault', 'uplink'))
|
||||
parser.add_argument('operation', nargs='?', choices=('setup', 'create', 'unseal', 'remove'),
|
||||
help='after vault: create, unseal or remove; after 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)')
|
||||
@@ -172,6 +240,9 @@ def main():
|
||||
help=f'the project (LXD_PROJECT, default {PROJECT})')
|
||||
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('--vault-address', default=os.environ.get('LXD_VAULT_ADDRESS'),
|
||||
help="the listen address of the Vault's forward, one of the uplink's routes "
|
||||
'(LXD_VAULT_ADDRESS); without it one is allocated')
|
||||
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'),
|
||||
@@ -187,8 +258,9 @@ def main():
|
||||
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')
|
||||
operations = {'uplink': ('setup', 'remove'), 'vault': ('create', 'unseal', 'remove')}
|
||||
if args.operation not in operations.get(args.action, (None,)):
|
||||
parser.error('setup or remove follows uplink, create, unseal or remove follows vault, and nothing else')
|
||||
|
||||
if args.action == 'uplink':
|
||||
try:
|
||||
@@ -196,8 +268,26 @@ def main():
|
||||
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'
|
||||
state = config_path(f'lxd-project-{args.project}.json')
|
||||
key = config_path(f'lxd-project-{args.project}.gpg')
|
||||
|
||||
if args.action == 'vault':
|
||||
try:
|
||||
if args.operation == 'create':
|
||||
vault_create(args)
|
||||
elif args.operation == 'unseal':
|
||||
current = vault_state(args.project)
|
||||
if current is None:
|
||||
sys.exit(f'project {args.project} has no Vault')
|
||||
vault_unseal(current)
|
||||
print(f'the Vault of {args.project} is unsealed')
|
||||
elif not vault_remove(args):
|
||||
sys.exit(1)
|
||||
except subprocess.CalledProcessError as error:
|
||||
sys.exit(f'{" ".join(error.cmd[:4])} failed: {error.stderr.strip()}')
|
||||
except (RuntimeError, VaultError) as error:
|
||||
sys.exit(str(error))
|
||||
return
|
||||
|
||||
if args.action == 'remove':
|
||||
sys.exit(0 if remove(args, state, key) else 1)
|
||||
@@ -212,7 +302,7 @@ def main():
|
||||
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:
|
||||
except (RuntimeError, VaultError) as error:
|
||||
sys.exit(f'{error}\nremove what was made with: {sys.argv[0]} remove --remote {args.remote} '
|
||||
f'--project {args.project}')
|
||||
|
||||
|
||||
Reference in new issue
Block a user