Add a script to kick off worker from a GCE instance.

This is modelled closely on the purge_gs.sh script from chromite and
will be used similarly from a GCE instance.

The script launches the worker once, using the password provided via GCE
instance metadata attributes. It then sleeps for 2-4 hours and finally
exits. Upon exit, the instance is expected to reboot, restarting the
whole process.

BUG=chromium:724619
TEST=None (yet).

Change-Id: Iebe430256444d9c419a2851f379365a714ec26c2
Reviewed-on: https://chromium-review.googlesource.com/513483
Reviewed-by: Prathmesh Prabhu <pprabhu@chromium.org>
Tested-by: Prathmesh Prabhu <pprabhu@chromium.org>
diff --git a/.gitignore b/.gitignore
index 0d20b64..6567cb3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
+logs/
 *.pyc
diff --git a/bin/dup_it_worker.sh b/bin/dup_it_worker.sh
new file mode 100755
index 0000000..03104b8
--- /dev/null
+++ b/bin/dup_it_worker.sh
@@ -0,0 +1,47 @@
+#!/bin/bash
+
+# Copyright 2017 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# This script is run on a GCE instance to do the heavy-lifting for the distfile
+# mirroring service. The GCE instance is expected to
+# - set custom metadata (via the GCE web UI) with the key
+#   "dup_it_passwd" with value set to the passoword obtained from
+#   https://dupit-hr.appspot.com
+# - kick this script on startup and reboot on exit from this script (to start
+#   all over again)
+
+set -eu
+
+DUP_IT_URL="http://metadata.google.internal/computeMetadata/v1/instance/attributes/dup_it_url"
+ATTR_URL="http://metadata.google.internal/computeMetadata/v1/instance/attributes/dup_it_passwd"
+TOP_DIR=$(dirname "$(dirname "$(readlink -f "$0")")")
+DUP_IT_SCRIPT=${TOP_DIR}/bin/dup_it_worker.py
+LOG_DIR=${TOP_DIR}/logs
+LOG_FILE=${LOG_DIR}/dup_it_worker.log
+
+mkdir -p "${LOG_DIR}"
+
+# Save the last 5 copies of ${OUTPUT}, numbered in order
+# from most to least recent.
+prev=.5
+for suffix in .4 .3 .2 .1 ''; do
+  mv -f ${LOG_FILE}${suffix} ${LOG_FILE}${prev} || :
+  prev=${suffix}
+done
+
+dup_it_url=$(curl -H "Metadata-Flavor: Google" ${DUP_IT_URL})
+dup_it_passwd=$(curl -H "Metadata-Flavor: Google" ${ATTR_URL})
+
+(date && \
+ git fetch -q origin && \
+ git checkout -qf --detach origin/master && \
+ git clean -qfxd && \
+ ${DUP_IT_SCRIPT} -s "${dup_it_url}" -p "${dup_it_passwd}" && \
+ date) >> "${LOG_FILE}" 2>&1
+
+# Sleep [2, 4) hours.
+sleep_time=$(( ( RANDOM % 2 ) + 2 )).$(( ( RANDOM % 60 ) ))
+echo "Sleeping for ${sleep_time} hours" >> ${LOG_FILE}
+sleep ${sleep_time}h