blob: 36b8003c62b55ceec9ccaf80d5572f7fd9c8ca3c [file] [log] [blame]
tiernoab8cb542017-04-21 15:41:58 +02001#!/usr/bin/env bash
2
3DB_NAME='mano_db'
4DB_ADMIN_USER="root"
5DB_USER="mano"
6DB_PASS="manopw"
7DB_ADMIN_PASSWD=""
8DB_PORT="3306"
9DB_HOST=""
10DB_HOST_PARAM=""
11QUIET_MODE=""
12FORCEDB=""
tierno443c84a2017-04-24 12:31:33 +020013UPDATEDB=""
tiernoab8cb542017-04-21 15:41:58 +020014NO_PACKAGES=""
15UNINSTALL=""
16
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030017
18function usage(){
19 echo -e "usage: sudo $0 [OPTIONS]"
tiernoab8cb542017-04-21 15:41:58 +020020 echo -e "Install openmano database server and the needed packages"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030021 echo -e " OPTIONS"
tiernoab8cb542017-04-21 15:41:58 +020022 echo -e " -U USER: database admin user. '$DB_ADMIN_USER' by default. Prompts if needed"
23 echo -e " -P PASS: database admin password to be used or installed. Prompts if needed"
24 echo -e " -d: database name, '$DB_NAME' by default"
25 echo -e " -u: database user, '$DB_USER' by default"
26 echo -e " -p: database pass, '$DB_PASS' by default"
27 echo -e " -H: HOST database host. 'localhost' by default"
28 echo -e " -T: PORT database port. '$DB_PORT' by default"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030029 echo -e " -q --quiet: install in unattended mode"
30 echo -e " -h --help: show this help"
tierno443c84a2017-04-24 12:31:33 +020031 echo -e " --forcedb: if database exists, it is dropped and a new one is created"
32 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 +030033 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 +020034 echo -e " --unistall: delete database"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030035}
36
tierno80fde982017-04-26 14:33:25 +020037function ask_user(){
38 # ask to the user and parse a response among 'y', 'yes', 'n' or 'no'. Case insensitive
39 # Params: $1 text to ask; $2 Action by default, can be 'y' for yes, 'n' for no, other or empty for not allowed
40 # Return: true(0) if user type 'yes'; false (1) if user type 'no'
41 read -e -p "$1" USER_CONFIRMATION
42 while true ; do
43 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'y' ] && return 0
44 [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'n' ] && return 1
45 [ "${USER_CONFIRMATION,,}" == "yes" ] || [ "${USER_CONFIRMATION,,}" == "y" ] && return 0
46 [ "${USER_CONFIRMATION,,}" == "no" ] || [ "${USER_CONFIRMATION,,}" == "n" ] && return 1
47 read -e -p "Please type 'yes' or 'no': " USER_CONFIRMATION
48 done
49}
50
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +030051function install_packages(){
52 [ -x /usr/bin/apt-get ] && apt-get install -y $*
53 [ -x /usr/bin/yum ] && yum install -y $*
54
55 #check properly installed
56 for PACKAGE in $*
57 do
58 PACKAGE_INSTALLED="no"
59 [ -x /usr/bin/apt-get ] && dpkg -l $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
60 [ -x /usr/bin/yum ] && yum list installed $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
61 if [ "$PACKAGE_INSTALLED" = "no" ]
62 then
63 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
64 exit 1
65 fi
66 done
67}
68
tiernoab8cb542017-04-21 15:41:58 +020069function _install_mysql_package(){
70 echo '
71 #################################################################
72 ##### INSTALL REQUIRED PACKAGES #####
73 #################################################################'
74 [ "$_DISTRO" == "Ubuntu" ] && ! install_packages "mysql-server" && exit 1
75 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && ! install_packages "mariadb mariadb-server" && exit 1
76
77 if [[ "$_DISTRO" == "Ubuntu" ]]
78 then
79 #start services. By default CentOS does not start services
80 service mysql start >> /dev/null
81 # try to set admin password, ignore if fails
82 [[ -n $DBPASSWD ]] && mysqladmin -u $DB_ADMIN_USER -s password $DB_ADMIN_PASSWD
83 fi
84
85 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
86 then
87 #start services. By default CentOS does not start services
88 service mariadb start
89 service httpd start
90 systemctl enable mariadb
91 systemctl enable httpd
tierno80fde982017-04-26 14:33:25 +020092 ask_user "Do you want to configure mariadb (recommended if not done before) (Y/n)? " y &&
93 mysql_secure_installation
tiernoab8cb542017-04-21 15:41:58 +020094
tierno80fde982017-04-26 14:33:25 +020095 ask_user "Do you want to set firewall to grant web access port 80,443 (Y/n)? " y &&
tiernoab8cb542017-04-21 15:41:58 +020096 firewall-cmd --permanent --zone=public --add-service=http &&
97 firewall-cmd --permanent --zone=public --add-service=https &&
98 firewall-cmd --reload
99 fi
100}
101
102function _create_db(){
103 echo '
104 #################################################################
tierno443c84a2017-04-24 12:31:33 +0200105 ##### CREATE AND INIT DATABASE #####
tiernoab8cb542017-04-21 15:41:58 +0200106 #################################################################'
tiernoab8cb542017-04-21 15:41:58 +0200107 echo "mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME}"
tierno443c84a2017-04-24 12:31:33 +0200108 mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME} \
109 || ! echo "Error creating ${DB_NAME} database" >&2 \
110 || exit 1
111 echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
112 || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
113 echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
114 || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
115 || exit 1
tiernoab8cb542017-04-21 15:41:58 +0200116 echo " Database '${DB_NAME}' created, user '$DB_USER' password '$DB_PASS'"
tierno443c84a2017-04-24 12:31:33 +0200117 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
118 ${DIRNAME}/init_mano_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
119 || ! echo "Error initializing database '$DB_NAME'" >&2 \
120 || exit 1
tiernoab8cb542017-04-21 15:41:58 +0200121}
122
tierno443c84a2017-04-24 12:31:33 +0200123function _delete_db(){
124 mysqladmin --defaults-extra-file="$TEMPFILE" -s drop "${DB_NAME}" $DBDELETEPARAM \
125 || ! echo "Error: Could not delete '${DB_NAME}' database" >&2 \
126 || exit 1
127}
128
129function _update_db(){
tiernoab8cb542017-04-21 15:41:58 +0200130 echo '
131 #################################################################
tierno443c84a2017-04-24 12:31:33 +0200132 ##### UPDATE DATABASE #####
tiernoab8cb542017-04-21 15:41:58 +0200133 #################################################################'
tierno443c84a2017-04-24 12:31:33 +0200134 echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
135 || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
136 echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
137 || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
138 || exit 1
139 echo " Granted privileges to user '$DB_USER' password '$DB_PASS' to existing database '${DB_NAME}'"
tiernoab8cb542017-04-21 15:41:58 +0200140 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
tierno443c84a2017-04-24 12:31:33 +0200141 ${DIRNAME}/migrate_mano_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
142 || ! echo "Error updating database '$DB_NAME'" >&2 \
143 || exit 1
tiernoab8cb542017-04-21 15:41:58 +0200144}
145
146function _uninstall_db(){
147echo '
148 #################################################################
149 ##### DELETE DATABASE #####
150 #################################################################'
151 DBDELETEPARAM=""
152 [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
tierno443c84a2017-04-24 12:31:33 +0200153 _delete_db
tiernoab8cb542017-04-21 15:41:58 +0200154}
155
156function db_exists(){ # (db_name, credential_file)
tierno107e4e32017-04-24 18:37:49 +0200157 # check credentials
158 mysqlshow --defaults-extra-file="$2" >/dev/null || exit 1
tierno070e95f2017-05-05 15:55:13 +0200159 if mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -w -q $1
160 then
tiernoab8cb542017-04-21 15:41:58 +0200161 # echo " DB $1 exists"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300162 return 0
163 fi
tiernoab8cb542017-04-21 15:41:58 +0200164 # echo " DB $1 does not exist"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300165 return 1
166}
167
tiernoab8cb542017-04-21 15:41:58 +0200168while getopts ":U:P:d:u:p:H:T:hiq-:" o; do
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300169 case "${o}" in
tiernoab8cb542017-04-21 15:41:58 +0200170 U)
171 export DB_ADMIN_USER="$OPTARG"
172 ;;
173 P)
174 export DB_ADMIN_PASSWD="$OPTARG"
175 ;;
176 d)
177 export DB_NAME="$OPTARG"
178 ;;
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300179 u)
tiernoab8cb542017-04-21 15:41:58 +0200180 export DB_USER="$OPTARG"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300181 ;;
182 p)
tiernoab8cb542017-04-21 15:41:58 +0200183 export DB_PASS="$OPTARG"
184 ;;
185 H)
186 export DB_HOST="$OPTARG"
187 export DB_HOST_PARAM="-h$DB_HOST"
188 ;;
189 T)
190 export DB_PORT="$OPTARG"
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300191 ;;
192 q)
193 export QUIET_MODE=yes
194 export DEBIAN_FRONTEND=noninteractive
195 ;;
196 h)
197 usage && exit 0
198 ;;
199 -)
200 [ "${OPTARG}" == "help" ] && usage && exit 0
201 [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
tierno443c84a2017-04-24 12:31:33 +0200202 [ "${OPTARG}" == "updatedb" ] && UPDATEDB="y" && continue
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300203 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
204 [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
tiernoab8cb542017-04-21 15:41:58 +0200205 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
206 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300207 exit 1
tiernoab8cb542017-04-21 15:41:58 +0200208 ;;
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300209 \?)
210 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
211 exit 1
212 ;;
213 :)
214 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
215 exit 1
216 ;;
217 *)
218 usage >&2
219 exit 1
220 ;;
221 esac
222done
tierno443c84a2017-04-24 12:31:33 +0200223if [ -n "$FORCEDB" ] && [ -n "$UPDATEDB" ] ; then
224 echo "Error: options --forcedb and --updatedb are mutually exclusive" >&2
225 exit 1
226fi
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300227
tiernoab8cb542017-04-21 15:41:58 +0200228# Discover Linux distribution
229# try redhat type
230[ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
231# if not assuming ubuntu type
232[ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null)
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300233
tiernof9975f12017-04-27 17:22:54 +0200234if [[ -z "$NO_PACKAGES" ]]
235then
236 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
237 _install_mysql_package || exit 1
238fi
239
tiernoab8cb542017-04-21 15:41:58 +0200240# Creating temporary file for MYSQL installation and initialization"
241TEMPFILE="$(mktemp -q --tmpdir "installdb.XXXXXX")"
242trap 'rm -f "$TEMPFILE"' EXIT
243chmod 0600 "$TEMPFILE"
244echo -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 +0300245
246#check and ask for database user password. Must be done after database installation
tiernoab8cb542017-04-21 15:41:58 +0200247if [[ -z $QUIET_MODE ]]
248then
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300249 echo -e "\nCheking database connection and ask for credentials"
tiernoab8cb542017-04-21 15:41:58 +0200250 # echo "mysqladmin --defaults-extra-file=$TEMPFILE -s status >/dev/null"
251 while ! mysqladmin --defaults-extra-file="$TEMPFILE" -s status >/dev/null
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300252 do
253 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
254 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
tiernoab8cb542017-04-21 15:41:58 +0200255 read -e -p "database admin user? ($DB_ADMIN_USER) " DBUSER_
256 [ -n "$DBUSER_" ] && DB_ADMIN_USER=$DBUSER_
257 read -e -s -p "database admin password? (Enter for not using password) " DBPASSWD_
258 [ -n "$DBPASSWD_" ] && DB_ADMIN_PASSWD="$DBPASSWD_"
259 [ -z "$DBPASSWD_" ] && DB_ADMIN_PASSWD=""
260 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 +0300261 logintry="yes"
262 done
263fi
264
tiernoab8cb542017-04-21 15:41:58 +0200265if [[ ! -z "$UNINSTALL" ]]
266then
267 _uninstall_db
268 exit
Gennadiy Dubina89cb0d12017-04-03 15:46:41 +0300269fi
270
tierno443c84a2017-04-24 12:31:33 +0200271# Create or update database
272if db_exists $DB_NAME $TEMPFILE ; then
273 if [[ -n $FORCEDB ]] ; then
274 # DBDELETEPARAM=""
275 # [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
276 DBDELETEPARAM="-f"
277 _delete_db
278 _create_db
279 elif [[ -n $UPDATEDB ]] ; then
280 _update_db
281 elif [[ -z $QUIET_MODE ]] ; then
282 echo "database '$DB_NAME' exist. Reinstall it?"
tierno80fde982017-04-26 14:33:25 +0200283 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 +0200284 _delete_db
285 _create_db
286 else
287 _update_db
288 fi
289 else
290 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"
291 exit 1
292 fi
293else
294 _create_db
295fi
296