split CREATE USER and GRANT (Mantis#1727)
[racktables] / tests / travis_setup_mysql.sh
1 #!/bin/sh
2
3 if [ $# -ne 3 ]; then
4 echo "This script creates a MySQL database for unit testing in Travis CI environment"
5 echo "Don't run it on a production system because it may cause lots of damage."
6 echo "Usage: $0 <MySQL database name> <MySQL user name> <MySQL user password>"
7 exit 1
8 fi
9
10 DBNAME="$1"
11 USERNAME="$2"
12 PASSWORD="$3"
13
14 if mysql -u root -e "SHOW TABLES FROM $DBNAME" >/dev/null 2>&1; then
15 echo "Error: database $DBNAME already exists!"
16 exit 1
17 fi
18
19 if [ -e wwwroot/inc/secret.php ]; then
20 echo "Error: wwwroot/inc/secret.php already exists!"
21 exit 1
22 fi
23
24 # The purpose of the explicit "mysql" DB below is not to fix a real bug
25 # but to prevent an error on my working copy when the MySQL client
26 # is configured (through ~/.my.cnf) to connect to the same database as
27 # I am trying to initialize with this script. In that specific case
28 # the client tries to connect to the database that doesn't yet exist
29 # and this script fails, hence the override to "mysql". -- Denis
30 mysql -u root mysql -e "CREATE DATABASE ${DBNAME} CHARACTER SET utf8 COLLATE utf8_general_ci;" || exit 2
31 mysql -u root -e "CREATE USER ${USERNAME}@localhost IDENTIFIED BY '${PASSWORD}';" || exit 2
32 mysql -u root -e "GRANT ALL PRIVILEGES ON ${DBNAME}.* TO ${USERNAME}@localhost;" || exit 2
33
34 cat >wwwroot/inc/secret.php <<EOF
35 <?php
36 \$pdo_dsn = 'mysql:host=localhost;port=3306;dbname=${DBNAME}';
37 \$db_username = '${USERNAME}';
38 \$db_password = '${PASSWORD}';
39 ?>
40 EOF
41
42 cat >cli_install.php <<EOF
43 <?php
44 require_once 'wwwroot/inc/pre-init.php';
45 require_once 'wwwroot/inc/dictionary.php';
46 require_once 'wwwroot/inc/config.php';
47 require_once 'wwwroot/inc/install.php';
48 ob_start();
49 init_database_static();
50 ob_end_clean();
51 ?>
52 EOF
53
54 php cli_install.php || exit 3
55 mysql -u "$USERNAME" -p"$PASSWORD" "$DBNAME" -e "INSERT INTO UserAccount (user_id, user_name, user_password_hash) VALUES (1, 'admin', SHA1('${PASSWORD}'));" || exit 3