18 echo -e "usage: sudo $0 [OPTIONS]"
19 echo -e "Install openmano database server and the needed packages"
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"
35 function install_packages
(){
36 [ -x /usr
/bin
/apt-get
] && apt-get
install -y $
*
37 [ -x /usr
/bin
/yum
] && yum
install -y $
*
39 #check properly installed
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" ]
47 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
53 function _install_mysql_package
(){
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
61 if [[ "$_DISTRO" == "Ubuntu" ]]
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
69 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
71 #start services. By default CentOS does not start services
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
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
&&
87 function _create_db
(){
89 #################################################################
90 ##### CREATE DATABASE #####
91 #################################################################'
93 if db_exists
$DB_NAME $TEMPFILE ; then
94 if [[ -n $FORCEDB ]]; then
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
103 echo "Database '$DB_NAME' exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
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'"
115 #################################################################
116 ##### INIT DATABASE #####
117 #################################################################'
118 DIRNAME
=$
(dirname $
(readlink
-f ${BASH_SOURCE[0]}))
119 ${DIRNAME}/init_mano_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
122 function _uninstall_db
(){
124 #################################################################
125 ##### DELETE DATABASE #####
126 #################################################################'
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
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"
139 # echo " DB $1 does not exist"
143 while getopts ":U:P:d:u:p:H:T:hiq-:" o
; do
146 export DB_ADMIN_USER
="$OPTARG"
149 export DB_ADMIN_PASSWD
="$OPTARG"
152 export DB_NAME
="$OPTARG"
155 export DB_USER
="$OPTARG"
158 export DB_PASS
="$OPTARG"
161 export DB_HOST
="$OPTARG"
162 export DB_HOST_PARAM
="-h$DB_HOST"
165 export DB_PORT
="$OPTARG"
168 export QUIET_MODE
=yes
169 export DEBIAN_FRONTEND
=noninteractive
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
184 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
188 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
198 # Discover Linux distribution
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
)
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"
210 #check and ask for database user password. Must be done after database installation
211 if [[ -z $QUIET_MODE ]]
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
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"
229 if [[ ! -z "$UNINSTALL" ]]
235 if [[ -z "$NO_PACKAGES" ]]
237 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
238 _install_mysql_package ||
exit 1