blob: 5a9b9abc5105ef86614232e87bddae4f70279f9d [file] [log] [blame]
mirabald87877c2017-03-31 15:15:52 +02001#!/usr/bin/env bash
2
3function 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"
mirabalee195762017-04-20 15:18:39 +020016 echo -e " --unistall: Delete DB, by default vim_db"
mirabald87877c2017-03-31 15:15:52 +020017
18
19}
20
21function _create_db(){
22 echo '
23 #################################################################
24 ##### CREATE DATABASE #####
25 #################################################################'
garciadeblas57697f92017-04-21 12:49:08 +020026 echo -e "\nCreating temporary file for MYSQL installation and initialization"
mirabald87877c2017-03-31 15:15:52 +020027 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
garciadeblas57697f92017-04-21 12:49:08 +020038 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
mirabald87877c2017-03-31 15:15:52 +020040 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
54function _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
mirabalee195762017-04-20 15:18:39 +020063function _uninstall_db(){
64echo '
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}
mirabald87877c2017-03-31 15:15:52 +020073function 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}
82DB_NAME='vim_db'
83DB_ADMIN_USER="root"
84DB_USER="vim"
85DB_PASS="vimpw"
86DB_ADMIN_PASSWD=""
87DBPASSWD_PARAM=""
88QUIET_MODE=""
89FORCEDB=""
90NO_PACKAGES=""
mirabalee195762017-04-20 15:18:39 +020091UNINSTALL=""
mirabald87877c2017-03-31 15:15:52 +020092while 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
mirabalee195762017-04-20 15:18:39 +0200122 [ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
mirabald87877c2017-03-31 15:15:52 +0200123 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
139done
140
141HERE=$(realpath $(dirname $0))
142OPENVIM_BASEFOLDER=$(dirname $HERE)
143[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
144
145if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
146then
147 export SUDO_USER='root'
148fi
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
157if [[ -n $QUIET_MODE ]]
158then
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
172fi
173
mirabalee195762017-04-20 15:18:39 +0200174if [[ ! -z "$UNINSTALL" ]]
175then
176 _uninstall_db
177 exit
178fi
179
180
mirabald87877c2017-03-31 15:15:52 +0200181if [[ -z "$NO_PACKAGES" ]]
182then
183 _create_db
184 _init_db
185fi
186
mirabalee195762017-04-20 15:18:39 +0200187