blob: 02291d682da1590cb453267a084d4bc9e7f0817c [file] [log] [blame]
Adam Sadovsky62e37f32014-10-23 18:14:20 -07001#!/bin/bash
2
Adam Sadovsky78f22452014-10-23 18:50:04 -07003# Script to rebuild and deploy compilerd and the Docker image (builder) to the
4# playground backends.
Adam Sadovsky62e37f32014-10-23 18:14:20 -07005#
6# Usage:
Adam Sadovsky78f22452014-10-23 18:50:04 -07007# gcutil ssh --project google.com:veyron playground-master
8# sudo su - veyron
Ivan Pilatddb61912014-12-29 21:16:23 -08009# v23 update
Ivan Pilat153037f2015-02-02 20:13:50 -080010# bash $VANADIUM_ROOT/release/projects/playground/go/src/playground/compilerd/update.sh
Adam Sadovsky62e37f32014-10-23 18:14:20 -070011
12set -e
Adam Sadovsky78f22452014-10-23 18:50:04 -070013set -u
14
15readonly DATE=$(date +"%Y%m%d-%H%M%S")
Adam Sadovsky62e37f32014-10-23 18:14:20 -070016readonly DISK="pg-data-${DATE}"
17
18function unmount() {
19 sudo umount /mnt
Adam Sadovsky99e1d4f2014-10-28 19:20:49 -070020 gcloud compute --project "google.com:veyron" instances detach-disk --disk=${DISK} $(hostname) --zone us-central1-a
Adam Sadovsky62e37f32014-10-23 18:14:20 -070021}
22
Adam Sadovskyd270f6c2014-10-31 11:39:03 -070023trap cleanup INT TERM EXIT
24
Adam Sadovsky62e37f32014-10-23 18:14:20 -070025function cleanup() {
Adam Sadovskyd270f6c2014-10-31 11:39:03 -070026 # Unset the trap so that it doesn't run again on exit.
27 trap - INT TERM EXIT
Adam Sadovsky62e37f32014-10-23 18:14:20 -070028 if [[ -e /mnt/compilerd ]]; then
Adam Sadovsky78f22452014-10-23 18:50:04 -070029 # The disk is still mounted on the master, which means it's not yet mounted
30 # on any backends. It's safe to unmount and delete it.
Adam Sadovsky62e37f32014-10-23 18:14:20 -070031 unmount
Adam Sadovsky99e1d4f2014-10-28 19:20:49 -070032 gcloud compute --project "google.com:veyron" disks delete ${DISK} --zone "us-central1-a"
Adam Sadovsky62e37f32014-10-23 18:14:20 -070033 fi
Adam Sadovsky99e1d4f2014-10-28 19:20:49 -070034 sudo docker rm ${DISK} &> /dev/null || true
Adam Sadovsky62e37f32014-10-23 18:14:20 -070035}
Adam Sadovsky62e37f32014-10-23 18:14:20 -070036
37function main() {
Ivan Pilatddb61912014-12-29 21:16:23 -080038 if [[ ! -e ~/.gitcookies ]]; then
39 echo "Unable to access git, missing ~/.gitcookies"
Adam Sadovsky62e37f32014-10-23 18:14:20 -070040 exit 1
41 fi
Ivan Pilataee5e9f2014-12-15 21:46:05 -080042 if [[ ! -e ~/.hgrc ]]; then
43 echo "Unable to access mercurial, missing ~/.hgrc"
44 exit 1
45 fi
Adam Sadovsky62e37f32014-10-23 18:14:20 -070046
Adam Sadovskyd270f6c2014-10-31 11:39:03 -070047 local ROLLING="1"
48 if [[ $# -gt 0 && ("$1" == "--no-rolling") ]]; then
49 local ROLLING="0"
50 fi
51
Adam Sadovsky99e1d4f2014-10-28 19:20:49 -070052 gcloud compute --project "google.com:veyron" disks create ${DISK} --size "200" --zone "us-central1-a" --source-snapshot "pg-data-20140702" --type "pd-standard"
53 gcloud compute --project "google.com:veyron" instances attach-disk --disk=${DISK} $(hostname) --zone us-central1-a
Adam Sadovsky62e37f32014-10-23 18:14:20 -070054 sudo mount /dev/sdb1 /mnt
55
56 # Build the docker image.
Ivan Pilat153037f2015-02-02 20:13:50 -080057 cd ${VANADIUM_ROOT}/release/projects/playground/go/src/playground
Ivan Pilat8ecbed32015-01-13 21:30:05 -080058 cp ~/.gitcookies ./builder/gitcookies
59 cp ~/.hgrc ./builder/hgrc
Adam Sadovsky62e37f32014-10-23 18:14:20 -070060 sudo docker build --no-cache -t playground .
Ivan Pilat8ecbed32015-01-13 21:30:05 -080061 rm -f ./builder/gitcookies
62 rm -f ./builder/hgrc
Adam Sadovsky62e37f32014-10-23 18:14:20 -070063
64 # Export the docker image to disk.
65 sudo docker save -o /mnt/playground.tar.gz playground
66
Adam Sadovskyd270f6c2014-10-31 11:39:03 -070067 # TODO(sadovsky): Before deploying the new playground image, we should run it
68 # with real input and make sure it works (produces the expected output).
Adam Sadovsky78f22452014-10-23 18:50:04 -070069
Adam Sadovsky62e37f32014-10-23 18:14:20 -070070 # Copy the compilerd binary from the docker image to the disk.
Adam Sadovsky78f22452014-10-23 18:50:04 -070071 # NOTE(sadovsky): The purpose of the following line is to create a container
72 # out of the docker image, so that we can copy out the compilerd binary.
73 # Annoyingly, the only way to create the container is to run the image.
Ivan Pilatddb61912014-12-29 21:16:23 -080074 # TODO(sadovsky): Why don't we just build compilerd using "v23 go install"?
Adam Sadovsky78f22452014-10-23 18:50:04 -070075 sudo docker run --name=${DISK} playground &> /dev/null || true
Ivan Pilat153037f2015-02-02 20:13:50 -080076 sudo docker cp ${DISK}:/usr/local/vanadium/release/projects/playground/go/bin/compilerd /tmp
Adam Sadovsky62e37f32014-10-23 18:14:20 -070077 sudo mv /tmp/compilerd /mnt/compilerd
78 sudo docker rm ${DISK}
79
80 # Detach the disk so the backends can mount it.
81 unmount
82
83 # Update the template to use the new disk.
Ivan Pilat8ecbed32015-01-13 21:30:05 -080084 cd compilerd
Adam Sadovsky62e37f32014-10-23 18:14:20 -070085 sed -i -e s/pg-data-20140820/${DISK}/ pool_template.json
86 gcloud preview replica-pools --zone=us-central1-a update-template --template=pool_template.json playground-pool
87 git checkout -- pool_template.json
88
Adam Sadovsky78f22452014-10-23 18:50:04 -070089 # Perform a rolling restart of all the replicas.
Adam Sadovsky62e37f32014-10-23 18:14:20 -070090 INSTANCES=$(gcloud preview replica-pools --zone=us-central1-a replicas --pool=playground-pool list|grep name:|cut -d: -f2)
91 for i in ${INSTANCES}; do
92 gcloud preview replica-pools --zone=us-central1-a replicas --pool=playground-pool restart ${i}
Adam Sadovskyd270f6c2014-10-31 11:39:03 -070093 if [[ "$ROLLING" == "1" ]]; then
94 sleep 5m
95 fi
Adam Sadovsky62e37f32014-10-23 18:14:20 -070096 done
97}
98
99main "$@"