-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_remotely.sh
37 lines (30 loc) · 1.14 KB
/
run_remotely.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash
# Copy all scripts to the machine
# Run one of them with parameters
# Remove the scripts
# Args:
# $1 gcloud project
# $2 gcloud zone
# $3 gcloud machine
# $4 script name (from ./remote_scripts/) to execute
# $5+ arguments for the script
project=$1
zone=$2
machine=$3
script=$4
shift 4
args=$*
gcloud beta compute scp --scp-flag="-o ConnectTimeout=30" --scp-flag="-o BatchMode=yes" --scp-flag="-o StrictHostKeyChecking=no" --project=$project --zone=$zone --recurse './.remote_scripts' $machine:'~/'
if (($? > 0)); then
echo "Failed to connect to '$project/$zone/$machine' during phase 1. Nothing needs cleanup."
exit 1
fi
echo "Step 1 completed for '$project/$zone/$machine'"
sleep 2
gcloud beta compute ssh --ssh-flag="-o ConnectTimeout=30" --ssh-flag="-o BatchMode=yes" --ssh-flag="-o StrictHostKeyChecking=no" --project=$project --zone=$zone $machine --command="cd ~/.remote_scripts && sudo bash ./$script.sh $args"
if (($? > 0)); then
echo "Failed to connect to '$project/$zone/$machine' during phase 2. '~/.remote_scripts' was not deleted."
exit 2
fi
echo "Step 2 completed for '$project/$zone/$machine'"
exit 0