blob: 8058a99cd5862287c517a0beddb7c72a3e12a5c6 [file] [log] [blame]
Chris Masone2aa55332011-12-30 14:41:07 -08001#!/bin/bash
2#
3# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
Simran Basi79b1a7b2013-02-13 11:16:00 -08006set -e
Chris Masone2aa55332011-12-30 14:41:07 -08007
8USAGE="Usage: setup_dev_autotest.sh [-p <password>] [-a </path/to/autotest>]"
9HELP="${USAGE}\n\n\
10Install and configure software needed to run autotest locally.\n\
11If you're just working on tests, you do not need to run this.\n\n\
12Options:\n\
13 -p Desired Autotest DB password\n\
14 -a Absolute path to autotest source tree.\n"
15
16AUTOTEST_DIR=
17PASSWD=
18while getopts ":p:a:h" opt; do
19 case $opt in
20 a)
21 AUTOTEST_DIR=$OPTARG
22 ;;
23 p)
24 PASSWD=$OPTARG
25 ;;
26 h)
27 echo -e "${HELP}" >&2
28 exit 0
29 ;;
30 \?)
31 echo "Invalid option: -$OPTARG" >&2
32 echo "${USAGE}" >&2
33 exit 1
34 ;;
35 :)
36 echo "Option -$OPTARG requires an argument." >&2
37 echo "${USAGE}" >&2
38 exit 1
39 ;;
40 esac
41done
42
Richard Barnette23ef4422012-11-02 13:46:20 -070043if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080044 read -s -p "Autotest DB password: " PASSWD
45 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070046 if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080047 echo "Empty passwords not allowed." >&2
48 exit 1
49 fi
50 read -s -p "Re-enter password: " PASSWD2
51 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070052 if [ "${PASSWD}" != "${PASSWD2}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080053 echo "Passwords don't match." >&2
54 exit 1
55 fi
56fi
57
58if [ -z "${AUTOTEST_DIR}" ]; then
59 CANDIDATE=$(dirname "$(readlink -f "$0")" | egrep -o '(/[^/]+)*/files')
60 read -p "Enter autotest dir [${CANDIDATE}]: " AUTOTEST_DIR
61 if [ -z "${AUTOTEST_DIR}" ]; then
62 AUTOTEST_DIR="${CANDIDATE}"
63 fi
64fi
65
Aviv Keshet2c603d82013-05-24 10:31:59 -070066
67# Sanity check AUTOTEST_DIR. If it's null, or doesn't exist on the filesystem
68# then die.
69if [ -z "${AUTOTEST_DIR}" ]; then
70 echo "No AUTOTEST_DIR. Aborting script."
71 exit 1
72fi
73
74if [ ! -d "${AUTOTEST_DIR}" ]; then
75 echo "Directory " ${AUTOTEST_DIR} " does not exist. Aborting script."
76 exit 1
77fi
78
79
Chris Masone2aa55332011-12-30 14:41:07 -080080SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini"
81echo "Autotest supports local overrides of global configuration through a "
82echo "'shadow' configuration file. Setting one up for you now."
Chris Sosa18c70b32013-02-15 14:12:43 -080083CLOBBER=0
Chris Masone2aa55332011-12-30 14:41:07 -080084if [ -f ${SHADOW_CONFIG_PATH} ]; then
85 clobber=
86 while read -n 1 -p "Clobber existing shadow config? [Y/n]: " clobber; do
87 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070088 if [[ -z "${clobber}" || $(echo ${clobber} | egrep -qi 'y|n') -eq 0 ]]; then
Chris Masone2aa55332011-12-30 14:41:07 -080089 break
90 fi
91 echo "Please enter y or n."
92 done
Richard Barnette23ef4422012-11-02 13:46:20 -070093 if [[ "${clobber}" = 'n' || "${clobber}" = 'N' ]]; then
Chris Sosa18c70b32013-02-15 14:12:43 -080094 CLOBBER=1
Chris Masone2aa55332011-12-30 14:41:07 -080095 echo "Refusing to clobber existing shadow_config.ini."
Chris Sosa18c70b32013-02-15 14:12:43 -080096 else
97 echo "Clobbering existing shadow_config.ini."
Chris Masone2aa55332011-12-30 14:41:07 -080098 fi
Chris Masone2aa55332011-12-30 14:41:07 -080099fi
100
Aviv Keshet2c603d82013-05-24 10:31:59 -0700101CROS_CHECKOUT=$(readlink -f ${AUTOTEST_DIR}/../../../..)
102
Chris Sosa18c70b32013-02-15 14:12:43 -0800103# Create clean shadow config if we're replacing it/creating a new one.
104if [ $CLOBBER -eq 0 ]; then
105 cat > "${SHADOW_CONFIG_PATH}" <<EOF
Chris Masone2aa55332011-12-30 14:41:07 -0800106[AUTOTEST_WEB]
107host: localhost
108password: ${PASSWD}
Chris Masonee61dd812012-01-10 18:04:21 -0800109readonly_host: localhost
110readonly_user: chromeosqa-admin
111readonly_password: ${PASSWD}
Chris Masone2aa55332011-12-30 14:41:07 -0800112
113[SERVER]
114hostname: localhost
Chris Sosae4181242012-09-10 16:54:30 -0700115
116[SCHEDULER]
117drones: localhost
Aviv Keshet2c603d82013-05-24 10:31:59 -0700118
119[CROS]
120source_tree: ${CROS_CHECKOUT}
Chris Masone2aa55332011-12-30 14:41:07 -0800121EOF
Chris Sosa18c70b32013-02-15 14:12:43 -0800122 echo -e "Done!\n"
123fi
Chris Masone2aa55332011-12-30 14:41:07 -0800124
125echo "Installing needed Ubuntu packages..."
Scott Zawalskia5745e92013-05-09 10:42:27 -0400126PKG_LIST="mysql-server mysql-common libapache2-mod-wsgi python-mysqldb \
Chris Masone2aa55332011-12-30 14:41:07 -0800127gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \
128sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
Simran Basi3d182082012-09-07 10:15:25 -0700129python-crypto python-dev subversion build-essential python-setuptools \
130python-numpy python-scipy"
Chris Masone2aa55332011-12-30 14:41:07 -0800131
132if ! sudo apt-get install -y ${PKG_LIST}; then
133 echo "Could not install packages: $?"
134 exit 1
135fi
136echo -e "Done!\n"
137
Alex Millerda3ecf42012-07-25 11:51:13 -0700138echo "Setting up Database: chromeos_autotest_db in MySQL..."
Chris Masone2aa55332011-12-30 14:41:07 -0800139if mysql -u root -e ';' 2> /dev/null ; then
140 PASSWD_STRING=
141elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
142 PASSWD_STRING="-p${PASSWD}"
143else
144 PASSWD_STRING="-p"
145fi
146
Simran Basi79b1a7b2013-02-13 11:16:00 -0800147if ! mysqladmin ping ; then
148 sudo service mysql start
149fi
150
Alex Millerda3ecf42012-07-25 11:51:13 -0700151CLOBBERDB=
152EXISTING_DATABASE=$(mysql -u root "${PASSWD_STRING}" -e "SELECT SCHEMA_NAME \
153FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'chromeos_autotest_db'")
154if [ -n "${EXISTING_DATABASE}" ]; then
155 while read -n 1 -p "Clobber existing MySQL database? [y/N]: " CLOBBERDB; do
156 echo
Richard Barnette23ef4422012-11-02 13:46:20 -0700157 if [[ -z "${CLOBBERDB}" ||
158 $(echo ${CLOBBERDB} | egrep -qi 'y|n') -eq 0 ]]; then
Alex Millerda3ecf42012-07-25 11:51:13 -0700159 break
160 fi
161 echo "Please enter y or n."
162 done
163else
164 CLOBBERDB='y'
165fi
166
167SQL_COMMAND="drop database if exists chromeos_autotest_db; \
168create database chromeos_autotest_db; \
169grant all privileges on chromeos_autotest_db.* TO \
Simran Basi3d182082012-09-07 10:15:25 -0700170'chromeosqa-admin'@'localhost' identified by '${PASSWD}'; \
Alex Millerda3ecf42012-07-25 11:51:13 -0700171FLUSH PRIVILEGES;"
172
Alex Millerda3ecf42012-07-25 11:51:13 -0700173if [[ "${CLOBBERDB}" = 'y' || "${CLOBBERDB}" = 'Y' ]]; then
174 mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}"
175fi
Chris Masone2aa55332011-12-30 14:41:07 -0800176echo -e "Done!\n"
177
178AT_DIR=/usr/local/autotest
179echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
180sudo mkdir -p "${AT_DIR}"
181sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
182echo -e "Done!\n"
183
184EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
185if [ -n "${EXISTING_MOUNT}" ]; then
186 echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
187 echo "We won't update /etc/fstab, but you should have a line line this:"
188 echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
189else
190 echo -n "Adding aforementioned bind-mount to /etc/fstab..."
191 # Is there a better way to elevate privs and do a redirect?
192 sudo su -c \
193 "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
194 >> /etc/fstab"
195 echo -e "Done!\n"
196fi
197
198echo -n "Reticulating splines..."
199"${AT_DIR}"/utils/build_externals.py &> /dev/null
200"${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
201echo -e "Done!\n"
202
203echo "Populating autotest mysql DB..."
204"${AT_DIR}"/database/migrate.py sync
205"${AT_DIR}"/frontend/manage.py syncdb
206# You may have to run this twice.
207"${AT_DIR}"/frontend/manage.py syncdb
208"${AT_DIR}"/utils/test_importer.py
209echo -e "Done!\n"
210
211echo "Configuring apache to run the autotest web interface..."
Simran Basi508ee922013-11-18 13:43:40 -0800212if [ ! -d /etc/apache2/run ]; then
213 sudo mkdir /etc/apache2/run
214fi
Chris Masone2aa55332011-12-30 14:41:07 -0800215sudo ln -sf "${AT_DIR}"/apache/apache-conf \
216 /etc/apache2/sites-available/autotest-server
217# disable currently active default
218sudo a2dissite default
219# enable autotest server
220sudo a2ensite autotest-server
221# Enable rewrite module
222sudo a2enmod rewrite
Scott Zawalskia5745e92013-05-09 10:42:27 -0400223# Enable wsgi
224sudo a2enmod wsgi
225# enable version
226sudo a2enmod version
Chris Masone2aa55332011-12-30 14:41:07 -0800227# Setup permissions so that Apache web user can read the proper files.
228chmod -R o+r "${AT_DIR}"
Alex Miller5e7b2542013-02-13 14:24:04 -0800229find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x
Chris Masone2aa55332011-12-30 14:41:07 -0800230chmod o+x "${AT_DIR}"/tko/*.cgi
231# restart server
232sudo /etc/init.d/apache2 restart
233
234echo "Browse to http://localhost to see if Autotest is working."
235echo "For further necessary set up steps, see https://sites.google.com/a/chromium.org/dev/chromium-os/testing/autotest-developer-faq/setup-autotest-server?pli=1"