e39339511b729c3e9461cf482b4df2dc93c63a82
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"
20 function _create_db
(){
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"
31 if db_exists
$DB_NAME $TEMPFILE ; then
32 if [[ -n $FORCEDB ]]; then
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'"
41 echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
44 echo "mysqladmin -u$DB_ADMIN_USER $DBPASSWD_PARAM -s create ${DB_NAME}"
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'"
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
62 function db_exists
() {
63 RESULT
=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
64 if [ "$RESULT" == "$1" ]; then
68 echo " DB $1 does not exist"
80 while getopts ":U:P:d:u:p:hiq-:" o
; do
83 export DB_ADMIN_USER
="$OPTARG"
86 export DB_ADMIN_PASSWD
="$OPTARG"
87 export DBPASSWD_PARAM
="-p$OPTARG"
90 export DB_NAME
="$OPTARG"
93 export DB_USER
="$OPTARG"
96 export DB_PASS
="$OPTARG"
100 export DEBIAN_FRONTEND
=noninteractive
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
114 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
118 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
128 HERE
=$
(realpath $
(dirname $0))
129 OPENVIM_BASEFOLDER
=$
(dirname $HERE)
130 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
132 if [[ -z "$SUDO_USER" ]] ||
[[ "$SUDO_USER" = "root" ]]
134 export SUDO_USER
='root'
137 #Discover Linux distribution
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
)
143 #check and ask for database user password. Must be done after database installation
144 if [[ -n $QUIET_MODE ]]
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
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
=""
161 if [[ -z "$NO_PACKAGES" ]]