blob: 248f821d33c2b98264d74dd7e18077e42fb12816 [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=""
31
tiernof7aa8c42016-09-06 16:43:04 +020032# Detect paths
33MYSQL=$(which mysql)
34AWK=$(which awk)
35GREP=$(which grep)
tierno95a9e832017-04-27 18:49:37 +020036DIRNAME=`dirname $(readlink -f $0)`
tiernof7aa8c42016-09-06 16:43:04 +020037
38function usage(){
tierno95a9e832017-04-27 18:49:37 +020039 echo -e "Usage: $0 OPTIONS [version]"
40 echo -e " Inits openvim database; deletes previous one and loads from ${DBNAME}_structure.sql"\
tiernof7aa8c42016-09-06 16:43:04 +020041 echo -e " and data from host_ranking.sql, nets.sql, of_ports_pci_correspondece*.sql"
tierno95a9e832017-04-27 18:49:37 +020042 "If [version] is not provided, it is upgraded to the last version"
tiernof7aa8c42016-09-06 16:43:04 +020043 echo -e " OPTIONS"
44 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
tierno95a9e832017-04-27 18:49:37 +020045 echo -e " -p PASS database password. If missing it tries without and '$DEFAULT_DBPASS' password before prompting"
tiernof7aa8c42016-09-06 16:43:04 +020046 echo -e " -P PORT database port. '$DBPORT' by default"
tierno95a9e832017-04-27 18:49:37 +020047 echo -e " -h HOST database host. 'localhost' by default"
tiernof7aa8c42016-09-06 16:43:04 +020048 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
tierno95a9e832017-04-27 18:49:37 +020049 echo -e " -q --quiet: Do not prompt for credentials and exit if cannot access to database"
tiernof7aa8c42016-09-06 16:43:04 +020050 echo -e " --help shows this help"
51}
52
tierno95a9e832017-04-27 18:49:37 +020053while getopts ":u:p:P:h:d:q-:" o; do
tiernof7aa8c42016-09-06 16:43:04 +020054 case "${o}" in
55 u)
56 DBUSER="$OPTARG"
57 ;;
58 p)
59 DBPASS="$OPTARG"
60 ;;
61 P)
62 DBPORT="$OPTARG"
63 ;;
64 d)
65 DBNAME="$OPTARG"
66 ;;
67 h)
68 DBHOST="$OPTARG"
69 ;;
tierno95a9e832017-04-27 18:49:37 +020070 q)
71 export QUIET_MODE="-q"
72 ;;
tiernof7aa8c42016-09-06 16:43:04 +020073 -)
74 [ "${OPTARG}" == "help" ] && usage && exit 0
tierno95a9e832017-04-27 18:49:37 +020075 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE="-q" && continue
76 echo "Invalid option: '--$OPTARG'. Type --help for more information" >&2
tiernof7aa8c42016-09-06 16:43:04 +020077 exit 1
78 ;;
79 \?)
tierno95a9e832017-04-27 18:49:37 +020080 echo "Invalid option: '-$OPTARG'. Type --help for more information" >&2
tiernof7aa8c42016-09-06 16:43:04 +020081 exit 1
82 ;;
83 :)
tierno95a9e832017-04-27 18:49:37 +020084 echo "Option '-$OPTARG' requires an argument. Type --help for more information" >&2
tiernof7aa8c42016-09-06 16:43:04 +020085 exit 1
86 ;;
87 *)
88 usage >&2
tierno95a9e832017-04-27 18:49:37 +020089 exit 1
tiernof7aa8c42016-09-06 16:43:04 +020090 ;;
91 esac
92done
93shift $((OPTIND-1))
94
tierno95a9e832017-04-27 18:49:37 +020095DB_VERSION=$1
96
97if [ -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
101fi
102
103# Creating temporary file
tierno115b64a2016-10-24 16:57:23 +0000104TEMPFILE="$(mktemp -q --tmpdir "initmanodb.XXXXXX")"
105trap 'rm -f "$TEMPFILE"' EXIT
106chmod 0600 "$TEMPFILE"
tierno115b64a2016-10-24 16:57:23 +0000107DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE"
tierno95a9e832017-04-27 18:49:37 +0200108echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
tierno115b64a2016-10-24 16:57:23 +0000109
tierno95a9e832017-04-27 18:49:37 +0200110# Check and ask for database user password
111FIRST_TRY="yes"
112while ! DB_ERROR=`mysql "$DEF_EXTRA_FILE_PARAM" $DBNAME -e "quit" 2>&1 >/dev/null`
tiernof7aa8c42016-09-06 16:43:04 +0200113do
tierno95a9e832017-04-27 18:49:37 +0200114 # 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" &&
117 continue
118 echo "$DB_ERROR"
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"
127 FIRST_TRY=""
128 echo
tiernof7aa8c42016-09-06 16:43:04 +0200129done
130
tierno95a9e832017-04-27 18:49:37 +0200131DBCMD="mysql $DEF_EXTRA_FILE_PARAM $DBNAME"
132DBUSER_="" && [ -n "$DBUSER" ] && DBUSER_="-u$DBUSER"
133DBPASS_="" && [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
134DBHOST_="" && [ -n "$DBHOST" ] && DBHOST_="-h$DBHOST"
135DBPORT_="-P$DBPORT"
tierno115b64a2016-10-24 16:57:23 +0000136
137
tiernof7aa8c42016-09-06 16:43:04 +0200138echo " loading ${DIRNAME}/vim_db_structure.sql"
tierno95a9e832017-04-27 18:49:37 +0200139sed -e "s/{{vim_db}}/$DBNAME/" ${DIRNAME}/vim_db_structure.sql | mysql $DEF_EXTRA_FILE_PARAM
tiernof7aa8c42016-09-06 16:43:04 +0200140
141echo " loading ${DIRNAME}/host_ranking.sql"
tierno95a9e832017-04-27 18:49:37 +0200142mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/host_ranking.sql
tiernof7aa8c42016-09-06 16:43:04 +0200143
144echo " loading ${DIRNAME}/of_ports_pci_correspondence.sql"
tierno95a9e832017-04-27 18:49:37 +0200145mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/of_ports_pci_correspondence.sql
tiernof7aa8c42016-09-06 16:43:04 +0200146#mysql -h $HOST -P $PORT -u $MUSER -p$MPASS $MDB < ${DIRNAME}/of_ports_pci_correspondence_centos.sql
147
148echo " loading ${DIRNAME}/nets.sql"
tierno95a9e832017-04-27 18:49:37 +0200149mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/nets.sql
150
151echo " 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
tiernof7aa8c42016-09-06 16:43:04 +0200154