blob: 12984cc0d80c52fab9c00c23c564da0b6e9062e8 [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
15 -v Show info logging from build_externals.py and compile_gwt_clients.py \n"
Chris Masone2aa55332011-12-30 14:41:07 -080016
17AUTOTEST_DIR=
18PASSWD=
Michael Liangb6a78f42014-07-11 15:05:30 -070019verbose="FALSE"
20while getopts ":p:a:vh" opt; do
Chris Masone2aa55332011-12-30 14:41:07 -080021 case $opt in
22 a)
23 AUTOTEST_DIR=$OPTARG
24 ;;
25 p)
26 PASSWD=$OPTARG
27 ;;
Michael Liangb6a78f42014-07-11 15:05:30 -070028 v)
29 verbose="TRUE"
30 ;;
Chris Masone2aa55332011-12-30 14:41:07 -080031 h)
32 echo -e "${HELP}" >&2
33 exit 0
34 ;;
35 \?)
36 echo "Invalid option: -$OPTARG" >&2
37 echo "${USAGE}" >&2
38 exit 1
39 ;;
40 :)
41 echo "Option -$OPTARG requires an argument." >&2
42 echo "${USAGE}" >&2
43 exit 1
44 ;;
45 esac
46done
47
Richard Barnette23ef4422012-11-02 13:46:20 -070048if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080049 read -s -p "Autotest DB password: " PASSWD
50 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070051 if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080052 echo "Empty passwords not allowed." >&2
53 exit 1
54 fi
55 read -s -p "Re-enter password: " PASSWD2
56 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070057 if [ "${PASSWD}" != "${PASSWD2}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -080058 echo "Passwords don't match." >&2
59 exit 1
60 fi
61fi
62
63if [ -z "${AUTOTEST_DIR}" ]; then
64 CANDIDATE=$(dirname "$(readlink -f "$0")" | egrep -o '(/[^/]+)*/files')
65 read -p "Enter autotest dir [${CANDIDATE}]: " AUTOTEST_DIR
66 if [ -z "${AUTOTEST_DIR}" ]; then
67 AUTOTEST_DIR="${CANDIDATE}"
68 fi
69fi
70
Aviv Keshet2c603d82013-05-24 10:31:59 -070071
72# Sanity check AUTOTEST_DIR. If it's null, or doesn't exist on the filesystem
73# then die.
74if [ -z "${AUTOTEST_DIR}" ]; then
75 echo "No AUTOTEST_DIR. Aborting script."
76 exit 1
77fi
78
79if [ ! -d "${AUTOTEST_DIR}" ]; then
80 echo "Directory " ${AUTOTEST_DIR} " does not exist. Aborting script."
81 exit 1
82fi
83
84
Chris Masone2aa55332011-12-30 14:41:07 -080085SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini"
86echo "Autotest supports local overrides of global configuration through a "
87echo "'shadow' configuration file. Setting one up for you now."
Chris Sosa18c70b32013-02-15 14:12:43 -080088CLOBBER=0
Chris Masone2aa55332011-12-30 14:41:07 -080089if [ -f ${SHADOW_CONFIG_PATH} ]; then
90 clobber=
91 while read -n 1 -p "Clobber existing shadow config? [Y/n]: " clobber; do
92 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070093 if [[ -z "${clobber}" || $(echo ${clobber} | egrep -qi 'y|n') -eq 0 ]]; then
Chris Masone2aa55332011-12-30 14:41:07 -080094 break
95 fi
96 echo "Please enter y or n."
97 done
Richard Barnette23ef4422012-11-02 13:46:20 -070098 if [[ "${clobber}" = 'n' || "${clobber}" = 'N' ]]; then
Chris Sosa18c70b32013-02-15 14:12:43 -080099 CLOBBER=1
Chris Masone2aa55332011-12-30 14:41:07 -0800100 echo "Refusing to clobber existing shadow_config.ini."
Chris Sosa18c70b32013-02-15 14:12:43 -0800101 else
102 echo "Clobbering existing shadow_config.ini."
Chris Masone2aa55332011-12-30 14:41:07 -0800103 fi
Chris Masone2aa55332011-12-30 14:41:07 -0800104fi
105
Aviv Keshet2c603d82013-05-24 10:31:59 -0700106CROS_CHECKOUT=$(readlink -f ${AUTOTEST_DIR}/../../../..)
107
Chris Sosa18c70b32013-02-15 14:12:43 -0800108# Create clean shadow config if we're replacing it/creating a new one.
109if [ $CLOBBER -eq 0 ]; then
110 cat > "${SHADOW_CONFIG_PATH}" <<EOF
Chris Masone2aa55332011-12-30 14:41:07 -0800111[AUTOTEST_WEB]
112host: localhost
113password: ${PASSWD}
Chris Masonee61dd812012-01-10 18:04:21 -0800114readonly_host: localhost
115readonly_user: chromeosqa-admin
116readonly_password: ${PASSWD}
Chris Masone2aa55332011-12-30 14:41:07 -0800117
118[SERVER]
119hostname: localhost
Chris Sosae4181242012-09-10 16:54:30 -0700120
121[SCHEDULER]
122drones: localhost
Aviv Keshet2c603d82013-05-24 10:31:59 -0700123
124[CROS]
125source_tree: ${CROS_CHECKOUT}
Chris Masone2aa55332011-12-30 14:41:07 -0800126EOF
Chris Sosa18c70b32013-02-15 14:12:43 -0800127 echo -e "Done!\n"
128fi
Chris Masone2aa55332011-12-30 14:41:07 -0800129
130echo "Installing needed Ubuntu packages..."
Scott Zawalskia5745e92013-05-09 10:42:27 -0400131PKG_LIST="mysql-server mysql-common libapache2-mod-wsgi python-mysqldb \
Chris Masone2aa55332011-12-30 14:41:07 -0800132gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \
133sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
Simran Basi3d182082012-09-07 10:15:25 -0700134python-crypto python-dev subversion build-essential python-setuptools \
135python-numpy python-scipy"
Chris Masone2aa55332011-12-30 14:41:07 -0800136
137if ! sudo apt-get install -y ${PKG_LIST}; then
138 echo "Could not install packages: $?"
139 exit 1
140fi
141echo -e "Done!\n"
142
Alex Millerda3ecf42012-07-25 11:51:13 -0700143echo "Setting up Database: chromeos_autotest_db in MySQL..."
Chris Masone2aa55332011-12-30 14:41:07 -0800144if mysql -u root -e ';' 2> /dev/null ; then
145 PASSWD_STRING=
146elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
147 PASSWD_STRING="-p${PASSWD}"
148else
149 PASSWD_STRING="-p"
150fi
151
Simran Basi79b1a7b2013-02-13 11:16:00 -0800152if ! mysqladmin ping ; then
153 sudo service mysql start
154fi
155
Alex Millerda3ecf42012-07-25 11:51:13 -0700156CLOBBERDB=
157EXISTING_DATABASE=$(mysql -u root "${PASSWD_STRING}" -e "SELECT SCHEMA_NAME \
158FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'chromeos_autotest_db'")
159if [ -n "${EXISTING_DATABASE}" ]; then
160 while read -n 1 -p "Clobber existing MySQL database? [y/N]: " CLOBBERDB; do
161 echo
Richard Barnette23ef4422012-11-02 13:46:20 -0700162 if [[ -z "${CLOBBERDB}" ||
163 $(echo ${CLOBBERDB} | egrep -qi 'y|n') -eq 0 ]]; then
Alex Millerda3ecf42012-07-25 11:51:13 -0700164 break
165 fi
166 echo "Please enter y or n."
167 done
168else
169 CLOBBERDB='y'
170fi
171
172SQL_COMMAND="drop database if exists chromeos_autotest_db; \
173create database chromeos_autotest_db; \
174grant all privileges on chromeos_autotest_db.* TO \
Simran Basi3d182082012-09-07 10:15:25 -0700175'chromeosqa-admin'@'localhost' identified by '${PASSWD}'; \
Alex Millerda3ecf42012-07-25 11:51:13 -0700176FLUSH PRIVILEGES;"
177
Alex Millerda3ecf42012-07-25 11:51:13 -0700178if [[ "${CLOBBERDB}" = 'y' || "${CLOBBERDB}" = 'Y' ]]; then
179 mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}"
180fi
Chris Masone2aa55332011-12-30 14:41:07 -0800181echo -e "Done!\n"
182
183AT_DIR=/usr/local/autotest
184echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
185sudo mkdir -p "${AT_DIR}"
186sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
187echo -e "Done!\n"
188
189EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
190if [ -n "${EXISTING_MOUNT}" ]; then
191 echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
192 echo "We won't update /etc/fstab, but you should have a line line this:"
193 echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
194else
195 echo -n "Adding aforementioned bind-mount to /etc/fstab..."
196 # Is there a better way to elevate privs and do a redirect?
197 sudo su -c \
198 "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
199 >> /etc/fstab"
200 echo -e "Done!\n"
201fi
202
203echo -n "Reticulating splines..."
Michael Liangb6a78f42014-07-11 15:05:30 -0700204
205if [ "${verbose}" = "TRUE" ]; then
206 "${AT_DIR}"/utils/build_externals.py
207 "${AT_DIR}"/utils/compile_gwt_clients.py -a
208else
209 "${AT_DIR}"/utils/build_externals.py &> /dev/null
210 "${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
211fi
212
Chris Masone2aa55332011-12-30 14:41:07 -0800213echo -e "Done!\n"
214
215echo "Populating autotest mysql DB..."
216"${AT_DIR}"/database/migrate.py sync
217"${AT_DIR}"/frontend/manage.py syncdb
218# You may have to run this twice.
219"${AT_DIR}"/frontend/manage.py syncdb
220"${AT_DIR}"/utils/test_importer.py
221echo -e "Done!\n"
222
223echo "Configuring apache to run the autotest web interface..."
Simran Basi508ee922013-11-18 13:43:40 -0800224if [ ! -d /etc/apache2/run ]; then
225 sudo mkdir /etc/apache2/run
226fi
Chris Masone2aa55332011-12-30 14:41:07 -0800227sudo ln -sf "${AT_DIR}"/apache/apache-conf \
228 /etc/apache2/sites-available/autotest-server
229# disable currently active default
230sudo a2dissite default
231# enable autotest server
232sudo a2ensite autotest-server
233# Enable rewrite module
234sudo a2enmod rewrite
Scott Zawalskia5745e92013-05-09 10:42:27 -0400235# Enable wsgi
236sudo a2enmod wsgi
237# enable version
238sudo a2enmod version
Chris Masone2aa55332011-12-30 14:41:07 -0800239# Setup permissions so that Apache web user can read the proper files.
240chmod -R o+r "${AT_DIR}"
Alex Miller5e7b2542013-02-13 14:24:04 -0800241find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x
Chris Masone2aa55332011-12-30 14:41:07 -0800242chmod o+x "${AT_DIR}"/tko/*.cgi
243# restart server
244sudo /etc/init.d/apache2 restart
245
246echo "Browse to http://localhost to see if Autotest is working."
247echo "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"