setup_dev_autotest.sh revision 1c53ae736c604b93a46cf21d76eaab7d77a0c384
1b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG#!/bin/bash
2b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG#
3b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG# Use of this source code is governed by a BSD-style license that can be
5b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG# found in the LICENSE file.
6b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGset -e
7b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG
8b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGUSAGE="Usage: setup_dev_autotest.sh [-p <password>] [-a </path/to/autotest>]"
9b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGHELP="${USAGE}\n\n\
10b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGInstall and configure software needed to run autotest locally.\n\
11b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGIf you're just working on tests, you do not need to run this.\n\n\
12b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGOptions:\n\
13b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  -p Desired Autotest DB password\n\
14b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  -a Absolute path to autotest source tree.\n
15b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  -v Show info logging from build_externals.py and compile_gwt_clients.py \n
16b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  -n Non-interactive mode, doesn't ask for any user input.
17b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG     Requires -p and -a to be set."
18b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG
19b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGfunction get_y_or_n_interactive {
20b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    local ret
21b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    while true; do
22b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG        read -p "$2" yn
23b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG        case $yn in
24b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG            [Yy]* ) ret="y"; break;;
25b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG            [Nn]* ) ret="n"; break;;
26b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG            * ) echo "Please enter y or n.";;
27b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG        esac
28b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    done
29b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    eval $1="'$ret'"
30b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG}
31b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG
32b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGfunction get_y_or_n {
33b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  local ret=$3
34b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  if [ "${noninteractive}" = "FALSE" ]; then
35b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    get_y_or_n_interactive sub "$2"
36b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    ret=$sub
37b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  fi
38b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  eval $1="'$ret'"
39b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG}
40b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG
41b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGAUTOTEST_DIR=
42b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGPASSWD=
43b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGverbose="FALSE"
44b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGnoninteractive="FALSE"
45b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGwhile getopts ":p:a:nvh" opt; do
46b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  case $opt in
47b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    a)
48b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      AUTOTEST_DIR=$OPTARG
49b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      ;;
50b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    p)
51b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      PASSWD=$OPTARG
52b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      ;;
53b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    v)
54b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      verbose="TRUE"
55b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      ;;
56b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    n)
57b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      noninteractive="TRUE"
58b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      ;;
59b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    h)
60b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      echo -e "${HELP}" >&2
61b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      exit 0
62b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      ;;
63b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    \?)
64b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      echo "Invalid option: -$OPTARG" >&2
65b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      echo "${USAGE}" >&2
66b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      exit 1
67b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      ;;
68b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    :)
69b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      echo "Option -$OPTARG requires an argument." >&2
70b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      echo "${USAGE}" >&2
71b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      exit 1
72b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG      ;;
73b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  esac
74b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGdone
75b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG
76b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG
77b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGif [ "${noninteractive}" = "TRUE" ]; then
78b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  if [ -z "${AUTOTEST_DIR}" ]; then
79b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    echo "-a must be specified in non-interactive mode." >&2
80b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    exit 1
81b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  fi
82b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  if [ -z "${PASSWD}" ]; then
83b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    echo "-p must be specified in non-interactive mode." >&2
84b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG    exit 1
85b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  fi
86b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGfi
87b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG
88b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG
89b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarGif [ -z "${PASSWD}" ]; then
90b0b195df6c4343219e339ccb37978e8432a5f871Tony-LunarG  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
177echo "Setting up Database: chromeos_autotest_db in MySQL..."
178if mysql -u root -e ';' 2> /dev/null ; then
179  PASSWD_STRING=
180elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
181  PASSWD_STRING="-p${PASSWD}"
182else
183  PASSWD_STRING="-p"
184fi
185
186if ! mysqladmin ping ; then
187  sudo service mysql start
188fi
189
190CLOBBERDB='y'
191EXISTING_DATABASE=$(mysql -u root "${PASSWD_STRING}" -e "SELECT SCHEMA_NAME \
192FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'chromeos_autotest_db'")
193
194if [ -n "${EXISTING_DATABASE}" ]; then
195  get_y_or_n CLOBBERDB "Clobber existing shadow config? [Y/n]: " "n"
196fi
197
198SQL_COMMAND="drop database if exists chromeos_autotest_db; \
199create database chromeos_autotest_db; \
200grant all privileges on chromeos_autotest_db.* TO \
201'chromeosqa-admin'@'localhost' identified by '${PASSWD}'; \
202FLUSH PRIVILEGES;"
203
204if [[ "${CLOBBERDB}" = 'y' ]]; then
205  mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}"
206fi
207echo -e "Done!\n"
208
209AT_DIR=/usr/local/autotest
210echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
211sudo mkdir -p "${AT_DIR}"
212sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
213echo -e "Done!\n"
214
215EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
216if [ -n "${EXISTING_MOUNT}" ]; then
217  echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
218  echo "We won't update /etc/fstab, but you should have a line line this:"
219  echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
220else
221  echo -n "Adding aforementioned bind-mount to /etc/fstab..."
222  # Is there a better way to elevate privs and do a redirect?
223  sudo su -c \
224    "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
225    >> /etc/fstab"
226  echo -e "Done!\n"
227fi
228
229echo -n "Reticulating splines..."
230
231if [ "${verbose}" = "TRUE" ]; then
232  "${AT_DIR}"/utils/build_externals.py
233  "${AT_DIR}"/utils/compile_gwt_clients.py -a
234else
235  "${AT_DIR}"/utils/build_externals.py &> /dev/null
236  "${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
237fi
238
239echo -e "Done!\n"
240
241echo "Populating autotest mysql DB..."
242"${AT_DIR}"/database/migrate.py sync -f
243"${AT_DIR}"/frontend/manage.py syncdb --noinput
244# You may have to run this twice.
245"${AT_DIR}"/frontend/manage.py syncdb --noinput
246"${AT_DIR}"/utils/test_importer.py
247echo -e "Done!\n"
248
249echo "Configuring apache to run the autotest web interface..."
250if [ ! -d /etc/apache2/run ]; then
251  sudo mkdir /etc/apache2/run
252fi
253sudo ln -sf "${AT_DIR}"/apache/apache-conf \
254  /etc/apache2/sites-available/autotest-server
255# disable currently active default
256sudo a2dissite default
257# enable autotest server
258sudo a2ensite autotest-server
259# Enable rewrite module
260sudo a2enmod rewrite
261# Enable wsgi
262sudo a2enmod wsgi
263# enable version
264sudo a2enmod version
265# Setup permissions so that Apache web user can read the proper files.
266chmod -R o+r "${AT_DIR}"
267find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x
268chmod o+x "${AT_DIR}"/tko/*.cgi
269# restart server
270sudo /etc/init.d/apache2 restart
271
272echo "Browse to http://localhost to see if Autotest is working."
273echo "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"
274