Add script for creating new bot instances

This script can be used to create new builder bot instances on
Google Compute Engine.

Change-Id: I7e1ef1f55b4a728283dd83896902f3d5b4a6edc2
diff --git a/scripts/gce-create-bot.sh b/scripts/gce-create-bot.sh
new file mode 100755
index 0000000..85dce41
--- /dev/null
+++ b/scripts/gce-create-bot.sh
@@ -0,0 +1,58 @@
+#!/bin/bash
+
+# Copyright 2016 The Fuchsia Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This script is used to create new bot instances on Google Compute Engine.
+
+declare ZONE="${ZONE:-us-central1-c}"
+declare MACHINE_TYPE="${MACHINE_TYPE:-n1-standard-4}"
+declare DISK_SIZE="${DISK_SIZE:-50}"
+declare DISK_TYPE="${DISK_TYPE:-pd-standard}"
+
+set -eo pipefail; [[ "$TRACE" ]] && set -x
+
+usage() {
+  printf >&2 '%s: [-r release] [-m mirror] [-s] [-E] [-e] [-c] [-d] [-t timezone] [-p packages] [-b]\n' "$0" && exit 1
+}
+
+create() {
+  local zone="$1" machine_type="$2" disk_size="$3" disk_type="$4"
+  local botid="$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-z0-9' | fold -w 4 | head -n 1)"
+
+  gcloud compute instances create "fuchsia-bot-${botid}" \
+    --zone "${zone}" \
+    --machine-type "${machine_type}" \
+    --network "default" \
+    --metadata-from-file "startup-script=gce-startup-script.sh" \
+    --maintenance-policy "MIGRATE" \
+    --scopes default="https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring.write","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/userinfo.email" \
+    --tags "use-swarming-auth" \
+    --image "/ubuntu-os-cloud/ubuntu-1404-trusty-v20160809a" \
+    --boot-disk-size "${disk_size}" \
+    --boot-disk-type "${disk_type}" \
+    --boot-disk-device-name "fuchsia-bot-${botid}"
+}
+
+while getopts "hz:m:s:t:" opt; do
+  case $opt in
+    z) ZONE="$OPTARG";;
+    m) MACHINE_TYPE="$OPTARG";;
+    s) DISK_SIZE="$OPTARG";;
+    t) DISK_TYPE="$OPTARG";;
+    *) usage;;
+  esac
+done
+
+create "${ZONE}" "${MACHINE_TYPE}" "${DISK_SIZE}" "${DISK_TYPE}"
diff --git a/scripts/gce-startup-script.sh b/scripts/gce-startup-script.sh
new file mode 100644
index 0000000..f82586e
--- /dev/null
+++ b/scripts/gce-startup-script.sh
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+# [START startup]
+set -v
+
+# Install logging monitor
+# [START logging]
+curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash
+service google-fluentd restart &
+# [END logging]
+
+# Install dependencies from apt
+apt-get update
+apt-get install -yq git build-essential python
+
+# Create a swarming user
+useradd -m -d /home/swarming swarming
+grep -q -F 'swarming ALL=NOPASSWD: /sbin/shutdown -r now' /etc/sudoers || echo 'swarming ALL=NOPASSWD: /sbin/shutdown -r now' >>/etc/sudoers
+
+# Setup swarming service
+mkdir -p /b/swarm_slave
+python - <<END
+import requests
+r = requests.get('http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token', headers={'Metadata-Flavor': 'Google'})
+r = requests.get('https://chromium-swarm-dev.appspot.com/bot_code', headers={'Authorization': 'Bearer %(access_token)s' % r.json()}, stream=True)
+with open('/b/swarm_slave/swarming_bot.zip', 'wb') as fd:
+  for chunk in r.iter_content(chunk_size=4096):
+    fd.write(chunk)
+END
+chown -R swarming:swarming /b/swarm_slave
+
+cat >/etc/init/swarming.conf <<EOF
+description "Swarming bot"
+
+start on filesystem or runlevel [2345]
+stop on shutdown
+
+respawn
+respawn limit 0 10
+
+setuid swarming
+
+exec /usr/bin/env python /b/swarm_slave/swarming_bot.zip start_bot
+EOF
+
+service swarming restart &
+
+# [END startup]