File openSUSE-gnuhealth-setup of Package gnuhealth

#!/bin/bash

# GNU Health installation script for openSUSE
# Version agnostic to GNU Health

##############################################################################
#
#    GNU Health Installer for openSUSE
#
#    Copyright (C) 2018 Axel Braun  <abraun@gnusolidario.org>

#    inspired by gnuhealth-control (C) 2018 Luis Falcon <falcon@gnu.org>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

#-----------------------------------------------------------------------------
# Change log
#-----------------------------------------------------------------------------
# V 0.2  20180617 Axel Replace tryton with ${DB_USER}
# V 0.3  20180712 Axel Set user 'tryton' to superuser (otherwise installation of DEMO-DB fails)
# V 0.32 20180720 Axel Warning for production use added
# V 0.33 20181123 Axel Start gnuhealth server at the end of procedure
#-----------------------------------------------------------------------------
# Variables declaration
#-----------------------------------------------------------------------------

#GNU Health installer for openSUSE version
VERSION="0.33"

# Colors constants
NONE="$(tput sgr0)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="\n$(tput setaf 3)"
WHITE="\n$(tput setaf 7)"

# Params
#ROOT_PW="root"
DB_NAME="GNUHEALTH"
DB_ADMIN_PWD="admin"
DB_USER="tryton"
DB_CONFIG="/var/lib/pgsql/data/pg_hba.conf"
GH_START="y"

#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------

message()
{
    local UTC="$(date -u +'%Y-%m-%d %H:%M:%S')"
    
    case $1 in
      ERROR ) echo -e "\e[00;31m${UTC} [ERROR] $2\e[00m";;
      WARNING ) echo -e "\e[0;33m${UTC} [WARNING] $2\e[m" ;;
      INFO ) echo -e "\e[0;36m${UTC} [INFO] $2\e[m" ;;
    esac
}

help()
{
    cat << EOF

This is GNU Health Setup for openSUSE ${VERSION}

usage: `basename $0` command

Command:
 
  version : Show version
  setup   : Setup a GNU Health server
  help    : shows this message

EOF
    exit 0
}

show_version () {
    case $1 in
        version) message "INFO" "This is is GNU Health setup for openSUSE V ${VERSION}"; exit 0;;
    esac

}

bailout() {
    message "INFO" "Bailing out !"
    message "INFO" "Cleaning up temporary file"
    rm -rf /tmp/pw
#    if [ -e ${BASEDIR}/.installation_ok ];then
#        message "WARNING" "Previous successful installation found. NOT removing base dir at ${BASEDIR}"
#    else
#        message "INFO" "removing base dir at ${BASEDIR}"
#        rm -rf ${BASEDIR}
#    fi
    exit 1
}

#-----------------------------------------------------------------------------
# go-ahead: perform update
#-----------------------------------------------------------------------------

go_ahead() {

# login as root, start postgres

echo "Starting database...."

systemctl start postgresql || bailout

echo "Database started"

# su -postgres || bailout
#then as postgres, and change parameter

echo "Changing postgres trust method"

sudo -u postgres sed -i -e 's/\(\(local\|host\).*all.*all.*\)\(peer\|ident\)/\1trust/g' ${DB_CONFIG} || bailout

echo "Postgres trust method changed"

echo "Create database user"
#create user
sudo -u postgres psql -c "CREATE USER ${DB_USER} WITH SUPERUSER;" 

echo "Database user created"

echo "Creating database ${DB_NAME}"
# create database
sudo -u postgres createdb ${DB_NAME} --encoding='UTF8' --owner=${DB_USER} || bailout

echo "Database created"
#exit as postgres
#exit

echo "Restarting database"

systemctl restart postgresql || bailout

# login as user tryton
#su tryton -s /bin/bash

echo "Initializing database - this may take a while"

echo ${DB_ADMIN_PWD} > /tmp/pw || bailout

sudo -u tryton env TRYTONPASSFILE=/tmp/pw trytond-admin -c /etc/tryton/trytond.conf --all -d ${DB_NAME} --password || bailout

rm /tmp/pw || bailout


if [ $GH_START = "y" ]; then
echo "Enabling GNU Health at boot time"

systemctl enable postgresql
systemctl enable gnuhealth

# ...and start gnuhealth in that case
systemctl start gnuhealth

fi

echo "All done"
message "WARNING" "Please review your setup before using the system in production!"

}

#-----------------------------------------------------------------------------
# Call user parameter
#-----------------------------------------------------------------------------

get_root_pw() {
    Y=''
    read -p "Please enter root-password [$ROOT_PW] " Y

    if [ ! -e $Y ]; then 
       ROOT_PW=$Y
    fi
    message "INFO" "root-password is $ROOT_PW"
}

get_db_name() {
    Y=''
    read -p "Please enter name for new database [$DB_NAME] " Y
    if [ ! -e $Y ]; then 
       DB_NAME=$Y
    fi
    message "INFO" "DB-Name is $DB_NAME"
}

get_db_admin() {
    Y=''
    read -p "Please enter admin-password for new database [$DB_ADMIN_PWD] " Y
    if [ ! -e $Y ]; then 
       DB_ADMIN_PWD=$Y
    fi
    message "INFO" "DB Admin password is $DB_ADMIN_PWD"
}

get_db_user() {
    Y=''
    read -p "Please enter database user [$DB_USER] " Y
    if [ ! -e $Y ]; then 
       DB_USER=$Y
    fi
    message "INFO" "DB User is $DB_USER"
}

get_db_file() {
    Y=''
    read -p "Please enter database config file [$DB_CONFIG] " Y
    if [ ! -e $Y ]; then 
       DB_CONFIG=$Y
    fi
    message "INFO" "DB config path & file $DB_CONFIG"
}

get_GH_start() {
    Y=''
    read -p "Shall GNU Health start automatically? [$GH_START] " Y
    if [ ! -e $Y ]; then 
       GH_START=$Y
    fi
    message "INFO" "GNU Health start at boot $GH_START"
}


#-----------------------------------------------------------------------------
# verify input
#-----------------------------------------------------------------------------

verify_input() {
    echo ;
    message "INFO" "DB Name is ..............$DB_NAME"
    message "INFO" "DB Admin password is ....$DB_ADMIN_PWD"
    message "INFO" "DB User is ..............$DB_USER"
    message "INFO" "DB config path & file is $DB_CONFIG"
    message "INFO" "GNU Health start at boot $GH_START"        
    read -p "Are these values correct? [y]es/[n]o/[a]bort " VAL
    
}       

#-----------------------------------------------------------------------------
# Install
#-----------------------------------------------------------------------------

setup() {

# check is it runs as root
    wai=`whoami`

    if [ $wai != "root" ]; then
      message "ERROR" " This script must run as user 'root'"
      bailout
    fi

    VAL="n"
    message "INFO" "Starting GNU Health setup for openSUSE V ${VERSION} ..." 
    message "INFO" "If the value displayed is OK, just hit <ENTER> to confirm" 
    
    while [ ${VAL} != "y" ]; do
        # (1) Get root password
#        get_root_pw

        # (2) get Database name
        get_db_name

        # (3) change login
        get_db_admin

        message "WARNING" "Change the following parameter only if you know what you are doing..."
    
        # (4) DB user
        get_db_user
        
        # (5) DB config file and path
        get_db_file
        
        # (6) enable GNUHealth at start?
        get_GH_start
        
        # (7) check if all are valid
        verify_input

        # request input until all is OK:
        if [ -e ${VAL} ]; then 
          message "ERROR" "Please enter y, n or a"
          verify_input
        else  
        
            if [ ${VAL} = "y" ]; then
                go_ahead
            exit 0
            fi
            if [ ${VAL} = "a" ]; then
                exit 1
            fi
        fi  
    done
        
}

#-----------------------------------------------------------------------------
# Parse command line
#-----------------------------------------------------------------------------


parse_command_line()
{
    if [ $# -eq 0 ]; then
        help
    fi
    
    case $1 in
        version) show_version $@;;
        update-deps) install_python_dependencies $@;;
        setup) setup $@;;
        help) help;;
        *) echo $1: Unrecognized command; exit 1;;
    esac
}

parse_command_line "$@"