blob: 4697581f34705d58e53b916039477e6612d4719f [file] [log] [blame]
Chris Masone2aa55332011-12-30 14:41:07 -08001#!/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 Basi79b1a7b2013-02-13 11:16:00 -08006set -e
Chris Masone2aa55332011-12-30 14:41:07 -08007
Prathmesh Prabhuf882c672017-12-21 13:03:30 -08008function usage() {
9 cat >&2 <<EOT
Prathmesh Prabhu8ad7e602017-12-21 13:09:51 -080010Usage: setup_dev_autotest.sh [-pavnms]
Prathmesh Prabhuf882c672017-12-21 13:03:30 -080011
12Install and configure software needed to run autotest locally.
13If you're just working on tests, you do not need to run this.
14Options:
15 -p Desired Autotest DB password. Must be non-empty.
16 -a Absolute path to autotest source tree.
17 -v Show info logging from build_externals.py and compile_gwt_clients.py
Jakob Juelich1c53ae72014-09-10 11:32:16 -070018 -n Non-interactive mode, doesn't ask for any user input.
Prathmesh Prabhuf882c672017-12-21 13:03:30 -080019 Requires -p and -a to be set.
20 -m Allow remote access for database.
Prathmesh Prabhu8ad7e602017-12-21 13:09:51 -080021 -s Skip steps handled via puppet in prod.
22 This is a transitional flag used to skip certain steps as they are migrated
23 to puppet for use in the autotest lab. Not to be used by developers.
Prathmesh Prabhuf882c672017-12-21 13:03:30 -080024
25EOT
26}
27
Jakob Juelich1c53ae72014-09-10 11:32:16 -070028
29function get_y_or_n_interactive {
30 local ret
31 while true; do
32 read -p "$2" yn
33 case $yn in
34 [Yy]* ) ret="y"; break;;
35 [Nn]* ) ret="n"; break;;
36 * ) echo "Please enter y or n.";;
37 esac
38 done
Allen Li5bfe6ab2016-08-16 17:11:28 -070039 eval "$1=\$ret"
Jakob Juelich1c53ae72014-09-10 11:32:16 -070040}
41
42function get_y_or_n {
43 local ret=$3
44 if [ "${noninteractive}" = "FALSE" ]; then
45 get_y_or_n_interactive sub "$2"
46 ret=$sub
47 fi
Allen Li5bfe6ab2016-08-16 17:11:28 -070048 eval "$1=\$ret"
Jakob Juelich1c53ae72014-09-10 11:32:16 -070049}
Chris Masone2aa55332011-12-30 14:41:07 -080050
51AUTOTEST_DIR=
52PASSWD=
Michael Liangb6a78f42014-07-11 15:05:30 -070053verbose="FALSE"
Jakob Juelich1c53ae72014-09-10 11:32:16 -070054noninteractive="FALSE"
MK Ryu50ab33e2015-08-28 14:28:37 -070055remotedb="FALSE"
Prathmesh Prabhu8ad7e602017-12-21 13:09:51 -080056skip_puppetized_steps="FALSE"
57while getopts ":p:a:vnmsh" opt; do
MK Ryu50ab33e2015-08-28 14:28:37 -070058 case ${opt} in
Chris Masone2aa55332011-12-30 14:41:07 -080059 a)
60 AUTOTEST_DIR=$OPTARG
61 ;;
62 p)
63 PASSWD=$OPTARG
64 ;;
Michael Liangb6a78f42014-07-11 15:05:30 -070065 v)
66 verbose="TRUE"
67 ;;
Jakob Juelich1c53ae72014-09-10 11:32:16 -070068 n)
69 noninteractive="TRUE"
70 ;;
MK Ryuc96e9592015-03-05 14:09:03 -080071 m)
MK Ryu50ab33e2015-08-28 14:28:37 -070072 remotedb="TRUE"
MK Ryuc96e9592015-03-05 14:09:03 -080073 ;;
Prathmesh Prabhu8ad7e602017-12-21 13:09:51 -080074 s)
75 skip_puppetized_steps="TRUE"
76 ;;
Chris Masone2aa55332011-12-30 14:41:07 -080077 h)
Prathmesh Prabhuf882c672017-12-21 13:03:30 -080078 usage
Chris Masone2aa55332011-12-30 14:41:07 -080079 exit 0
80 ;;
81 \?)
82 echo "Invalid option: -$OPTARG" >&2
Prathmesh Prabhuf882c672017-12-21 13:03:30 -080083 usage
Chris Masone2aa55332011-12-30 14:41:07 -080084 exit 1
85 ;;
86 :)
87 echo "Option -$OPTARG requires an argument." >&2
Prathmesh Prabhuf882c672017-12-21 13:03:30 -080088 usage
Chris Masone2aa55332011-12-30 14:41:07 -080089 exit 1
90 ;;
91 esac
92done
93
Prathmesh Prabhu8ad7e602017-12-21 13:09:51 -080094if [[ "${skip_puppetized_steps}" == "TRUE" ]]; then
95 echo "Requested to skip certain steps. Will tell you when I skip things."
96fi
97
Prathmesh Prabhu864de132015-04-08 14:45:13 -070098if [[ $EUID -eq 0 ]]; then
99 echo "Running with sudo / as root is not recommended"
100 get_y_or_n verify "Continue as root? [y/N]: " "n"
101 if [[ "${verify}" = 'n' ]]; then
102 echo "Bailing!"
103 exit 1
104 fi
105fi
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700106
107if [ "${noninteractive}" = "TRUE" ]; then
108 if [ -z "${AUTOTEST_DIR}" ]; then
109 echo "-a must be specified in non-interactive mode." >&2
110 exit 1
111 fi
112 if [ -z "${PASSWD}" ]; then
113 echo "-p must be specified in non-interactive mode." >&2
114 exit 1
115 fi
116fi
117
118
Richard Barnette23ef4422012-11-02 13:46:20 -0700119if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -0800120 read -s -p "Autotest DB password: " PASSWD
121 echo
Richard Barnette23ef4422012-11-02 13:46:20 -0700122 if [ -z "${PASSWD}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -0800123 echo "Empty passwords not allowed." >&2
124 exit 1
125 fi
126 read -s -p "Re-enter password: " PASSWD2
127 echo
Richard Barnette23ef4422012-11-02 13:46:20 -0700128 if [ "${PASSWD}" != "${PASSWD2}" ]; then
Chris Masone2aa55332011-12-30 14:41:07 -0800129 echo "Passwords don't match." >&2
130 exit 1
131 fi
132fi
133
134if [ -z "${AUTOTEST_DIR}" ]; then
135 CANDIDATE=$(dirname "$(readlink -f "$0")" | egrep -o '(/[^/]+)*/files')
136 read -p "Enter autotest dir [${CANDIDATE}]: " AUTOTEST_DIR
137 if [ -z "${AUTOTEST_DIR}" ]; then
138 AUTOTEST_DIR="${CANDIDATE}"
139 fi
140fi
141
Aviv Keshet2c603d82013-05-24 10:31:59 -0700142
143# Sanity check AUTOTEST_DIR. If it's null, or doesn't exist on the filesystem
144# then die.
145if [ -z "${AUTOTEST_DIR}" ]; then
146 echo "No AUTOTEST_DIR. Aborting script."
147 exit 1
148fi
149
150if [ ! -d "${AUTOTEST_DIR}" ]; then
Allen Li5bfe6ab2016-08-16 17:11:28 -0700151 echo "Directory $AUTOTEST_DIR does not exist. Aborting script."
Aviv Keshet2c603d82013-05-24 10:31:59 -0700152 exit 1
153fi
154
155
Chris Masone2aa55332011-12-30 14:41:07 -0800156SHADOW_CONFIG_PATH="${AUTOTEST_DIR}/shadow_config.ini"
157echo "Autotest supports local overrides of global configuration through a "
158echo "'shadow' configuration file. Setting one up for you now."
Chris Sosa18c70b32013-02-15 14:12:43 -0800159CLOBBER=0
Chris Masone2aa55332011-12-30 14:41:07 -0800160if [ -f ${SHADOW_CONFIG_PATH} ]; then
Jakob Juelich1c53ae72014-09-10 11:32:16 -0700161 get_y_or_n clobber "Clobber existing shadow config? [Y/n]: " "n"
162 if [[ "${clobber}" = 'n' ]]; then
Chris Sosa18c70b32013-02-15 14:12:43 -0800163 CLOBBER=1
Chris Masone2aa55332011-12-30 14:41:07 -0800164 echo "Refusing to clobber existing shadow_config.ini."
Chris Sosa18c70b32013-02-15 14:12:43 -0800165 else
166 echo "Clobbering existing shadow_config.ini."
Chris Masone2aa55332011-12-30 14:41:07 -0800167 fi
Chris Masone2aa55332011-12-30 14:41:07 -0800168fi
169
Allen Li5bfe6ab2016-08-16 17:11:28 -0700170CROS_CHECKOUT=$(readlink -f "$AUTOTEST_DIR/../../../..")
Aviv Keshet2c603d82013-05-24 10:31:59 -0700171
Chris Sosa18c70b32013-02-15 14:12:43 -0800172# Create clean shadow config if we're replacing it/creating a new one.
173if [ $CLOBBER -eq 0 ]; then
174 cat > "${SHADOW_CONFIG_PATH}" <<EOF
Chris Masone2aa55332011-12-30 14:41:07 -0800175[AUTOTEST_WEB]
176host: localhost
177password: ${PASSWD}
Chris Masonee61dd812012-01-10 18:04:21 -0800178readonly_host: localhost
179readonly_user: chromeosqa-admin
180readonly_password: ${PASSWD}
Chris Masone2aa55332011-12-30 14:41:07 -0800181
182[SERVER]
183hostname: localhost
Chris Sosae4181242012-09-10 16:54:30 -0700184
185[SCHEDULER]
186drones: localhost
Aviv Keshet2c603d82013-05-24 10:31:59 -0700187
188[CROS]
189source_tree: ${CROS_CHECKOUT}
Allen Li5bfe6ab2016-08-16 17:11:28 -0700190# Edit the following line as needed.
191#dev_server: http://10.10.10.10:8080
192enable_ssh_tunnel_for_servo: True
193enable_ssh_tunnel_for_chameleon: True
194enable_ssh_connection_for_devserver: True
195enable_ssh_tunnel_for_moblab: True
Chris Masone2aa55332011-12-30 14:41:07 -0800196EOF
Chris Sosa18c70b32013-02-15 14:12:43 -0800197 echo -e "Done!\n"
198fi
Chris Masone2aa55332011-12-30 14:41:07 -0800199
200echo "Installing needed Ubuntu packages..."
Shuqian Zhao13470bb2016-02-18 16:25:45 -0800201PKG_LIST="libapache2-mod-wsgi gnuplot apache2-mpm-prefork unzip \
202python-imaging libpng12-dev libfreetype6-dev \
Chris Masone2aa55332011-12-30 14:41:07 -0800203sqlite3 python-pysqlite2 git-core pbzip2 openjdk-6-jre openjdk-6-jdk \
Simran Basi3d182082012-09-07 10:15:25 -0700204python-crypto python-dev subversion build-essential python-setuptools \
Justin Giorgi1214c772016-07-04 22:43:37 -0700205python-numpy python-scipy libmysqlclient-dev"
Chris Masone2aa55332011-12-30 14:41:07 -0800206
207if ! sudo apt-get install -y ${PKG_LIST}; then
208 echo "Could not install packages: $?"
209 exit 1
210fi
211echo -e "Done!\n"
212
Allen Lidff8fb12018-04-24 13:15:54 -0700213INFRA_VENV_SYMLINK=/opt/infra_virtualenv
214if [ -e "${INFRA_VENV_SYMLINK}" ]; then
215 echo "infra_virtualenv already set up at ${INFRA_VENV_SYMLINK}"
216else
217 echo -n "Setting up symlink for infra_virtualenv..."
218 INFRA_VENV_PATH=$(realpath "${CROS_CHECKOUT}/infra_virtualenv")
219 if ! [ -e "$INFRA_VENV_PATH/bin/create_venv" ]; then
220 echo "Could not find infra_virtualenv repo at ${INFRA_VENV_PATH}"
221 echo "Make sure you're using a full repo checkout"
222 exit 1
223 fi
224 sudo ln -s "$INFRA_VENV_PATH" "$INFRA_VENV_SYMLINK"
225 echo -e "Done!\n"
226fi
227
Chris Masone2aa55332011-12-30 14:41:07 -0800228AT_DIR=/usr/local/autotest
229echo -n "Bind-mounting your autotest dir at ${AT_DIR}..."
230sudo mkdir -p "${AT_DIR}"
231sudo mount --bind "${AUTOTEST_DIR}" "${AT_DIR}"
232echo -e "Done!\n"
233
Fang Dengaf82bca2014-11-18 17:19:06 -0800234sudo chown -R "$(whoami)" "${AT_DIR}"
235
Chris Masone2aa55332011-12-30 14:41:07 -0800236EXISTING_MOUNT=$(egrep "/.+[[:space:]]${AT_DIR}" /etc/fstab || /bin/true)
237if [ -n "${EXISTING_MOUNT}" ]; then
238 echo "${EXISTING_MOUNT}" | awk '{print $1 " already automounting at " $2}'
239 echo "We won't update /etc/fstab, but you should have a line line this:"
240 echo -e "${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0"
241else
242 echo -n "Adding aforementioned bind-mount to /etc/fstab..."
243 # Is there a better way to elevate privs and do a redirect?
244 sudo su -c \
245 "echo -e '${AUTOTEST_DIR}\t${AT_DIR}\tbind defaults,bind\t0\t0' \
246 >> /etc/fstab"
247 echo -e "Done!\n"
248fi
249
250echo -n "Reticulating splines..."
Michael Liangb6a78f42014-07-11 15:05:30 -0700251
252if [ "${verbose}" = "TRUE" ]; then
253 "${AT_DIR}"/utils/build_externals.py
254 "${AT_DIR}"/utils/compile_gwt_clients.py -a
255else
256 "${AT_DIR}"/utils/build_externals.py &> /dev/null
257 "${AT_DIR}"/utils/compile_gwt_clients.py -a &> /dev/null
258fi
259
Chris Masone2aa55332011-12-30 14:41:07 -0800260echo -e "Done!\n"
261
Shuqian Zhao13470bb2016-02-18 16:25:45 -0800262echo "Start setting up Database..."
263get_y_or_n clobberdb "Clobber MySQL database if it exists? [Y/n]: " "n"
264opts_string="-p ${PASSWD} -a ${AT_DIR}"
265if [[ "${clobberdb}" = 'y' ]]; then
266 opts_string="${opts_string} -c"
267fi
268if [[ "${remotedb}" = 'TRUE' ]]; then
269 opts_string="${opts_string} -m"
270fi
271"${AT_DIR}"/site_utils/setup_db.sh ${opts_string}
Chris Masone2aa55332011-12-30 14:41:07 -0800272
Dan Shi7f0c1832014-10-27 16:05:57 -0700273echo -e "Done!\n"
274
Chris Masone2aa55332011-12-30 14:41:07 -0800275echo "Configuring apache to run the autotest web interface..."
Simran Basi508ee922013-11-18 13:43:40 -0800276if [ ! -d /etc/apache2/run ]; then
277 sudo mkdir /etc/apache2/run
278fi
Chris Masone2aa55332011-12-30 14:41:07 -0800279sudo ln -sf "${AT_DIR}"/apache/apache-conf \
Dan Shi18808492014-11-05 10:25:26 -0800280 /etc/apache2/sites-available/autotest-server.conf
Allen Li5bfe6ab2016-08-16 17:11:28 -0700281
MK Ryuc96e9592015-03-05 14:09:03 -0800282# Disable currently active default
Michael Janssena7427612014-11-14 15:44:39 -0800283sudo a2dissite 000-default default || true
Allen Li6761d712017-09-14 23:55:55 +0000284sudo a2ensite autotest-server.conf
Allen Li5bfe6ab2016-08-16 17:11:28 -0700285
Chris Masone2aa55332011-12-30 14:41:07 -0800286sudo a2enmod rewrite
Scott Zawalskia5745e92013-05-09 10:42:27 -0400287sudo a2enmod wsgi
Allen Li5bfe6ab2016-08-16 17:11:28 -0700288sudo a2enmod version || true # built-in on trusty
Dan Shia85f6d82014-10-01 08:58:01 -0700289sudo a2enmod headers
MK Ryuc96e9592015-03-05 14:09:03 -0800290sudo a2enmod cgid
Allen Li5bfe6ab2016-08-16 17:11:28 -0700291
Chris Masone2aa55332011-12-30 14:41:07 -0800292# Setup permissions so that Apache web user can read the proper files.
293chmod -R o+r "${AT_DIR}"
Alex Miller5e7b2542013-02-13 14:24:04 -0800294find "${AT_DIR}"/ -type d -print0 | xargs --null chmod o+x
Chris Masone2aa55332011-12-30 14:41:07 -0800295chmod o+x "${AT_DIR}"/tko/*.cgi
296# restart server
297sudo /etc/init.d/apache2 restart
298
Dan Shidca745a2015-05-07 13:36:26 -0700299# Setup lxc and base container for server-side packaging support.
300sudo apt-get install lxc -y
301sudo python "${AT_DIR}"/site_utils/lxc.py -s
302
Allen Li5bfe6ab2016-08-16 17:11:28 -0700303# Set up keys for www-data/apache user.
304APACHE_USER=www-data
305APACHE_HOME=/var/www
306APACHE_SSH_DIR="$APACHE_HOME/.ssh"
307SSH_KEYS_PATH=src/third_party/chromiumos-overlay/chromeos-base/chromeos-ssh-testkeys/files
308sudo mkdir -p "$APACHE_SSH_DIR"
309sudo bash <<EOF
310cd "${APACHE_SSH_DIR:-/dev/null}" || exit 1
311sudo cp "$CROS_CHECKOUT/$SSH_KEYS_PATH/"* .
312sudo tee config >/dev/null <<EOF2
313Host *
314User root
315IdentityFile ~/.ssh/testing_rsa
316EOF2
317sudo chown -R "$APACHE_USER:" .
318sudo chmod -R go-rwx .
319EOF
320if [ $? -ne 0 ]; then
321 echo "apache user SSH setup failed."
322fi
323
Chris Masone2aa55332011-12-30 14:41:07 -0800324echo "Browse to http://localhost to see if Autotest is working."
325echo "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"