blob: f3b39d9ace259fdcdf5ff6498c067c9ae9850911 [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
66SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini"
67echo "Autotest supports local overrides of global configuration through a "
68echo "'shadow' configuration file. Setting one up for you now."
Chris Sosa18c70b32013-02-15 14:12:43 -080069CLOBBER=0
Chris Masone2aa55332011-12-30 14:41:07 -080070if [ -f ${SHADOW_CONFIG_PATH} ]; then
71 clobber=
72 while read -n 1 -p "Clobber existing shadow config? [Y/n]: " clobber; do
73 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070074 if [[ -z "${clobber}" || $(echo ${clobber} | egrep -qi 'y|n') -eq 0 ]]; then
Chris Masone2aa55332011-12-30 14:41:07 -080075 break
76 fi
77 echo "Please enter y or n."
78 done
Richard Barnette23ef4422012-11-02 13:46:20 -070079 if [[ "${clobber}" = 'n' || "${clobber}" = 'N' ]]; then
Chris Sosa18c70b32013-02-15 14:12:43 -080080 CLOBBER=1
Chris Masone2aa55332011-12-30 14:41:07 -080081 echo "Refusing to clobber existing shadow_config.ini."
Chris Sosa18c70b32013-02-15 14:12:43 -080082 else
83 echo "Clobbering existing shadow_config.ini."
Chris Masone2aa55332011-12-30 14:41:07 -080084 fi
Chris Masone2aa55332011-12-30 14:41:07 -080085fi
86
Chris Sosa18c70b32013-02-15 14:12:43 -080087# Create clean shadow config if we're replacing it/creating a new one.
88if [ $CLOBBER -eq 0 ]; then
89 cat > "${SHADOW_CONFIG_PATH}" <<EOF
Chris Masone2aa55332011-12-30 14:41:07 -080090[AUTOTEST_WEB]
91host: localhost
92password: ${PASSWD}
Chris Masonee61dd812012-01-10 18:04:21 -080093readonly_host: localhost
94readonly_user: chromeosqa-admin
95readonly_password: ${PASSWD}
Chris Masone2aa55332011-12-30 14:41:07 -080096
97[SERVER]
98hostname: localhost
Chris Sosae4181242012-09-10 16:54:30 -070099
100[SCHEDULER]
101drones: localhost
Chris Masone2aa55332011-12-30 14:41:07 -0800102EOF
Chris Sosa18c70b32013-02-15 14:12:43 -0800103 echo -e "Done!\n"
104fi
Chris Masone2aa55332011-12-30 14:41:07 -0800105
106echo "Installing needed Ubuntu packages..."
107PKG_LIST="mysql-server mysql-common libapache2-mod-python python-mysqldb \
108gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \
109sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
Simran Basi3d182082012-09-07 10:15:25 -0700110python-crypto python-dev subversion build-essential python-setuptools \
111python-numpy python-scipy"
Chris Masone2aa55332011-12-30 14:41:07 -0800112
113if ! sudo apt-get install -y ${PKG_LIST}; then
114 echo "Could not install packages: $?"
115 exit 1
116fi
117echo -e "Done!\n"
118
Alex Millerda3ecf42012-07-25 11:51:13 -0700119echo "Setting up Database: chromeos_autotest_db in MySQL..."
Chris Masone2aa55332011-12-30 14:41:07 -0800120if mysql -u root -e ';' 2> /dev/null ; then
121 PASSWD_STRING=
122elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
123 PASSWD_STRING="-p${PASSWD}"
124else
125 PASSWD_STRING="-p"
126fi
127
Simran Basi79b1a7b2013-02-13 11:16:00 -0800128if ! mysqladmin ping ; then
129 sudo service mysql start
130fi
131
Alex Millerda3ecf42012-07-25 11:51:13 -0700132CLOBBERDB=
133EXISTING_DATABASE=$(mysql -u root "${PASSWD_STRING}" -e "SELECT SCHEMA_NAME \
134FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'chromeos_autotest_db'")
135if [ -n "${EXISTING_DATABASE}" ]; then
136 while read -n 1 -p "Clobber existing MySQL database? [y/N]: " CLOBBERDB; do
137 echo
Richard Barnette23ef4422012-11-02 13:46:20 -0700138 if [[ -z "${CLOBBERDB}" ||
139 $(echo ${CLOBBERDB} | egrep -qi 'y|n') -eq 0 ]]; then
Alex Millerda3ecf42012-07-25 11:51:13 -0700140 break
141 fi
142 echo "Please enter y or n."
143 done
144else
145 CLOBBERDB='y'
146fi
147
148SQL_COMMAND="drop database if exists chromeos_autotest_db; \
149create database chromeos_autotest_db; \
150grant all privileges on chromeos_autotest_db.* TO \
Simran Basi3d182082012-09-07 10:15:25 -0700151'chromeosqa-admin'@'localhost' identified by '${PASSWD}'; \
Alex Millerda3ecf42012-07-25 11:51:13 -0700152FLUSH PRIVILEGES;"
153
Alex Millerda3ecf42012-07-25 11:51:13 -0700154if [[ "${CLOBBERDB}" = 'y' || "${CLOBBERDB}" = 'Y' ]]; then
155 mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}"
156fi
Chris Masone2aa55332011-12-30 14:41:07 -0800157echo -e "Done!\n"
158
159AT_DIR=/usr/local/autotest
160echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
161sudo mkdir -p "${AT_DIR}"
162sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
163echo -e "Done!\n"
164
165EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
166if [ -n "${EXISTING_MOUNT}" ]; then
167 echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
168 echo "We won't update /etc/fstab, but you should have a line line this:"
169 echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
170else
171 echo -n "Adding aforementioned bind-mount to /etc/fstab..."
172 # Is there a better way to elevate privs and do a redirect?
173 sudo su -c \
174 "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
175 >> /etc/fstab"
176 echo -e "Done!\n"
177fi
178
179echo -n "Reticulating splines..."
180"${AT_DIR}"/utils/build_externals.py &> /dev/null
181"${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
182echo -e "Done!\n"
183
184echo "Populating autotest mysql DB..."
185"${AT_DIR}"/database/migrate.py sync
186"${AT_DIR}"/frontend/manage.py syncdb
187# You may have to run this twice.
188"${AT_DIR}"/frontend/manage.py syncdb
189"${AT_DIR}"/utils/test_importer.py
190echo -e "Done!\n"
191
192echo "Configuring apache to run the autotest web interface..."
193sudo ln -sf "${AT_DIR}"/apache/apache-conf \
194 /etc/apache2/sites-available/autotest-server
195# disable currently active default
196sudo a2dissite default
197# enable autotest server
198sudo a2ensite autotest-server
199# Enable rewrite module
200sudo a2enmod rewrite
beepse1b236d2013-01-11 11:46:20 -0800201# Enable mod-python
202sudo a2enmod python
Chris Masone2aa55332011-12-30 14:41:07 -0800203# Setup permissions so that Apache web user can read the proper files.
204chmod -R o+r "${AT_DIR}"
Alex Miller5e7b2542013-02-13 14:24:04 -0800205find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x
Chris Masone2aa55332011-12-30 14:41:07 -0800206chmod o+x "${AT_DIR}"/tko/*.cgi
207# restart server
208sudo /etc/init.d/apache2 restart
209
210echo "Browse to http://localhost to see if Autotest is working."
211echo "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"