blob: 147ea389c2b7116d5b1473381faab98ec9fe3312 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001#!/bin/bash
2
3##
tierno92021022018-09-12 16:29:23 +02004# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
tierno7edb6752016-03-21 17:37:52 +01005# 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=""
tierno11f81f62017-04-27 17:22:14 +020026DEFAULT_DBPASS="manopw"
27DBHOST=""
tierno7edb6752016-03-21 17:37:52 +010028DBPORT="3306"
29DBNAME="mano_db"
tierno11f81f62017-04-27 17:22:14 +020030QUIET_MODE=""
garciadeblas0c317ee2016-08-29 12:33:06 +020031CREATEDB=""
tierno7edb6752016-03-21 17:37:52 +010032
33# Detect paths
34MYSQL=$(which mysql)
35AWK=$(which awk)
36GREP=$(which grep)
tierno11f81f62017-04-27 17:22:14 +020037DIRNAME=`dirname $(readlink -f $0)`
tierno7edb6752016-03-21 17:37:52 +010038
39function usage(){
tierno11f81f62017-04-27 17:22:14 +020040 echo -e "Usage: $0 OPTIONS [version]"
41 echo -e " Inits openmano database; deletes previous one and loads from ${DBNAME}_structure.sql"\
42 echo -e " and data from host_ranking.sql, nets.sql, of_ports_pci_correspondece*.sql"
43 "If [version] is not provided, it is upgraded to the last version"
tierno7edb6752016-03-21 17:37:52 +010044 echo -e " OPTIONS"
45 echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails"
tierno11f81f62017-04-27 17:22:14 +020046 echo -e " -p PASS database password. If missing it tries without and '$DEFAULT_DBPASS' password before prompting"
tierno7edb6752016-03-21 17:37:52 +010047 echo -e " -P PORT database port. '$DBPORT' by default"
tierno11f81f62017-04-27 17:22:14 +020048 echo -e " -h HOST database host. 'localhost' by default"
tierno462e3882016-07-06 17:52:14 +020049 echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails"
tierno11f81f62017-04-27 17:22:14 +020050 echo -e " -q --quiet: Do not prompt for credentials and exit if cannot access to database"
garciadeblas0c317ee2016-08-29 12:33:06 +020051 echo -e " --createdb forces the deletion and creation of the database"
tierno11f81f62017-04-27 17:22:14 +020052 echo -e " --help shows this help"
tierno7edb6752016-03-21 17:37:52 +010053}
54
tierno11f81f62017-04-27 17:22:14 +020055while getopts ":u:p:P:h:d:q-:" o; do
tierno7edb6752016-03-21 17:37:52 +010056 case "${o}" in
57 u)
58 DBUSER="$OPTARG"
59 ;;
60 p)
61 DBPASS="$OPTARG"
62 ;;
63 P)
64 DBPORT="$OPTARG"
65 ;;
tierno462e3882016-07-06 17:52:14 +020066 d)
67 DBNAME="$OPTARG"
68 ;;
tierno7edb6752016-03-21 17:37:52 +010069 h)
70 DBHOST="$OPTARG"
71 ;;
tierno11f81f62017-04-27 17:22:14 +020072 q)
73 export QUIET_MODE="-q"
74 ;;
tierno7edb6752016-03-21 17:37:52 +010075 -)
tierno11f81f62017-04-27 17:22:14 +020076 [ "${OPTARG}" == "help" ] && usage && exit 0
77 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE="-q" && continue
78 [ "${OPTARG}" == "createdb" ] && export CREATEDB=yes && continue
79 echo "Invalid option: '--$OPTARG'. Type --help for more information" >&2
80 exit 1
tierno7edb6752016-03-21 17:37:52 +010081 ;;
82 \?)
tierno11f81f62017-04-27 17:22:14 +020083 echo "Invalid option: '-$OPTARG'. Type --help for more information" >&2
tierno7edb6752016-03-21 17:37:52 +010084 exit 1
85 ;;
86 :)
tierno11f81f62017-04-27 17:22:14 +020087 echo "Option '-$OPTARG' requires an argument. Type --help for more information" >&2
tierno7edb6752016-03-21 17:37:52 +010088 exit 1
89 ;;
90 *)
91 usage >&2
tierno11f81f62017-04-27 17:22:14 +020092 exit 1
tierno7edb6752016-03-21 17:37:52 +010093 ;;
94 esac
95done
96shift $((OPTIND-1))
97
tierno11f81f62017-04-27 17:22:14 +020098DB_VERSION=$1
garciadeblas89b3d842016-09-19 15:18:33 +020099
tierno11f81f62017-04-27 17:22:14 +0200100if [ -n "$DB_VERSION" ] ; then
101 # check it is a number and an allowed one
102 [ "$DB_VERSION" -eq "$DB_VERSION" ] 2>/dev/null ||
103 ! echo "parameter 'version' requires a integer value" >&2 || exit 1
104fi
105
106# Creating temporary file
107TEMPFILE="$(mktemp -q --tmpdir "initdb.XXXXXX")"
garciadeblas4b3b4462016-09-27 11:16:14 +0200108trap 'rm -f "$TEMPFILE"' EXIT
garciadeblas89b3d842016-09-19 15:18:33 +0200109chmod 0600 "$TEMPFILE"
garciadeblas89b3d842016-09-19 15:18:33 +0200110DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE"
tierno11f81f62017-04-27 17:22:14 +0200111echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
garciadeblas89b3d842016-09-19 15:18:33 +0200112
tierno11f81f62017-04-27 17:22:14 +0200113if [ -n "${CREATEDB}" ] ; then
114 FIRST_TRY="yes"
115 while ! DB_ERROR=`mysqladmin "$DEF_EXTRA_FILE_PARAM" -s status 2>&1 >/dev/null` ; do
116 # if password is not provided, try silently with $DEFAULT_DBPASS before exit or prompt for credentials
117 [[ -n "$FIRST_TRY" ]] && [[ -z "$DBPASS" ]] && DBPASS="$DEFAULT_DBPASS" &&
118 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" &&
119 continue
120 echo "$DB_ERROR"
121 [[ -n "$QUIET_MODE" ]] && echo -e "Invalid admin database credentials!!!" >&2 && exit 1
122 echo -e "Provide database credentials (Ctrl+c to abort):"
123 read -e -p " mysql user($DBUSER): " KK
garciadeblas89b3d842016-09-19 15:18:33 +0200124 [ -n "$KK" ] && DBUSER="$KK"
tierno11f81f62017-04-27 17:22:14 +0200125 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=""
tierno7edb6752016-03-21 17:37:52 +0100128 echo
tierno11f81f62017-04-27 17:22:14 +0200129 done
130 # echo " deleting previous database ${DBNAME} if it exists"
tierno0a693532017-05-04 15:30:55 +0200131 mysqladmin $DEF_EXTRA_FILE_PARAM DROP "${DBNAME}" -f && echo "Previous database deleted"
garciadeblas0c317ee2016-08-29 12:33:06 +0200132 echo " creating database ${DBNAME}"
tierno11f81f62017-04-27 17:22:14 +0200133 mysqladmin $DEF_EXTRA_FILE_PARAM create "${DBNAME}" || exit 1
garciadeblas0c317ee2016-08-29 12:33:06 +0200134fi
135
tierno11f81f62017-04-27 17:22:14 +0200136# Check and ask for database user password
137FIRST_TRY="yes"
138while ! DB_ERROR=`mysql "$DEF_EXTRA_FILE_PARAM" $DBNAME -e "quit" 2>&1 >/dev/null`
139do
140 # if password is not provided, try silently with $DEFAULT_DBPASS before exit or prompt for credentials
141 [[ -n "$FIRST_TRY" ]] && [[ -z "$DBPASS" ]] && DBPASS="$DEFAULT_DBPASS" &&
142 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" &&
143 continue
144 echo "$DB_ERROR"
145 [[ -n "$QUIET_MODE" ]] && echo -e "Invalid database credentials!!!" >&2 && exit 1
146 echo -e "Provide database name and credentials (Ctrl+c to abort):"
147 read -e -p " mysql database name($DBNAME): " KK
148 [ -n "$KK" ] && DBNAME="$KK"
149 read -e -p " mysql user($DBUSER): " KK
150 [ -n "$KK" ] && DBUSER="$KK"
151 read -e -s -p " mysql password: " DBPASS
152 echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
153 FIRST_TRY=""
154 echo
155done
156
157DBCMD="mysql $DEF_EXTRA_FILE_PARAM $DBNAME"
158DBUSER_="" && [ -n "$DBUSER" ] && DBUSER_="-u$DBUSER"
159DBPASS_="" && [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
160DBHOST_="" && [ -n "$DBHOST" ] && DBHOST_="-h$DBHOST"
161DBPORT_="-P$DBPORT"
162
163echo " loading ${DIRNAME}/mano_db_structure.sql"
tiernofc7df372018-12-21 10:19:38 +0000164sed -e "s/{{mano_db}}/$DBNAME/" ${DIRNAME}/mano_db_structure.sql | mysql $DEF_EXTRA_FILE_PARAM ||
165 ! echo "ERROR at init $DBNAME" || exit 1
tierno7edb6752016-03-21 17:37:52 +0100166
167echo " migrage database version"
tierno11f81f62017-04-27 17:22:14 +0200168# echo "${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION"
169${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION
tierno7edb6752016-03-21 17:37:52 +0100170