1#!/bin/bash
2#
3# Copyright 2016 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.
6set -e
7
8USAGE='Usage: setup_db.sh [-pacm]'
9HELP="${USAGE}\n\n\
10Setup database needed to run autotest.\n\
11Options:\n\
12  -p Desired Autotest DB password. Must be non-empty.\n\
13  -a Path to user local autotest directiry. Default is /usr/local/autotest.\n\
14  -c Clobber existing database if it exists.\n\
15  -m Allow remote access for database."
16
17PASSWD=
18AT_DIR=/usr/local/autotest
19clobberdb="FALSE"
20remotedb="FALSE"
21while getopts ":p:a:cmh" opt; do
22  case ${opt} in
23    p)
24      PASSWD=$OPTARG
25      ;;
26    a)
27      AT_DIR=$OPTARG
28      ;;
29    c)
30      clobberdb="TRUE"
31      ;;
32    m)
33      remotedb="TRUE"
34      ;;
35    h)
36      echo -e "${HELP}" >&2
37      exit 0
38      ;;
39    \?)
40      echo "Invalid option: -$OPTARG" >&2
41      echo -e "${HELP}" >&2
42      exit 1
43      ;;
44    :)
45      echo "Option -$OPTARG requires an argument." >&2
46      echo -e "${HELP}" >&2
47      exit 1
48      ;;
49  esac
50done
51
52if [ -z "${PASSWD}" ]; then
53  echo "Option -p is required." >&2
54  echo -e "${HELP}" >&2
55  exit 1
56fi
57
58echo "Installing needed Ubuntu packages for mysql db."
59DB_PKG_LIST="mysql-server mysql-common python-mysqldb"
60
61if ! sudo apt-get install -y ${DB_PKG_LIST}; then
62  echo "Could not install packages: $?"
63  exit 1
64fi
65echo -e "Done!\n"
66
67# Check if database exists, clobber existing database with user consent.
68#
69# Arguments: Name of the database
70check_database()
71{
72  local db_name=$1
73  echo "Setting up Database: $db_name in MySQL..."
74  if mysql -u root -e ';' 2> /dev/null ; then
75    PASSWD_STRING=
76  elif mysql -u root -p"${PASSWD}" -e ';' 2> /dev/null ; then
77    PASSWD_STRING="-p${PASSWD}"
78  else
79    PASSWD_STRING="-p"
80  fi
81
82  if [ -z "${PASSWD_STRING}" ]; then
83    mysqladmin -u root ping >/dev/null
84  else
85    mysqladmin -u root "${PASSWD_STRING}" ping >/dev/null
86  fi
87
88  if [ $? -ne 0 ]; then
89    sudo service mysql start
90  fi
91
92  local existing_database=$(mysql -u root "${PASSWD_STRING}" -e "SELECT \
93  SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db_name'")
94
95  local sql_priv="GRANT ALL PRIVILEGES ON $db_name.* TO \
96  'chromeosqa-admin'@'localhost' IDENTIFIED BY '${PASSWD}';"
97
98  if [ "${remotedb}" = "TRUE" ]; then
99    sql_priv="${sql_priv} GRANT ALL PRIVILEGES ON $db_name.* TO \
100    'chromeosqa-admin'@'%' IDENTIFIED BY '${PASSWD}';"
101  fi
102
103  local sql_command="drop database if exists $db_name; \
104  create database $db_name; \
105  ${sql_priv} FLUSH PRIVILEGES;"
106
107  if [[ -z "${existing_database}" || "${clobberdb}" = 'TRUE' ]]; then
108    mysql -u root "${PASSWD_STRING}" -e "${sql_command}"
109  else
110    echo "Use existing database. Use option -c to clobber it."
111  fi
112  echo -e "Done!\n"
113}
114
115check_database 'chromeos_autotest_db'
116check_database 'chromeos_lab_servers'
117
118echo "Populating autotest mysql DB..."
119"${AT_DIR}"/database/migrate.py sync -f
120"${AT_DIR}"/frontend/manage.py syncdb --noinput
121# You may have to run this twice.
122"${AT_DIR}"/frontend/manage.py syncdb --noinput
123"${AT_DIR}"/utils/test_importer.py
124echo -e "Done!\n"
125
126echo "Initializing chromeos_lab_servers mysql DB..."
127"${AT_DIR}"/database/migrate.py sync -f -d AUTOTEST_SERVER_DB
128echo -e "Done!\n"
129
130