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