install-db-server enhancement
[osm/openvim.git] / database_utils / install-db-server.sh
1 #!/usr/bin/env bash
2
3 DB_NAME='vim_db'
4 DB_ADMIN_USER="root"
5 DB_USER="vim"
6 DB_PASS="vimpw"
7 DB_ADMIN_PASSWD=""
8 DB_PORT="3306"
9 DB_HOST=""
10 DB_HOST_PARAM=""
11 QUIET_MODE=""
12 FORCEDB=""
13 NO_PACKAGES=""
14 UNINSTALL=""
15
16
17 function usage(){
18 echo -e "usage: sudo $0 [OPTIONS]"
19 echo -e "Install openvim database server and the needed packages"
20 echo -e " OPTIONS"
21 echo -e " -U USER: database admin user. '$DB_ADMIN_USER' by default. Prompts if needed"
22 echo -e " -P PASS: database admin password to be used or installed. Prompts if needed"
23 echo -e " -d: database name, '$DB_NAME' by default"
24 echo -e " -u: database user, '$DB_USER' by default"
25 echo -e " -p: database pass, '$DB_PASS' by default"
26 echo -e " -H: HOST database host. 'localhost' by default"
27 echo -e " -T: PORT database port. '$DB_PORT' by default"
28 echo -e " -q --quiet: install in unattended mode"
29 echo -e " -h --help: show this help"
30 echo -e " --forcedb: reinstall database, deleting previous database if exists and creating a new one"
31 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"
32 echo -e " --unistall: delete database"
33 }
34
35 function install_packages(){
36 [ -x /usr/bin/apt-get ] && apt-get install -y $*
37 [ -x /usr/bin/yum ] && yum install -y $*
38
39 #check properly installed
40 for PACKAGE in $*
41 do
42 PACKAGE_INSTALLED="no"
43 [ -x /usr/bin/apt-get ] && dpkg -l $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
44 [ -x /usr/bin/yum ] && yum list installed $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
45 if [ "$PACKAGE_INSTALLED" = "no" ]
46 then
47 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
48 exit 1
49 fi
50 done
51 }
52
53 function _install_mysql_package(){
54 echo '
55 #################################################################
56 ##### INSTALL REQUIRED PACKAGES #####
57 #################################################################'
58 [ "$_DISTRO" == "Ubuntu" ] && ! install_packages "mysql-server" && exit 1
59 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && ! install_packages "mariadb mariadb-server" && exit 1
60
61 if [[ "$_DISTRO" == "Ubuntu" ]]
62 then
63 #start services. By default CentOS does not start services
64 service mysql start >> /dev/null
65 # try to set admin password, ignore if fails
66 [[ -n $DBPASSWD ]] && mysqladmin -u $DB_ADMIN_USER -s password $DB_ADMIN_PASSWD
67 fi
68
69 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
70 then
71 #start services. By default CentOS does not start services
72 service mariadb start
73 service httpd start
74 systemctl enable mariadb
75 systemctl enable httpd
76 read -e -p "Do you want to configure mariadb (recommended if not done before) (Y/n)" KK
77 [ "$KK" != "n" -a "$KK" != "no" ] && mysql_secure_installation
78
79 read -e -p "Do you want to set firewall to grant web access port 80,443 (Y/n)" KK
80 [ "$KK" != "n" -a "$KK" != "no" ] &&
81 firewall-cmd --permanent --zone=public --add-service=http &&
82 firewall-cmd --permanent --zone=public --add-service=https &&
83 firewall-cmd --reload
84 fi
85 }
86
87 function _create_db(){
88 echo '
89 #################################################################
90 ##### CREATE DATABASE #####
91 #################################################################'
92
93 if db_exists $DB_NAME $TEMPFILE ; then
94 if [[ -n $FORCEDB ]]; then
95 DBDELETEPARAM=""
96 [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
97 mysqladmin --defaults-extra-file="$TEMPFILE" -s drop ${DB_NAME} $DBDELETEPARAM || ! echo "Could not delete ${DB_NAME} database" || exit 1
98 elif [[ -z $QUIET_MODE ]] ; then
99 read -e -p "Drop exiting database '$DB_NAME'. All the content will be lost (y/N)? " KK_
100 [ "$KK_" != "yes" ] && [ "$KK_" != "y" ] && echo "Aborted!" && exit 1
101 mysqladmin --defaults-extra-file="$TEMPFILE" -s drop ${DB_NAME} -f || ! echo "Could not delete ${DB_NAME} database" || exit 1
102 else
103 echo "Database '$DB_NAME' exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
104 fi
105 fi
106 echo "mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME}"
107 mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME} || ! echo "1 Error creating ${DB_NAME} database" || exit 1
108 echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist"
109 echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s || ! echo "Error: Granting privileges to user '$DB_USER' at database" || exit 1
110 echo " Database '${DB_NAME}' created, user '$DB_USER' password '$DB_PASS'"
111 }
112
113 function _init_db(){
114 echo '
115 #################################################################
116 ##### INIT DATABASE #####
117 #################################################################'
118 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
119 ${DIRNAME}/init_vim_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM || ! echo "Error initializing database '$DB_NAME'" || exit 1
120 }
121
122 function _uninstall_db(){
123 echo '
124 #################################################################
125 ##### DELETE DATABASE #####
126 #################################################################'
127 DBDELETEPARAM=""
128 [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
129 mysqladmin --defaults-extra-file="$TEMPFILE" -s drop "${DB_NAME}" $DBDELETEPARAM || ! echo "Error: Could not delete '${DB_NAME}' database" || exit 1
130
131 }
132
133 function db_exists(){ # (db_name, credential_file)
134 RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
135 if [ "$RESULT" == "$1" ]; then
136 # echo " DB $1 exists"
137 return 0
138 fi
139 # echo " DB $1 does not exist"
140 return 1
141 }
142
143 while getopts ":U:P:d:u:p:H:T:hiq-:" o; do
144 case "${o}" in
145 U)
146 export DB_ADMIN_USER="$OPTARG"
147 ;;
148 P)
149 export DB_ADMIN_PASSWD="$OPTARG"
150 ;;
151 d)
152 export DB_NAME="$OPTARG"
153 ;;
154 u)
155 export DB_USER="$OPTARG"
156 ;;
157 p)
158 export DB_PASS="$OPTARG"
159 ;;
160 H)
161 export DB_HOST="$OPTARG"
162 export DB_HOST_PARAM="-h$DB_HOST"
163 ;;
164 T)
165 export DB_PORT="$OPTARG"
166 ;;
167 q)
168 export QUIET_MODE=yes
169 export DEBIAN_FRONTEND=noninteractive
170 ;;
171 h)
172 usage && exit 0
173 ;;
174 -)
175 [ "${OPTARG}" == "help" ] && usage && exit 0
176 [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
177 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
178 [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
179 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
180 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
181 exit 1
182 ;;
183 \?)
184 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
185 exit 1
186 ;;
187 :)
188 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
189 exit 1
190 ;;
191 *)
192 usage >&2
193 exit 1
194 ;;
195 esac
196 done
197
198 # Discover Linux distribution
199 # try redhat type
200 [ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
201 # if not assuming ubuntu type
202 [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null)
203
204 # Creating temporary file for MYSQL installation and initialization"
205 TEMPFILE="$(mktemp -q --tmpdir "installdb.XXXXXX")"
206 trap 'rm -f "$TEMPFILE"' EXIT
207 chmod 0600 "$TEMPFILE"
208 echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE"
209
210 #check and ask for database user password. Must be done after database installation
211 if [[ -z $QUIET_MODE ]]
212 then
213 echo -e "\nCheking database connection and ask for credentials"
214 # echo "mysqladmin --defaults-extra-file=$TEMPFILE -s status >/dev/null"
215 while ! mysqladmin --defaults-extra-file="$TEMPFILE" -s status >/dev/null
216 do
217 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
218 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
219 read -e -p "database admin user? ($DB_ADMIN_USER) " DBUSER_
220 [ -n "$DBUSER_" ] && DB_ADMIN_USER=$DBUSER_
221 read -e -s -p "database admin password? (Enter for not using password) " DBPASSWD_
222 [ -n "$DBPASSWD_" ] && DB_ADMIN_PASSWD="$DBPASSWD_"
223 [ -z "$DBPASSWD_" ] && DB_ADMIN_PASSWD=""
224 echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE"
225 logintry="yes"
226 done
227 fi
228
229 if [[ ! -z "$UNINSTALL" ]]
230 then
231 _uninstall_db
232 exit
233 fi
234
235 if [[ -z "$NO_PACKAGES" ]]
236 then
237 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
238 _install_mysql_package || exit 1
239 fi
240
241 _create_db || exit 1
242 _init_db || exit 1