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
+75
-47
@@ -3,34 +3,41 @@
|
||||
|
||||
create launches the VM --vm (lm-dev by default) on the remote --remote (LXD_REMOTE) in the project
|
||||
--project (LXD_PROJECT, lm-dev by default) with that project's default profile. In a test project
|
||||
made by lm-dev-lxd-project.py, which tmp/lxd-project-PROJECT.json describes, the VM gets the package
|
||||
made by lm-dev-lxd-project.py, which .lm-dev-config/lxd-project-PROJECT.json describes, the VM gets the package
|
||||
from that project's repository, as its apt source, so later dev builds reach it as updates;
|
||||
elsewhere it gets the newest out/linux-manager_*_all.deb. The administrator is admin with a
|
||||
generated password, set as ADMIN_USERNAME and ADMIN_PASSWORD in the repository's .env. The VM
|
||||
gets a configuration from tests/remote/configs/ (--config, core.json by default), whose @ETH0@ is
|
||||
the interface of its default route, @CIDR0@ its network and @PSK@ a generated key, written to
|
||||
tmp/vm-VM.env.
|
||||
elsewhere it gets --deb, or else the package built last, in out/ or out/dev/. The administrator is admin with a
|
||||
generated password, set as ADMIN_USERNAME and ADMIN_PASSWORD in the repository's .env, and a TOTP
|
||||
token, the key VM-admin of the project's Vault's TOTP engine, unless --no-otp is given;
|
||||
lm-dev-otp-code.py prints its codes. The VM gets a configuration from tests/remote/configs/
|
||||
(--config, core.json by default), whose @ETH0@ is the interface of its default route, @CIDR0@ its
|
||||
network and @PSK@ a generated key. The VM gets an AppRole of its own in the project's Vault
|
||||
(lm_dev.enrol_instance), with which it reads the debconf preseed with the admin's password and the
|
||||
configuration with its key, kept there as VM/preseed and VM/config, and creates its TOTP key; no
|
||||
secret is an argument or on standard input, so create needs a project with a Vault
|
||||
(lm-dev-lxd-project.py vault create). The admin's password is also kept there as VM/admin.
|
||||
|
||||
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
|
||||
network forward's on the project's WAN, --forward-address (LXD_FORWARD_ADDRESS) or else one allocated
|
||||
from its uplink's routes; elsewhere NAT proxy
|
||||
devices listen on the host address --listen (LXD_LISTEN, by default the IPv4 address of the host of
|
||||
the remote's URL). remove deletes the VM, its forward and tmp/vm-VM.env; .env is left as it is.
|
||||
the remote's URL). remove deletes the VM, its forward and what the Vault keeps for it; .env is left
|
||||
as it is.
|
||||
"""
|
||||
|
||||
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
|
||||
|
||||
from lm_dev import PROJECT, ROOT, TESTS, lxc_exec, set_dotenv
|
||||
from lm_dev import HANDOVER_DIR, PROJECT, ROOT, TESTS, VAULT_MOUNT, VaultError, config_path, enrol_instance, enrol_otp, \
|
||||
forget_instance, hand_over, host_address, lxc_exec, newest_deb, set_dotenv, store_secret, vault_state
|
||||
|
||||
# The listen port and the VM port of each forward or proxy: the Manager, the status web service and SSH.
|
||||
PORTS = ((7080, 7080), (7081, 7081), (2222, 22))
|
||||
@@ -50,28 +57,27 @@ def main():
|
||||
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('--forward-address', default=os.environ.get('LXD_FORWARD_ADDRESS'),
|
||||
help="with create in a test project, the forward's listen address, one of the uplink's routes "
|
||||
"(LXD_FORWARD_ADDRESS); without it one is allocated")
|
||||
parser.add_argument('--no-otp', action='store_true', help='with create, enrol no TOTP token for admin')
|
||||
parser.add_argument('--deb', help='outside a test project, the package to install (default the one built last, '
|
||||
'in out/ or out/dev/)')
|
||||
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_file = config_path(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 args.action == 'create' 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
|
||||
args.listen = host_address(args.remote)
|
||||
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.vm} (project {args.project})'
|
||||
exists = subprocess.run(['lxc', 'info', f'{args.remote}:{args.vm}', '--project', args.project],
|
||||
capture_output=True).returncode == 0
|
||||
env = ROOT / 'tmp' / f'vm-{args.vm}.env'
|
||||
|
||||
def inside(*command, input=None):
|
||||
return lxc_exec(args.remote, args.project, args.vm, *command, input=input)
|
||||
@@ -95,7 +101,11 @@ def main():
|
||||
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.vm}', '--project', args.project], check=True)
|
||||
env.unlink(missing_ok=True)
|
||||
if (vault := vault_state(args.project)) is not None:
|
||||
try:
|
||||
forget_instance(vault, args.vm)
|
||||
except VaultError as error:
|
||||
print(f'the Vault keeps what it had for {args.vm}: {error}', file=sys.stderr)
|
||||
print(f'removed {where}')
|
||||
return
|
||||
|
||||
@@ -104,16 +114,16 @@ def main():
|
||||
config = TESTS / 'configs' / args.config
|
||||
if not config.is_file():
|
||||
parser.error(f'no tests/remote/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
|
||||
deb = pathlib.Path(args.deb) if args.deb else newest_deb()
|
||||
if not project and (deb is None or not deb.is_file()):
|
||||
sys.exit(f'no package {args.deb}' if args.deb else 'no out/linux-manager_*_all.deb; run ./build.sh first')
|
||||
source = f'the repository {project["repository"]}' if project else str(deb)
|
||||
vault = vault_state(args.project)
|
||||
if vault is None:
|
||||
sys.exit(f'project {args.project} has no Vault, which the secrets reach the VM through; make it with '
|
||||
'lm-dev-lxd-project.py vault create')
|
||||
|
||||
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'PSK={psk}\n', encoding='utf-8')
|
||||
set_dotenv({'ADMIN_USERNAME': 'admin', 'ADMIN_PASSWORD': password})
|
||||
|
||||
reached = f'a forward on {project["networks"][0]}' if project else f'proxies on {args.listen}'
|
||||
@@ -130,6 +140,9 @@ def main():
|
||||
time.sleep(5)
|
||||
else:
|
||||
sys.exit(f'{args.vm} did not boot; remove it with remove')
|
||||
problem = enrol_instance(args.remote, args.project, args.vm, vault)
|
||||
if problem:
|
||||
sys.exit(f'{problem}; remove the VM with remove')
|
||||
for _ in range(24):
|
||||
route = re.search(r'\bdev (\S+)', inside('ip', '-4', '-o', 'route', 'show', 'default').stdout)
|
||||
address = route and re.search(r' inet (\S+) ',
|
||||
@@ -152,8 +165,10 @@ def main():
|
||||
subprocess.run(['lxc', 'config', 'device', 'override', f'{args.remote}:{args.vm}', 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.vm}',
|
||||
# An OVN network refuses NAT proxies; the forward's listen address is --forward-address, or one allocated
|
||||
# from the uplink's routes.
|
||||
address = [args.forward_address] if args.forward_address else ['--allocate=ipv4']
|
||||
subprocess.run(['lxc', 'network', 'forward', 'create', wan, *address, f'user.vm={args.vm}',
|
||||
'--project', args.project], check=True)
|
||||
args.listen = forward_of_vm()
|
||||
if not args.listen:
|
||||
@@ -180,12 +195,22 @@ def main():
|
||||
sys.exit(f'apt-get update failed in {args.vm}: {updated.stderr.strip()}')
|
||||
package = 'linux-manager'
|
||||
else:
|
||||
if not push(debs[-1], '/tmp/linux-manager.deb', '0644'):
|
||||
sys.exit(f'could not copy {debs[-1].name} into {args.vm}')
|
||||
if not push(deb, '/tmp/linux-manager.deb', '0644'):
|
||||
sys.exit(f'could not copy {deb} into {args.vm}')
|
||||
package = '/tmp/linux-manager.deb'
|
||||
preseed = (f'linux-manager linux-manager/admin_username string admin\n'
|
||||
preseed = ('linux-manager linux-manager/admin_username string admin\n'
|
||||
f'linux-manager linux-manager/admin_password password {password}\n')
|
||||
if inside('debconf-set-selections', input=preseed).returncode != 0:
|
||||
try:
|
||||
store_secret(vault, f'{args.vm}/admin', {'password': password})
|
||||
except VaultError as error:
|
||||
sys.exit(f'the admin password cannot be kept in the Vault: {error}')
|
||||
if inside('install', '-d', '-m', '0700', HANDOVER_DIR).returncode != 0:
|
||||
sys.exit(f'{HANDOVER_DIR} cannot be made in {args.vm}')
|
||||
problem = hand_over(args.remote, args.project, args.vm, vault, 'preseed', preseed, f'{HANDOVER_DIR}/preseed', '0600')
|
||||
if problem:
|
||||
sys.exit(problem)
|
||||
if inside('sh', '-c', f'debconf-set-selections {HANDOVER_DIR}/preseed; status=$?; rm -f {HANDOVER_DIR}/preseed; '
|
||||
'exit $status').returncode != 0:
|
||||
sys.exit(f'debconf-set-selections failed in {args.vm}')
|
||||
result = inside('env', 'DEBIAN_FRONTEND=noninteractive', 'apt-get', 'install', '-y', package)
|
||||
log = ROOT / 'tmp' / f'vm-{args.vm}-install.log'
|
||||
@@ -194,22 +219,25 @@ def main():
|
||||
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.vm}.json'
|
||||
rendered.touch(mode=0o600)
|
||||
rendered.chmod(0o600)
|
||||
rendered.write_text(text, encoding='utf-8')
|
||||
pushed = push(rendered, '/etc/linux-manager/linux-manager.json', '0600')
|
||||
rendered.unlink()
|
||||
if not pushed:
|
||||
sys.exit(f'could not copy {args.config} into {args.vm}')
|
||||
text = config.read_text(encoding='utf-8').replace('@ETH0@', nic).replace('@CIDR0@', str(interface.network))
|
||||
problem = hand_over(args.remote, args.project, args.vm, vault, 'config', text.replace('@PSK@', psk),
|
||||
'/etc/linux-manager/linux-manager.json', '0600')
|
||||
if problem:
|
||||
sys.exit(f'could not give {args.vm} {args.config}: {problem}')
|
||||
for _ in range(2):
|
||||
if inside('systemctl', 'restart', 'linux-manager-setup').returncode != 0:
|
||||
if inside('linux-manager-config-edit', 'apply').returncode != 0:
|
||||
sys.exit(f'linux-manager-setup failed with {args.config}; see journalctl -u linux-manager-setup in the VM')
|
||||
|
||||
if not args.no_otp:
|
||||
problem = enrol_otp(args.remote, args.project, args.vm, 'admin')
|
||||
if problem:
|
||||
sys.exit(f'{args.vm} is up, but {problem}')
|
||||
|
||||
print(f'{args.vm} is up at {interface.ip} with {args.config}')
|
||||
print(f'administrator: admin password: {password} (also in {ROOT / ".env"})')
|
||||
print(f'administrator: admin, password in {ROOT / ".env"} (ADMIN_PASSWORD) and the Vault ({VAULT_MOUNT}/'
|
||||
f'{args.vm}/admin)')
|
||||
if not args.no_otp:
|
||||
print(f'administrator TOTP codes: bin/lm-dev-otp-code.py --project {args.project} --vm {args.vm}')
|
||||
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.vm} --project {args.project} -- su -l')
|
||||
print(f'remove: {sys.argv[0]} remove --remote {args.remote} --project {args.project} --vm {args.vm}')
|
||||
|
||||
Reference in new issue
Block a user