blob: 83d60a8c441cf261de446311c2c792fec6f1c3f5 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001#!/bin/bash
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5# This file is part of openmano
6# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
24DBUSER="mano"
25DBPASS=""
26DBHOST="localhost"
27DBPORT="3306"
28DBNAME="mano_db"
garciadeblas0c317ee2016-08-29 12:33:06 +020029CREATEDB=""
tierno7edb6752016-03-21 17:37:52 +010030
31# Detect paths
32MYSQL=$(which mysql)
33AWK=$(which awk)
34GREP=$(which grep)
35DIRNAME=`dirname $0`
36
37function usage(){
38 echo -e "Usage: $0 OPTIONS"
39 echo -e " Inits openmano database; deletes previous one and loads from ${DBNAME}_structure.sql"
40 echo -e " OPTIONS"
41 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
tiernoaa5832d2016-12-07 16:20:25 +010042 echo -e " -p PASS database password. 'No password' or 'manopw' by default. Prompts if DB access fails"
tierno7edb6752016-03-21 17:37:52 +010043 echo -e " -P PORT database port. '$DBPORT' by default"
44 echo -e " -h HOST database host. '$DBHOST' by default"
tierno462e3882016-07-06 17:52:14 +020045 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
tierno7edb6752016-03-21 17:37:52 +010046 echo -e " --help shows this help"
garciadeblas0c317ee2016-08-29 12:33:06 +020047 echo -e " --createdb forces the deletion and creation of the database"
tierno7edb6752016-03-21 17:37:52 +010048}
49
tierno462e3882016-07-06 17:52:14 +020050while getopts ":u:p:P:d:h:-:" o; do
tierno7edb6752016-03-21 17:37:52 +010051 case "${o}" in
52 u)
53 DBUSER="$OPTARG"
54 ;;
55 p)
56 DBPASS="$OPTARG"
57 ;;
58 P)
59 DBPORT="$OPTARG"
60 ;;
tierno462e3882016-07-06 17:52:14 +020061 d)
62 DBNAME="$OPTARG"
63 ;;
tierno7edb6752016-03-21 17:37:52 +010064 h)
65 DBHOST="$OPTARG"
66 ;;
67 -)
garciadeblas0c317ee2016-08-29 12:33:06 +020068 if [ "${OPTARG}" == "help" ]; then
69 usage && exit 0
70 elif [ "${OPTARG}" == "createdb" ]; then
71 CREATEDB="yes"
72 else
73 echo "Invalid option: --$OPTARG" >&2 && usage >&2
74 exit 1
75 fi
tierno7edb6752016-03-21 17:37:52 +010076 ;;
77 \?)
78 echo "Invalid option: -$OPTARG" >&2 && usage >&2
79 exit 1
80 ;;
81 :)
82 echo "Option -$OPTARG requires an argument." >&2 && usage >&2
83 exit 1
84 ;;
85 *)
86 usage >&2
87 exit -1
88 ;;
89 esac
90done
91shift $((OPTIND-1))
92
93#check and ask for database user password
garciadeblasd1b86302016-09-20 11:54:32 +020094DBUSER_="-u$DBUSER"
95DBPASS_=""
96[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
tierno7edb6752016-03-21 17:37:52 +010097DBHOST_="-h$DBHOST"
98DBPORT_="-P$DBPORT"
garciadeblas89b3d842016-09-19 15:18:33 +020099
100TEMPFILE="$(mktemp -q --tmpdir "initmanodb.XXXXXX")"
garciadeblas4b3b4462016-09-27 11:16:14 +0200101trap 'rm -f "$TEMPFILE"' EXIT
garciadeblas89b3d842016-09-19 15:18:33 +0200102chmod 0600 "$TEMPFILE"
garciadeblas89b3d842016-09-19 15:18:33 +0200103DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE"
tiernoaa5832d2016-12-07 16:20:25 +0100104if [ -z "${DBPASS}" ]
105then
106 password_ok=""
107 echo -e "[client]\nuser='${DBUSER}'\npassword='manopw'" > "$TEMPFILE"
108 mysql --defaults-extra-file="$TEMPFILE" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 && DBPASS="manopw"
109 echo -e "[client]\nuser='${DBUSER}'\npassword=''" > "$TEMPFILE"
110 mysql --defaults-extra-file="$TEMPFILE" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 && DBPASS=""
111fi
112echo -e "[client]\nuser='${DBUSER}'\npassword='${DBPASS}'" > "$TEMPFILE"
garciadeblas89b3d842016-09-19 15:18:33 +0200113
114while ! mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ -e "quit" >/dev/null 2>&1
tierno7edb6752016-03-21 17:37:52 +0100115do
116 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
117 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
118# read -e -p "mysql database name($DBNAME): " KK
119# [ -n "$KK" ] && DBNAME="$KK"
120 read -e -p "mysql user($DBUSER): " KK
garciadeblas89b3d842016-09-19 15:18:33 +0200121 [ -n "$KK" ] && DBUSER="$KK"
tierno7edb6752016-03-21 17:37:52 +0100122 read -e -s -p "mysql password: " DBPASS
tiernoaa5832d2016-12-07 16:20:25 +0100123 echo -e "[client]\nuser='${DBUSER}'\npassword='${DBPASS}'" > "$TEMPFILE"
tierno7edb6752016-03-21 17:37:52 +0100124 logintry="yes"
125 echo
126done
tiernoaa5832d2016-12-07 16:20:25 +0100127[ -z "${DBPASS}" ] && DBPASS_=""
128[ -n "${DBPASS}" ] && DBPASS_="-p${DBPASS}"
tierno7edb6752016-03-21 17:37:52 +0100129
garciadeblas0c317ee2016-08-29 12:33:06 +0200130if [ -n "${CREATEDB}" ]; then
131 echo " deleting previous database ${DBNAME}"
garciadeblas89b3d842016-09-19 15:18:33 +0200132 echo "DROP DATABASE IF EXISTS ${DBNAME}" | mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_
garciadeblas0c317ee2016-08-29 12:33:06 +0200133 echo " creating database ${DBNAME}"
Adam Israel33f0a7e2016-10-04 12:03:31 -0700134 mysqladmin $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ -s create ${DBNAME} || exit 1
garciadeblas0c317ee2016-08-29 12:33:06 +0200135fi
136
tierno7edb6752016-03-21 17:37:52 +0100137echo " loading ${DIRNAME}/${DBNAME}_structure.sql"
garciadeblasd1b86302016-09-20 11:54:32 +0200138#echo 'mysql '$DEF_EXTRA_FILE_PARAM' '$DBHOST_' '$DBPORT_' '$DBNAME' < '${DIRNAME}'/mano_db_structure.sql'
139mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME < ${DIRNAME}/mano_db_structure.sql
tierno7edb6752016-03-21 17:37:52 +0100140
141echo " migrage database version"
142${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME
143