Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 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. |
Simran Basi | 79b1a7b | 2013-02-13 11:16:00 -0800 | [diff] [blame] | 6 | set -e |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 7 | |
| 8 | USAGE="Usage: setup_dev_autotest.sh [-p <password>] [-a </path/to/autotest>]" |
| 9 | HELP="${USAGE}\n\n\ |
| 10 | Install and configure software needed to run autotest locally.\n\ |
| 11 | If you're just working on tests, you do not need to run this.\n\n\ |
| 12 | Options:\n\ |
| 13 | -p Desired Autotest DB password\n\ |
| 14 | -a Absolute path to autotest source tree.\n" |
| 15 | |
| 16 | AUTOTEST_DIR= |
| 17 | PASSWD= |
| 18 | while 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 |
| 41 | done |
| 42 | |
Richard Barnette | 23ef442 | 2012-11-02 13:46:20 -0700 | [diff] [blame] | 43 | if [ -z "${PASSWD}" ]; then |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 44 | read -s -p "Autotest DB password: " PASSWD |
| 45 | echo |
Richard Barnette | 23ef442 | 2012-11-02 13:46:20 -0700 | [diff] [blame] | 46 | if [ -z "${PASSWD}" ]; then |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 47 | echo "Empty passwords not allowed." >&2 |
| 48 | exit 1 |
| 49 | fi |
| 50 | read -s -p "Re-enter password: " PASSWD2 |
| 51 | echo |
Richard Barnette | 23ef442 | 2012-11-02 13:46:20 -0700 | [diff] [blame] | 52 | if [ "${PASSWD}" != "${PASSWD2}" ]; then |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 53 | echo "Passwords don't match." >&2 |
| 54 | exit 1 |
| 55 | fi |
| 56 | fi |
| 57 | |
| 58 | if [ -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 |
| 64 | fi |
| 65 | |
Aviv Keshet | 2c603d8 | 2013-05-24 10:31:59 -0700 | [diff] [blame] | 66 | |
| 67 | # Sanity check AUTOTEST_DIR. If it's null, or doesn't exist on the filesystem |
| 68 | # then die. |
| 69 | if [ -z "${AUTOTEST_DIR}" ]; then |
| 70 | echo "No AUTOTEST_DIR. Aborting script." |
| 71 | exit 1 |
| 72 | fi |
| 73 | |
| 74 | if [ ! -d "${AUTOTEST_DIR}" ]; then |
| 75 | echo "Directory " ${AUTOTEST_DIR} " does not exist. Aborting script." |
| 76 | exit 1 |
| 77 | fi |
| 78 | |
| 79 | |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 80 | SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini" |
| 81 | echo "Autotest supports local overrides of global configuration through a " |
| 82 | echo "'shadow' configuration file. Setting one up for you now." |
Chris Sosa | 18c70b3 | 2013-02-15 14:12:43 -0800 | [diff] [blame] | 83 | CLOBBER=0 |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 84 | if [ -f ${SHADOW_CONFIG_PATH} ]; then |
| 85 | clobber= |
| 86 | while read -n 1 -p "Clobber existing shadow config? [Y/n]: " clobber; do |
| 87 | echo |
Richard Barnette | 23ef442 | 2012-11-02 13:46:20 -0700 | [diff] [blame] | 88 | if [[ -z "${clobber}" || $(echo ${clobber} | egrep -qi 'y|n') -eq 0 ]]; then |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 89 | break |
| 90 | fi |
| 91 | echo "Please enter y or n." |
| 92 | done |
Richard Barnette | 23ef442 | 2012-11-02 13:46:20 -0700 | [diff] [blame] | 93 | if [[ "${clobber}" = 'n' || "${clobber}" = 'N' ]]; then |
Chris Sosa | 18c70b3 | 2013-02-15 14:12:43 -0800 | [diff] [blame] | 94 | CLOBBER=1 |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 95 | echo "Refusing to clobber existing shadow_config.ini." |
Chris Sosa | 18c70b3 | 2013-02-15 14:12:43 -0800 | [diff] [blame] | 96 | else |
| 97 | echo "Clobbering existing shadow_config.ini." |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 98 | fi |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 99 | fi |
| 100 | |
Aviv Keshet | 2c603d8 | 2013-05-24 10:31:59 -0700 | [diff] [blame] | 101 | CROS_CHECKOUT=$(readlink -f ${AUTOTEST_DIR}/../../../..) |
| 102 | |
Chris Sosa | 18c70b3 | 2013-02-15 14:12:43 -0800 | [diff] [blame] | 103 | # Create clean shadow config if we're replacing it/creating a new one. |
| 104 | if [ $CLOBBER -eq 0 ]; then |
| 105 | cat > "${SHADOW_CONFIG_PATH}" <<EOF |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 106 | [AUTOTEST_WEB] |
| 107 | host: localhost |
| 108 | password: ${PASSWD} |
Chris Masone | e61dd81 | 2012-01-10 18:04:21 -0800 | [diff] [blame] | 109 | readonly_host: localhost |
| 110 | readonly_user: chromeosqa-admin |
| 111 | readonly_password: ${PASSWD} |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 112 | |
| 113 | [SERVER] |
| 114 | hostname: localhost |
Chris Sosa | e418124 | 2012-09-10 16:54:30 -0700 | [diff] [blame] | 115 | |
| 116 | [SCHEDULER] |
| 117 | drones: localhost |
Aviv Keshet | 2c603d8 | 2013-05-24 10:31:59 -0700 | [diff] [blame] | 118 | |
| 119 | [CROS] |
| 120 | source_tree: ${CROS_CHECKOUT} |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 121 | EOF |
Chris Sosa | 18c70b3 | 2013-02-15 14:12:43 -0800 | [diff] [blame] | 122 | echo -e "Done!\n" |
| 123 | fi |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 124 | |
| 125 | echo "Installing needed Ubuntu packages..." |
Scott Zawalski | a5745e9 | 2013-05-09 10:42:27 -0400 | [diff] [blame] | 126 | PKG_LIST="mysql-server mysql-common libapache2-mod-wsgi python-mysqldb \ |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 127 | gnuplot apache2-mpm-prefork unzip python-imaging libpng12-dev libfreetype6-dev \ |
| 128 | sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \ |
Simran Basi | 3d18208 | 2012-09-07 10:15:25 -0700 | [diff] [blame] | 129 | python-crypto python-dev subversion build-essential python-setuptools \ |
| 130 | python-numpy python-scipy" |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 131 | |
| 132 | if ! sudo apt-get install -y ${PKG_LIST}; then |
| 133 | echo "Could not install packages: $?" |
| 134 | exit 1 |
| 135 | fi |
| 136 | echo -e "Done!\n" |
| 137 | |
Alex Miller | da3ecf4 | 2012-07-25 11:51:13 -0700 | [diff] [blame] | 138 | echo "Setting up Database: chromeos_autotest_db in MySQL..." |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 139 | if mysql -u root -e ';' 2> /dev/null ; then |
| 140 | PASSWD_STRING= |
| 141 | elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then |
| 142 | PASSWD_STRING="-p${PASSWD}" |
| 143 | else |
| 144 | PASSWD_STRING="-p" |
| 145 | fi |
| 146 | |
Simran Basi | 79b1a7b | 2013-02-13 11:16:00 -0800 | [diff] [blame] | 147 | if ! mysqladmin ping ; then |
| 148 | sudo service mysql start |
| 149 | fi |
| 150 | |
Alex Miller | da3ecf4 | 2012-07-25 11:51:13 -0700 | [diff] [blame] | 151 | CLOBBERDB= |
| 152 | EXISTING_DATABASE=$(mysql -u root "${PASSWD_STRING}" -e "SELECT SCHEMA_NAME \ |
| 153 | FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'chromeos_autotest_db'") |
| 154 | if [ -n "${EXISTING_DATABASE}" ]; then |
| 155 | while read -n 1 -p "Clobber existing MySQL database? [y/N]: " CLOBBERDB; do |
| 156 | echo |
Richard Barnette | 23ef442 | 2012-11-02 13:46:20 -0700 | [diff] [blame] | 157 | if [[ -z "${CLOBBERDB}" || |
| 158 | $(echo ${CLOBBERDB} | egrep -qi 'y|n') -eq 0 ]]; then |
Alex Miller | da3ecf4 | 2012-07-25 11:51:13 -0700 | [diff] [blame] | 159 | break |
| 160 | fi |
| 161 | echo "Please enter y or n." |
| 162 | done |
| 163 | else |
| 164 | CLOBBERDB='y' |
| 165 | fi |
| 166 | |
| 167 | SQL_COMMAND="drop database if exists chromeos_autotest_db; \ |
| 168 | create database chromeos_autotest_db; \ |
| 169 | grant all privileges on chromeos_autotest_db.* TO \ |
Simran Basi | 3d18208 | 2012-09-07 10:15:25 -0700 | [diff] [blame] | 170 | 'chromeosqa-admin'@'localhost' identified by '${PASSWD}'; \ |
Alex Miller | da3ecf4 | 2012-07-25 11:51:13 -0700 | [diff] [blame] | 171 | FLUSH PRIVILEGES;" |
| 172 | |
Alex Miller | da3ecf4 | 2012-07-25 11:51:13 -0700 | [diff] [blame] | 173 | if [[ "${CLOBBERDB}" = 'y' || "${CLOBBERDB}" = 'Y' ]]; then |
| 174 | mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}" |
| 175 | fi |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 176 | echo -e "Done!\n" |
| 177 | |
| 178 | AT_DIR=/usr/local/autotest |
| 179 | echo -n "Bind-mounting your autotest dir at ${AT_DIR}..." |
| 180 | sudo mkdir -p "${AT_DIR}" |
| 181 | sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}" |
| 182 | echo -e "Done!\n" |
| 183 | |
| 184 | EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true) |
| 185 | if [ -n "${EXISTING_MOUNT}" ]; then |
| 186 | echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}' |
| 187 | echo "We won't update /etc/fstab, but you should have a line line this:" |
| 188 | echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0" |
| 189 | else |
| 190 | echo -n "Adding aforementioned bind-mount to /etc/fstab..." |
| 191 | # Is there a better way to elevate privs and do a redirect? |
| 192 | sudo su -c \ |
| 193 | "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \ |
| 194 | >> /etc/fstab" |
| 195 | echo -e "Done!\n" |
| 196 | fi |
| 197 | |
| 198 | echo -n "Reticulating splines..." |
| 199 | "${AT_DIR}"/utils/build_externals.py &> /dev/null |
| 200 | "${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null |
| 201 | echo -e "Done!\n" |
| 202 | |
| 203 | echo "Populating autotest mysql DB..." |
| 204 | "${AT_DIR}"/database/migrate.py sync |
| 205 | "${AT_DIR}"/frontend/manage.py syncdb |
| 206 | # You may have to run this twice. |
| 207 | "${AT_DIR}"/frontend/manage.py syncdb |
| 208 | "${AT_DIR}"/utils/test_importer.py |
| 209 | echo -e "Done!\n" |
| 210 | |
| 211 | echo "Configuring apache to run the autotest web interface..." |
Simran Basi | 508ee92 | 2013-11-18 13:43:40 -0800 | [diff] [blame^] | 212 | if [ ! -d /etc/apache2/run ]; then |
| 213 | sudo mkdir /etc/apache2/run |
| 214 | fi |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 215 | sudo ln -sf "${AT_DIR}"/apache/apache-conf \ |
| 216 | /etc/apache2/sites-available/autotest-server |
| 217 | # disable currently active default |
| 218 | sudo a2dissite default |
| 219 | # enable autotest server |
| 220 | sudo a2ensite autotest-server |
| 221 | # Enable rewrite module |
| 222 | sudo a2enmod rewrite |
Scott Zawalski | a5745e9 | 2013-05-09 10:42:27 -0400 | [diff] [blame] | 223 | # Enable wsgi |
| 224 | sudo a2enmod wsgi |
| 225 | # enable version |
| 226 | sudo a2enmod version |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 227 | # Setup permissions so that Apache web user can read the proper files. |
| 228 | chmod -R o+r "${AT_DIR}" |
Alex Miller | 5e7b254 | 2013-02-13 14:24:04 -0800 | [diff] [blame] | 229 | find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x |
Chris Masone | 2aa5533 | 2011-12-30 14:41:07 -0800 | [diff] [blame] | 230 | chmod o+x "${AT_DIR}"/tko/*.cgi |
| 231 | # restart server |
| 232 | sudo /etc/init.d/apache2 restart |
| 233 | |
| 234 | echo "Browse to http://localhost to see if Autotest is working." |
| 235 | echo "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" |