[autotest] Script to clear out all data from an autotest instance.

This provides a script to reset an autotest instance, i.e. a shard.

This is especially useful in case the master fails and all shards
need to be reset.

BUG=None
TEST=Manually.

Change-Id: I9e0003b1169500b4e3fa6f4eb2269a7815e57375
Reviewed-on: https://chromium-review.googlesource.com/219819
Tested-by: Jakob Jülich <jakobjuelich@chromium.org>
Reviewed-by: Richard Barnette <jrbarnette@chromium.org>
Commit-Queue: Jakob Jülich <jakobjuelich@chromium.org>
diff --git a/site_utils/clear.sh b/site_utils/clear.sh
new file mode 100755
index 0000000..45fd1cc
--- /dev/null
+++ b/site_utils/clear.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+#
+# Copyright 2014 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.
+
+# The purpose of this script is to be able to reset an autotest instance.
+# This means cleaning up the database and all log and results files.
+# The main use case for this is if the master ever fails and all shards need to
+# be reset.
+
+declare -a SERVICES=("apache2" "scheduler" "host-scheduler" "shard-client"
+                     "gs_offloader" "gs_offloader_s")
+AUTOTEST_DIR=$(dirname $(dirname $0))
+
+function service_action {
+  local s
+  for s in "${SERVICES[@]}"; do
+    if [[ -e "/etc/init/$s.conf" || -e "/etc/init.d/$s" ]]; then
+      sudo service $s $1
+    fi
+  done
+}
+
+service_action stop
+
+${AUTOTEST_DIR}/frontend/manage.py dbshell <<END
+DROP DATABASE chromeos_autotest_db;
+CREATE DATABASE chromeos_autotest_db;
+END
+
+${AUTOTEST_DIR}/database/migrate.py sync -f
+${AUTOTEST_DIR}/frontend/manage.py syncdb --noinput
+${AUTOTEST_DIR}/frontend/manage.py syncdb --noinput
+
+sudo rm -rf ${AUTOTEST_DIR}/results/*
+sudo rm -rf ${AUTOTEST_DIR}/logs/*
+
+service_action start