File tree Expand file tree Collapse file tree 1 file changed +18
-10
lines changed
Expand file tree Collapse file tree 1 file changed +18
-10
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,8 @@ PORT="5432"
3939TARGET_PORT=" 5432"
4040TARGET_ACCOUNT=" "
4141DB_INSTANCE=" "
42+ RETRY_INTERVAL=5
43+ MAX_RETRIES=12
4244
4345display_usage () {
4446 printf " \nThis script creates a SSH tunnel between the local machine and a GCP database using a GCP instance deployed in the same VPC."
137139# Getting the first IP
138140ip=$( echo $ips | sed " s/\['\([^']*\)'.*/\1/" )
139141
140- # Wait 15s to allow the machine to be up before creating the tunnel
141- echo " Sleeping for 15s..."
142- sleep 15
143- echo " Compute engine should be ready to use now"
144-
145- # Creating SSH tunnel
146- ssh -o StrictHostKeyChecking=no -fN -L ${PORT} :${target_ip} :${TARGET_PORT} ${TARGET_ACCOUNT} @${ip}
142+ # Creating SSH tunnel with retry mechanism
143+ retry_count=0
144+ while [ $retry_count -lt $MAX_RETRIES ]; do
145+ ssh -o StrictHostKeyChecking=no -fN -L ${PORT} :${target_ip} :${TARGET_PORT} ${TARGET_ACCOUNT} @${ip}
146+ if [ $? -eq 0 ]; then
147+ echo " SSH tunnel created successfully"
148+ break
149+ else
150+ echo " Error creating SSH tunnel, retrying in ${RETRY_INTERVAL} s..."
151+ sleep ${RETRY_INTERVAL}
152+ retry_count=$(( retry_count + 1 ))
153+ fi
154+ done
147155
148- if [ $? -ne 0 ]; then
149- printf " \nError creating SSH tunnel\n"
156+ if [ $retry_count -eq $MAX_RETRIES ]; then
157+ printf " \nError creating SSH tunnel after ${MAX_RETRIES} attempts \n"
150158 exit 1
151- fi
159+ fi
You can’t perform that action at this time.
0 commit comments