diff options
author | Helmut Grohne <helmutg@debian.org> | 2023-01-07 20:08:52 +0000 |
---|---|---|
committer | Helmut Grohne <helmutg@debian.org> | 2023-01-07 20:08:52 +0000 |
commit | de79a0307f460cb572a259a3ea09c5c2bf920ee9 (patch) | |
tree | 7b925d26e04fb8bb46bc1e46bdbb64ccbde8ca7a | |
parent | ba0206c069b00fed5911da62f56c9860bd1f8350 (diff) | |
parent | 472bc82b69e2785f0fb715efbb796bdd9f5507c0 (diff) | |
download | debvm-de79a0307f460cb572a259a3ea09c5c2bf920ee9.tar.gz |
Merge branch 'waitssh' into 'main'
add tool debvm-waitssh
See merge request helmutg/debvm!16
-rw-r--r-- | .gitlab-ci.yml | 4 | ||||
-rwxr-xr-x | debvm-waitssh | 144 | ||||
-rwxr-xr-x | tests/create-and-run.sh | 2 | ||||
-rwxr-xr-x | tests/dist-upgrades.sh | 4 | ||||
-rw-r--r-- | tests/test_common.sh | 11 |
5 files changed, 149 insertions, 16 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d2a1bbf..456e8d9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -27,7 +27,7 @@ release_test: script: - apt-get update - apt-get dist-upgrade --yes - - apt-get --no-install-recommends --yes install e2fsprogs genext2fs mmdebstrap openssh-client sleepenh qemu-kvm + - apt-get --no-install-recommends --yes install e2fsprogs genext2fs mmdebstrap openssh-client qemu-kvm - PATH=.:$PATH ./tests/create-and-run.sh $(dpkg --print-architecture) "$RELEASE" arch_test: @@ -45,5 +45,5 @@ arch_test: - test -e /proc/sys/fs/binfmt_misc/status || mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc - apt-get update - apt-get dist-upgrade --yes - - apt-get --no-install-recommends --yes install e2fsprogs genext2fs mmdebstrap openssh-client sleepenh qemu-system binfmt-support arch-test qemu-user-static + - apt-get --no-install-recommends --yes install e2fsprogs genext2fs mmdebstrap openssh-client qemu-system binfmt-support arch-test qemu-user-static - PATH=.:$PATH ./tests/create-and-run.sh "$ARCHITECTURE" sid diff --git a/debvm-waitssh b/debvm-waitssh new file mode 100755 index 0000000..4d69657 --- /dev/null +++ b/debvm-waitssh @@ -0,0 +1,144 @@ +#!/bin/sh +# Copyright 2023 Helmut Grohne <helmut@subdivi.de> +# SPDX-License-Identifier: MIT + +: <<'POD2MAN' +=head1 NAME + +debvm-waitssh - Wait for a ssh server to be reachable + +=head1 SYNOPSIS + +B<debvm-waitssh> [B<-t> I<timeout>] [I<hostname>:]I<port> + +=head1 DESCRIPTION + +B<debvm-waitssh> can be used to wait for a virtual machine with exposed ssh port to be reachable on that port. +If no hostname is given, B<127.0.0.1> is assumed. + +=head1 OPTIONS + +=over 8 + +=item B<-t> I<timeout>, B<--timeout>=I<timeout> + +Set the maximum duration for waiting in seconds. +Defaults to one minute. + +=back + +=head1 EXIT VALUES + +=over 8 + +=item B<0> + +The server is reachable. + +=item B<1> + +A timeout was reached before the server answered. + +=item B<2> + +Usage error. + +=back + +=head1 SEE ALSO + + debvm-run(1) + +=cut +POD2MAN + +set -u + +TOTALTIMEOUT=60 +SCANTIMEOUT=10 +SCANDELAY=1 + +nth_arg() { + shift "$1" + printf "%s" "$1" +} + +die() { + echo "$*" >&2 + exit 2 +} +usage() { + die "usage: $0 [-t <timeout>] [<host>:]<port>" +} +usage_error() { + echo "error: $*" >&2 + usage +} + +opt_timeout() { + TOTALTIMEOUT=$1 +} + +while getopts :t:-: OPTCHAR; do + case "$OPTCHAR" in + t) opt_timeout "$OPTARG" ;; + -) + case "$OPTARG" in + help) + usage + ;; + timeout) + test "$OPTIND" -gt "$#" && usage_error "missing argument for --$OPTARG" + "opt_$OPTARG" "$(nth_arg "$OPTIND" "$@")" + OPTIND=$((OPTIND+1)) + ;; + timeout=) + "opt_${OPTARG%%=*}" "${OPTARG#*=}" + ;; + *) + usage_error "unrecognized option --$OPTARG" + ;; + esac + ;; + :) + usage_error "missing argument for -$OPTARG" + ;; + '?') + usage_error "unrecognized option -$OPTARG" + ;; + *) + die "internal error while parsing command options, please report a bug" + ;; + esac +done +shift "$((OPTIND - 1))" + +test "$#" = 1 || usage + +case "$1" in + "") + usage + ;; + *:*) + HOST=${1%:*} + PORT=${1##*:} + ;; + *) + HOST=127.0.0.1 + PORT=$1 + ;; +esac + +now=$(date +%s) +deadline=$((now + TOTALTIMEOUT)) +while test "$now" -lt "$deadline"; do + start=$now + ssh-keyscan -t rsa -T "$SCANTIMEOUT" -p "$PORT" "$HOST" >/dev/null 2>&1 && exit 0 + now=$(date +%s) + if test "$((now - start))" -lt "$SCANTIMEOUT"; then + sleep "$SCANDELAY" + now=$(date +%s) + fi +done +exit 1 + diff --git a/tests/create-and-run.sh b/tests/create-and-run.sh index 540e6c4..f978052 100755 --- a/tests/create-and-run.sh +++ b/tests/create-and-run.sh @@ -26,6 +26,6 @@ SSH_PORT=2222 timeout 240s debvm-run -s "$SSH_PORT" -i "$IMAGE" & set -- localhost test "$RELEASE" = jessie && set -- -o PubkeyAcceptedKeyTypes=+ssh-rsa "$@" -wait_ssh "$@" +debvm-waitssh -t 150 "$SSH_PORT" run_ssh "$@" poweroff wait diff --git a/tests/dist-upgrades.sh b/tests/dist-upgrades.sh index 85c9f4a..177712b 100755 --- a/tests/dist-upgrades.sh +++ b/tests/dist-upgrades.sh @@ -44,12 +44,12 @@ for RELEASE in stretch buster bullseye bookworm sid; do timeout 15m debvm-run -s "$SSH_PORT" & set -- localhost test "$RELEASE" = stretch && set -- -o PubkeyAcceptedKeyTypes=+ssh-rsa "$@" - wait_ssh "$@" + debvm-waitssh -t 150 "$SSH_PORT" run_ssh "$@" "upgrade $RELEASE" wait done timeout 5m debvm-run -s "$SSH_PORT" & -wait_ssh localhost +debvm-waitssh -t 150 "$SSH_PORT" run_ssh localhost poweroff wait diff --git a/tests/test_common.sh b/tests/test_common.sh index f42a26a..cba9693 100644 --- a/tests/test_common.sh +++ b/tests/test_common.sh @@ -5,14 +5,3 @@ run_ssh() { test -n "${SSH_PORT:-}" && set -- -p "$SSH_PORT" "$@" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l root "$@" } - -wait_ssh() { - wait_ssh_timeout=5 - wait_ssh_ts=$(sleepenh 0 || [ $? -eq 1 ]) - for _ in $(seq 30); do - run_ssh -o ConnectTimeout="$wait_ssh_timeout" "$@" echo success && return 0 - wait_ssh_ts=$(sleepenh "$wait_ssh_ts" "$wait_ssh_timeout" || [ $? -eq 1 ]) - done - echo "timeout reached" >&2 - return 1 -} |