4 echo -e "usage: sudo $0 [OPTIONS]"
5 echo -e "Install openvim database server"
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"
21 function _create_db
(){
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"
32 if db_exists
$DB_NAME $TEMPFILE ; then
33 if [[ -n $FORCEDB ]]; then
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'"
42 echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
45 echo "mysqladmin -u$DB_ADMIN_USER $DBPASSWD_PARAM -s create ${DB_NAME}"
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'"
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
63 function _uninstall_db
(){
65 #################################################################
66 ##### DELETE DATABASE #####
67 #################################################################'
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
73 function db_exists
() {
74 RESULT
=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
75 if [ "$RESULT" == "$1" ]; then
79 echo " DB $1 does not exist"
92 while getopts ":U:P:d:u:p:hiq-:" o
; do
95 export DB_ADMIN_USER
="$OPTARG"
98 export DB_ADMIN_PASSWD
="$OPTARG"
99 export DBPASSWD_PARAM
="-p$OPTARG"
102 export DB_NAME
="$OPTARG"
105 export DB_USER
="$OPTARG"
108 export DB_PASS
="$OPTARG"
111 export QUIET_MODE
=yes
112 export DEBIAN_FRONTEND
=noninteractive
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
127 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
131 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
141 HERE
=$
(realpath $
(dirname $0))
142 OPENVIM_BASEFOLDER
=$
(dirname $HERE)
143 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
145 if [[ -z "$SUDO_USER" ]] ||
[[ "$SUDO_USER" = "root" ]]
147 export SUDO_USER
='root'
150 #Discover Linux distribution
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
)
156 #check and ask for database user password. Must be done after database installation
157 if [[ -n $QUIET_MODE ]]
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
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
=""
174 if [[ ! -z "$UNINSTALL" ]]
181 if [[ -z "$NO_PACKAGES" ]]