diff --git a/Dockerfile b/Dockerfile index ca0b8d4e..70f3d098 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,9 @@ RUN rm -f /etc/machine-id /var/lib/dbus/machine-id && \ ln -sf /etc/machine-id /var/lib/dbus/machine-id RUN ssh-keygen -A +RUN sed -i -e 's/^#\?UseDNS .*/UseDNS no/' \ + -e 's/^#\?GSSAPIAuthentication .*/GSSAPIAuthentication no/' \ + /etc/ssh/sshd_config RUN echo 'root:rootpwd' | chpasswd COPY third-party/docker-systemctl-replacement/systemctl3.py /usr/local/bin/systemctl diff --git a/ansible/roles/dependencies/tasks/retrieve.yml b/ansible/roles/dependencies/tasks/retrieve.yml index b5d30298..e4e64262 100644 --- a/ansible/roles/dependencies/tasks/retrieve.yml +++ b/ansible/roles/dependencies/tasks/retrieve.yml @@ -13,6 +13,7 @@ force: true depth: 1 version: token + accept_newhostkey: true - name: Fetch the repository token value diff --git a/ansible/roles/key_exchange/tasks/main.yml b/ansible/roles/key_exchange/tasks/main.yml index 7523f4fb..d26381a7 100644 --- a/ansible/roles/key_exchange/tasks/main.yml +++ b/ansible/roles/key_exchange/tasks/main.yml @@ -30,7 +30,7 @@ hostvars[item.0][item.1] ~ ' ' ~ ( - lookup('pipe', 'ssh-keyscan -T 5 -t ecdsa ' ~ hostvars[item.0].ansible_host).split()[1:] | join(' ') + lookup('pipe', 'ssh-keyscan -q -T 5 -t ecdsa ' ~ hostvars[item.0].ansible_host).split()[1:3] | join(' ') ) }} with_nested: diff --git a/deployment/__init__.py b/deployment/__init__.py index 52ca5a92..ab3ea92e 100644 --- a/deployment/__init__.py +++ b/deployment/__init__.py @@ -122,6 +122,10 @@ def sshLogin(ip, user='root'): f'{user}@{ip}', '-o', 'PasswordAuthentication=false', + '-o', + 'BatchMode=yes', + '-o', + 'ConnectTimeout=5', '"exit"' ], stdout=STDOUT, @@ -386,6 +390,10 @@ def _vagrantList(): return [m.decode().strip().replace('discos_', '') for m in machines] def _vboxList(): + import shutil + if not shutil.which('vboxmanage'): + # VirtualBox not installed, e.g. Docker-only hosts + return [] cmd = subprocess.Popen( "vboxmanage list vms | awk '{print $1}' | sed 's/\"//g'", shell=True, @@ -409,7 +417,7 @@ def machineList(inventory='development'): return machines def isRunning(name, inventory='development'): - if name not in machineList(): + if name not in machineList() and name not in dockerMachineList(inventory): error(f'Machine {name} unknown!') return ping(getIp(name, inventory)) @@ -545,6 +553,15 @@ def dockerContainerRunning(name): sp = _docker_output(['inspect', '-f', '{{.State.Running}}', name]) return sp.returncode == 0 and sp.stdout.strip() == 'true' +def dockerMachineList(inventory='development'): + """Return the inventory machines that exist as Docker containers.""" + try: + hosts, _, _ = parseInventory(inventory) + return [m for m in hosts if dockerContainerExists(m)] + except OSError: + # Docker not installed + return [] + def dockerEnsureContainer( name, ip, @@ -553,9 +570,11 @@ def dockerEnsureContainer( ): """Create container if missing, otherwise start it.""" if dockerContainerExists(name): - if dockerContainerRunning(name): - return 0 - return dockerStartContainer(name, ip=ip) + if not dockerContainerRunning(name): + rc = dockerStartContainer(name, ip=ip) + if rc != 0: + return rc + return dockerInjectRSAKey(name) rc = _docker([ 'run', '-d', diff --git a/scripts/discos-login b/scripts/discos-login index 359537fb..3286888c 100644 --- a/scripts/discos-login +++ b/scripts/discos-login @@ -15,7 +15,9 @@ parser.add_argument( required=False, default='discos' ) -parser.add_argument('machine', choices=deployment.machineList()) +vms = list(deployment.machineList()) +containers = [m for m in deployment.dockerMachineList() if m not in vms] +parser.add_argument('machine', choices=vms + containers) args = parser.parse_args() if deployment.isRunning(args.machine): diff --git a/scripts/discos-vnc b/scripts/discos-vnc index 3132e537..ea858ec9 100644 --- a/scripts/discos-vnc +++ b/scripts/discos-vnc @@ -6,6 +6,7 @@ $ discos-vnc -u observer console """ import os +import shutil import subprocess from multiprocessing import Process from argparse import ArgumentParser @@ -17,7 +18,8 @@ users = { 'discos': 1, 'observer': 2 } -machines = deployment.machineList() +machines = list(deployment.machineList()) +machines += [m for m in deployment.dockerMachineList() if m not in machines] try: machines.remove('storage') except: @@ -61,6 +63,12 @@ def detach(vnc_command, ssh_command): shell=True ) +if not shutil.which('vncviewer'): + deployment.error( + 'vncviewer not found, install a TigerVNC viewer ' + '(e.g. "sudo apt install tigervnc-viewer").' + ) + if deployment.isRunning(args.machine): remote_port = remote_starting_port + users[args.user] local_port = local_starting_port diff --git a/scripts/discos.ps1 b/scripts/discos.ps1 new file mode 100644 index 00000000..acdbfa7f --- /dev/null +++ b/scripts/discos.ps1 @@ -0,0 +1,43 @@ +#Requires -Version 7.3 +<# +.SYNOPSIS + PowerShell wrappers for the DISCOS deployment tools installed in WSL2. + +.DESCRIPTION + Ansible cannot run on Windows, so discos-deploy and the other tools run + inside a WSL2 distribution. These functions forward each call to that + distribution, passing the arguments through unchanged. + + Requirements inside the distribution: + - the deployment package installed, so the tools are on the login + PATH (~/.local/bin); + - the Docker engine installed in the distribution itself, not Docker + Desktop: with Docker Desktop the container IPs (192.168.56.0/24) + are not reachable from WSL; + - REPOSITORY_TOKEN exported in ~/.profile, since a non-interactive + login shell does not read ~/.bashrc. + + File arguments must be WSL paths (/mnt/d/... or ~/...), not Windows ones. + +.EXAMPLE + . D:\Work\Develop\deployment\scripts\discos.ps1 + discos-deploy manager:development --docker + discos-container status + discos-login manager + + Add the first line to $PROFILE to load the functions in every session. + Set $env:DISCOS_WSL_DISTRO to use a distribution other than 'Ubuntu-26.04'. +#> + +$DiscosWslDistro = if ($env:DISCOS_WSL_DISTRO) { $env:DISCOS_WSL_DISTRO } else { 'Ubuntu-26.04' } + +# A login shell loads PATH and REPOSITORY_TOKEN from ~/.profile; +# "$0" "$@" hands the arguments over without re-parsing them. +$DiscosWslPrefix = @( + '-d', $DiscosWslDistro, '--cd', '~', '--exec', + 'bash', '-lc', 'exec "$0" "$@"' +) + +function discos-deploy { wsl.exe @DiscosWslPrefix discos-deploy @args } +function discos-container { wsl.exe @DiscosWslPrefix discos-container @args } +function discos-login { wsl.exe @DiscosWslPrefix discos-login @args }