Skip to content

Instantly share code, notes, and snippets.

@xingxing
Last active May 20, 2021 04:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xingxing/46632290e062106028f8ed08e5de31ff to your computer and use it in GitHub Desktop.
Save xingxing/46632290e062106028f8ed08e5de31ff to your computer and use it in GitHub Desktop.
Wait for GCE instance is ready by try ssh in
function wait_vm_up {
local counter=0
local readonly project=${1:?"project required"}
local readonly instance=${2:?"instance required"}
local readonly zone=${3:?"zone required"}
local readonly user=${4:?"user required"}
local readonly maxRetry=${5:-100}
echo "Project: $project"
echo "Instance: $instance"
echo "MaxRetry: $maxRetry"
while true ; do
if (( $counter == $maxRetry )) ; then
echo "Reach the retry upper limit $counter"
exit 1
fi
gcloud compute ssh --quiet --zone "$zone" "$user@$instance" --tunnel-through-iap --project "$project" --command="true" 2> /dev/null
if (( $? == 0 )) ;then
echo "The machine is UP !!!"
exit 0
else
echo "Maybe later? $counter"
((counter++))
sleep 1
fi
done
}
wait_vm_up $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment