Initial openvim v0.4.6 upload
[osm/openvim.git] / database_utils / init_vim_db.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # 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
24 DBUSER="vim"
25 DBPASS=""
26 DBHOST="localhost"
27 DBPORT="3306"
28 DBNAME="vim_db"
29
30 # Detect paths
31 MYSQL=$(which mysql)
32 AWK=$(which awk)
33 GREP=$(which grep)
34 DIRNAME=`dirname $0`
35
36 function 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"
43 echo -e " -p PASS database password. 'No password' by default. Prompts if DB access fails"
44 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
50 while 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
85 done
86 shift $((OPTIND-1))
87
88 #check and ask for database user password
89 DBUSER_="-u$DBUSER"
90 DBPASS_=""
91 [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
92 DBHOST_="-h$DBHOST"
93 DBPORT_="-P$DBPORT"
94 while ! echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME >/dev/null 2>&1
95 do
96 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
97 [ -z "$logintry" ] && echo -e "\nProvide database name and credentials"
98 read -e -p "mysql database name($DBNAME): " KK
99 [ -n "$KK" ] && DBNAME="$KK"
100 read -e -p "mysql user($DBUSER): " KK
101 [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER"
102 read -e -s -p "mysql password: " DBPASS
103 [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
104 [ -z "$DBPASS" ] && DBPASS_=""
105 logintry="yes":
106 echo
107 done
108
109 echo " loading ${DIRNAME}/vim_db_structure.sql"
110 sed -e "s/vim_db/$DBNAME/" ${DIRNAME}/vim_db_structure.sql | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_
111
112 echo " migrage database version"
113 ${DIRNAME}/migrate_vim_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $1
114
115 echo " loading ${DIRNAME}/host_ranking.sql"
116 mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME < ${DIRNAME}/host_ranking.sql
117
118 echo " loading ${DIRNAME}/of_ports_pci_correspondence.sql"
119 mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME < ${DIRNAME}/of_ports_pci_correspondence.sql
120 #mysql -h $HOST -P $PORT -u $MUSER -p$MPASS $MDB < ${DIRNAME}/of_ports_pci_correspondence_centos.sql
121
122 echo " loading ${DIRNAME}/nets.sql"
123 mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME < ${DIRNAME}/nets.sql
124