• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ! mysqladmin -u root "${PASSWD_STRING}" ping ; then
83    sudo service mysql start
84  fi
85
86  local existing_database=$(mysql -u root "${PASSWD_STRING}" -e "SELECT \
87  SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db_name'")
88
89  local sql_priv="GRANT ALL PRIVILEGES ON $db_name.* TO \
90  'chromeosqa-admin'@'localhost' IDENTIFIED BY '${PASSWD}';"
91
92  if [ "${remotedb}" = "TRUE" ]; then
93    sql_priv="${sql_priv} GRANT ALL PRIVILEGES ON $db_name.* TO \
94    'chromeosqa-admin'@'%' IDENTIFIED BY '${PASSWD}';"
95  fi
96
97  local sql_command="drop database if exists $db_name; \
98  create database $db_name; \
99  ${sql_priv} FLUSH PRIVILEGES;"
100
101  if [[ -z "${existing_database}" || "${clobberdb}" = 'TRUE' ]]; then
102    mysql -u root "${PASSWD_STRING}" -e "${sql_command}"
103  else
104    echo "Use existing database. Use option -c to clobber it."
105  fi
106  echo -e "Done!\n"
107}
108
109check_database 'chromeos_autotest_db'
110check_database 'chromeos_lab_servers'
111
112echo "Populating autotest mysql DB..."
113"${AT_DIR}"/database/migrate.py sync -f
114"${AT_DIR}"/frontend/manage.py syncdb --noinput
115# You may have to run this twice.
116"${AT_DIR}"/frontend/manage.py syncdb --noinput
117"${AT_DIR}"/utils/test_importer.py
118echo -e "Done!\n"
119
120echo "Initializing chromeos_lab_servers mysql DB..."
121"${AT_DIR}"/database/migrate.py sync -f -d AUTOTEST_SERVER_DB
122echo -e "Done!\n"
123
124