inject_user_key routine fixes
[osm/RO.git] / database_utils / install-db-server.sh
1 #!/usr/bin/env bash
2
3 DB_NAME='mano_db'
4 DB_ADMIN_USER="root"
5 DB_USER="mano"
6 DB_PASS="manopw"
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 openmano 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 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
51 function 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
69 function _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
92 ask_user "Do you want to configure mariadb (recommended if not done before) (Y/n)? " y &&
93 mysql_secure_installation
94
95 ask_user "Do you want to set firewall to grant web access port 80,443 (Y/n)? " y &&
96 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
102 function _create_db(){
103 echo '
104 #################################################################
105 ##### CREATE AND INIT DATABASE #####
106 #################################################################'
107 echo "mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME}"
108 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
116 echo " Database '${DB_NAME}' created, user '$DB_USER' password '$DB_PASS'"
117 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
121 }
122
123 function _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
129 function _update_db(){
130 echo '
131 #################################################################
132 ##### UPDATE DATABASE #####
133 #################################################################'
134 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}'"
140 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
141 ${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
144 }
145
146 function _uninstall_db(){
147 echo '
148 #################################################################
149 ##### DELETE DATABASE #####
150 #################################################################'
151 DBDELETEPARAM=""
152 [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
153 _delete_db
154 }
155
156 function db_exists(){ # (db_name, credential_file)
157 # check credentials
158 mysqlshow --defaults-extra-file="$2" >/dev/null || exit 1
159 if mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -w -q $1
160 then
161 # echo " DB $1 exists"
162 return 0
163 fi
164 # echo " DB $1 does not exist"
165 return 1
166 }
167
168 while getopts ":U:P:d:u:p:H:T:hiq-:" o; do
169 case "${o}" in
170 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 ;;
179 u)
180 export DB_USER="$OPTARG"
181 ;;
182 p)
183 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"
191 ;;
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
202 [ "${OPTARG}" == "updatedb" ] && UPDATEDB="y" && continue
203 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
204 [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
205 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
206 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
207 exit 1
208 ;;
209 \?)
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
222 done
223 if [ -n "$FORCEDB" ] && [ -n "$UPDATEDB" ] ; then
224 echo "Error: options --forcedb and --updatedb are mutually exclusive" >&2
225 exit 1
226 fi
227
228 # 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)
233
234 if [[ -z "$NO_PACKAGES" ]]
235 then
236 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
237 _install_mysql_package || exit 1
238 fi
239
240 # Creating temporary file for MYSQL installation and initialization"
241 TEMPFILE="$(mktemp -q --tmpdir "installdb.XXXXXX")"
242 trap 'rm -f "$TEMPFILE"' EXIT
243 chmod 0600 "$TEMPFILE"
244 echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE"
245
246 #check and ask for database user password. Must be done after database installation
247 if [[ -z $QUIET_MODE ]]
248 then
249 echo -e "\nCheking database connection and ask for credentials"
250 # echo "mysqladmin --defaults-extra-file=$TEMPFILE -s status >/dev/null"
251 while ! mysqladmin --defaults-extra-file="$TEMPFILE" -s status >/dev/null
252 do
253 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
254 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
255 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"
261 logintry="yes"
262 done
263 fi
264
265 if [[ ! -z "$UNINSTALL" ]]
266 then
267 _uninstall_db
268 exit
269 fi
270
271 # Create or update database
272 if 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?"
283 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
284 _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
293 else
294 _create_db
295 fi
296