4 echo -e "usage: sudo $0 [OPTIONS]"
5 echo -e "Install openmano database server"
6 echo -e "On a Ubuntu 16.04 it configures openmano as a service"
8 echo -e " -u USER: database admin user. 'root' by default. Prompts if needed"
9 echo -e " -p PASS: database admin password to be used or installed. Prompts if needed"
10 echo -e " -q --quiet: install in unattended mode"
11 echo -e " -h --help: show this help"
12 echo -e " --forcedb: reinstall mano_db DB, deleting previous database if exists and creating a new one"
13 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"
16 function install_packages
(){
17 [ -x /usr
/bin
/apt-get
] && apt-get
install -y $
*
18 [ -x /usr
/bin
/yum
] && yum
install -y $
*
20 #check properly installed
23 PACKAGE_INSTALLED
="no"
24 [ -x /usr
/bin
/apt-get
] && dpkg
-l $PACKAGE &>> /dev
/null
&& PACKAGE_INSTALLED
="yes"
25 [ -x /usr
/bin
/yum
] && yum list installed
$PACKAGE &>> /dev
/null
&& PACKAGE_INSTALLED
="yes"
26 if [ "$PACKAGE_INSTALLED" = "no" ]
28 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
34 function db_exists
() {
35 RESULT
=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
36 if [ "$RESULT" == "$1" ]; then
40 echo " DB $1 does not exist"
51 while getopts ":u:p:hiq-:" o
; do
54 export DBUSER
="$OPTARG"
57 export DBPASSWD
="$OPTARG"
58 export DBPASSWD_PARAM
="-p$OPTARG"
62 export DEBIAN_FRONTEND
=noninteractive
68 [ "${OPTARG}" == "help" ] && usage
&& exit 0
69 [ "${OPTARG}" == "forcedb" ] && FORCEDB
="y" && continue
70 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE
=yes && export DEBIAN_FRONTEND
=noninteractive
&& continue
71 [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES
=yes && continue
72 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
76 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
80 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
90 HERE
=$
(realpath $
(dirname $0))
91 OPENMANO_BASEFOLDER
=$
(dirname $HERE)
93 #Discover Linux distribution
95 [ -f /etc
/redhat-release
] && _DISTRO
=$
(cat /etc
/redhat-release
2>/dev
/null | cut
-d" " -f1)
96 #if not assuming ubuntu type
97 [ -f /etc
/redhat-release
] || _DISTRO
=$
(lsb_release
-is 2>/dev
/null
)
99 if [[ -z "$NO_PACKAGES" ]]
102 #################################################################
103 ##### INSTALL REQUIRED PACKAGES #####
104 #################################################################'
105 [ "$_DISTRO" == "Ubuntu" ] && install_packages
"mysql-server"
106 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages
"mariadb mariadb-server"
108 if [[ "$_DISTRO" == "Ubuntu" ]]
110 #start services. By default CentOS does not start services
111 service mysql start
>> /dev
/null
112 # try to set admin password, ignore if fails
113 [[ -n $DBPASSWD ]] && mysqladmin
-u $DBUSER -s password
$DBPASSWD
116 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
118 #start services. By default CentOS does not start services
119 service mariadb start
121 systemctl
enable mariadb
122 systemctl
enable httpd
123 read -e -p "Do you want to configure mariadb (recommended if not done before) (Y/n)" KK
124 [ "$KK" != "n" -a "$KK" != "no" ] && mysql_secure_installation
126 read -e -p "Do you want to set firewall to grant web access port 80,443 (Y/n)" KK
127 [ "$KK" != "n" -a "$KK" != "no" ] &&
128 firewall-cmd
--permanent --zone=public
--add-service=http
&&
129 firewall-cmd
--permanent --zone=public
--add-service=https
&&
130 firewall-cmd
--reload
132 fi #[[ -z "$NO_PACKAGES" ]]
134 #check and ask for database user password. Must be done after database installation
135 if [[ -n $QUIET_MODE ]]
137 echo -e "\nCheking database connection and ask for credentials"
138 while ! mysqladmin
-s -u$DBUSER $DBPASSWD_PARAM status
>/dev
/null
140 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
141 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
142 read -e -p "database user? ($DBUSER) " DBUSER_
143 [ -n "$DBUSER_" ] && DBUSER
=$DBUSER_
144 read -e -s -p "database password? (Enter for not using password) " DBPASSWD_
145 [ -n "$DBPASSWD_" ] && DBPASSWD
="$DBPASSWD_" && DBPASSWD_PARAM
="-p$DBPASSWD_"
146 [ -z "$DBPASSWD_" ] && DBPASSWD
="" && DBPASSWD_PARAM
=""
152 #################################################################
153 ##### CREATE DATABASE #####
154 #################################################################'
155 echo -e "\nCreating temporary file for MYSQL installation and initialization"
156 TEMPFILE
="$(mktemp -q --tmpdir "installopenmano.XXXXXX
")"
157 trap 'rm -f "$TEMPFILE"' EXIT
158 chmod 0600 "$TEMPFILE"
159 echo -e "[client]\n user='$DBUSER'\n password='$DBPASSWD'">"$TEMPFILE"
161 if db_exists
"mano_db" $TEMPFILE ; then
162 if [[ -n $FORCEDB ]]; then
163 echo " Deleting previous database mano_db"
165 [[ -n $QUIET_MODE ]] && DBDELETEPARAM
="-f"
166 mysqladmin
--defaults-extra-file=$TEMPFILE -s drop mano_db
$DBDELETEPARAM ||
! echo "Could not delete mano_db database" ||
exit 1
167 #echo "REVOKE ALL PRIVILEGES ON mano_db.* FROM 'mano'@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user mano at database" || exit 1
168 #echo "DELETE USER 'mano'@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user mano at database" || exit 1
169 mysqladmin
--defaults-extra-file=$TEMPFILE -s create mano_db ||
! echo "Error creating mano_db database" ||
exit 1
170 echo "CREATE USER 'mano'@'localhost' identified by 'manopw';" | mysql
--defaults-extra-file=$TEMPFILE -s ||
! echo "Failed while creating user mano at database"
171 echo "GRANT ALL PRIVILEGES ON mano_db.* TO 'mano'@'localhost';" | mysql
--defaults-extra-file=$TEMPFILE -s ||
! echo "Failed while creating user mano at database" ||
exit 1
172 echo " Database 'mano_db' created, user 'mano' password 'manopw'"
174 echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
177 mysqladmin
-u$DBUSER $DBPASSWD_PARAM -s create mano_db ||
! echo "Error creating mano_db database" ||
exit 1
178 echo "CREATE USER 'mano'@'localhost' identified by 'manopw';" | mysql
--defaults-extra-file=$TEMPFILE -s ||
! echo "Failed while creating user mano at database" ||
exit 1
179 echo "GRANT ALL PRIVILEGES ON mano_db.* TO 'mano'@'localhost';" | mysql
--defaults-extra-file=$TEMPFILE -s ||
! echo "Failed while creating user mano at database" ||
exit 1
180 echo " Database 'mano_db' created, user 'mano' password 'manopw'"
185 #################################################################
186 ##### INIT DATABASE #####
187 #################################################################'
188 su
$SUDO_USER -c "${OPENMANO_BASEFOLDER}/database_utils/init_mano_db.sh -u mano -p manopw -d mano_db" ||
! echo "Failed while initializing database" ||
exit 1