-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstop-cluster
executable file
·31 lines (25 loc) · 1.13 KB
/
stop-cluster
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
#!/usr/bin/env bash
## Leave them empty, set them in file 'cluster.conf'
PROJECT=""
ZONE=""
NODES=""
SSH_PUB_KEY=""
source cluster.conf
## Two color codes.
SY=$'\x1b[33;1m'
NC=$'\x1b[0m'
## Poweroff the machines. We want them to have a clean shutdown if we are going to start them again. Also create a string with the node and route names to use later.
echo "${SY}Powering off VMs.${NC}"
for ((i=1; i<=$NODES; i++)); do
n="$(printf "%02d" $i)"
[[ $(gcloud compute instances list --project $PROJECT | grep node-$n | grep RUN) ]] && \
gcloud compute ssh root@node-$n --zone $ZONE --project $PROJECT --ssh-flag "-o StrictHostKeyChecking=no" --command 'poweroff'
done
## Wait for the instances to shutdown
while (( $(gcloud compute instances list --project $PROJECT | grep -c TERMINATED) < $NODES )); do echo "Waiting for instances to complete shutdown..."; sleep 15; done
## Delete the VMs but keep the boot disks.
echo "${SY}Deleting VMs while preserving their disks.${NC}"
for ((i=1; i<=$NODES; i++)); do
n="$(printf "%02d" $i)"
gcloud compute instances delete node-$n --zone $ZONE --keep-disks boot --quiet --project $PROJECT
done