Files
OpenDRS/install.sh

311 lines
12 KiB
Bash

#!/bin/bash
# install.sh
# OpenDRS Online Discrepancy Reporting System
# Copyright (C) 2022 Rod Wright
# SPDX-License-Identifier: GPL-2.0
DRS_VERSION="1.3.0"
DOC_ROOT="/var/www"
INSTALL_LOC="opendrs"
APACHE_USER="www-data"
APACHE_GROUP="www-data"
APACHE_CONF_DIR="/etc/apache2/conf-available"
BACKUP_DIR="opendrs-previous-installation"
# test for superuser rights
if [[ $EUID -ne 0 ]]; then
echo "This script must be run with superuser privileges. Try sudo ./install.sh"
exit 1
fi
# initial stuff
echo ""
echo ""
echo "* * * * * Welcome to OpenDRS $DRS_VERSION Installation * * * * *"
echo ""
echo ""
echo "Just press enter at the prompts to accept the defaults shown."
echo ""
# prompt for install location
echo "Where would you like to install this version of OpenDRS? This should"
echo "be somewhere the webserver can find it. If you don't know, refer to the"
echo "webserver's documentation and check the server's configuration files."
echo "Note that this is where the directory containing OpenDRS's files will be"
echo "created."
echo -n "Enter the webserver's install location [$DOC_ROOT]:"
read doc_root_in
echo ""
echo -n "Enter name of OpenDRS installation directory [$INSTALL_LOC] :"
read install_loc_in
echo ""
if [ "$doc_root_in" = "" ]
then
doc_root_in=$DOC_ROOT
fi
if [ "$install_loc_in" = "" ]
then
install_loc_in=$INSTALL_LOC
fi
install_path="$doc_root_in/$install_loc_in"
if [ -d "$install_path" ]
then
# Install path exists.
if [ -e "$install_path/db.php" ]
then
# A db.php file was found in the install path
prev_inst_present=1
grep -q -e "OpenDRS" -e "OpenMTS" $install_path/db.php
prev_inst_present=$?
if [ "$prev_inst_present" -eq 0 ]
then
# the db.php file is part of an OpenDRS/OpenMTS installation
echo "The installation path you chose already exists."
echo "Would you like to upgrade an existing installation or cancel "
echo "and restart the install using a new installation location."
echo -n "[U]pgrade or [C]ancel? [C] :"
read upgrade_choice
if [ "$upgrade_choice" = "U" -o "$upgrade_choice" = "u" ] # Upgrade chosen
then
# proceed with upgrade
operation="upgraded"
# back up existing database
echo "Backing up the current database to the package directory."
curr_dbname=`grep dbname $install_path/db.php | head -1 | cut -d "\"" -f2`
curr_username=`grep username $install_path/db.php | head -1 | cut -d "\"" -f2`
curr_dbpass=`grep dbpass $install_path/db.php | head -1 | cut -d "\"" -f2`
export MYSQL_PWD="$curr_dbpass"
mysqldump -u"$curr_username" $curr_dbname > $curr_dbname-`date +%Y-%m-%d_%H:%M:%S`-backup.sql
# back up existing installation
echo "Backing up current installation to the package directory."
cp -R $install_path $install_loc_in-`date +%Y-%m-%d_%H:%M:%S`-backup
# delete all existing installation files except db.php
find $install_path/ -mindepth 1 -not -name 'db.php' -delete
# apply database updates
mysql -u"$curr_username" -f -D $curr_dbname < opendrs-update.sql >/dev/null 2>&1
# apply updates to existing db.php
./opendrs-update.sh $install_path
# copy distribution to install location
echo "Installing distribution . . ."
cp -R distfiles/* $install_path
if [ ! -L "$install_path/jquery-ui" ]
then
ln -s $install_path/jquery-ui* $install_path/jquery-ui
fi
if [ ! -L "$install_path/index.php" ]
then
ln -s $install_path/writeups.php $install_path/index.php
fi
# set ownership
echo "In order to set the ownership correctly, we need to know the user and"
echo "group that the webserver expects its files to be owned by. This is"
echo "usually not the root account. Refer to the ownership of other files in"
echo "your webserver's document tree to see what this should be."
echo "Enter the user and group separated by a colon (username:groupname)."
echo "Enter the user:group of the OpenDRS installation"
echo -n "directory [$APACHE_USER:$APACHE_GROUP] :"
read httpd_user_group
if [ "$httpd_user_group" = "" ]
then
httpd_user_group="$APACHE_USER:$APACHE_GROUP"
fi
echo ""
echo "Setting file ownership . . ."
chown -R $httpd_user_group $install_path
else
echo "Cancelling installation"
exit
fi
else
# This was a mistake. Exit now to avoid clobbering another app.
echo "The installation directory you chose exists, but no previous installation"
echo "of OpenDRS was found. Please investigate the situation. Aborting now."
exit 1
fi
else
# This was a mistake. Exit now to avoid clobbering another app.
echo "The installation directory you chose exists, but no previous installation"
echo "of OpenDRS was found. Please investigate the situation. Aborting now."
exit 1
fi
else
# This is a fresh install
operation="installed"
# prompt for mysql information
bad_mysql_adm=1
while [ $bad_mysql_adm -eq 1 ]
do
admintestdb="drsadm`date +%Y%m%d`"
echo "We need to know the username and password of a MySQL administrator"
echo "to be able to create the database and user for OpenDRS. Note that this"
echo "is not necessarily the same as the login and password for the computer."
echo -n "MySQL admin username: "
read mysql_adm_user
echo ""
echo -n "MySQL admin password: "
read -s mysql_adm_pass
export MYSQL_PWD="$mysql_adm_pass"
mysql -u"$mysql_adm_user" -e "create database $admintestdb;drop database $admintestdb"
if [ $? -eq 0 ]
then
bad_mysql_adm=0
else
echo "ERROR: Either the username/password you supplied were incorrect or the user"
echo -n "is not a MySQL administrator. Try again? [Y/N]: "
read admtry
if [ "$admtry" != "Y" -o "$admtry" != "y" ]
then
exit
fi
fi
done
echo ""
echo "What would you like to call this installation of OpenDRS? If you accept"
echo "the default, it will be called OpenDRS. If you will be running multiple"
echo "instances of OpenDRS on this server, you should choose a distinctive name."
echo "This can be changed later in the application itself."
echo -n "Installation name [OpenDRS]: "
read new_inst_name
echo ""
# Create initial database
db_create_fail=1
while [ $db_create_fail -eq 1 ]
do
echo "What would you like to call the database for this installation of OpenDRS?"
echo "If you accept the default, it will be called opendrs. Again, this is fine"
echo "if you will only be running this one instance, but if you will be running"
echo "multiple instances of OpenDRS on this server, you should choose a "
echo "distinctive name."
echo -n "database name [opendrs]: "
read new_db_name
if [ "$new_db_name" = "" ]
then
new_db_name=opendrs
fi
echo ""
export MYSQL_PWD="$mysql_adm_pass"
mysql -u"$mysql_adm_user" -e "create database $new_db_name"
if [ $? -eq 0 ]
then
db_create_fail=0
else
echo "An error was encountered when trying to create the database."
echo "This probably means the database already exists."
echo -n "Try again with a different database name? [Y/N]: "
read dbcreatetry
if [ "$dbcreatetry" != "Y" -o "$dbcreatetry" != "y" ]
then
exit
fi
fi
done
# Create user and give rights to database
drs_user="$new_db_name`date +%Y%m%d%H%M%S`"
drs_pass=`uuidgen`
export MYSQL_PWD="$mysql_adm_pass"
mysql -u"$mysql_adm_user" -e "create user if not exists '$drs_user'@'localhost' identified by '$drs_pass'"
mysql -u"$mysql_adm_user" -e "grant all on $new_db_name.* to '$drs_user'@'localhost'"
mysql -u"$mysql_adm_user" -e "flush privileges"
# Populate initial database
mysql -u"$mysql_adm_user" $new_db_name < opendrs-initial.sql
# Set the installation name
export MYSQL_PWD="$drs_pass"
mysql -u"$drs_user" $new_db_name -e "update config set value=\"$new_inst_name\",defaultvalue=\"$new_inst_name\" where name=\"app_name\""
# Create install path
echo "Creating installation directory . . ."
mkdir $install_path
# Create db.php if it doesn't exist
if [ ! -f "$install_path/db.php" ]
then
cp db.php.template $install_path/db.php
sed -i "s/DBNAME/$new_db_name/g" $install_path/db.php
sed -i "s/DBUSER/$drs_user/g" $install_path/db.php
sed -i "s/DBPASS/$drs_pass/g" $install_path/db.php
fi
# copy distribution to install location
echo "Installing distribution . . ."
cp -Rv distfiles/* $install_path
if [ ! -L "$install_path/jquery-ui" ]
then
ln -s $install_path/jquery-ui* $install_path/jquery-ui
fi
if [ ! -L "$install_path/index.php" ]
then
ln -s $install_path/writeups.php $install_path/index.php
fi
# set ownership
echo "In order to set the ownership correctly, we need to know the user and"
echo "group that the webserver expects its files to be owned by. This is"
echo "usually not the root account. Refer to the ownership of other files in"
echo "your webserver's document tree to see what this should be."
echo "Enter the user and group separated by a colon (username:groupname)."
echo "Enter the user:group of the OpenDRS installation"
echo -n "directory [$APACHE_USER:$APACHE_GROUP] :"
read httpd_user_group
if [ "$httpd_user_group" = "" ]
then
httpd_user_group="$APACHE_USER:$APACHE_GROUP"
fi
echo ""
echo "Setting file ownership . . ."
chown -R $httpd_user_group $install_path
fi
echo ""
# tell user to launch web browser to continue
echo ""
echo ""
echo ""
echo "OpenDRS has been $operation."
base_url=$install_loc_in
if [ "$operation" = "installed" ]
then
echo "# OpenDRS default Apache configuration" > $APACHE_CONF_DIR/$base_url.conf
echo "" >> $APACHE_CONF_DIR/$base_url.conf
echo "Alias /$base_url $doc_root_in/$base_url" >> $APACHE_CONF_DIR/$base_url.conf
echo "" >> $APACHE_CONF_DIR/$base_url.conf
echo "<Directory $doc_root_in/$base_url>" >> $APACHE_CONF_DIR/$base_url.conf
echo " Options FollowSymLinks" >> $APACHE_CONF_DIR/$base_url.conf
echo " DirectoryIndex index.php" >> $APACHE_CONF_DIR/$base_url.conf
echo "</Directory>" >> $APACHE_CONF_DIR/$base_url.conf
echo -n "The apache2 $base_url configuration must be enabled. Would you like to do that now? [Y] :"
read enconf
if [ "$enconf" = "" -o "$enconf" = "Y" -o "$enconf" = "y" ]
then
a2enconf $base_url.conf
service apache2 reload
echo "You can now point a web browser to $base_url on your web server "
echo "to set up and configure OpenDRS."
echo ""
echo "IMPORTANT: The default login credentials for the initial admin user are:"
echo ""
echo "login: drsadmin"
echo "password: OpenDRS-1"
echo ""
echo "Remember these credentials. Otherwise, you will not be able to log in and"
echo "configure your new installation."
echo "It is highly recommended that you change the initial password to something"
echo "secure after logging in for the first time."
else
echo "You will need to manually enable the configuration by executing:"
echo " a2enconf $base_url.conf"
echo " service apache2 reload"
echo "as a superuser before it can be accessed."
fi
else
echo "You can now point a web browser to $base_url on your web server "
echo "to use your upgraded OpenDRS."
fi