Minor typo fixed in install-db-server.sh
[osm/RO.git] / scripts / install-db-server.sh
1 #!/bin/bash
2
3 function usage(){
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"
7 echo -e " OPTIONS"
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"
14 }
15
16 function install_packages(){
17 [ -x /usr/bin/apt-get ] && apt-get install -y $*
18 [ -x /usr/bin/yum ] && yum install -y $*
19
20 #check properly installed
21 for PACKAGE in $*
22 do
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" ]
27 then
28 echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
29 exit 1
30 fi
31 done
32 }
33
34 function db_exists() {
35 RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
36 if [ "$RESULT" == "$1" ]; then
37 echo " DB $1 exists"
38 return 0
39 fi
40 echo " DB $1 does not exist"
41 return 1
42 }
43
44
45 DBUSER="root"
46 DBPASSWD=""
47 DBPASSWD_PARAM=""
48 QUIET_MODE=""
49 FORCEDB=""
50 NO_PACKAGES=""
51 while getopts ":u:p:hiq-:" o; do
52 case "${o}" in
53 u)
54 export DBUSER="$OPTARG"
55 ;;
56 p)
57 export DBPASSWD="$OPTARG"
58 export DBPASSWD_PARAM="-p$OPTARG"
59 ;;
60 q)
61 export QUIET_MODE=yes
62 export DEBIAN_FRONTEND=noninteractive
63 ;;
64 h)
65 usage && exit 0
66 ;;
67 -)
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
73 exit 1
74 ;;
75 \?)
76 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
77 exit 1
78 ;;
79 :)
80 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
81 exit 1
82 ;;
83 *)
84 usage >&2
85 exit 1
86 ;;
87 esac
88 done
89
90 HERE=$(realpath $(dirname $0))
91 OPENMANO_BASEFOLDER=$(dirname $HERE)
92
93 #Discover Linux distribution
94 #try redhat type
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)
98
99 if [[ -z "$NO_PACKAGES" ]]
100 then
101 echo '
102 #################################################################
103 ##### INSTALL REQUIRED PACKAGES #####
104 #################################################################'
105 [ "$_DISTRO" == "Ubuntu" ] && install_packages "mysql-server"
106 [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && install_packages "mariadb mariadb-server"
107
108 if [[ "$_DISTRO" == "Ubuntu" ]]
109 then
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
114 fi
115
116 if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
117 then
118 #start services. By default CentOS does not start services
119 service mariadb start
120 service httpd 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
125
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
131 fi
132 fi #[[ -z "$NO_PACKAGES" ]]
133
134 #check and ask for database user password. Must be done after database installation
135 if [[ -n $QUIET_MODE ]]
136 then
137 echo -e "\nCheking database connection and ask for credentials"
138 while ! mysqladmin -s -u$DBUSER $DBPASSWD_PARAM status >/dev/null
139 do
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=""
147 logintry="yes"
148 done
149 fi
150
151 echo '
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"
160
161 if db_exists "mano_db" $TEMPFILE ; then
162 if [[ -n $FORCEDB ]]; then
163 echo " Deleting previous database mano_db"
164 DBDELETEPARAM=""
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'"
173 else
174 echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
175 fi
176 else
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'"
181 fi
182
183
184 echo '
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