|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script deletes & re-creates the etcd VMs, one-by-one waiting for each |
| 4 | +# updated node to boot and become healthy before moving on to the next. |
| 5 | +# |
| 6 | +# It's intended to be used to update the software running on the etcd nodes, |
| 7 | +# but can also be useful in situations where the etcd node(s) are unable to |
| 8 | +# boot/function properly (e.g. if their root disks fill up.) |
| 9 | +# |
| 10 | +# The persistent disks are left intact, and are attached to the updated |
| 11 | +# VM instances. |
| 12 | +# |
| 13 | +# The expected outcome from running this script is that the etcd cluster |
| 14 | +# updates to the newest project etcd image, and the data within etcd remains |
| 15 | +# intact. |
| 16 | + |
| 17 | +# TODO(alcutter): Factor out common code with update_mirror_vm_image.sh script |
| 18 | +DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) |
| 19 | +if [ "$1" == "" ]; then |
| 20 | + echo "Usage: $0 <config.sh file>" |
| 21 | + exit 1; |
| 22 | +fi |
| 23 | +source ${DIR}/config.sh $1 |
| 24 | +source ${DIR}/util.sh |
| 25 | + |
| 26 | +set -e |
| 27 | +GCLOUD="gcloud --project ${PROJECT}" |
| 28 | + |
| 29 | +Header "Recreating etcd instances..." |
| 30 | +for i in `seq 0 $((${ETCD_NUM_REPLICAS} - 1))`; do |
| 31 | + echo "Deleting instance ${ETCD_MACHINES[$i]}" |
| 32 | + set +e |
| 33 | + ${GCLOUD} compute instances delete -q ${ETCD_MACHINES[${i}]} \ |
| 34 | + --zone ${ETCD_ZONES[${i}]} \ |
| 35 | + --keep-disks data |
| 36 | + set -e |
| 37 | + |
| 38 | + MANIFEST=$(mktemp) |
| 39 | + sed --e "s^@@PROJECT@@^${PROJECT}^ |
| 40 | + s^@@DISCOVERY@@^${DISCOVERY}^ |
| 41 | + s^@@ETCD_NAME@@^${ETCD_MACHINES[$i]}^ |
| 42 | + s^@@CONTAINER_HOST@@^${ETCD_MACHINES[$i]}^" \ |
| 43 | + < ${DIR}/etcd_container.yaml > ${MANIFEST} |
| 44 | + |
| 45 | + echo "Recreating instance ${ETCD_MACHINES[$i]}" |
| 46 | + ${GCLOUD} compute instances create -q ${ETCD_MACHINES[${i}]} \ |
| 47 | + --zone ${ETCD_ZONES[${i}]} \ |
| 48 | + --machine-type ${ETCD_MACHINE_TYPE} \ |
| 49 | + --image container-vm \ |
| 50 | + --disk name=${ETCD_DISKS[${i}]},mode=rw,boot=no,auto-delete=no \ |
| 51 | + --tags etcd-node \ |
| 52 | + --scopes "monitoring,storage-ro,compute-ro,logging-write" \ |
| 53 | + --metadata-from-file startup-script=${DIR}/node_init.sh,google-container-manifest=${MANIFEST} |
| 54 | + |
| 55 | + set +e |
| 56 | + echo "Waiting for instance ${ETCD_MACHINES[${i}]}..." |
| 57 | + WaitForStatus instances ${ETCD_MACHINES[${i}]} ${ETCD_ZONES[${i}]} RUNNING |
| 58 | + echo "Waiting for etcd service on ${ETCD_MACHINES[${i}]}..." |
| 59 | + WaitHttpStatus ${ETCD_MACHINES[${i}]} ${ETCD_ZONES[${i}]} /v2/keys/root/cluster_config 200 4001 |
| 60 | + set -e |
| 61 | + rm "${MANIFEST}" |
| 62 | +done |
0 commit comments