Adding cover to tox.ini default envs
[osm/RO.git] / RO / osm_ro / database_utils / 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 DEFAULT_DBPASS="manopw"
27 DBHOST=""
28 DBPORT="3306"
29 DBNAME="mano_db"
30 QUIET_MODE=""
31 CREATEDB=""
32
33 # Detect paths
34 MYSQL=$(which mysql)
35 AWK=$(which awk)
36 GREP=$(which grep)
37 DIRNAME=`dirname $(readlink -f $0)`
38
39 function usage(){
40 echo -e "Usage: $0 OPTIONS [version]"
41 echo -e " Inits openmano database; deletes previous one and loads from ${DBNAME}_structure.sql"\
42 echo -e " and data from host_ranking.sql, nets.sql, of_ports_pci_correspondece*.sql"
43 "If [version] is not provided, it is upgraded to the last version"
44 echo -e " OPTIONS"
45 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
46 echo -e " -p PASS database password. If missing it tries without and '$DEFAULT_DBPASS' password before prompting"
47 echo -e " -P PORT database port. '$DBPORT' by default"
48 echo -e " -h HOST database host. 'localhost' by default"
49 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
50 echo -e " -q --quiet: Do not prompt for credentials and exit if cannot access to database"
51 echo -e " --createdb forces the deletion and creation of the database"
52 echo -e " --help shows this help"
53 }
54
55 while getopts ":u:p:P:h:d:q-:" o; do
56 case "${o}" in
57 u)
58 DBUSER="$OPTARG"
59 ;;
60 p)
61 DBPASS="$OPTARG"
62 ;;
63 P)
64 DBPORT="$OPTARG"
65 ;;
66 d)
67 DBNAME="$OPTARG"
68 ;;
69 h)
70 DBHOST="$OPTARG"
71 ;;
72 q)
73 export QUIET_MODE="-q"
74 ;;
75 -)
76 [ "${OPTARG}" == "help" ] && usage && exit 0
77 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE="-q" && continue
78 [ "${OPTARG}" == "createdb" ] && export CREATEDB=yes && continue
79 echo "Invalid option: '--$OPTARG'. Type --help for more information" >&2
80 exit 1
81 ;;
82 \?)
83 echo "Invalid option: '-$OPTARG'. Type --help for more information" >&2
84 exit 1
85 ;;
86 :)
87 echo "Option '-$OPTARG' requires an argument. Type --help for more information" >&2
88 exit 1
89 ;;
90 *)
91 usage >&2
92 exit 1
93 ;;
94 esac
95 done
96 shift $((OPTIND-1))
97
98 DB_VERSION=$1
99
100 if [ -n "$DB_VERSION" ] ; then
101 # check it is a number and an allowed one
102 [ "$DB_VERSION" -eq "$DB_VERSION" ] 2>/dev/null ||
103 ! echo "parameter 'version' requires a integer value" >&2 || exit 1
104 fi
105
106 # Creating temporary file
107 TEMPFILE="$(mktemp -q --tmpdir "initdb.XXXXXX")"
108 trap 'rm -f "$TEMPFILE"' EXIT
109 chmod 0600 "$TEMPFILE"
110 DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE"
111 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
112
113 if [ -n "${CREATEDB}" ] ; then
114 FIRST_TRY="yes"
115 while ! DB_ERROR=`mysqladmin "$DEF_EXTRA_FILE_PARAM" -s status 2>&1 >/dev/null` ; do
116 # if password is not provided, try silently with $DEFAULT_DBPASS before exit or prompt for credentials
117 [[ -n "$FIRST_TRY" ]] && [[ -z "$DBPASS" ]] && DBPASS="$DEFAULT_DBPASS" &&
118 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" &&
119 continue
120 echo "$DB_ERROR"
121 [[ -n "$QUIET_MODE" ]] && echo -e "Invalid admin database credentials!!!" >&2 && exit 1
122 echo -e "Provide database credentials (Ctrl+c to abort):"
123 read -e -p " mysql user($DBUSER): " KK
124 [ -n "$KK" ] && DBUSER="$KK"
125 read -e -s -p " mysql password: " DBPASS
126 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
127 FIRST_TRY=""
128 echo
129 done
130 # echo " deleting previous database ${DBNAME} if it exists"
131 mysqladmin $DEF_EXTRA_FILE_PARAM DROP "${DBNAME}" -f && echo "Previous database deleted"
132 echo " creating database ${DBNAME}"
133 mysqladmin $DEF_EXTRA_FILE_PARAM create "${DBNAME}" || exit 1
134 fi
135
136 # Check and ask for database user password
137 FIRST_TRY="yes"
138 while ! DB_ERROR=`mysql "$DEF_EXTRA_FILE_PARAM" $DBNAME -e "quit" 2>&1 >/dev/null`
139 do
140 # if password is not provided, try silently with $DEFAULT_DBPASS before exit or prompt for credentials
141 [[ -n "$FIRST_TRY" ]] && [[ -z "$DBPASS" ]] && DBPASS="$DEFAULT_DBPASS" &&
142 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" &&
143 continue
144 echo "$DB_ERROR"
145 [[ -n "$QUIET_MODE" ]] && echo -e "Invalid database credentials!!!" >&2 && exit 1
146 echo -e "Provide database name and credentials (Ctrl+c to abort):"
147 read -e -p " mysql database name($DBNAME): " KK
148 [ -n "$KK" ] && DBNAME="$KK"
149 read -e -p " mysql user($DBUSER): " KK
150 [ -n "$KK" ] && DBUSER="$KK"
151 read -e -s -p " mysql password: " DBPASS
152 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
153 FIRST_TRY=""
154 echo
155 done
156
157 DBCMD="mysql $DEF_EXTRA_FILE_PARAM $DBNAME"
158 DBUSER_="" && [ -n "$DBUSER" ] && DBUSER_="-u$DBUSER"
159 DBPASS_="" && [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
160 DBHOST_="" && [ -n "$DBHOST" ] && DBHOST_="-h$DBHOST"
161 DBPORT_="-P$DBPORT"
162
163 echo " loading ${DIRNAME}/mano_db_structure.sql"
164 sed -e "s/{{mano_db}}/$DBNAME/" ${DIRNAME}/mano_db_structure.sql | mysql $DEF_EXTRA_FILE_PARAM ||
165 ! echo "ERROR at init $DBNAME" || exit 1
166
167 echo " migrage database version"
168 # echo "${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION"
169 ${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION
170