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"
36 DIRNAME
=`dirname $(readlink -f $0)`
39 echo -e "Usage: $0 OPTIONS [version]"
40 echo -e " Inits openvim database; deletes previous one and loads from ${DBNAME}_structure.sql"\
41 echo -e " and data from host_ranking.sql, nets.sql, of_ports_pci_correspondece*.sql"
42 "If [version] is not provided, it is upgraded to the last version"
44 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
45 echo -e " -p PASS database password. If missing it tries without and '$DEFAULT_DBPASS' password before prompting"
46 echo -e " -P PORT database port. '$DBPORT' by default"
47 echo -e " -h HOST database host. 'localhost' by default"
48 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
49 echo -e " -q --quiet: Do not prompt for credentials and exit if cannot access to database"
50 echo -e " --help shows this help"
53 while getopts ":u:p:P:h:d:q-:" o
; do
71 export QUIET_MODE
="-q"
74 [ "${OPTARG}" == "help" ] && usage
&& exit 0
75 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE
="-q" && continue
76 echo "Invalid option: '--$OPTARG'. Type --help for more information" >&2
80 echo "Invalid option: '-$OPTARG'. Type --help for more information" >&2
84 echo "Option '-$OPTARG' requires an argument. Type --help for more information" >&2
97 if [ -n "$DB_VERSION" ] ; then
98 # check it is a number and an allowed one
99 [ "$DB_VERSION" -eq "$DB_VERSION" ] 2>/dev
/null ||
100 ! echo "parameter 'version' requires a integer value" >&2 ||
exit 1
103 # Creating temporary file
104 TEMPFILE
="$(mktemp -q --tmpdir "initmanodb.XXXXXX
")"
105 trap 'rm -f "$TEMPFILE"' EXIT
106 chmod 0600 "$TEMPFILE"
107 DEF_EXTRA_FILE_PARAM
="--defaults-extra-file=$TEMPFILE"
108 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
110 # Check and ask for database user password
112 while ! DB_ERROR
=`mysql "$DEF_EXTRA_FILE_PARAM" $DBNAME -e "quit" 2>&1 >/dev/null`
114 # if password is not provided, try silently with $DEFAULT_DBPASS before exit or prompt for credentials
115 [[ -n "$FIRST_TRY" ]] && [[ -z "$DBPASS" ]] && DBPASS
="$DEFAULT_DBPASS" &&
116 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" &&
119 [[ -n "$QUIET_MODE" ]] && echo -e "Invalid database credentials!!!" >&2 && exit 1
120 echo -e "Provide database name and credentials (Ctrl+c to abort):"
121 read -e -p " mysql database name($DBNAME): " KK
122 [ -n "$KK" ] && DBNAME
="$KK"
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"
131 DBCMD
="mysql $DEF_EXTRA_FILE_PARAM $DBNAME"
132 DBUSER_
="" && [ -n "$DBUSER" ] && DBUSER_
="-u$DBUSER"
133 DBPASS_
="" && [ -n "$DBPASS" ] && DBPASS_
="-p$DBPASS"
134 DBHOST_
="" && [ -n "$DBHOST" ] && DBHOST_
="-h$DBHOST"
138 echo " loading ${DIRNAME}/vim_db_structure.sql"
139 sed -e "s/{{vim_db}}/$DBNAME/" ${DIRNAME}/vim_db_structure.sql | mysql
$DEF_EXTRA_FILE_PARAM
141 echo " loading ${DIRNAME}/host_ranking.sql"
142 mysql
$DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/host_ranking.sql
144 echo " loading ${DIRNAME}/of_ports_pci_correspondence.sql"
145 mysql
$DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/of_ports_pci_correspondence.sql
146 #mysql -h $HOST -P $PORT -u $MUSER -p$MPASS $MDB < ${DIRNAME}/of_ports_pci_correspondence_centos.sql
148 echo " loading ${DIRNAME}/nets.sql"
149 mysql
$DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/nets.sql
151 echo " migrage database version"
152 # echo "${DIRNAME}/migrate_vim_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION"
153 ${DIRNAME}/migrate_vim_db.sh
$DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION