19 echo -e "usage: sudo $0 [OPTIONS]"
20 echo -e "Install openmano database server and the needed packages"
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"
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
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
51 function install_packages
(){
52 [ -x /usr
/bin
/apt-get
] && apt-get
install -y $
*
53 [ -x /usr
/bin
/yum
] && yum
install -y $
*
55 #check properly installed
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" ]
63 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
69 function _install_mysql_package
(){
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
77 if [[ "$_DISTRO" == "Ubuntu" ]]
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
85 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
87 #start services. By default CentOS does not start services
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
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
&&
102 function _create_db
(){
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 \
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 \
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 \
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 \
129 function _update_db
(){
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 \
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 \
146 function _uninstall_db
(){
148 #################################################################
149 ##### DELETE DATABASE #####
150 #################################################################'
152 [[ -n $QUIET_MODE ]] && DBDELETEPARAM
="-f"
156 function db_exists
(){ # (db_name, credential_file)
158 mysqlshow
--defaults-extra-file="$2" >/dev
/null ||
exit 1
159 if mysqlshow
--defaults-extra-file="$2" |
grep -v Wildcard |
grep -w -q $1
161 # echo " DB $1 exists"
164 # echo " DB $1 does not exist"
168 while getopts ":U:P:d:u:p:H:T:hiq-:" o
; do
171 export DB_ADMIN_USER
="$OPTARG"
174 export DB_ADMIN_PASSWD
="$OPTARG"
177 export DB_NAME
="$OPTARG"
180 export DB_USER
="$OPTARG"
183 export DB_PASS
="$OPTARG"
186 export DB_HOST
="$OPTARG"
187 export DB_HOST_PARAM
="-h$DB_HOST"
190 export DB_PORT
="$OPTARG"
193 export QUIET_MODE
=yes
194 export DEBIAN_FRONTEND
=noninteractive
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
210 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
214 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
223 if [ -n "$FORCEDB" ] && [ -n "$UPDATEDB" ] ; then
224 echo "Error: options --forcedb and --updatedb are mutually exclusive" >&2
228 # Discover Linux distribution
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
)
234 if [[ -z "$NO_PACKAGES" ]]
236 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
237 _install_mysql_package ||
exit 1
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"
246 #check and ask for database user password. Must be done after database installation
247 if [[ -z $QUIET_MODE ]]
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
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"
265 if [[ ! -z "$UNINSTALL" ]]
271 # Create or update database
272 if db_exists
$DB_NAME $TEMPFILE ; then
273 if [[ -n $FORCEDB ]] ; then
275 # [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
279 elif [[ -n $UPDATEDB ]] ; then
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
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"