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