4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
26 DEFAULT_DBPASS
="vimpw"
37 DIRNAME
=`dirname $(readlink -f $0)`
40 echo -e "Usage: $0 OPTIONS [version]"
41 echo -e " Inits openvim 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"
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"
55 while getopts ":u:p:P:h:d:q-:" o
; do
73 export QUIET_MODE
="-q"
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
83 echo "Invalid option: '-$OPTARG'. Type --help for more information" >&2
87 echo "Option '-$OPTARG' requires an argument. Type --help for more information" >&2
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
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"
113 if [ -n "${CREATEDB}" ] ; then
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" &&
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"
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
136 # Check and ask for database user password
138 while ! DB_ERROR
=`mysql "$DEF_EXTRA_FILE_PARAM" $DBNAME -e "quit" 2>&1 >/dev/null`
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" &&
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"
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"
164 echo " loading ${DIRNAME}/vim_db_structure.sql"
165 sed -e "s/{{vim_db}}/$DBNAME/" ${DIRNAME}/vim_db_structure.sql | mysql
$DEF_EXTRA_FILE_PARAM
167 echo " loading ${DIRNAME}/host_ranking.sql"
168 mysql
$DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/host_ranking.sql
170 echo " loading ${DIRNAME}/of_ports_pci_correspondence.sql"
171 mysql
$DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/of_ports_pci_correspondence.sql
172 #mysql -h $HOST -P $PORT -u $MUSER -p$MPASS $MDB < ${DIRNAME}/of_ports_pci_correspondence_centos.sql
174 echo " loading ${DIRNAME}/nets.sql"
175 mysql
$DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/nets.sql
177 echo " migrage database version"
178 # echo "${DIRNAME}/migrate_vim_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION"
179 ${DIRNAME}/migrate_vim_db.sh
$DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION