blob: a15627ba2528fe63aa0d6a035620e6fa35c253a7 [file] [log] [blame]
tiernof7aa8c42016-09-06 16:43:04 +02001#!/bin/bash
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
tierno9a61c6b2016-09-08 10:57:02 +02005# This file is part of openvim
tiernof7aa8c42016-09-06 16:43:04 +02006# 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
24DBUSER="vim"
25DBPASS=""
tierno95a9e832017-04-27 18:49:37 +020026DEFAULT_DBPASS="vimpw"
27DBHOST=""
tiernof7aa8c42016-09-06 16:43:04 +020028DBPORT="3306"
29DBNAME="vim_db"
tierno95a9e832017-04-27 18:49:37 +020030QUIET_MODE=""
tierno9f72b5d2017-05-03 14:02:30 +020031CREATEDB=""
tierno95a9e832017-04-27 18:49:37 +020032
tiernof7aa8c42016-09-06 16:43:04 +020033# Detect paths
34MYSQL=$(which mysql)
35AWK=$(which awk)
36GREP=$(which grep)
tierno95a9e832017-04-27 18:49:37 +020037DIRNAME=`dirname $(readlink -f $0)`
tiernof7aa8c42016-09-06 16:43:04 +020038
39function usage(){
tierno95a9e832017-04-27 18:49:37 +020040 echo -e "Usage: $0 OPTIONS [version]"
41 echo -e " Inits openvim database; deletes previous one and loads from ${DBNAME}_structure.sql"\
tiernof7aa8c42016-09-06 16:43:04 +020042 echo -e " and data from host_ranking.sql, nets.sql, of_ports_pci_correspondece*.sql"
tierno95a9e832017-04-27 18:49:37 +020043 "If [version] is not provided, it is upgraded to the last version"
tiernof7aa8c42016-09-06 16:43:04 +020044 echo -e " OPTIONS"
45 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
tierno95a9e832017-04-27 18:49:37 +020046 echo -e " -p PASS database password. If missing it tries without and '$DEFAULT_DBPASS' password before prompting"
tiernof7aa8c42016-09-06 16:43:04 +020047 echo -e " -P PORT database port. '$DBPORT' by default"
tierno95a9e832017-04-27 18:49:37 +020048 echo -e " -h HOST database host. 'localhost' by default"
tiernof7aa8c42016-09-06 16:43:04 +020049 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
tierno95a9e832017-04-27 18:49:37 +020050 echo -e " -q --quiet: Do not prompt for credentials and exit if cannot access to database"
tierno9f72b5d2017-05-03 14:02:30 +020051 echo -e " --createdb forces the deletion and creation of the database"
tiernof7aa8c42016-09-06 16:43:04 +020052 echo -e " --help shows this help"
53}
54
tierno95a9e832017-04-27 18:49:37 +020055while getopts ":u:p:P:h:d:q-:" o; do
tiernof7aa8c42016-09-06 16:43:04 +020056 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 ;;
tierno95a9e832017-04-27 18:49:37 +020072 q)
73 export QUIET_MODE="-q"
74 ;;
tiernof7aa8c42016-09-06 16:43:04 +020075 -)
76 [ "${OPTARG}" == "help" ] && usage && exit 0
tierno95a9e832017-04-27 18:49:37 +020077 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE="-q" && continue
tierno9f72b5d2017-05-03 14:02:30 +020078 [ "${OPTARG}" == "createdb" ] && export CREATEDB=yes && continue
tierno95a9e832017-04-27 18:49:37 +020079 echo "Invalid option: '--$OPTARG'. Type --help for more information" >&2
tiernof7aa8c42016-09-06 16:43:04 +020080 exit 1
81 ;;
82 \?)
tierno95a9e832017-04-27 18:49:37 +020083 echo "Invalid option: '-$OPTARG'. Type --help for more information" >&2
tiernof7aa8c42016-09-06 16:43:04 +020084 exit 1
85 ;;
86 :)
tierno95a9e832017-04-27 18:49:37 +020087 echo "Option '-$OPTARG' requires an argument. Type --help for more information" >&2
tiernof7aa8c42016-09-06 16:43:04 +020088 exit 1
89 ;;
90 *)
91 usage >&2
tierno95a9e832017-04-27 18:49:37 +020092 exit 1
tiernof7aa8c42016-09-06 16:43:04 +020093 ;;
94 esac
95done
96shift $((OPTIND-1))
97
tierno95a9e832017-04-27 18:49:37 +020098DB_VERSION=$1
99
100if [ -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
104fi
105
106# Creating temporary file
tierno9f72b5d2017-05-03 14:02:30 +0200107TEMPFILE="$(mktemp -q --tmpdir "initdb.XXXXXX")"
tierno115b64a2016-10-24 16:57:23 +0000108trap 'rm -f "$TEMPFILE"' EXIT
109chmod 0600 "$TEMPFILE"
tierno115b64a2016-10-24 16:57:23 +0000110DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE"
tierno95a9e832017-04-27 18:49:37 +0200111echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
tierno115b64a2016-10-24 16:57:23 +0000112
tierno9f72b5d2017-05-03 14:02:30 +0200113if [ -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}" ${QUIET_MODE/q/f} && echo "Previous database deleted"
132 echo " creating database ${DBNAME}"
133 mysqladmin $DEF_EXTRA_FILE_PARAM create "${DBNAME}" || exit 1
134fi
135
tierno95a9e832017-04-27 18:49:37 +0200136# Check and ask for database user password
137FIRST_TRY="yes"
138while ! DB_ERROR=`mysql "$DEF_EXTRA_FILE_PARAM" $DBNAME -e "quit" 2>&1 >/dev/null`
tiernof7aa8c42016-09-06 16:43:04 +0200139do
tierno95a9e832017-04-27 18:49:37 +0200140 # 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
tiernof7aa8c42016-09-06 16:43:04 +0200155done
156
tierno95a9e832017-04-27 18:49:37 +0200157DBCMD="mysql $DEF_EXTRA_FILE_PARAM $DBNAME"
158DBUSER_="" && [ -n "$DBUSER" ] && DBUSER_="-u$DBUSER"
159DBPASS_="" && [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
160DBHOST_="" && [ -n "$DBHOST" ] && DBHOST_="-h$DBHOST"
161DBPORT_="-P$DBPORT"
tierno115b64a2016-10-24 16:57:23 +0000162
163
tiernof7aa8c42016-09-06 16:43:04 +0200164echo " loading ${DIRNAME}/vim_db_structure.sql"
tierno95a9e832017-04-27 18:49:37 +0200165sed -e "s/{{vim_db}}/$DBNAME/" ${DIRNAME}/vim_db_structure.sql | mysql $DEF_EXTRA_FILE_PARAM
tiernof7aa8c42016-09-06 16:43:04 +0200166
167echo " loading ${DIRNAME}/host_ranking.sql"
tierno95a9e832017-04-27 18:49:37 +0200168mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/host_ranking.sql
tiernof7aa8c42016-09-06 16:43:04 +0200169
170echo " loading ${DIRNAME}/of_ports_pci_correspondence.sql"
tierno95a9e832017-04-27 18:49:37 +0200171mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/of_ports_pci_correspondence.sql
tiernof7aa8c42016-09-06 16:43:04 +0200172#mysql -h $HOST -P $PORT -u $MUSER -p$MPASS $MDB < ${DIRNAME}/of_ports_pci_correspondence_centos.sql
173
174echo " loading ${DIRNAME}/nets.sql"
tierno95a9e832017-04-27 18:49:37 +0200175mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/nets.sql
176
177echo " 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
tiernof7aa8c42016-09-06 16:43:04 +0200180