blob: 8de4ebb26644d5dd9f6c8c012626334471ad6cae [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\
MK Ryuc96e9592015-03-05 14:09:03 -080013 -p Desired Autotest DB password.\n\
14 -a Absolute path to autotest source tree.\n\
15 -v Show info logging from build_externals.py and compile_gwt_clients.py \n\
Jakob Juelich1c53ae72014-09-10 11:32:16 -070016 -n Non-interactive mode, doesn't ask for any user input.
MK Ryuc96e9592015-03-05 14:09:03 -080017 Requires -p and -a to be set.\n\
18 -m Master node."
Jakob Juelich1c53ae72014-09-10 11:32:16 -070019
20function get_y_or_n_interactive {
21 local ret
22 while true; do
23 read -p "$2" yn
24 case $yn in
25 [Yy]* ) ret="y"; break;;
26 [Nn]* ) ret="n"; break;;
27 * ) echo "Please enter y or n.";;
28 esac
29 done
30 eval $1="'$ret'"
31}
32
33function get_y_or_n {
34 local ret=$3
35 if [ "${noninteractive}" = "FALSE" ]; then
36 get_y_or_n_interactive sub "$2"
37 ret=$sub
38 fi
39 eval $1="'$ret'"
40}
Chris Masone2aa55332011-12-30 14:41:07 -080041
42AUTOTEST_DIR=
43PASSWD=
Michael Liangb6a78f42014-07-11 15:05:30 -070044verbose="FALSE"
Jakob Juelich1c53ae72014-09-10 11:32:16 -070045noninteractive="FALSE"
MK Ryuc96e9592015-03-05 14:09:03 -080046master="FALSE"
47while getopts ":p:a:vnmh" opt; do
Chris Masone2aa55332011-12-30 14:41:07 -080048 case $opt in
49 a)
50 AUTOTEST_DIR=$OPTARG
51 ;;
52 p)
53 PASSWD=$OPTARG
54 ;;
Michael Liangb6a78f42014-07-11 15:05:30 -070055 v)
56 verbose="TRUE"
57 ;;
Jakob Juelich1c53ae72014-09-10 11:32:16 -070058 n)
59 noninteractive="TRUE"
60 ;;
MK Ryuc96e9592015-03-05 14:09:03 -080061 m)
62 master="TRUE"
63 ;;
Chris Masone2aa55332011-12-30 14:41:07 -080064 h)
65 echo -e "${HELP}" >&2
66 exit 0
67 ;;
68 \?)
69 echo "Invalid option: -$OPTARG" >&2
70 echo "${USAGE}" >&2
71 exit 1
72 ;;
73 :)
74 echo "Option -$OPTARG requires an argument." >&2
75 echo "${USAGE}" >&2
76 exit 1
77 ;;
78 esac
79done
80
Jakob Juelich1c53ae72014-09-10 11:32:16 -070081
82if [ "${noninteractive}" = "TRUE" ]; then
83 if [ -z "${AUTOTEST_DIR}" ]; then
84 echo "-a must be specified in non-interactive mode." >&2
85 exit 1
86 fi
87 if [ -z "${PASSWD}" ]; then
88 echo "-p must be specified in non-interactive mode." >&2
89 exit 1
90 fi
91fi
92
93
Richard Barnette23ef4422012-11-02 13:46:20 -070094if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080095 read -s -p "Autotest DB password: " PASSWD
96 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070097 if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080098 echo "Empty passwords not allowed." >&2
99 exit 1
100 fi
101 read -s -p "Re-enter password: " PASSWD2
102 echo
Richard Barnette23ef4422012-11-02 13:46:20 -0700103 if [ "${PASSWD}" != "${PASSWD2}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -0800104 echo "Passwords don't match." >&2
105 exit 1
106 fi
107fi
108
109if [ -z "${AUTOTEST_DIR}" ]; then
110 CANDIDATE=$(dirname "$(readlink -f "$0")" | egrep -o '(/[^/]+)*/files')
111 read -p "Enter autotest dir [${CANDIDATE}]: " AUTOTEST_DIR
112 if [ -z "${AUTOTEST_DIR}" ]; then
113 AUTOTEST_DIR="${CANDIDATE}"
114 fi
115fi
116
Aviv Keshet2c603d82013-05-24 10:31:59 -0700117
118# Sanity check AUTOTEST_DIR. If it's null, or doesn't exist on the filesystem
119# then die.
120if [ -z "${AUTOTEST_DIR}" ]; then
121 echo "No AUTOTEST_DIR. Aborting script."
122 exit 1
123fi
124
125if [ ! -d "${AUTOTEST_DIR}" ]; then
126 echo "Directory " ${AUTOTEST_DIR} " does not exist. Aborting script."
127 exit 1
128fi
129
130
Chris Masone2aa55332011-12-30 14:41:07 -0800131SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini"
132echo "Autotest supports local overrides of global configuration through a "
133echo "'shadow' configuration file. Setting one up for you now."
Chris Sosa18c70b32013-02-15 14:12:43 -0800134CLOBBER=0
Chris Masone2aa55332011-12-30 14:41:07 -0800135if [ -f ${SHADOW_CONFIG_PATH} ]; then
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700136 get_y_or_n clobber "Clobber existing shadow config? [Y/n]: " "n"
137 if [[ "${clobber}" = 'n' ]]; then
Chris Sosa18c70b32013-02-15 14:12:43 -0800138 CLOBBER=1
Chris Masone2aa55332011-12-30 14:41:07 -0800139 echo "Refusing to clobber existing shadow_config.ini."
Chris Sosa18c70b32013-02-15 14:12:43 -0800140 else
141 echo "Clobbering existing shadow_config.ini."
Chris Masone2aa55332011-12-30 14:41:07 -0800142 fi
Chris Masone2aa55332011-12-30 14:41:07 -0800143fi
144
Aviv Keshet2c603d82013-05-24 10:31:59 -0700145CROS_CHECKOUT=$(readlink -f ${AUTOTEST_DIR}/../../../..)
146
Chris Sosa18c70b32013-02-15 14:12:43 -0800147# Create clean shadow config if we're replacing it/creating a new one.
148if [ $CLOBBER -eq 0 ]; then
149 cat > "${SHADOW_CONFIG_PATH}" <<EOF
Chris Masone2aa55332011-12-30 14:41:07 -0800150[AUTOTEST_WEB]
151host: localhost
152password: ${PASSWD}
Chris Masonee61dd812012-01-10 18:04:21 -0800153readonly_host: localhost
154readonly_user: chromeosqa-admin
155readonly_password: ${PASSWD}
Chris Masone2aa55332011-12-30 14:41:07 -0800156
157[SERVER]
158hostname: localhost
Chris Sosae4181242012-09-10 16:54:30 -0700159
160[SCHEDULER]
161drones: localhost
Aviv Keshet2c603d82013-05-24 10:31:59 -0700162
163[CROS]
164source_tree: ${CROS_CHECKOUT}
Chris Masone2aa55332011-12-30 14:41:07 -0800165EOF
Chris Sosa18c70b32013-02-15 14:12:43 -0800166 echo -e "Done!\n"
167fi
Chris Masone2aa55332011-12-30 14:41:07 -0800168
169echo "Installing needed Ubuntu packages..."
Scott Zawalskia5745e92013-05-09 10:42:27 -0400170PKG_LIST="mysql-server mysql-common libapache2-mod-wsgi python-mysqldb \
Chris Masone2aa55332011-12-30 14:41:07 -0800171gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \
172sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
Simran Basi3d182082012-09-07 10:15:25 -0700173python-crypto python-dev subversion build-essential python-setuptools \
174python-numpy python-scipy"
Chris Masone2aa55332011-12-30 14:41:07 -0800175
176if ! sudo apt-get install -y ${PKG_LIST}; then
177 echo "Could not install packages: $?"
178 exit 1
179fi
180echo -e "Done!\n"
181
Dan Shi7f0c1832014-10-27 16:05:57 -0700182# Check if database exists, clobber existing database with user consent.
183#
184# Arguments: Name of the database
185check_database()
186{
187 local db_name=$1
188 echo "Setting up Database: $db_name in MySQL..."
189 if mysql -u root -e ';' 2> /dev/null ; then
190 PASSWD_STRING=
191 elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
192 PASSWD_STRING="-p${PASSWD}"
193 else
194 PASSWD_STRING="-p"
195 fi
Chris Masone2aa55332011-12-30 14:41:07 -0800196
MK Ryu1e20b982015-01-06 16:39:21 -0800197 if ! mysqladmin -u root "${PASSWD_STRING}" ping ; then
Dan Shi7f0c1832014-10-27 16:05:57 -0700198 sudo service mysql start
199 fi
Simran Basi79b1a7b2013-02-13 11:16:00 -0800200
Dan Shi7f0c1832014-10-27 16:05:57 -0700201 local clobberdb='y'
202 local existing_database=$(mysql -u root "${PASSWD_STRING}" -e "SELECT \
203 SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db_name'")
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700204
Dan Shi7f0c1832014-10-27 16:05:57 -0700205 if [ -n "${existing_database}" ]; then
206 get_y_or_n clobberdb "Clobber existing MySQL database? [Y/n]: " "n"
207 fi
Alex Millerda3ecf42012-07-25 11:51:13 -0700208
MK Ryuc96e9592015-03-05 14:09:03 -0800209 local sql_priv="GRANT ALL PRIVILEGES ON $db_name.* TO \
210 'chromeosqa-admin'@'localhost' IDENTIFIED BY '${PASSWD}';"
211
212 # Master DB needs to enable connection from shard nodes.
213 if [ "${master}" = "TRUE" ]; then
214 sql_priv="${sql_priv} GRANT ALL PRIVILEGES ON $db_name.* TO \
215 'chromeosqa-admin'@'%' IDENTIFIED BY '${PASSWD}';"
216 fi
217
Dan Shi7f0c1832014-10-27 16:05:57 -0700218 local sql_command="drop database if exists $db_name; \
219 create database $db_name; \
MK Ryuc96e9592015-03-05 14:09:03 -0800220 ${sql_priv} FLUSH PRIVILEGES;"
Alex Millerda3ecf42012-07-25 11:51:13 -0700221
Dan Shi7f0c1832014-10-27 16:05:57 -0700222 if [[ "${clobberdb}" = 'y' ]]; then
223 mysql -u root "${PASSWD_STRING}" -e "${sql_command}"
224 fi
225 echo -e "Done!\n"
226}
227
228check_database 'chromeos_autotest_db'
229check_database 'chromeos_lab_servers'
Chris Masone2aa55332011-12-30 14:41:07 -0800230
231AT_DIR=/usr/local/autotest
232echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
233sudo mkdir -p "${AT_DIR}"
234sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
235echo -e "Done!\n"
236
Fang Dengaf82bca2014-11-18 17:19:06 -0800237sudo chown -R "$(whoami)" "${AT_DIR}"
238
Chris Masone2aa55332011-12-30 14:41:07 -0800239EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
240if [ -n "${EXISTING_MOUNT}" ]; then
241 echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
242 echo "We won't update /etc/fstab, but you should have a line line this:"
243 echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
244else
245 echo -n "Adding aforementioned bind-mount to /etc/fstab..."
246 # Is there a better way to elevate privs and do a redirect?
247 sudo su -c \
248 "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
249 >> /etc/fstab"
250 echo -e "Done!\n"
251fi
252
253echo -n "Reticulating splines..."
Michael Liangb6a78f42014-07-11 15:05:30 -0700254
255if [ "${verbose}" = "TRUE" ]; then
256 "${AT_DIR}"/utils/build_externals.py
257 "${AT_DIR}"/utils/compile_gwt_clients.py -a
258else
259 "${AT_DIR}"/utils/build_externals.py &> /dev/null
260 "${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
261fi
262
Chris Masone2aa55332011-12-30 14:41:07 -0800263echo -e "Done!\n"
264
265echo "Populating autotest mysql DB..."
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700266"${AT_DIR}"/database/migrate.py sync -f
267"${AT_DIR}"/frontend/manage.py syncdb --noinput
Chris Masone2aa55332011-12-30 14:41:07 -0800268# You may have to run this twice.
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700269"${AT_DIR}"/frontend/manage.py syncdb --noinput
Chris Masone2aa55332011-12-30 14:41:07 -0800270"${AT_DIR}"/utils/test_importer.py
271echo -e "Done!\n"
272
Dan Shi7f0c1832014-10-27 16:05:57 -0700273echo "Initializing chromeos_lab_servers mysql DB..."
274"${AT_DIR}"/database/migrate.py sync -f -d AUTOTEST_SERVER_DB
275echo -e "Done!\n"
276
Chris Masone2aa55332011-12-30 14:41:07 -0800277echo "Configuring apache to run the autotest web interface..."
Simran Basi508ee922013-11-18 13:43:40 -0800278if [ ! -d /etc/apache2/run ]; then
279 sudo mkdir /etc/apache2/run
280fi
Chris Masone2aa55332011-12-30 14:41:07 -0800281sudo ln -sf "${AT_DIR}"/apache/apache-conf \
Dan Shi18808492014-11-05 10:25:26 -0800282 /etc/apache2/sites-available/autotest-server.conf
MK Ryuc96e9592015-03-05 14:09:03 -0800283# Disable currently active default
Michael Janssena7427612014-11-14 15:44:39 -0800284sudo a2dissite 000-default default || true
MK Ryuc96e9592015-03-05 14:09:03 -0800285# Enable autotest server
Dan Shi18808492014-11-05 10:25:26 -0800286sudo a2ensite autotest-server.conf
Chris Masone2aa55332011-12-30 14:41:07 -0800287# Enable rewrite module
288sudo a2enmod rewrite
Scott Zawalskia5745e92013-05-09 10:42:27 -0400289# Enable wsgi
290sudo a2enmod wsgi
MK Ryuc96e9592015-03-05 14:09:03 -0800291# Enable version
Michael Janssena7427612014-11-14 15:44:39 -0800292# built-in on trusty
293sudo a2enmod version || true
MK Ryuc96e9592015-03-05 14:09:03 -0800294# Enable headers
Dan Shia85f6d82014-10-01 08:58:01 -0700295sudo a2enmod headers
MK Ryuc96e9592015-03-05 14:09:03 -0800296# Enable cgid
297sudo a2enmod cgid
Chris Masone2aa55332011-12-30 14:41:07 -0800298# Setup permissions so that Apache web user can read the proper files.
299chmod -R o+r "${AT_DIR}"
Alex Miller5e7b2542013-02-13 14:24:04 -0800300find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x
Chris Masone2aa55332011-12-30 14:41:07 -0800301chmod o+x "${AT_DIR}"/tko/*.cgi
302# restart server
303sudo /etc/init.d/apache2 restart
304
305echo "Browse to http://localhost to see if Autotest is working."
306echo "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"