blob: cbb76ef4055c7440296f02e6402e356ed3bc875b [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001#!/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
24DBUSER="mano"
25DBPASS=""
26DBHOST="localhost"
27DBPORT="3306"
28DBNAME="mano_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"
38 echo -e " Inits openmano database; deletes previous one and loads from ${DBNAME}_structure.sql"
39 echo -e " OPTIONS"
40 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
41 echo -e " -p PASS database password. 'No password' by default. Prompts if DB access fails"
42 echo -e " -P PORT database port. '$DBPORT' by default"
43 echo -e " -h HOST database host. '$DBHOST' by default"
tierno462e3882016-07-06 17:52:14 +020044 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
tierno7edb6752016-03-21 17:37:52 +010045 echo -e " --help shows this help"
46}
47
tierno462e3882016-07-06 17:52:14 +020048while getopts ":u:p:P:d:h:-:" o; do
tierno7edb6752016-03-21 17:37:52 +010049 case "${o}" in
50 u)
51 DBUSER="$OPTARG"
52 ;;
53 p)
54 DBPASS="$OPTARG"
55 ;;
56 P)
57 DBPORT="$OPTARG"
58 ;;
tierno462e3882016-07-06 17:52:14 +020059 d)
60 DBNAME="$OPTARG"
61 ;;
tierno7edb6752016-03-21 17:37:52 +010062 h)
63 DBHOST="$OPTARG"
64 ;;
65 -)
66 [ "${OPTARG}" == "help" ] && usage && exit 0
67 echo "Invalid option: --$OPTARG" >&2 && usage >&2
68 exit 1
69 ;;
70 \?)
71 echo "Invalid option: -$OPTARG" >&2 && usage >&2
72 exit 1
73 ;;
74 :)
75 echo "Option -$OPTARG requires an argument." >&2 && usage >&2
76 exit 1
77 ;;
78 *)
79 usage >&2
80 exit -1
81 ;;
82 esac
83done
84shift $((OPTIND-1))
85
86#check and ask for database user password
87DBUSER_="-u$DBUSER"
88DBPASS_=""
89[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
90DBHOST_="-h$DBHOST"
91DBPORT_="-P$DBPORT"
92while ! echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ >/dev/null 2>&1
93do
94 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
95 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
96# read -e -p "mysql database name($DBNAME): " KK
97# [ -n "$KK" ] && DBNAME="$KK"
98 read -e -p "mysql user($DBUSER): " KK
99 [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER"
100 read -e -s -p "mysql password: " DBPASS
101 [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
102 [ -z "$DBPASS" ] && DBPASS_=""
103 logintry="yes"
104 echo
105done
106
107#${DIRNAME}/quick_delete_db.sh $MUSER $MPASS $MDB $HOST $PORT
108echo " loading ${DIRNAME}/${DBNAME}_structure.sql"
tierno462e3882016-07-06 17:52:14 +0200109mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME < ${DIRNAME}/mano_db_structure.sql
tierno7edb6752016-03-21 17:37:52 +0100110
111echo " migrage database version"
112${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME
113