blob: c28f4fdd3536ac296d5dbb1da35268c0414be4a1 [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
Prathmesh Prabhu864de132015-04-08 14:45:13 -07008USAGE='Usage: setup_dev_autotest.sh [-pavnm]'
Chris Masone2aa55332011-12-30 14:41:07 -08009HELP="${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\
Prathmesh Prabhu864de132015-04-08 14:45:13 -070013 -p Desired Autotest DB password. Must be non-empty.\n\
MK Ryuc96e9592015-03-05 14:09:03 -080014 -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
Prathmesh Prabhu864de132015-04-08 14:45:13 -070070 echo -e "${HELP}" >&2
Chris Masone2aa55332011-12-30 14:41:07 -080071 exit 1
72 ;;
73 :)
74 echo "Option -$OPTARG requires an argument." >&2
Prathmesh Prabhu864de132015-04-08 14:45:13 -070075 echo -e "${HELP}" >&2
Chris Masone2aa55332011-12-30 14:41:07 -080076 exit 1
77 ;;
78 esac
79done
80
Prathmesh Prabhu864de132015-04-08 14:45:13 -070081if [[ $EUID -eq 0 ]]; then
82 echo "Running with sudo / as root is not recommended"
83 get_y_or_n verify "Continue as root? [y/N]: " "n"
84 if [[ "${verify}" = 'n' ]]; then
85 echo "Bailing!"
86 exit 1
87 fi
88fi
Jakob Juelich1c53ae72014-09-10 11:32:16 -070089
90if [ "${noninteractive}" = "TRUE" ]; then
91 if [ -z "${AUTOTEST_DIR}" ]; then
92 echo "-a must be specified in non-interactive mode." >&2
93 exit 1
94 fi
95 if [ -z "${PASSWD}" ]; then
96 echo "-p must be specified in non-interactive mode." >&2
97 exit 1
98 fi
99fi
100
101
Richard Barnette23ef4422012-11-02 13:46:20 -0700102if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -0800103 read -s -p "Autotest DB password: " PASSWD
104 echo
Richard Barnette23ef4422012-11-02 13:46:20 -0700105 if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -0800106 echo "Empty passwords not allowed." >&2
107 exit 1
108 fi
109 read -s -p "Re-enter password: " PASSWD2
110 echo
Richard Barnette23ef4422012-11-02 13:46:20 -0700111 if [ "${PASSWD}" != "${PASSWD2}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -0800112 echo "Passwords don't match." >&2
113 exit 1
114 fi
115fi
116
117if [ -z "${AUTOTEST_DIR}" ]; then
118 CANDIDATE=$(dirname "$(readlink -f "$0")" | egrep -o '(/[^/]+)*/files')
119 read -p "Enter autotest dir [${CANDIDATE}]: " AUTOTEST_DIR
120 if [ -z "${AUTOTEST_DIR}" ]; then
121 AUTOTEST_DIR="${CANDIDATE}"
122 fi
123fi
124
Aviv Keshet2c603d82013-05-24 10:31:59 -0700125
126# Sanity check AUTOTEST_DIR. If it's null, or doesn't exist on the filesystem
127# then die.
128if [ -z "${AUTOTEST_DIR}" ]; then
129 echo "No AUTOTEST_DIR. Aborting script."
130 exit 1
131fi
132
133if [ ! -d "${AUTOTEST_DIR}" ]; then
134 echo "Directory " ${AUTOTEST_DIR} " does not exist. Aborting script."
135 exit 1
136fi
137
138
Chris Masone2aa55332011-12-30 14:41:07 -0800139SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini"
140echo "Autotest supports local overrides of global configuration through a "
141echo "'shadow' configuration file. Setting one up for you now."
Chris Sosa18c70b32013-02-15 14:12:43 -0800142CLOBBER=0
Chris Masone2aa55332011-12-30 14:41:07 -0800143if [ -f ${SHADOW_CONFIG_PATH} ]; then
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700144 get_y_or_n clobber "Clobber existing shadow config? [Y/n]: " "n"
145 if [[ "${clobber}" = 'n' ]]; then
Chris Sosa18c70b32013-02-15 14:12:43 -0800146 CLOBBER=1
Chris Masone2aa55332011-12-30 14:41:07 -0800147 echo "Refusing to clobber existing shadow_config.ini."
Chris Sosa18c70b32013-02-15 14:12:43 -0800148 else
149 echo "Clobbering existing shadow_config.ini."
Chris Masone2aa55332011-12-30 14:41:07 -0800150 fi
Chris Masone2aa55332011-12-30 14:41:07 -0800151fi
152
Aviv Keshet2c603d82013-05-24 10:31:59 -0700153CROS_CHECKOUT=$(readlink -f ${AUTOTEST_DIR}/../../../..)
154
Chris Sosa18c70b32013-02-15 14:12:43 -0800155# Create clean shadow config if we're replacing it/creating a new one.
156if [ $CLOBBER -eq 0 ]; then
157 cat > "${SHADOW_CONFIG_PATH}" <<EOF
Chris Masone2aa55332011-12-30 14:41:07 -0800158[AUTOTEST_WEB]
159host: localhost
160password: ${PASSWD}
Chris Masonee61dd812012-01-10 18:04:21 -0800161readonly_host: localhost
162readonly_user: chromeosqa-admin
163readonly_password: ${PASSWD}
Chris Masone2aa55332011-12-30 14:41:07 -0800164
165[SERVER]
166hostname: localhost
Chris Sosae4181242012-09-10 16:54:30 -0700167
168[SCHEDULER]
169drones: localhost
Aviv Keshet2c603d82013-05-24 10:31:59 -0700170
171[CROS]
172source_tree: ${CROS_CHECKOUT}
Chris Masone2aa55332011-12-30 14:41:07 -0800173EOF
Chris Sosa18c70b32013-02-15 14:12:43 -0800174 echo -e "Done!\n"
175fi
Chris Masone2aa55332011-12-30 14:41:07 -0800176
177echo "Installing needed Ubuntu packages..."
Scott Zawalskia5745e92013-05-09 10:42:27 -0400178PKG_LIST="mysql-server mysql-common libapache2-mod-wsgi python-mysqldb \
Chris Masone2aa55332011-12-30 14:41:07 -0800179gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \
180sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
Simran Basi3d182082012-09-07 10:15:25 -0700181python-crypto python-dev subversion build-essential python-setuptools \
182python-numpy python-scipy"
Chris Masone2aa55332011-12-30 14:41:07 -0800183
184if ! sudo apt-get install -y ${PKG_LIST}; then
185 echo "Could not install packages: $?"
186 exit 1
187fi
188echo -e "Done!\n"
189
Dan Shi7f0c1832014-10-27 16:05:57 -0700190# Check if database exists, clobber existing database with user consent.
191#
192# Arguments: Name of the database
193check_database()
194{
195 local db_name=$1
196 echo "Setting up Database: $db_name in MySQL..."
197 if mysql -u root -e ';' 2> /dev/null ; then
198 PASSWD_STRING=
199 elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
200 PASSWD_STRING="-p${PASSWD}"
201 else
202 PASSWD_STRING="-p"
203 fi
Chris Masone2aa55332011-12-30 14:41:07 -0800204
MK Ryu1e20b982015-01-06 16:39:21 -0800205 if ! mysqladmin -u root "${PASSWD_STRING}" ping ; then
Dan Shi7f0c1832014-10-27 16:05:57 -0700206 sudo service mysql start
207 fi
Simran Basi79b1a7b2013-02-13 11:16:00 -0800208
Dan Shi7f0c1832014-10-27 16:05:57 -0700209 local clobberdb='y'
210 local existing_database=$(mysql -u root "${PASSWD_STRING}" -e "SELECT \
211 SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db_name'")
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700212
Dan Shi7f0c1832014-10-27 16:05:57 -0700213 if [ -n "${existing_database}" ]; then
214 get_y_or_n clobberdb "Clobber existing MySQL database? [Y/n]: " "n"
215 fi
Alex Millerda3ecf42012-07-25 11:51:13 -0700216
MK Ryuc96e9592015-03-05 14:09:03 -0800217 local sql_priv="GRANT ALL PRIVILEGES ON $db_name.* TO \
218 'chromeosqa-admin'@'localhost' IDENTIFIED BY '${PASSWD}';"
219
220 # Master DB needs to enable connection from shard nodes.
221 if [ "${master}" = "TRUE" ]; then
222 sql_priv="${sql_priv} GRANT ALL PRIVILEGES ON $db_name.* TO \
223 'chromeosqa-admin'@'%' IDENTIFIED BY '${PASSWD}';"
224 fi
225
Dan Shi7f0c1832014-10-27 16:05:57 -0700226 local sql_command="drop database if exists $db_name; \
227 create database $db_name; \
MK Ryuc96e9592015-03-05 14:09:03 -0800228 ${sql_priv} FLUSH PRIVILEGES;"
Alex Millerda3ecf42012-07-25 11:51:13 -0700229
Dan Shi7f0c1832014-10-27 16:05:57 -0700230 if [[ "${clobberdb}" = 'y' ]]; then
231 mysql -u root "${PASSWD_STRING}" -e "${sql_command}"
232 fi
233 echo -e "Done!\n"
234}
235
236check_database 'chromeos_autotest_db'
237check_database 'chromeos_lab_servers'
Chris Masone2aa55332011-12-30 14:41:07 -0800238
239AT_DIR=/usr/local/autotest
240echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
241sudo mkdir -p "${AT_DIR}"
242sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
243echo -e "Done!\n"
244
Fang Dengaf82bca2014-11-18 17:19:06 -0800245sudo chown -R "$(whoami)" "${AT_DIR}"
246
Chris Masone2aa55332011-12-30 14:41:07 -0800247EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
248if [ -n "${EXISTING_MOUNT}" ]; then
249 echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
250 echo "We won't update /etc/fstab, but you should have a line line this:"
251 echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
252else
253 echo -n "Adding aforementioned bind-mount to /etc/fstab..."
254 # Is there a better way to elevate privs and do a redirect?
255 sudo su -c \
256 "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
257 >> /etc/fstab"
258 echo -e "Done!\n"
259fi
260
261echo -n "Reticulating splines..."
Michael Liangb6a78f42014-07-11 15:05:30 -0700262
263if [ "${verbose}" = "TRUE" ]; then
264 "${AT_DIR}"/utils/build_externals.py
265 "${AT_DIR}"/utils/compile_gwt_clients.py -a
266else
267 "${AT_DIR}"/utils/build_externals.py &> /dev/null
268 "${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
269fi
270
Chris Masone2aa55332011-12-30 14:41:07 -0800271echo -e "Done!\n"
272
273echo "Populating autotest mysql DB..."
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700274"${AT_DIR}"/database/migrate.py sync -f
275"${AT_DIR}"/frontend/manage.py syncdb --noinput
Chris Masone2aa55332011-12-30 14:41:07 -0800276# You may have to run this twice.
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700277"${AT_DIR}"/frontend/manage.py syncdb --noinput
Chris Masone2aa55332011-12-30 14:41:07 -0800278"${AT_DIR}"/utils/test_importer.py
279echo -e "Done!\n"
280
Dan Shi7f0c1832014-10-27 16:05:57 -0700281echo "Initializing chromeos_lab_servers mysql DB..."
282"${AT_DIR}"/database/migrate.py sync -f -d AUTOTEST_SERVER_DB
283echo -e "Done!\n"
284
Chris Masone2aa55332011-12-30 14:41:07 -0800285echo "Configuring apache to run the autotest web interface..."
Simran Basi508ee922013-11-18 13:43:40 -0800286if [ ! -d /etc/apache2/run ]; then
287 sudo mkdir /etc/apache2/run
288fi
Chris Masone2aa55332011-12-30 14:41:07 -0800289sudo ln -sf "${AT_DIR}"/apache/apache-conf \
Dan Shi18808492014-11-05 10:25:26 -0800290 /etc/apache2/sites-available/autotest-server.conf
MK Ryuc96e9592015-03-05 14:09:03 -0800291# Disable currently active default
Michael Janssena7427612014-11-14 15:44:39 -0800292sudo a2dissite 000-default default || true
MK Ryuc96e9592015-03-05 14:09:03 -0800293# Enable autotest server
Dan Shi18808492014-11-05 10:25:26 -0800294sudo a2ensite autotest-server.conf
Chris Masone2aa55332011-12-30 14:41:07 -0800295# Enable rewrite module
296sudo a2enmod rewrite
Scott Zawalskia5745e92013-05-09 10:42:27 -0400297# Enable wsgi
298sudo a2enmod wsgi
MK Ryuc96e9592015-03-05 14:09:03 -0800299# Enable version
Michael Janssena7427612014-11-14 15:44:39 -0800300# built-in on trusty
301sudo a2enmod version || true
MK Ryuc96e9592015-03-05 14:09:03 -0800302# Enable headers
Dan Shia85f6d82014-10-01 08:58:01 -0700303sudo a2enmod headers
MK Ryuc96e9592015-03-05 14:09:03 -0800304# Enable cgid
305sudo a2enmod cgid
Chris Masone2aa55332011-12-30 14:41:07 -0800306# Setup permissions so that Apache web user can read the proper files.
307chmod -R o+r "${AT_DIR}"
Alex Miller5e7b2542013-02-13 14:24:04 -0800308find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x
Chris Masone2aa55332011-12-30 14:41:07 -0800309chmod o+x "${AT_DIR}"/tko/*.cgi
310# restart server
311sudo /etc/init.d/apache2 restart
312
313echo "Browse to http://localhost to see if Autotest is working."
314echo "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"