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