blob: 8ef780c9826e98932d4236b6f240ceb094156d72 [file] [log] [blame]
tiernoab8cb542017-04-21 15:41:58 +02001#!/usr/bin/env bash
2
tiernoed3e4d42019-10-21 15:31:27 +00003##
4# Copyright Telefonica Investigacion y Desarrollo, S.A.U.
5# All Rights Reserved.
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License. You may obtain
9# a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16# License for the specific language governing permissions and limitations
17# under the License.
18##
19
tiernoab8cb542017-04-21 15:41:58 +020020DB_NAME='mano_db'
21DB_ADMIN_USER="root"
22DB_USER="mano"
23DB_PASS="manopw"
24DB_ADMIN_PASSWD=""
25DB_PORT="3306"
26DB_HOST=""
27DB_HOST_PARAM=""
28QUIET_MODE=""
29FORCEDB=""
tierno443c84a2017-04-24 12:31:33 +020030UPDATEDB=""
tiernoab8cb542017-04-21 15:41:58 +020031NO_PACKAGES=""
32UNINSTALL=""
33
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030034
35function usage(){
36 echo -e "usage: sudo $0 [OPTIONS]"
tiernoab8cb542017-04-21 15:41:58 +020037 echo -e "Install openmano database server and the needed packages"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030038 echo -e " OPTIONS"
tiernoab8cb542017-04-21 15:41:58 +020039 echo -e " -U USER: database admin user. '$DB_ADMIN_USER' by default. Prompts if needed"
40 echo -e " -P PASS: database admin password to be used or installed. Prompts if needed"
41 echo -e " -d: database name, '$DB_NAME' by default"
42 echo -e " -u: database user, '$DB_USER' by default"
43 echo -e " -p: database pass, '$DB_PASS' by default"
44 echo -e " -H: HOST database host. 'localhost' by default"
45 echo -e " -T: PORT database port. '$DB_PORT' by default"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030046 echo -e " -q --quiet: install in unattended mode"
47 echo -e " -h --help: show this help"
tierno443c84a2017-04-24 12:31:33 +020048 echo -e " --forcedb: if database exists, it is dropped and a new one is created"
49 echo -e " --updatedb: if database exists, it preserves the content and it is updated to the needed version"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030050 echo -e " --no-install-packages: use this option to skip updating and installing the requires packages. This avoid wasting time if you are sure requires packages are present e.g. because of a previous installation"
tiernoab8cb542017-04-21 15:41:58 +020051 echo -e " --unistall: delete database"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030052}
53
tierno80fde982017-04-26 14:33:25 +020054function ask_user(){
55 # ask to the user and parse a response among 'y', 'yes', 'n' or 'no'. Case insensitive
56 # Params: $1 text to ask; $2 Action by default, can be 'y' for yes, 'n' for no, other or empty for not allowed
57 # Return: true(0) if user type 'yes'; false (1) if user type 'no'
58 read -e -p "$1" USER_CONFIRMATION
59 while true ; do
60 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'y' ] && return 0
61 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'n' ] && return 1
62 [ "${USER_CONFIRMATION,,}" == "yes" ] || [ "${USER_CONFIRMATION,,}" == "y" ] && return 0
63 [ "${USER_CONFIRMATION,,}" == "no" ] || [ "${USER_CONFIRMATION,,}" == "n" ] && return 1
64 read -e -p "Please type 'yes' or 'no': " USER_CONFIRMATION
65 done
66}
67
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030068function install_packages(){
69 [ -x /usr/bin/apt-get ] && apt-get install -y $*
70 [ -x /usr/bin/yum ] && yum install -y $*
71
72 #check properly installed
73 for PACKAGE in $*
74 do
75 PACKAGE_INSTALLED="no"
76 [ -x /usr/bin/apt-get ] && dpkg -l $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
77 [ -x /usr/bin/yum ] && yum list installed $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
78 if [ "$PACKAGE_INSTALLED" = "no" ]
79 then
80 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
81 exit 1
82 fi
83 done
84}
85
tiernoab8cb542017-04-21 15:41:58 +020086function _install_mysql_package(){
87 echo '
88 #################################################################
89 ##### INSTALL REQUIRED PACKAGES #####
90 #################################################################'
91 [ "$_DISTRO" == "Ubuntu" ] && ! install_packages "mysql-server" && exit 1
92 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && ! install_packages "mariadb mariadb-server" && exit 1
93
94 if [[ "$_DISTRO" == "Ubuntu" ]]
95 then
96 #start services. By default CentOS does not start services
97 service mysql start >> /dev/null
98 # try to set admin password, ignore if fails
99 [[ -n $DBPASSWD ]] && mysqladmin -u $DB_ADMIN_USER -s password $DB_ADMIN_PASSWD
100 fi
101
102 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
103 then
104 #start services. By default CentOS does not start services
105 service mariadb start
106 service httpd start
107 systemctl enable mariadb
108 systemctl enable httpd
tierno80fde982017-04-26 14:33:25 +0200109 ask_user "Do you want to configure mariadb (recommended if not done before) (Y/n)? " y &&
110 mysql_secure_installation
tiernoab8cb542017-04-21 15:41:58 +0200111
tierno80fde982017-04-26 14:33:25 +0200112 ask_user "Do you want to set firewall to grant web access port 80,443 (Y/n)? " y &&
tiernoab8cb542017-04-21 15:41:58 +0200113 firewall-cmd --permanent --zone=public --add-service=http &&
114 firewall-cmd --permanent --zone=public --add-service=https &&
115 firewall-cmd --reload
116 fi
117}
118
119function _create_db(){
120 echo '
121 #################################################################
tierno443c84a2017-04-24 12:31:33 +0200122 ##### CREATE AND INIT DATABASE #####
tiernoab8cb542017-04-21 15:41:58 +0200123 #################################################################'
tiernoab8cb542017-04-21 15:41:58 +0200124 echo "mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME}"
tierno443c84a2017-04-24 12:31:33 +0200125 mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME} \
126 || ! echo "Error creating ${DB_NAME} database" >&2 \
127 || exit 1
128 echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
129 || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
130 echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
131 || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
132 || exit 1
tiernoab8cb542017-04-21 15:41:58 +0200133 echo " Database '${DB_NAME}' created, user '$DB_USER' password '$DB_PASS'"
tierno443c84a2017-04-24 12:31:33 +0200134 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
135 ${DIRNAME}/init_mano_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
136 || ! echo "Error initializing database '$DB_NAME'" >&2 \
137 || exit 1
tiernoab8cb542017-04-21 15:41:58 +0200138}
139
tierno443c84a2017-04-24 12:31:33 +0200140function _delete_db(){
141 mysqladmin --defaults-extra-file="$TEMPFILE" -s drop "${DB_NAME}" $DBDELETEPARAM \
142 || ! echo "Error: Could not delete '${DB_NAME}' database" >&2 \
143 || exit 1
144}
145
146function _update_db(){
tiernoab8cb542017-04-21 15:41:58 +0200147 echo '
148 #################################################################
tierno443c84a2017-04-24 12:31:33 +0200149 ##### UPDATE DATABASE #####
tiernoab8cb542017-04-21 15:41:58 +0200150 #################################################################'
tierno443c84a2017-04-24 12:31:33 +0200151 echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
152 || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
153 echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
154 || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
155 || exit 1
156 echo " Granted privileges to user '$DB_USER' password '$DB_PASS' to existing database '${DB_NAME}'"
tiernoab8cb542017-04-21 15:41:58 +0200157 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
tierno443c84a2017-04-24 12:31:33 +0200158 ${DIRNAME}/migrate_mano_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
159 || ! echo "Error updating database '$DB_NAME'" >&2 \
160 || exit 1
tiernoab8cb542017-04-21 15:41:58 +0200161}
162
163function _uninstall_db(){
164echo '
165 #################################################################
166 ##### DELETE DATABASE #####
167 #################################################################'
168 DBDELETEPARAM=""
169 [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
tierno443c84a2017-04-24 12:31:33 +0200170 _delete_db
tiernoab8cb542017-04-21 15:41:58 +0200171}
172
173function db_exists(){ # (db_name, credential_file)
tierno107e4e32017-04-24 18:37:49 +0200174 # check credentials
175 mysqlshow --defaults-extra-file="$2" >/dev/null || exit 1
tierno070e95f2017-05-05 15:55:13 +0200176 if mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -w -q $1
177 then
tiernoab8cb542017-04-21 15:41:58 +0200178 # echo " DB $1 exists"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300179 return 0
180 fi
tiernoab8cb542017-04-21 15:41:58 +0200181 # echo " DB $1 does not exist"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300182 return 1
183}
184
tiernoab8cb542017-04-21 15:41:58 +0200185while getopts ":U:P:d:u:p:H:T:hiq-:" o; do
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300186 case "${o}" in
tiernoab8cb542017-04-21 15:41:58 +0200187 U)
188 export DB_ADMIN_USER="$OPTARG"
189 ;;
190 P)
191 export DB_ADMIN_PASSWD="$OPTARG"
192 ;;
193 d)
194 export DB_NAME="$OPTARG"
195 ;;
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300196 u)
tiernoab8cb542017-04-21 15:41:58 +0200197 export DB_USER="$OPTARG"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300198 ;;
199 p)
tiernoab8cb542017-04-21 15:41:58 +0200200 export DB_PASS="$OPTARG"
201 ;;
202 H)
203 export DB_HOST="$OPTARG"
204 export DB_HOST_PARAM="-h$DB_HOST"
205 ;;
206 T)
207 export DB_PORT="$OPTARG"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300208 ;;
209 q)
210 export QUIET_MODE=yes
211 export DEBIAN_FRONTEND=noninteractive
212 ;;
213 h)
214 usage && exit 0
215 ;;
216 -)
217 [ "${OPTARG}" == "help" ] && usage && exit 0
218 [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
tierno443c84a2017-04-24 12:31:33 +0200219 [ "${OPTARG}" == "updatedb" ] && UPDATEDB="y" && continue
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300220 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
221 [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
tiernoab8cb542017-04-21 15:41:58 +0200222 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
223 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300224 exit 1
tiernoab8cb542017-04-21 15:41:58 +0200225 ;;
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300226 \?)
227 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
228 exit 1
229 ;;
230 :)
231 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
232 exit 1
233 ;;
234 *)
235 usage >&2
236 exit 1
237 ;;
238 esac
239done
tierno443c84a2017-04-24 12:31:33 +0200240if [ -n "$FORCEDB" ] && [ -n "$UPDATEDB" ] ; then
241 echo "Error: options --forcedb and --updatedb are mutually exclusive" >&2
242 exit 1
243fi
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300244
tiernoab8cb542017-04-21 15:41:58 +0200245# Discover Linux distribution
246# try redhat type
247[ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
248# if not assuming ubuntu type
249[ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null)
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300250
tiernof9975f12017-04-27 17:22:54 +0200251if [[ -z "$NO_PACKAGES" ]]
252then
253 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
254 _install_mysql_package || exit 1
255fi
256
tiernoab8cb542017-04-21 15:41:58 +0200257# Creating temporary file for MYSQL installation and initialization"
258TEMPFILE="$(mktemp -q --tmpdir "installdb.XXXXXX")"
259trap 'rm -f "$TEMPFILE"' EXIT
260chmod 0600 "$TEMPFILE"
261echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300262
263#check and ask for database user password. Must be done after database installation
tiernoab8cb542017-04-21 15:41:58 +0200264if [[ -z $QUIET_MODE ]]
265then
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300266 echo -e "\nCheking database connection and ask for credentials"
tiernoab8cb542017-04-21 15:41:58 +0200267 # echo "mysqladmin --defaults-extra-file=$TEMPFILE -s status >/dev/null"
268 while ! mysqladmin --defaults-extra-file="$TEMPFILE" -s status >/dev/null
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300269 do
270 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
271 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
tiernoab8cb542017-04-21 15:41:58 +0200272 read -e -p "database admin user? ($DB_ADMIN_USER) " DBUSER_
273 [ -n "$DBUSER_" ] && DB_ADMIN_USER=$DBUSER_
274 read -e -s -p "database admin password? (Enter for not using password) " DBPASSWD_
275 [ -n "$DBPASSWD_" ] && DB_ADMIN_PASSWD="$DBPASSWD_"
276 [ -z "$DBPASSWD_" ] && DB_ADMIN_PASSWD=""
277 echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300278 logintry="yes"
279 done
280fi
281
tiernoab8cb542017-04-21 15:41:58 +0200282if [[ ! -z "$UNINSTALL" ]]
283then
284 _uninstall_db
285 exit
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300286fi
287
tierno443c84a2017-04-24 12:31:33 +0200288# Create or update database
289if db_exists $DB_NAME $TEMPFILE ; then
290 if [[ -n $FORCEDB ]] ; then
291 # DBDELETEPARAM=""
292 # [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
293 DBDELETEPARAM="-f"
294 _delete_db
295 _create_db
296 elif [[ -n $UPDATEDB ]] ; then
297 _update_db
298 elif [[ -z $QUIET_MODE ]] ; then
299 echo "database '$DB_NAME' exist. Reinstall it?"
tierno80fde982017-04-26 14:33:25 +0200300 if ask_user "Type 'y' to drop and reinstall existing database (content will be lost), Type 'n' to update existing database (y/N)? " n ; then
tierno443c84a2017-04-24 12:31:33 +0200301 _delete_db
302 _create_db
303 else
304 _update_db
305 fi
306 else
307 echo "Database '$DB_NAME' exists. Use option '--forcedb' to force the deletion of the existing one, or '--updatedb' to use existing one and update it"
308 exit 1
309 fi
310else
311 _create_db
312fi
313