setup_dev_autotest.sh revision 18808492a45be908fc4dda104c9425e6c7b3d3d5
1#!/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.
6set -e
7
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  -v Show info logging from build_externals.py and compile_gwt_clients.py \n
16  -n Non-interactive mode, doesn't ask for any user input.
17     Requires -p and -a to be set."
18
19function get_y_or_n_interactive {
20    local ret
21    while true; do
22        read -p "$2" yn
23        case $yn in
24            [Yy]* ) ret="y"; break;;
25            [Nn]* ) ret="n"; break;;
26            * ) echo "Please enter y or n.";;
27        esac
28    done
29    eval $1="'$ret'"
30}
31
32function get_y_or_n {
33  local ret=$3
34  if [ "${noninteractive}" = "FALSE" ]; then
35    get_y_or_n_interactive sub "$2"
36    ret=$sub
37  fi
38  eval $1="'$ret'"
39}
40
41AUTOTEST_DIR=
42PASSWD=
43verbose="FALSE"
44noninteractive="FALSE"
45while getopts ":p:a:nvh" opt; do
46  case $opt in
47    a)
48      AUTOTEST_DIR=$OPTARG
49      ;;
50    p)
51      PASSWD=$OPTARG
52      ;;
53    v)
54      verbose="TRUE"
55      ;;
56    n)
57      noninteractive="TRUE"
58      ;;
59    h)
60      echo -e "${HELP}" >&2
61      exit 0
62      ;;
63    \?)
64      echo "Invalid option: -$OPTARG" >&2
65      echo "${USAGE}" >&2
66      exit 1
67      ;;
68    :)
69      echo "Option -$OPTARG requires an argument." >&2
70      echo "${USAGE}" >&2
71      exit 1
72      ;;
73  esac
74done
75
76
77if [ "${noninteractive}" = "TRUE" ]; then
78  if [ -z "${AUTOTEST_DIR}" ]; then
79    echo "-a must be specified in non-interactive mode." >&2
80    exit 1
81  fi
82  if [ -z "${PASSWD}" ]; then
83    echo "-p must be specified in non-interactive mode." >&2
84    exit 1
85  fi
86fi
87
88
89if [ -z "${PASSWD}" ]; then
90  read -s -p "Autotest DB password: " PASSWD
91  echo
92  if [ -z "${PASSWD}" ]; then
93    echo "Empty passwords not allowed." >&2
94    exit 1
95  fi
96  read -s -p "Re-enter password: " PASSWD2
97  echo
98  if [ "${PASSWD}" != "${PASSWD2}" ]; then
99    echo "Passwords don't match." >&2
100    exit 1
101  fi
102fi
103
104if [ -z "${AUTOTEST_DIR}" ]; then
105  CANDIDATE=$(dirname "$(readlink -f "$0")" | egrep -o '(/[^/]+)*/files')
106  read -p "Enter autotest dir [${CANDIDATE}]: " AUTOTEST_DIR
107  if [ -z "${AUTOTEST_DIR}" ]; then
108    AUTOTEST_DIR="${CANDIDATE}"
109  fi
110fi
111
112
113# Sanity check AUTOTEST_DIR. If it's null, or doesn't exist on the filesystem
114# then die.
115if [ -z "${AUTOTEST_DIR}" ]; then
116  echo "No AUTOTEST_DIR. Aborting script."
117  exit 1
118fi
119
120if [ ! -d "${AUTOTEST_DIR}" ]; then
121  echo "Directory " ${AUTOTEST_DIR} " does not exist. Aborting script."
122  exit 1
123fi
124
125
126SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini"
127echo "Autotest supports local overrides of global configuration through a "
128echo "'shadow' configuration file.  Setting one up for you now."
129CLOBBER=0
130if [ -f ${SHADOW_CONFIG_PATH} ]; then
131  get_y_or_n clobber "Clobber existing shadow config? [Y/n]: " "n"
132  if [[ "${clobber}" = 'n' ]]; then
133    CLOBBER=1
134    echo "Refusing to clobber existing shadow_config.ini."
135  else
136    echo "Clobbering existing shadow_config.ini."
137  fi
138fi
139
140CROS_CHECKOUT=$(readlink -f ${AUTOTEST_DIR}/../../../..)
141
142# Create clean shadow config if we're replacing it/creating a new one.
143if [ $CLOBBER -eq 0 ]; then
144  cat > "${SHADOW_CONFIG_PATH}" <<EOF
145[AUTOTEST_WEB]
146host: localhost
147password: ${PASSWD}
148readonly_host: localhost
149readonly_user: chromeosqa-admin
150readonly_password: ${PASSWD}
151
152[SERVER]
153hostname: localhost
154
155[SCHEDULER]
156drones: localhost
157
158[CROS]
159source_tree: ${CROS_CHECKOUT}
160EOF
161  echo -e "Done!\n"
162fi
163
164echo "Installing needed Ubuntu packages..."
165PKG_LIST="mysql-server mysql-common libapache2-mod-wsgi python-mysqldb \
166gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \
167sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
168python-crypto  python-dev subversion build-essential python-setuptools \
169python-numpy python-scipy"
170
171if ! sudo apt-get install -y ${PKG_LIST}; then
172  echo "Could not install packages: $?"
173  exit 1
174fi
175echo -e "Done!\n"
176
177# Check if database exists, clobber existing database with user consent.
178#
179# Arguments: Name of the database
180check_database()
181{
182  local db_name=$1
183  echo "Setting up Database: $db_name in MySQL..."
184  if mysql -u root -e ';' 2> /dev/null ; then
185    PASSWD_STRING=
186  elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
187    PASSWD_STRING="-p${PASSWD}"
188  else
189    PASSWD_STRING="-p"
190  fi
191
192  if ! mysqladmin ping ; then
193    sudo service mysql start
194  fi
195
196  local clobberdb='y'
197  local existing_database=$(mysql -u root "${PASSWD_STRING}" -e "SELECT \
198  SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db_name'")
199
200  if [ -n "${existing_database}" ]; then
201    get_y_or_n clobberdb "Clobber existing MySQL database? [Y/n]: " "n"
202  fi
203
204  local sql_command="drop database if exists $db_name; \
205  create database $db_name; \
206  grant all privileges on $db_name.* TO \
207  'chromeosqa-admin'@'localhost' identified by '${PASSWD}'; \
208  FLUSH PRIVILEGES;"
209
210  if [[ "${clobberdb}" = 'y' ]]; then
211    mysql -u root "${PASSWD_STRING}" -e "${sql_command}"
212  fi
213  echo -e "Done!\n"
214}
215
216check_database 'chromeos_autotest_db'
217check_database 'chromeos_lab_servers'
218
219AT_DIR=/usr/local/autotest
220echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
221sudo mkdir -p "${AT_DIR}"
222sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
223echo -e "Done!\n"
224
225EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
226if [ -n "${EXISTING_MOUNT}" ]; then
227  echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
228  echo "We won't update /etc/fstab, but you should have a line line this:"
229  echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
230else
231  echo -n "Adding aforementioned bind-mount to /etc/fstab..."
232  # Is there a better way to elevate privs and do a redirect?
233  sudo su -c \
234    "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
235    >> /etc/fstab"
236  echo -e "Done!\n"
237fi
238
239echo -n "Reticulating splines..."
240
241if [ "${verbose}" = "TRUE" ]; then
242  "${AT_DIR}"/utils/build_externals.py
243  "${AT_DIR}"/utils/compile_gwt_clients.py -a
244else
245  "${AT_DIR}"/utils/build_externals.py &> /dev/null
246  "${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
247fi
248
249echo -e "Done!\n"
250
251echo "Populating autotest mysql DB..."
252"${AT_DIR}"/database/migrate.py sync -f
253"${AT_DIR}"/frontend/manage.py syncdb --noinput
254# You may have to run this twice.
255"${AT_DIR}"/frontend/manage.py syncdb --noinput
256"${AT_DIR}"/utils/test_importer.py
257echo -e "Done!\n"
258
259echo "Initializing chromeos_lab_servers mysql DB..."
260"${AT_DIR}"/database/migrate.py sync -f -d AUTOTEST_SERVER_DB
261echo -e "Done!\n"
262
263echo "Configuring apache to run the autotest web interface..."
264if [ ! -d /etc/apache2/run ]; then
265  sudo mkdir /etc/apache2/run
266fi
267sudo ln -sf "${AT_DIR}"/apache/apache-conf \
268  /etc/apache2/sites-available/autotest-server.conf
269# disable currently active default
270sudo a2dissite default
271# enable autotest server
272sudo a2ensite autotest-server.conf
273# Enable rewrite module
274sudo a2enmod rewrite
275# Enable wsgi
276sudo a2enmod wsgi
277# enable version
278sudo a2enmod version
279# enable headers
280sudo a2enmod headers
281# Setup permissions so that Apache web user can read the proper files.
282chmod -R o+r "${AT_DIR}"
283find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x
284chmod o+x "${AT_DIR}"/tko/*.cgi
285# restart server
286sudo /etc/init.d/apache2 restart
287
288echo "Browse to http://localhost to see if Autotest is working."
289echo "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"
290