4b73441d5ef359fe5f56f5e6f0592d9d381fdab4
[osm/RO.git] / database_utils / init_mano_db.sh
1 #!/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
24 DBUSER="mano"
25 DBPASS=""
26 DBHOST="localhost"
27 DBPORT="3306"
28 DBNAME="mano_db"
29 CREATEDB=""
30
31 # Detect paths
32 MYSQL=$(which mysql)
33 AWK=$(which awk)
34 GREP=$(which grep)
35 DIRNAME=`dirname $0`
36
37 function 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"
42 echo -e " -p PASS database password. 'No password' by default. Prompts if DB access fails"
43 echo -e " -P PORT database port. '$DBPORT' by default"
44 echo -e " -h HOST database host. '$DBHOST' by default"
45 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
46 echo -e " --help shows this help"
47 echo -e " --createdb forces the deletion and creation of the database"
48 }
49
50 while getopts ":u:p:P:d:h:-:" o; do
51 case "${o}" in
52 u)
53 DBUSER="$OPTARG"
54 ;;
55 p)
56 DBPASS="$OPTARG"
57 ;;
58 P)
59 DBPORT="$OPTARG"
60 ;;
61 d)
62 DBNAME="$OPTARG"
63 ;;
64 h)
65 DBHOST="$OPTARG"
66 ;;
67 -)
68 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
76 ;;
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
90 done
91 shift $((OPTIND-1))
92
93 #check and ask for database user password
94 #DBUSER_="-u$DBUSER"
95 #DBPASS_=""
96 #[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
97 DBHOST_="-h$DBHOST"
98 DBPORT_="-P$DBPORT"
99
100 TEMPFILE="$(mktemp -q --tmpdir "initmanodb.XXXXXX")"
101 trap 'rm -f "$TEMPFILE"' EXIT
102 chmod 0600 "$TEMPFILE"
103 cat >"$TEMPFILE" <<EOF
104 [client]
105 user="${DBUSER}"
106 password="${DBPASS}"
107 EOF
108 DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE"
109
110 while ! mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ -e "quit" >/dev/null 2>&1
111 do
112 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
113 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
114 # read -e -p "mysql database name($DBNAME): " KK
115 # [ -n "$KK" ] && DBNAME="$KK"
116 read -e -p "mysql user($DBUSER): " KK
117 [ -n "$KK" ] && DBUSER="$KK"
118 read -e -s -p "mysql password: " DBPASS
119 cat >"$TEMPFILE" <<EOF
120 [client]
121 user="${DBUSER}"
122 password="${DBPASS}"
123 EOF
124 logintry="yes"
125 echo
126 done
127
128 if [ -n "${CREATEDB}" ]; then
129 echo " deleting previous database ${DBNAME}"
130 echo "DROP DATABASE IF EXISTS ${DBNAME}" | mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_
131 echo " creating database ${DBNAME}"
132 mysqladmin $DEF_EXTRA_FILE_PARAM -s create ${DBNAME} || exit 1
133 fi
134
135 echo " loading ${DIRNAME}/${DBNAME}_structure.sql"
136 mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT $DBNAME < ${DIRNAME}/mano_db_structure.sql
137
138 echo " migrage database version"
139 ${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME
140