changes at migrate_vim_db to use database version as a parameter and not use openvim...
[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 openvim
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 DEFAULT_DBPASS="vimpw"
27 DBHOST=""
28 DBPORT="3306"
29 DBNAME="vim_db"
30 QUIET_MODE=""
31
32 # Detect paths
33 MYSQL=$(which mysql)
34 AWK=$(which awk)
35 GREP=$(which grep)
36 DIRNAME=`dirname $(readlink -f $0)`
37
38 function usage(){
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"
43 echo -e " OPTIONS"
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"
51 }
52
53 while getopts ":u:p:P:h:d:q-:" o; do
54 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 ;;
70 q)
71 export QUIET_MODE="-q"
72 ;;
73 -)
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
77 exit 1
78 ;;
79 \?)
80 echo "Invalid option: '-$OPTARG'. Type --help for more information" >&2
81 exit 1
82 ;;
83 :)
84 echo "Option '-$OPTARG' requires an argument. Type --help for more information" >&2
85 exit 1
86 ;;
87 *)
88 usage >&2
89 exit 1
90 ;;
91 esac
92 done
93 shift $((OPTIND-1))
94
95 DB_VERSION=$1
96
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
101 fi
102
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"
109
110 # Check and ask for database user password
111 FIRST_TRY="yes"
112 while ! DB_ERROR=`mysql "$DEF_EXTRA_FILE_PARAM" $DBNAME -e "quit" 2>&1 >/dev/null`
113 do
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" &&
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
129 done
130
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"
135 DBPORT_="-P$DBPORT"
136
137
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
140
141 echo " loading ${DIRNAME}/host_ranking.sql"
142 mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/host_ranking.sql
143
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
147
148 echo " loading ${DIRNAME}/nets.sql"
149 mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/nets.sql
150
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
154