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