blob: 16f34eac16366571d1ed10e92dd20baabfbd4ed8 [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=""
26DBHOST="localhost"
27DBPORT="3306"
28DBNAME="vim_db"
29
30# Detect paths
31MYSQL=$(which mysql)
32AWK=$(which awk)
33GREP=$(which grep)
34DIRNAME=`dirname $0`
35
36function usage(){
37 echo -e "Usage: $0 OPTIONS [{openvim_version}]"
38 echo -e " Inits openvim database; deletes previous one and loads from ${DBNAME}_structure.sql"
39 echo -e " and data from host_ranking.sql, nets.sql, of_ports_pci_correspondece*.sql"
40 echo -e " If openvim_version is not provided it tries to get from openvimd.py using relative path"
41 echo -e " OPTIONS"
42 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
tierno115b64a2016-10-24 16:57:23 +000043 echo -e " -p PASS database password. 'No password' or 'vimpw' by default. Prompts if DB access fails"
tiernof7aa8c42016-09-06 16:43:04 +020044 echo -e " -P PORT database port. '$DBPORT' by default"
45 echo -e " -h HOST database host. '$DBHOST' by default"
46 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
47 echo -e " --help shows this help"
48}
49
50while getopts ":u:p:P:h:d:-:" o; do
51 case "${o}" in
52 u)
53 DBUSER="$OPTARG"
54 ;;
55 p)
56 DBPASS="$OPTARG"
57 ;;
58 P)
59 DBPORT="$OPTARG"
60 ;;
61 d)
62 DBNAME="$OPTARG"
63 ;;
64 h)
65 DBHOST="$OPTARG"
66 ;;
67 -)
68 [ "${OPTARG}" == "help" ] && usage && exit 0
69 echo "Invalid option: --$OPTARG" >&2 && usage >&2
70 exit 1
71 ;;
72 \?)
73 echo "Invalid option: -$OPTARG" >&2 && usage >&2
74 exit 1
75 ;;
76 :)
77 echo "Option -$OPTARG requires an argument." >&2 && usage >&2
78 exit 1
79 ;;
80 *)
81 usage >&2
82 exit -1
83 ;;
84 esac
85done
86shift $((OPTIND-1))
87
tierno115b64a2016-10-24 16:57:23 +000088#Creating temporary file
89TEMPFILE="$(mktemp -q --tmpdir "initmanodb.XXXXXX")"
90trap 'rm -f "$TEMPFILE"' EXIT
91chmod 0600 "$TEMPFILE"
92
93#if password is missing, before prompting for it try without password and with "manopw"
tiernof7aa8c42016-09-06 16:43:04 +020094DBHOST_="-h$DBHOST"
95DBPORT_="-P$DBPORT"
tierno115b64a2016-10-24 16:57:23 +000096DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE"
97if [ -z "${DBPASS}" ]
98then
99 password_ok=""
100 echo -e "[client]\nuser='${DBUSER}'\npassword='vimpw'" > "$TEMPFILE"
101 mysql --defaults-extra-file="$TEMPFILE" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 && DBPASS="vimpw"
102 echo -e "[client]\nuser='${DBUSER}'\npassword=''" > "$TEMPFILE"
103 mysql --defaults-extra-file="$TEMPFILE" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 && DBPASS=""
104fi
105echo -e "[client]\nuser='${DBUSER}'\npassword='${DBPASS}'" > "$TEMPFILE"
106
107#check and ask for database user password
108while ! mysql "$DEF_EXTRA_FILE_PARAM" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1
tiernof7aa8c42016-09-06 16:43:04 +0200109do
110 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
111 [ -z "$logintry" ] && echo -e "\nProvide database name and credentials"
112 read -e -p "mysql database name($DBNAME): " KK
113 [ -n "$KK" ] && DBNAME="$KK"
114 read -e -p "mysql user($DBUSER): " KK
tierno115b64a2016-10-24 16:57:23 +0000115 [ -n "$KK" ] && DBUSER="$KK"
tiernof7aa8c42016-09-06 16:43:04 +0200116 read -e -s -p "mysql password: " DBPASS
tierno115b64a2016-10-24 16:57:23 +0000117 echo -e "[client]\nuser='${DBUSER}'\npassword='${DBPASS}'" > "$TEMPFILE"
118 logintry="yes"
tiernof7aa8c42016-09-06 16:43:04 +0200119 echo
120done
121
tierno115b64a2016-10-24 16:57:23 +0000122DBCMD="mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME"
123DBUSER_="-u$DBUSER"
124DBPASS_="-p$DBPASS"
125
126
tiernof7aa8c42016-09-06 16:43:04 +0200127echo " loading ${DIRNAME}/vim_db_structure.sql"
tierno115b64a2016-10-24 16:57:23 +0000128sed -e "s/vim_db/$DBNAME/" ${DIRNAME}/vim_db_structure.sql | mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_
tiernof7aa8c42016-09-06 16:43:04 +0200129
130echo " migrage database version"
131${DIRNAME}/migrate_vim_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $1
132
133echo " loading ${DIRNAME}/host_ranking.sql"
tierno115b64a2016-10-24 16:57:23 +0000134mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME < ${DIRNAME}/host_ranking.sql
tiernof7aa8c42016-09-06 16:43:04 +0200135
136echo " loading ${DIRNAME}/of_ports_pci_correspondence.sql"
tierno115b64a2016-10-24 16:57:23 +0000137mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME < ${DIRNAME}/of_ports_pci_correspondence.sql
tiernof7aa8c42016-09-06 16:43:04 +0200138#mysql -h $HOST -P $PORT -u $MUSER -p$MPASS $MDB < ${DIRNAME}/of_ports_pci_correspondence_centos.sql
139
140echo " loading ${DIRNAME}/nets.sql"
tierno115b64a2016-10-24 16:57:23 +0000141mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME < ${DIRNAME}/nets.sql
tiernof7aa8c42016-09-06 16:43:04 +0200142