Fix bug when installing vim_db based on ovim.py version
[osm/openvim.git] / database_utils / install-db-server.sh
1 #!/usr/bin/env bash
2
3 function usage(){
4 echo -e "usage: sudo $0 [OPTIONS]"
5 echo -e "Install openvim database server"
6 echo -e " OPTIONS"
7 echo -e " -U USER: database admin user. 'root' by default. Prompts if needed"
8 echo -e " -P PASS: database admin password to be used or installed. Prompts if needed"
9 echo -e " -d: d database name, default name vim_db"
10 echo -e " -u: database user, default name vim"
11 echo -e " -p: database pass, default name vimpw"
12 echo -e " -q --quiet: install in unattended mode"
13 echo -e " -h --help: show this help"
14 echo -e " --forcedb: reinstall vim_db DB, deleting previous database if exists and creating a new one"
15 echo -e " --no-install-packages: <deprecate> 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 echo -e " --unistall: Delete DB, by default vim_db"
17
18
19 }
20
21 function _create_db(){
22 echo '
23 #################################################################
24 ##### CREATE DATABASE #####
25 #################################################################'
26 echo -e "\nCreating temporary file for MYSQL installation and initialization"
27 TEMPFILE="$(mktemp -q --tmpdir "installopenvim.XXXXXX")"
28 trap 'rm -f "$TEMPFILE"' EXIT
29 chmod 0600 "$TEMPFILE"
30 echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'">"$TEMPFILE"
31
32 if db_exists $DB_NAME $TEMPFILE ; then
33 if [[ -n $FORCEDB ]]; then
34 DBDELETEPARAM=""
35 [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
36 mysqladmin --defaults-extra-file=$TEMPFILE -s drop ${DB_NAME} $DBDELETEPARAM || ! echo "Could not delete ${DB_NAME} database" || exit 1
37 mysqladmin --defaults-extra-file=$TEMPFILE -s create ${DB_NAME} || ! echo "1 Error creating ${DB_NAME} database" || exit 1
38 echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "2 Failed while creating user ${DB_USER}"
39 echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO $DB_USER@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "3 Failed while granting privileges to user ${DB_USER} at database ${DB_NAME}" || exit 1
40 echo " Database '${DB_NAME}' created, user $DB_USER password '$DB_PASS'"
41 else
42 echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
43 fi
44 else
45 echo "mysqladmin -u$DB_ADMIN_USER $DBPASSWD_PARAM -s create ${DB_NAME}"
46
47 mysqladmin -u$DB_ADMIN_USER $DBPASSWD_PARAM -s create ${DB_NAME} || ! echo "4 Error creating ${DB_NAME} database" || exit 1
48 echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user vim at database"
49 echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO $DB_USER@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed giving creating user vim at database" || exit 1
50 echo " Database '${DB_NAME}' created, user $DB_USER password '$DB_PASS'"
51 fi
52 }
53
54 function _init_db(){
55 echo '
56 #################################################################
57 ##### INIT DATABASE #####
58 #################################################################'
59 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
60 su $SUDO_USER -c "${DIRNAME}/init_vim_db.sh -u $DB_USER -p $DB_PASS -d ${DB_NAME}" || ! echo "Failed while initializing database" || exit 1
61 }
62
63 function _uninstall_db(){
64 echo '
65 #################################################################
66 ##### DELETE DATABASE #####
67 #################################################################'
68 DBDELETEPARAM=""
69 [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
70 mysqladmin --defaults-extra-file=$TEMPFILE -s drop ${DB_NAME} $DBDELETEPARAM || ! echo "Could not delete ${DB_NAME} database" || exit 1
71
72 }
73 function db_exists() {
74 RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
75 if [ "$RESULT" == "$1" ]; then
76 echo " DB $1 exists"
77 return 0
78 fi
79 echo " DB $1 does not exist"
80 return 1
81 }
82 DB_NAME='vim_db'
83 DB_ADMIN_USER="root"
84 DB_USER="vim"
85 DB_PASS="vimpw"
86 DB_ADMIN_PASSWD=""
87 DBPASSWD_PARAM=""
88 QUIET_MODE=""
89 FORCEDB=""
90 NO_PACKAGES=""
91 UNINSTALL=""
92 while getopts ":U:P:d:u:p:hiq-:" o; do
93 case "${o}" in
94 U)
95 export DB_ADMIN_USER="$OPTARG"
96 ;;
97 P)
98 export DB_ADMIN_PASSWD="$OPTARG"
99 export DBPASSWD_PARAM="-p$OPTARG"
100 ;;
101 d)
102 export DB_NAME="$OPTARG"
103 ;;
104 u)
105 export DB_USER="$OPTARG"
106 ;;
107 p)
108 export DB_PASS="$OPTARG"
109 ;;
110 q)
111 export QUIET_MODE=yes
112 export DEBIAN_FRONTEND=noninteractive
113 ;;
114 h)
115 usage && exit 0
116 ;;
117 -)
118 [ "${OPTARG}" == "help" ] && usage && exit 0
119 [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
120 [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
121 [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
122 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
123 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
124 exit 1
125 ;;
126 \?)
127 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
128 exit 1
129 ;;
130 :)
131 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
132 exit 1
133 ;;
134 *)
135 usage >&2
136 exit 1
137 ;;
138 esac
139 done
140
141 HERE=$(realpath $(dirname $0))
142 OPENVIM_BASEFOLDER=$(dirname $HERE)
143 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
144
145 if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
146 then
147 export SUDO_USER='root'
148 fi
149
150 #Discover Linux distribution
151 #try redhat type
152 [ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
153 #if not assuming ubuntu type
154 [ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null)
155
156 #check and ask for database user password. Must be done after database installation
157 if [[ -n $QUIET_MODE ]]
158 then
159 echo -e "\nCheking database connection and ask for credentials"
160 echo "mysqladmin -s -u$DB_ADMIN_USER $DBPASSWD_PARAM status >/dev/null"
161 while ! mysqladmin -s -u$DB_ADMIN_USER $DBPASSWD_PARAM status >/dev/null
162 do
163 [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
164 [ -z "$logintry" ] && echo -e "\nProvide database credentials"
165 read -e -p "database user? ($DB_ADMIN_USER) " DBUSER_
166 [ -n "$DBUSER_" ] && DB_ADMIN_USER=$DBUSER_
167 read -e -s -p "database password? (Enter for not using password) " DBPASSWD_
168 [ -n "$DBPASSWD_" ] && DB_ADMIN_PASSWD="$DBPASSWD_" && DBPASSWD_PARAM="-p$DBPASSWD_"
169 [ -z "$DBPASSWD_" ] && DB_ADMIN_PASSWD="" && DBPASSWD_PARAM=""
170 logintry="yes"
171 done
172 fi
173
174 if [[ ! -z "$UNINSTALL" ]]
175 then
176 _uninstall_db
177 exit
178 fi
179
180
181 if [[ -z "$NO_PACKAGES" ]]
182 then
183 _create_db
184 _init_db
185 fi
186
187