diff options
author | Helmut Grohne <helmut@subdivi.de> | 2023-01-03 13:08:57 +0100 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2023-01-03 13:08:57 +0100 |
commit | 4e959cc83446ab426649425add520d3da58bfdd3 (patch) | |
tree | 94c70dea3ca69196906eb9d840c3ddd9b05e3f17 | |
parent | 0234911df5000ef720829c1002687e6e90badaf3 (diff) | |
download | debvm-4e959cc83446ab426649425add520d3da58bfdd3.tar.gz |
debvm-waitssh: make timeout configurable and restore previous value for tests
-rwxr-xr-x | debvm-waitssh | 84 | ||||
-rwxr-xr-x | tests/create-and-run.sh | 2 | ||||
-rwxr-xr-x | tests/dist-upgrades.sh | 4 |
3 files changed, 79 insertions, 11 deletions
diff --git a/debvm-waitssh b/debvm-waitssh index 092b46f..843963d 100755 --- a/debvm-waitssh +++ b/debvm-waitssh @@ -9,13 +9,24 @@ debvm-waitssh - Wait for a ssh server to be reachable =head1 SYNOPSIS -B<debvm-waitssh> [I<hostname>:]I<port> +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<localhost> 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 @@ -43,10 +54,70 @@ POD2MAN set -u -case "${1:-}" in +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 "") - echo "usage: $0 <port>" >&2 - exit 2 + usage ;; *:*) HOST=${1%:*} @@ -58,9 +129,6 @@ case "${1:-}" in ;; esac -TOTALTIMEOUT=60 -SCANTIMEOUT=10 -SCANDELAY=1 now=$(date +%s) deadline=$((now + TOTALTIMEOUT)) while test "$now" -lt "$deadline"; do @@ -68,7 +136,7 @@ while test "$now" -lt "$deadline"; do ssh-keyscan -T "$SCANTIMEOUT" -p "$PORT" "$HOST" >/dev/null 2>&1 && exit 0 now=$(date +%s) if test "$((now - start))" -lt "$SCANTIMEOUT"; then - sleep "$SCANDELAY" + sleep "$SCANDELAY" now=$(date +%s) fi done diff --git a/tests/create-and-run.sh b/tests/create-and-run.sh index 4ec07eb..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 "$@" -debvm-waitssh "$SSH_PORT" +debvm-waitssh -t 150 "$SSH_PORT" run_ssh "$@" poweroff wait diff --git a/tests/dist-upgrades.sh b/tests/dist-upgrades.sh index 3bab4ab..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 "$@" - debvm-waitssh "$SSH_PORT" + debvm-waitssh -t 150 "$SSH_PORT" run_ssh "$@" "upgrade $RELEASE" wait done timeout 5m debvm-run -s "$SSH_PORT" & -debvm-waitssh "$SSH_PORT" +debvm-waitssh -t 150 "$SSH_PORT" run_ssh localhost poweroff wait |