blob: bda41494aca84c94d6ec2c9bcd36a8972fd7ba3a [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\
Michael Liangb6a78f42014-07-11 15:05:30 -070014 -a Absolute path to autotest source tree.\n
Jakob Juelich1c53ae72014-09-10 11:32:16 -070015 -v Show info logging from build_externals.py and compile_gwt_clients.py \n
16 -n Non-interactive mode, doesn't ask for any user input.
17 Requires -p and -a to be set."
18
19function get_y_or_n_interactive {
20 local ret
21 while true; do
22 read -p "$2" yn
23 case $yn in
24 [Yy]* ) ret="y"; break;;
25 [Nn]* ) ret="n"; break;;
26 * ) echo "Please enter y or n.";;
27 esac
28 done
29 eval $1="'$ret'"
30}
31
32function get_y_or_n {
33 local ret=$3
34 if [ "${noninteractive}" = "FALSE" ]; then
35 get_y_or_n_interactive sub "$2"
36 ret=$sub
37 fi
38 eval $1="'$ret'"
39}
Chris Masone2aa55332011-12-30 14:41:07 -080040
41AUTOTEST_DIR=
42PASSWD=
Michael Liangb6a78f42014-07-11 15:05:30 -070043verbose="FALSE"
Jakob Juelich1c53ae72014-09-10 11:32:16 -070044noninteractive="FALSE"
45while getopts ":p:a:nvh" opt; do
Chris Masone2aa55332011-12-30 14:41:07 -080046 case $opt in
47 a)
48 AUTOTEST_DIR=$OPTARG
49 ;;
50 p)
51 PASSWD=$OPTARG
52 ;;
Michael Liangb6a78f42014-07-11 15:05:30 -070053 v)
54 verbose="TRUE"
55 ;;
Jakob Juelich1c53ae72014-09-10 11:32:16 -070056 n)
57 noninteractive="TRUE"
58 ;;
Chris Masone2aa55332011-12-30 14:41:07 -080059 h)
60 echo -e "${HELP}" >&2
61 exit 0
62 ;;
63 \?)
64 echo "Invalid option: -$OPTARG" >&2
65 echo "${USAGE}" >&2
66 exit 1
67 ;;
68 :)
69 echo "Option -$OPTARG requires an argument." >&2
70 echo "${USAGE}" >&2
71 exit 1
72 ;;
73 esac
74done
75
Jakob Juelich1c53ae72014-09-10 11:32:16 -070076
77if [ "${noninteractive}" = "TRUE" ]; then
78 if [ -z "${AUTOTEST_DIR}" ]; then
79 echo "-a must be specified in non-interactive mode." >&2
80 exit 1
81 fi
82 if [ -z "${PASSWD}" ]; then
83 echo "-p must be specified in non-interactive mode." >&2
84 exit 1
85 fi
86fi
87
88
Richard Barnette23ef4422012-11-02 13:46:20 -070089if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080090 read -s -p "Autotest DB password: " PASSWD
91 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070092 if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080093 echo "Empty passwords not allowed." >&2
94 exit 1
95 fi
96 read -s -p "Re-enter password: " PASSWD2
97 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070098 if [ "${PASSWD}" != "${PASSWD2}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080099 echo "Passwords don't match." >&2
100 exit 1
101 fi
102fi
103
104if [ -z "${AUTOTEST_DIR}" ]; then
105 CANDIDATE=$(dirname "$(readlink -f "$0")" | egrep -o '(/[^/]+)*/files')
106 read -p "Enter autotest dir [${CANDIDATE}]: " AUTOTEST_DIR
107 if [ -z "${AUTOTEST_DIR}" ]; then
108 AUTOTEST_DIR="${CANDIDATE}"
109 fi
110fi
111
Aviv Keshet2c603d82013-05-24 10:31:59 -0700112
113# Sanity check AUTOTEST_DIR. If it's null, or doesn't exist on the filesystem
114# then die.
115if [ -z "${AUTOTEST_DIR}" ]; then
116 echo "No AUTOTEST_DIR. Aborting script."
117 exit 1
118fi
119
120if [ ! -d "${AUTOTEST_DIR}" ]; then
121 echo "Directory " ${AUTOTEST_DIR} " does not exist. Aborting script."
122 exit 1
123fi
124
125
Chris Masone2aa55332011-12-30 14:41:07 -0800126SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini"
127echo "Autotest supports local overrides of global configuration through a "
128echo "'shadow' configuration file. Setting one up for you now."
Chris Sosa18c70b32013-02-15 14:12:43 -0800129CLOBBER=0
Chris Masone2aa55332011-12-30 14:41:07 -0800130if [ -f ${SHADOW_CONFIG_PATH} ]; then
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700131 get_y_or_n clobber "Clobber existing shadow config? [Y/n]: " "n"
132 if [[ "${clobber}" = 'n' ]]; then
Chris Sosa18c70b32013-02-15 14:12:43 -0800133 CLOBBER=1
Chris Masone2aa55332011-12-30 14:41:07 -0800134 echo "Refusing to clobber existing shadow_config.ini."
Chris Sosa18c70b32013-02-15 14:12:43 -0800135 else
136 echo "Clobbering existing shadow_config.ini."
Chris Masone2aa55332011-12-30 14:41:07 -0800137 fi
Chris Masone2aa55332011-12-30 14:41:07 -0800138fi
139
Aviv Keshet2c603d82013-05-24 10:31:59 -0700140CROS_CHECKOUT=$(readlink -f ${AUTOTEST_DIR}/../../../..)
141
Chris Sosa18c70b32013-02-15 14:12:43 -0800142# Create clean shadow config if we're replacing it/creating a new one.
143if [ $CLOBBER -eq 0 ]; then
144 cat > "${SHADOW_CONFIG_PATH}" <<EOF
Chris Masone2aa55332011-12-30 14:41:07 -0800145[AUTOTEST_WEB]
146host: localhost
147password: ${PASSWD}
Chris Masonee61dd812012-01-10 18:04:21 -0800148readonly_host: localhost
149readonly_user: chromeosqa-admin
150readonly_password: ${PASSWD}
Chris Masone2aa55332011-12-30 14:41:07 -0800151
152[SERVER]
153hostname: localhost
Chris Sosae4181242012-09-10 16:54:30 -0700154
155[SCHEDULER]
156drones: localhost
Aviv Keshet2c603d82013-05-24 10:31:59 -0700157
158[CROS]
159source_tree: ${CROS_CHECKOUT}
Chris Masone2aa55332011-12-30 14:41:07 -0800160EOF
Chris Sosa18c70b32013-02-15 14:12:43 -0800161 echo -e "Done!\n"
162fi
Chris Masone2aa55332011-12-30 14:41:07 -0800163
164echo "Installing needed Ubuntu packages..."
Scott Zawalskia5745e92013-05-09 10:42:27 -0400165PKG_LIST="mysql-server mysql-common libapache2-mod-wsgi python-mysqldb \
Chris Masone2aa55332011-12-30 14:41:07 -0800166gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \
167sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
Simran Basi3d182082012-09-07 10:15:25 -0700168python-crypto python-dev subversion build-essential python-setuptools \
169python-numpy python-scipy"
Chris Masone2aa55332011-12-30 14:41:07 -0800170
171if ! sudo apt-get install -y ${PKG_LIST}; then
172 echo "Could not install packages: $?"
173 exit 1
174fi
175echo -e "Done!\n"
176
Dan Shi7f0c1832014-10-27 16:05:57 -0700177# Check if database exists, clobber existing database with user consent.
178#
179# Arguments: Name of the database
180check_database()
181{
182 local db_name=$1
183 echo "Setting up Database: $db_name in MySQL..."
184 if mysql -u root -e ';' 2> /dev/null ; then
185 PASSWD_STRING=
186 elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
187 PASSWD_STRING="-p${PASSWD}"
188 else
189 PASSWD_STRING="-p"
190 fi
Chris Masone2aa55332011-12-30 14:41:07 -0800191
Dan Shi7f0c1832014-10-27 16:05:57 -0700192 if ! mysqladmin ping ; then
193 sudo service mysql start
194 fi
Simran Basi79b1a7b2013-02-13 11:16:00 -0800195
Dan Shi7f0c1832014-10-27 16:05:57 -0700196 local clobberdb='y'
197 local existing_database=$(mysql -u root "${PASSWD_STRING}" -e "SELECT \
198 SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db_name'")
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700199
Dan Shi7f0c1832014-10-27 16:05:57 -0700200 if [ -n "${existing_database}" ]; then
201 get_y_or_n clobberdb "Clobber existing MySQL database? [Y/n]: " "n"
202 fi
Alex Millerda3ecf42012-07-25 11:51:13 -0700203
Dan Shi7f0c1832014-10-27 16:05:57 -0700204 local sql_command="drop database if exists $db_name; \
205 create database $db_name; \
206 grant all privileges on $db_name.* TO \
207 'chromeosqa-admin'@'localhost' identified by '${PASSWD}'; \
208 FLUSH PRIVILEGES;"
Alex Millerda3ecf42012-07-25 11:51:13 -0700209
Dan Shi7f0c1832014-10-27 16:05:57 -0700210 if [[ "${clobberdb}" = 'y' ]]; then
211 mysql -u root "${PASSWD_STRING}" -e "${sql_command}"
212 fi
213 echo -e "Done!\n"
214}
215
216check_database 'chromeos_autotest_db'
217check_database 'chromeos_lab_servers'
Chris Masone2aa55332011-12-30 14:41:07 -0800218
219AT_DIR=/usr/local/autotest
220echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
221sudo mkdir -p "${AT_DIR}"
222sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
223echo -e "Done!\n"
224
Fang Dengaf82bca2014-11-18 17:19:06 -0800225sudo chown -R "$(whoami)" "${AT_DIR}"
226
Chris Masone2aa55332011-12-30 14:41:07 -0800227EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
228if [ -n "${EXISTING_MOUNT}" ]; then
229 echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
230 echo "We won't update /etc/fstab, but you should have a line line this:"
231 echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
232else
233 echo -n "Adding aforementioned bind-mount to /etc/fstab..."
234 # Is there a better way to elevate privs and do a redirect?
235 sudo su -c \
236 "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
237 >> /etc/fstab"
238 echo -e "Done!\n"
239fi
240
241echo -n "Reticulating splines..."
Michael Liangb6a78f42014-07-11 15:05:30 -0700242
243if [ "${verbose}" = "TRUE" ]; then
244 "${AT_DIR}"/utils/build_externals.py
245 "${AT_DIR}"/utils/compile_gwt_clients.py -a
246else
247 "${AT_DIR}"/utils/build_externals.py &> /dev/null
248 "${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
249fi
250
Chris Masone2aa55332011-12-30 14:41:07 -0800251echo -e "Done!\n"
252
253echo "Populating autotest mysql DB..."
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700254"${AT_DIR}"/database/migrate.py sync -f
255"${AT_DIR}"/frontend/manage.py syncdb --noinput
Chris Masone2aa55332011-12-30 14:41:07 -0800256# You may have to run this twice.
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700257"${AT_DIR}"/frontend/manage.py syncdb --noinput
Chris Masone2aa55332011-12-30 14:41:07 -0800258"${AT_DIR}"/utils/test_importer.py
259echo -e "Done!\n"
260
Dan Shi7f0c1832014-10-27 16:05:57 -0700261echo "Initializing chromeos_lab_servers mysql DB..."
262"${AT_DIR}"/database/migrate.py sync -f -d AUTOTEST_SERVER_DB
263echo -e "Done!\n"
264
Chris Masone2aa55332011-12-30 14:41:07 -0800265echo "Configuring apache to run the autotest web interface..."
Simran Basi508ee922013-11-18 13:43:40 -0800266if [ ! -d /etc/apache2/run ]; then
267 sudo mkdir /etc/apache2/run
268fi
Chris Masone2aa55332011-12-30 14:41:07 -0800269sudo ln -sf "${AT_DIR}"/apache/apache-conf \
Dan Shi18808492014-11-05 10:25:26 -0800270 /etc/apache2/sites-available/autotest-server.conf
Chris Masone2aa55332011-12-30 14:41:07 -0800271# disable currently active default
Michael Janssena7427612014-11-14 15:44:39 -0800272sudo a2dissite 000-default default || true
Chris Masone2aa55332011-12-30 14:41:07 -0800273# enable autotest server
Dan Shi18808492014-11-05 10:25:26 -0800274sudo a2ensite autotest-server.conf
Chris Masone2aa55332011-12-30 14:41:07 -0800275# Enable rewrite module
276sudo a2enmod rewrite
Scott Zawalskia5745e92013-05-09 10:42:27 -0400277# Enable wsgi
278sudo a2enmod wsgi
279# enable version
Michael Janssena7427612014-11-14 15:44:39 -0800280# built-in on trusty
281sudo a2enmod version || true
Dan Shia85f6d82014-10-01 08:58:01 -0700282# enable headers
283sudo a2enmod headers
Chris Masone2aa55332011-12-30 14:41:07 -0800284# Setup permissions so that Apache web user can read the proper files.
285chmod -R o+r "${AT_DIR}"
Alex Miller5e7b2542013-02-13 14:24:04 -0800286find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x
Chris Masone2aa55332011-12-30 14:41:07 -0800287chmod o+x "${AT_DIR}"/tko/*.cgi
288# restart server
289sudo /etc/init.d/apache2 restart
290
291echo "Browse to http://localhost to see if Autotest is working."
292echo "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"