blob: 67e6da85cb5d763a896fd9f6dfe6cd949a946c8e [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."
69if [ -f ${SHADOW_CONFIG_PATH} ]; then
70 clobber=
71 while read -n 1 -p "Clobber existing shadow config? [Y/n]: " clobber; do
72 echo
Richard Barnette23ef4422012-11-02 13:46:20 -070073 if [[ -z "${clobber}" || $(echo ${clobber} | egrep -qi 'y|n') -eq 0 ]]; then
Chris Masone2aa55332011-12-30 14:41:07 -080074 break
75 fi
76 echo "Please enter y or n."
77 done
Richard Barnette23ef4422012-11-02 13:46:20 -070078 if [[ "${clobber}" = 'n' || "${clobber}" = 'N' ]]; then
Chris Masone2aa55332011-12-30 14:41:07 -080079 echo "Refusing to clobber existing shadow_config.ini."
Chris Masone2aa55332011-12-30 14:41:07 -080080 fi
81 echo "Clobbering existing shadow_config.ini."
82fi
83
84cat > "${SHADOW_CONFIG_PATH}" <<EOF
85[AUTOTEST_WEB]
86host: localhost
87password: ${PASSWD}
Chris Masonee61dd812012-01-10 18:04:21 -080088readonly_host: localhost
89readonly_user: chromeosqa-admin
90readonly_password: ${PASSWD}
Chris Masone2aa55332011-12-30 14:41:07 -080091
92[SERVER]
93hostname: localhost
Chris Sosae4181242012-09-10 16:54:30 -070094
95[SCHEDULER]
96drones: localhost
Chris Masone2aa55332011-12-30 14:41:07 -080097EOF
98echo -e "Done!\n"
99
100echo "Installing needed Ubuntu packages..."
101PKG_LIST="mysql-server mysql-common libapache2-mod-python python-mysqldb \
102gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \
103sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
Simran Basi3d182082012-09-07 10:15:25 -0700104python-crypto python-dev subversion build-essential python-setuptools \
105python-numpy python-scipy"
Chris Masone2aa55332011-12-30 14:41:07 -0800106
107if ! sudo apt-get install -y ${PKG_LIST}; then
108 echo "Could not install packages: $?"
109 exit 1
110fi
111echo -e "Done!\n"
112
Alex Millerda3ecf42012-07-25 11:51:13 -0700113echo "Setting up Database: chromeos_autotest_db in MySQL..."
Chris Masone2aa55332011-12-30 14:41:07 -0800114if mysql -u root -e ';' 2> /dev/null ; then
115 PASSWD_STRING=
116elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
117 PASSWD_STRING="-p${PASSWD}"
118else
119 PASSWD_STRING="-p"
120fi
121
Simran Basi79b1a7b2013-02-13 11:16:00 -0800122if ! mysqladmin ping ; then
123 sudo service mysql start
124fi
125
Alex Millerda3ecf42012-07-25 11:51:13 -0700126CLOBBERDB=
127EXISTING_DATABASE=$(mysql -u root "${PASSWD_STRING}" -e "SELECT SCHEMA_NAME \
128FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'chromeos_autotest_db'")
129if [ -n "${EXISTING_DATABASE}" ]; then
130 while read -n 1 -p "Clobber existing MySQL database? [y/N]: " CLOBBERDB; do
131 echo
Richard Barnette23ef4422012-11-02 13:46:20 -0700132 if [[ -z "${CLOBBERDB}" ||
133 $(echo ${CLOBBERDB} | egrep -qi 'y|n') -eq 0 ]]; then
Alex Millerda3ecf42012-07-25 11:51:13 -0700134 break
135 fi
136 echo "Please enter y or n."
137 done
138else
139 CLOBBERDB='y'
140fi
141
142SQL_COMMAND="drop database if exists chromeos_autotest_db; \
143create database chromeos_autotest_db; \
144grant all privileges on chromeos_autotest_db.* TO \
Simran Basi3d182082012-09-07 10:15:25 -0700145'chromeosqa-admin'@'localhost' identified by '${PASSWD}'; \
Alex Millerda3ecf42012-07-25 11:51:13 -0700146FLUSH PRIVILEGES;"
147
Alex Millerda3ecf42012-07-25 11:51:13 -0700148if [[ "${CLOBBERDB}" = 'y' || "${CLOBBERDB}" = 'Y' ]]; then
149 mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}"
150fi
Chris Masone2aa55332011-12-30 14:41:07 -0800151echo -e "Done!\n"
152
153AT_DIR=/usr/local/autotest
154echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
155sudo mkdir -p "${AT_DIR}"
156sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
157echo -e "Done!\n"
158
159EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
160if [ -n "${EXISTING_MOUNT}" ]; then
161 echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
162 echo "We won't update /etc/fstab, but you should have a line line this:"
163 echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
164else
165 echo -n "Adding aforementioned bind-mount to /etc/fstab..."
166 # Is there a better way to elevate privs and do a redirect?
167 sudo su -c \
168 "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
169 >> /etc/fstab"
170 echo -e "Done!\n"
171fi
172
173echo -n "Reticulating splines..."
174"${AT_DIR}"/utils/build_externals.py &> /dev/null
175"${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
176echo -e "Done!\n"
177
178echo "Populating autotest mysql DB..."
179"${AT_DIR}"/database/migrate.py sync
180"${AT_DIR}"/frontend/manage.py syncdb
181# You may have to run this twice.
182"${AT_DIR}"/frontend/manage.py syncdb
183"${AT_DIR}"/utils/test_importer.py
184echo -e "Done!\n"
185
186echo "Configuring apache to run the autotest web interface..."
187sudo ln -sf "${AT_DIR}"/apache/apache-conf \
188 /etc/apache2/sites-available/autotest-server
189# disable currently active default
190sudo a2dissite default
191# enable autotest server
192sudo a2ensite autotest-server
193# Enable rewrite module
194sudo a2enmod rewrite
beepse1b236d2013-01-11 11:46:20 -0800195# Enable mod-python
196sudo a2enmod python
Chris Masone2aa55332011-12-30 14:41:07 -0800197# Setup permissions so that Apache web user can read the proper files.
198chmod -R o+r "${AT_DIR}"
199find "${AT_DIR}"/ -type d | xargs chmod o+x
200chmod o+x "${AT_DIR}"/tko/*.cgi
201# restart server
202sudo /etc/init.d/apache2 restart
203
204echo "Browse to http://localhost to see if Autotest is working."
205echo "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"