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"
37 function install_packages
(){
38 [ -x /usr
/bin
/apt-get
] && apt-get
install -y $
*
39 [ -x /usr
/bin
/yum
] && yum
install -y $
*
41 #check properly installed
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" ]
49 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
55 function _install_mysql_package
(){
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
63 if [[ "$_DISTRO" == "Ubuntu" ]]
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
71 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
73 #start services. By default CentOS does not start services
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
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
&&
89 function _create_db
(){
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 \
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 \
103 echo " Database '${DB_NAME}' created, user '$DB_USER' password '$DB_PASS'"
104 DIRNAME
=$
(dirname $
(readlink
-f ${BASH_SOURCE[0]}))
105 ${DIRNAME}/init_mano_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 \
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 \
116 function _update_db
(){
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 \
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_mano_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 \
133 function _uninstall_db
(){
135 #################################################################
136 ##### DELETE DATABASE #####
137 #################################################################'
139 [[ -n $QUIET_MODE ]] && DBDELETEPARAM
="-f"
143 function db_exists
(){ # (db_name, credential_file)
145 mysqlshow
--defaults-extra-file="$2" >/dev
/null ||
exit 1
146 RESULT
=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
147 if [ "$RESULT" == "$1" ]; then
148 # echo " DB $1 exists"
151 # echo " DB $1 does not exist"
155 while getopts ":U:P:d:u:p:H:T:hiq-:" o
; do
158 export DB_ADMIN_USER
="$OPTARG"
161 export DB_ADMIN_PASSWD
="$OPTARG"
164 export DB_NAME
="$OPTARG"
167 export DB_USER
="$OPTARG"
170 export DB_PASS
="$OPTARG"
173 export DB_HOST
="$OPTARG"
174 export DB_HOST_PARAM
="-h$DB_HOST"
177 export DB_PORT
="$OPTARG"
180 export QUIET_MODE
=yes
181 export DEBIAN_FRONTEND
=noninteractive
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
197 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
201 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
210 if [ -n "$FORCEDB" ] && [ -n "$UPDATEDB" ] ; then
211 echo "Error: options --forcedb and --updatedb are mutually exclusive" >&2
215 # Discover Linux distribution
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
)
221 if [[ -z "$NO_PACKAGES" ]]
223 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
224 _install_mysql_package ||
exit 1
227 # Creating temporary file for MYSQL installation and initialization"
228 TEMPFILE
="$(mktemp -q --tmpdir "installdb.XXXXXX
")"
229 trap 'rm -f "$TEMPFILE"' EXIT
230 chmod 0600 "$TEMPFILE"
231 echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE"
233 #check and ask for database user password. Must be done after database installation
234 if [[ -z $QUIET_MODE ]]
236 echo -e "\nCheking database connection and ask for credentials"
237 # echo "mysqladmin --defaults-extra-file=$TEMPFILE -s status >/dev/null"
238 while ! mysqladmin
--defaults-extra-file="$TEMPFILE" -s status
>/dev
/null
240 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
241 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
242 read -e -p "database admin user? ($DB_ADMIN_USER) " DBUSER_
243 [ -n "$DBUSER_" ] && DB_ADMIN_USER
=$DBUSER_
244 read -e -s -p "database admin password? (Enter for not using password) " DBPASSWD_
245 [ -n "$DBPASSWD_" ] && DB_ADMIN_PASSWD
="$DBPASSWD_"
246 [ -z "$DBPASSWD_" ] && DB_ADMIN_PASSWD
=""
247 echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE"
252 if [[ ! -z "$UNINSTALL" ]]
258 # Create or update database
259 if db_exists
$DB_NAME $TEMPFILE ; then
260 if [[ -n $FORCEDB ]] ; then
262 # [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
266 elif [[ -n $UPDATEDB ]] ; then
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
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"