Revert "Removing deprecated/unused/outdated code"
[osm/RO.git] / charms / layers / openmano / scripts / init_mano_db.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefonica Investigacion 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 DIRNAME=/opt/openmano/database_utils
37
38 function usage(){
39 echo -e "Usage: $0 OPTIONS"
40 echo -e " Inits openmano database; deletes previous one and loads from ${DBNAME}_structure.sql"
41 echo -e " OPTIONS"
42 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
43 echo -e " -p PASS database password. 'No password' by default. Prompts if DB access fails"
44 echo -e " -P PORT database port. '$DBPORT' by default"
45 echo -e " -h HOST database host. '$DBHOST' by default"
46 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
47 echo -e " --help shows this help"
48 echo -e " --createdb forces the deletion and creation of the database"
49 }
50
51 while getopts ":u:p:P:d:h:-:" o; do
52 case "${o}" in
53 u)
54 DBUSER="$OPTARG"
55 ;;
56 p)
57 DBPASS="$OPTARG"
58 ;;
59 P)
60 DBPORT="$OPTARG"
61 ;;
62 d)
63 DBNAME="$OPTARG"
64 ;;
65 h)
66 DBHOST="$OPTARG"
67 ;;
68 -)
69 if [ "${OPTARG}" == "help" ]; then
70 usage && exit 0
71 elif [ "${OPTARG}" == "createdb" ]; then
72 CREATEDB="yes"
73 else
74 echo "Invalid option: --$OPTARG" >&2 && usage >&2
75 exit 1
76 fi
77 ;;
78 \?)
79 echo "Invalid option: -$OPTARG" >&2 && usage >&2
80 exit 1
81 ;;
82 :)
83 echo "Option -$OPTARG requires an argument." >&2 && usage >&2
84 exit 1
85 ;;
86 *)
87 usage >&2
88 exit -1
89 ;;
90 esac
91 done
92 shift $((OPTIND-1))
93
94 #check and ask for database user password
95 DBUSER_="-u$DBUSER"
96 DBPASS_=""
97 [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
98 DBHOST_="-h$DBHOST"
99 DBPORT_="-P$DBPORT"
100
101 TEMPFILE="$(mktemp -q --tmpdir "initmanodb.XXXXXX")"
102 trap 'rm -f "$TEMPFILE"' EXIT SIGINT SIGTERM
103 chmod 0600 "$TEMPFILE"
104 cat >"$TEMPFILE" <<EOF
105 [client]
106 user="${DBUSER}"
107 password="${DBPASS}"
108 EOF
109 DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE"
110
111 while ! mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ -e "quit" >/dev/null 2>&1
112 do
113 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
114 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
115 # read -e -p "mysql database name($DBNAME): " KK
116 # [ -n "$KK" ] && DBNAME="$KK"
117 read -e -p "mysql user($DBUSER): " KK
118 [ -n "$KK" ] && DBUSER="$KK"
119 read -e -s -p "mysql password: " DBPASS
120 cat >"$TEMPFILE" <<EOF
121 [client]
122 user="${DBUSER}"
123 password="${DBPASS}"
124 EOF
125 logintry="yes"
126 echo
127 done
128
129 if [ -n "${CREATEDB}" ]; then
130 echo " deleting previous database ${DBNAME}"
131 echo "DROP DATABASE IF EXISTS ${DBNAME}" | mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_
132 echo " creating database ${DBNAME}"
133 mysqladmin $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ -s create ${DBNAME} || exit 1
134 fi
135
136 echo " loading ${DIRNAME}/${DBNAME}_structure.sql"
137 #echo 'mysql '$DEF_EXTRA_FILE_PARAM' '$DBHOST_' '$DBPORT_' '$DBNAME' < '${DIRNAME}'/mano_db_structure.sql'
138 mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME < ${DIRNAME}/mano_db_structure.sql
139
140 echo " migrage database version"
141 ${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME
142