Script to spin up a demo scope cluster.

This commit is contained in:
Tom Wilkie
2015-07-20 13:38:12 +00:00
parent 7b1b8731f1
commit 4305b917a7

149
experimental/demo/gce.sh Executable file
View File

@@ -0,0 +1,149 @@
#!/bin/bash
# This script has a bunch of GCE-related functions:
# ./gce.sh setup - starts two VMs on GCE and configures them to run our integration tests
# . ./gce.sh; ./run_all.sh - set a bunch of environment variables for the tests
# ./gce.sh destroy - tear down the VMs
# ./gce.sh make_template - make a fresh VM template; update TEMPLATE_NAME first!
set -ex
KEY_FILE=/tmp/gce_private_key.json
SSH_KEY_FILE=$HOME/.ssh/gce_ssh_key
PROJECT=positive-cocoa-90213
IMAGE=ubuntu-14-04
ZONE=us-central1-a
NUM_HOSTS=2
# Setup authentication
gcloud auth activate-service-account --key-file $KEY_FILE
gcloud config set project $PROJECT
function vm_names {
local names=
for i in $(seq 1 $NUM_HOSTS); do
names="twilkie-test-$i $names"
done
echo "$names"
}
# Delete all vms in this account
function destroy {
names="$(vm_names)"
for i in {0..10}; do
# gcloud instances delete can sometimes hang.
case $(set +e; timeout 60s /bin/bash -c "gcloud compute instances delete --zone $ZONE -q $names >/dev/null 2>&1 || true"; echo $?) in
0)
return 0
;;
124)
# 124 means it timed out
break
;;
*)
return 1
esac
done
}
function external_ip {
gcloud compute instances list $1 --format=yaml | grep "^ natIP\:" | cut -d: -f2 | tr -d ' '
}
function internal_ip {
gcloud compute instances list $1 --format=yaml | grep "^ networkIP\:" | cut -d: -f2 | tr -d ' '
}
function try_connect {
for i in {0..10}; do
ssh -t $1 true && return
sleep 2
done
}
function install_on {
name=$1
otherpeers=$2
ssh -t $name sudo bash -x -s <<EOF
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9;
echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list;
apt-get update -qq;
apt-get install -q -y --force-yes --no-install-recommends lxc-docker ethtool emacs23-nox git make binutils mercurial;
usermod -a -G docker vagrant;
echo 'DOCKER_OPTS="-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375"' >> /etc/default/docker;
service docker restart;
curl -L git.io/weave -o /usr/local/bin/weave;
chmod a+x /usr/local/bin/weave;
weave launch $otherpeers;
weave launch-dns;
weave launch-proxy;
wget -q https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz;
tar -C /usr/local -xzf go1.4.2.linux-amd64.tar.gz;
/usr/local/go/bin/go clean -i net
/usr/local/go/bin/go install -tags netgo std
EOF
ssh -t $name bash -x -s <<EOF
echo "export PATH=$PATH:/usr/local/go/bin:~/bin" >>~/.profile
echo "export GOPATH=$HOME" >>~/.profile
. ~/.profile
mkdir -p ~/src/github.com/weaveworks
cd ~/src/github.com/weaveworks
git clone http://github.com/weaveworks/scope.git
cd scope
make deps
make
./scope launch
EOF
}
# Create new set of VMS
function setup {
destroy
names="$(vm_names)"
gcloud compute instances create $names --image $IMAGE --zone $ZONE --machine-type n1-standard-4
gcloud compute config-ssh --ssh-key-file $SSH_KEY_FILE
for name in $names; do
hostname="$name.$ZONE.$PROJECT"
# Add the remote ip to the local /etc/hosts
sudo -- sh -c "echo \"$(external_ip $name) $hostname\" >>/etc/hosts"
try_connect $hostname
# Add the local ips to the remote /etc/hosts
otherpeers=""
for othername in $names; do
otherip="$(internal_ip $othername)"
otherpeers="$otherip $otherpeers"
entry="$otherip $othername.$ZONE.$PROJECT"
ssh -t "$hostname" "sudo -- sh -c \"echo \\\"$entry\\\" >>/etc/hosts\""
done
install_on $hostname "$otherpeers"
done
}
function hosts {
hosts=
args=
for name in $(vm_names); do
hostname="$name.$ZONE.$PROJECT"
hosts="$hostname $hosts"
args="--add-host=$hostname:$(internal_ip $name) $args"
done
export SSH=ssh
export HOSTS="$hosts"
export WEAVE_DOCKER_ARGS="$args"
}
case "$1" in
setup)
setup
;;
hosts)
hosts
;;
destroy)
destroy
;;
esac