Last active
May 20, 2021 04:42
-
-
Save xingxing/46632290e062106028f8ed08e5de31ff to your computer and use it in GitHub Desktop.
Wait for GCE instance is ready by try ssh in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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