| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 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="mano" |
| 25 | DBPASS="" |
| 26 | DBHOST="localhost" |
| 27 | DBPORT="3306" |
| 28 | DBNAME="mano_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" |
| 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" |
| tierno | 462e388 | 2016-07-06 17:52:14 +0200 | [diff] [blame] | 44 | echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 45 | echo -e " --help shows this help" |
| 46 | } |
| 47 | |
| tierno | 462e388 | 2016-07-06 17:52:14 +0200 | [diff] [blame] | 48 | while getopts ":u:p:P:d:h:-:" o; do |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 49 | case "${o}" in |
| 50 | u) |
| 51 | DBUSER="$OPTARG" |
| 52 | ;; |
| 53 | p) |
| 54 | DBPASS="$OPTARG" |
| 55 | ;; |
| 56 | P) |
| 57 | DBPORT="$OPTARG" |
| 58 | ;; |
| tierno | 462e388 | 2016-07-06 17:52:14 +0200 | [diff] [blame] | 59 | d) |
| 60 | DBNAME="$OPTARG" |
| 61 | ;; |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 62 | 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 |
| 83 | done |
| 84 | shift $((OPTIND-1)) |
| 85 | |
| 86 | #check and ask for database user password |
| 87 | DBUSER_="-u$DBUSER" |
| 88 | DBPASS_="" |
| 89 | [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS" |
| 90 | DBHOST_="-h$DBHOST" |
| 91 | DBPORT_="-P$DBPORT" |
| 92 | while ! echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ >/dev/null 2>&1 |
| 93 | do |
| 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 |
| 105 | done |
| 106 | |
| 107 | #${DIRNAME}/quick_delete_db.sh $MUSER $MPASS $MDB $HOST $PORT |
| 108 | echo " loading ${DIRNAME}/${DBNAME}_structure.sql" |
| tierno | 462e388 | 2016-07-06 17:52:14 +0200 | [diff] [blame] | 109 | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME < ${DIRNAME}/mano_db_structure.sql |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 110 | |
| 111 | echo " migrage database version" |
| 112 | ${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME |
| 113 | |