Skip to content

Commit

Permalink
Add connection check at the end
Browse files Browse the repository at this point in the history
On Windows platform, target host restart is required after an install
step. If the machine takes long to be available again, this can cause
problems further down the "pipeline". So before proceding further,
check the ssh connection is back up.
  • Loading branch information
jsliacan authored and adrianriobo committed Feb 22, 2024
1 parent 4a4851f commit 7723006
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ $(scp_to_cmd "${ASSETS_FOLDER}/*" "${TARGET_FOLDER}/")
# Exec command
$(ssh_cmd "$@")

# Check ssh connection to target host
CHECK_CONNECTION_ATTEMPTS=${CHECK_CONNECTION_ATTEMPTS:-30}
CHECK_CONNECTION_DELAY=${CHECK_CONNECTION_DELAY:-10}
check_connection ${CHECK_CONNECTION_ATTEMPTS} ${CHECK_CONNECTION_DELAY}
if [[ $? -gt 0 ]]
then
exit 1
fi

# Copy results
if [[ ! -z "${TARGET_RESULTS+x}" ]]; then
# If exec create some reuslts we define the env and they will be copied to OUTPUT_FOLDER
Expand Down
29 changes: 29 additions & 0 deletions lib/remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,38 @@ connect_options() {
options="$options -o UserKnownHostsFile=/dev/null"
options="$options -o ServerAliveInterval=30"
options="$options -o ServerAliveCountMax=1200"
options="$options -o BatchMode=yes"
options="$options -o ConnectTimeout=3"
echo $options
}

# If restart is involved, it can take a moment for the target host to become available again
# Check the connection to the host; "delay" in seconds, "repeats" in number of reps
# Run as: check_connection <repeats int> <delay int>
# e.g. check_connection 30 10
check_connection() {
repeats=$1
while [[ $repeats -gt 0 ]]
do
ssh_cmd "pwd"
if [[ $? -gt 0 ]]
then
echo "reps remaining: $repeats" >&2
((repeats--))
sleep $2
else
break
fi
done
# fail or pass the check
if [[ $repeats -gt 0 ]]
then
return 0
else
return 1
fi
}

# Define remote connection
uri () {
local remote="${TARGET_HOST_USERNAME}@${TARGET_HOST}"
Expand Down

0 comments on commit 7723006

Please sign in to comment.