setup_dev_autotest.sh revision da3ecf43a262250caf3f75d5fbc32e57e06051c3
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.
6
7USAGE="Usage: setup_dev_autotest.sh [-p <password>] [-a </path/to/autotest>]"
8HELP="${USAGE}\n\n\
9Install and configure software needed to run autotest locally.\n\
10If you're just working on tests, you do not need to run this.\n\n\
11Options:\n\
12  -p Desired Autotest DB password\n\
13  -a Absolute path to autotest source tree.\n"
14
15AUTOTEST_DIR=
16PASSWD=
17while getopts ":p:a:h" opt; do
18  case $opt in
19    a)
20      AUTOTEST_DIR=$OPTARG
21      ;;
22    p)
23      PASSWD=$OPTARG
24      ;;
25    h)
26      echo -e "${HELP}" >&2
27      exit 0
28      ;;
29    \?)
30      echo "Invalid option: -$OPTARG" >&2
31      echo "${USAGE}" >&2
32      exit 1
33      ;;
34    :)
35      echo "Option -$OPTARG requires an argument." >&2
36      echo "${USAGE}" >&2
37      exit 1
38      ;;
39  esac
40done
41
42if [ -z ${PASSWD} ]; then
43  read -s -p "Autotest DB password: " PASSWD
44  echo
45  if [ -z ${PASSWD} ]; then
46    echo "Empty passwords not allowed." >&2
47    exit 1
48  fi
49  read -s -p "Re-enter password: " PASSWD2
50  echo
51  if [ ${PASSWD} != ${PASSWD2} ]; then
52    echo "Passwords don't match." >&2
53    exit 1
54  fi
55fi
56
57if [ -z "${AUTOTEST_DIR}" ]; then
58  CANDIDATE=$(dirname "$(readlink -f "$0")" | egrep -o '(/[^/]+)*/files')
59  read -p "Enter autotest dir [${CANDIDATE}]: " AUTOTEST_DIR
60  if [ -z "${AUTOTEST_DIR}" ]; then
61    AUTOTEST_DIR="${CANDIDATE}"
62  fi
63fi
64
65SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini"
66echo "Autotest supports local overrides of global configuration through a "
67echo "'shadow' configuration file.  Setting one up for you now."
68if [ -f ${SHADOW_CONFIG_PATH} ]; then
69  clobber=
70  while read -n 1 -p "Clobber existing shadow config? [Y/n]: " clobber; do
71    echo
72    if [[ -z ${clobber} || $(echo ${clobber} | egrep -qi 'y|n') -eq 0 ]]; then
73      break
74    fi
75    echo "Please enter y or n."
76  done
77  if [[ ${clobber} = 'n' || ${clobber} = 'N' ]]; then
78    echo "Refusing to clobber existing shadow_config.ini."
79    exit 0
80  fi
81  echo "Clobbering existing shadow_config.ini."
82fi
83
84cat > "${SHADOW_CONFIG_PATH}" <<EOF
85[AUTOTEST_WEB]
86host: localhost
87password: ${PASSWD}
88readonly_host: localhost
89readonly_user: chromeosqa-admin
90readonly_password: ${PASSWD}
91
92[SERVER]
93hostname: localhost
94EOF
95echo -e "Done!\n"
96
97echo "Installing needed Ubuntu packages..."
98PKG_LIST="mysql-server mysql-common libapache2-mod-python python-mysqldb \
99gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \
100sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
101python-crypto  python-dev subversion build-essential python-setuptools"
102
103if ! sudo apt-get install -y ${PKG_LIST}; then
104  echo "Could not install packages: $?"
105  exit 1
106fi
107echo -e "Done!\n"
108
109echo "Setting up Database: chromeos_autotest_db in MySQL..."
110if mysql -u root -e ';' 2> /dev/null ; then
111  PASSWD_STRING=
112elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
113  PASSWD_STRING="-p${PASSWD}"
114else
115  PASSWD_STRING="-p"
116fi
117
118CLOBBERDB=
119EXISTING_DATABASE=$(mysql -u root "${PASSWD_STRING}" -e "SELECT SCHEMA_NAME \
120FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'chromeos_autotest_db'")
121if [ -n "${EXISTING_DATABASE}" ]; then
122  while read -n 1 -p "Clobber existing MySQL database? [y/N]: " CLOBBERDB; do
123    echo
124    if [[ -z ${CLOBBERDB} || $(echo ${CLOBBERDB} | egrep -qi 'y|n') -eq 0 ]];
125    then
126      break
127    fi
128    echo "Please enter y or n."
129  done
130else
131  CLOBBERDB='y'
132fi
133
134SQL_COMMAND="drop database if exists chromeos_autotest_db; \
135create database chromeos_autotest_db; \
136grant all privileges on chromeos_autotest_db.* TO \
137'chromeosqa-admin'@'%' identified by '${PASSWD}'; \
138FLUSH PRIVILEGES;"
139
140set -e
141
142if [[ "${CLOBBERDB}" = 'y' || "${CLOBBERDB}" = 'Y' ]]; then
143  mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}"
144fi
145echo -e "Done!\n"
146
147AT_DIR=/usr/local/autotest
148echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
149sudo mkdir -p "${AT_DIR}"
150sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
151echo -e "Done!\n"
152
153EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
154if [ -n "${EXISTING_MOUNT}" ]; then
155  echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
156  echo "We won't update /etc/fstab, but you should have a line line this:"
157  echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
158else
159  echo -n "Adding aforementioned bind-mount to /etc/fstab..."
160  # Is there a better way to elevate privs and do a redirect?
161  sudo su -c \
162    "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
163    >> /etc/fstab"
164  echo -e "Done!\n"
165fi
166
167echo -n "Reticulating splines..."
168"${AT_DIR}"/utils/build_externals.py &> /dev/null
169"${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
170echo -e "Done!\n"
171
172echo "Populating autotest mysql DB..."
173"${AT_DIR}"/database/migrate.py sync
174"${AT_DIR}"/frontend/manage.py syncdb
175# You may have to run this twice.
176"${AT_DIR}"/frontend/manage.py syncdb
177"${AT_DIR}"/utils/test_importer.py
178echo -e "Done!\n"
179
180echo "Configuring apache to run the autotest web interface..."
181sudo ln -sf "${AT_DIR}"/apache/apache-conf \
182  /etc/apache2/sites-available/autotest-server
183# disable currently active default
184sudo a2dissite default
185# enable autotest server
186sudo a2ensite autotest-server
187# Enable rewrite module
188sudo a2enmod rewrite
189# Setup permissions so that Apache web user can read the proper files.
190chmod -R o+r "${AT_DIR}"
191find "${AT_DIR}"/ -type d | xargs chmod o+x
192chmod o+x "${AT_DIR}"/tko/*.cgi
193# restart server
194sudo /etc/init.d/apache2 restart
195
196echo "Browse to http://localhost to see if Autotest is working."
197echo "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"
198