Files
shell_sequencer/seqs/postfixadmin.sh

329 lines
9.6 KiB
Bash
Executable File

#!/bin/bash
toolName=postfixadmin
toolPhpDeps='php${PFA_PHP_VERSION}-fpm php${PFA_PHP_VERSION}-imap php${PFA_PHP_VERSION}-mbstring php${PFA_PHP_VERSION}-mysql php${PFA_PHP_VERSION}-json php${PFA_PHP_VERSION}-curl php${PFA_PHP_VERSION}-zip php${PFA_PHP_VERSION}-xml php${PFA_PHP_VERSION}-bz2 php${PFA_PHP_VERSION}-intl php${PFA_PHP_VERSION}-gmp'
toolConfName="config.local.php"
toolTemplates="templates_c"
toolTemplatesLoc="$PFA_SRV_LOC/$toolTemplates"
toolAdditionsLoc="$PFA_SRV_LOC/ADDITIONS"
latestUrl="https://api.github.com/repos/$toolName/$toolName/releases/latest"
fetchmailUser="fetchmail"
fetchmailDeps="fetchmail liblockfile-simple-perl"
# Get script working directory
# (when called from a different directory)
WDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )"
CONFIG=0
CONFIG_FILE_NAME="${toolName}.cfg"
CONFIG_FILE_TEMPLATE="$WDIR/${CONFIG_FILE_NAME}.example"
step_config() {
initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE"
if [ $? -eq 0 ] ; then
echo " ${toolName} path: ${PFA_WEB_LOC}"
echo " ${toolName} backup: ${PFA_BACKUP}"
echo " php Version: ${PFA_PHP_VERSION}"
CONFIG=1
fi
}
step_1_info() {
# eval needed to expand sourced configuration variables
local localDeps=`eval "echo \"$toolPhpDeps\""`
echo "Install $toolName dependencies:"
echoinfo "$localDeps"
}
step_1_alias() { ALIAS="install"; }
step_1() {
# eval needed to expand sourced configuration variables
local localDeps=`eval "echo \"$toolPhpDeps\""`
local aptOpt=
if [ $QUIET -ne 0 ];then
aptOpt="-y"
fi
exe apt update
exe apt install $localDeps $aptOpt
}
step_2_info() { echo "Install $toolName to $PFA_SRV_LOC"; }
step_2() {
step upgrade
}
step_3_info() { echo "Install fetchmail"; }
step_3_alias() { ALIAS="install_fetchmail"; }
step_3() {
local aptOpt=
if [ $QUIET -ne 0 ];then
aptOpt="-y"
fi
exe apt install $fetchmailDeps $aptOpt
endReturn -o $? "Failed to install fetchmail"
exe systemctl stop fetchmail
exe systemctl disable fetchmail
}
step_4_info() { echo "Configure postfixadmin to use fetchmail"; }
step_4() {
echo "# Create mysql config"
echo " [$PFA_SRV_LOC/fetchmail.conf]"
echo " # Follow instructions in $toolAdditionsLoc/fetchmail.pl"
echo " \$db_type = 'mysql';"
echo " \$run_dir=\"/var/lock\";"
echo
echo " [$toolAdditionsLoc/fetchmail.pl]"
echo " # Change path to fetchmail.conf (see above)"
echo
echo " [I] Run step \"timer\" when configuration is done"
}
step_6_info() {
echo "Create postfixadmin fetchmail systemd timer and reduce syslog entries"
echoinfo "Needs to start after the mysql service"
}
step_6_alias() { ALIAS="timer"; }
step_6() {
# eval needed to expand sourced configuration variables
local localService=`eval "echo \"$fetchPluginService\""`
addConf -s "$localService" "$fetchPluginServiceLoc"
addConf -s "$fetchPluginTimer" "$fetchPluginTimerLoc"
exe systemctl enable --now ${fetchPluginServiceName}.timer
addConf -s "$fetchPluginRsyslog" "$fetchPluginRsyslogLoc"
exe service rsyslog restart
}
fetchPluginServiceName="pfafetchmail"
fetchPluginServiceLoc="/etc/systemd/system/${fetchPluginServiceName}.service"
fetchPluginService="[Unit]
Description=Postfix fetchmail plugin
After=mysql.service
[Service]
# emerg (lowest log level, only highest priority messages),
# alert, crit, err, warning, notice, info, debug
LogLevelMax=notice
User=$fetchmailUser
ExecStart=\${PFA_SRV_LOC}/ADDITIONS/fetchmail.pl >>/dev/null
[Install]
WantedBy=multi-user.target"
fetchPluginTimerLoc="/etc/systemd/system/${fetchPluginServiceName}.timer"
fetchPluginTimer="[Unit]
Description=Postfix fetchmail plugin execute every minute
[Timer]
OnCalendar=minutely
Persistent=true
[Install]
WantedBy=basic.target"
fetchPluginRsyslogLoc="/etc/rsyslog.d/31-fetchmailreduce.conf"
fetchPluginRsyslog="if \$programname == 'systemd' and re_match(\$msg, \"Started.*fetchmail\") then stop
if \$programname == 'systemd' and re_match(\$msg, \"fetchmail.*Succeeded\") then stop"
step_7_info() { echo "Create separate log file for fetchmail using rsyslog"; }
step_7_alias() { ALIAS="newlog"; }
step_7() {
addConf -f "$fetchmailRsyslog" "$fetchmailRsyslogLoc"
addConf -f "$fetchmailRotate" "$fetchmailRotateLoc"
exe service rsyslog restart
}
fetchmailLogLoc="/var/log/fetchmail.log"
fetchmailRotateLoc="/etc/logrotate.d/fetchmail"
fetchmailRotate="$fetchmailLogLoc {
rotate 14
weekly
missingok
daily
compress
delaycompress
}"
fetchmailRsyslogLoc="/etc/rsyslog.d/30-fetchmail.conf"
fetchmailRsyslog="if \$programname == 'fetchmail' or \$programname == 'fetchmail-all' then $fetchmailLogLoc
& stop"
step_18_info() { echoinfoArgs "[NEW VERSION]"; echo "Check for updates"; }
step_18_alias() { ALIAS="updatecheck"; }
step_18() {
shift
local isInstalled=
local latestVersion=
if [ ! -z $1 ] ; then
latestVersion="$1"
else
latestVersion=$(curl --silent "$latestUrl" | grep -Po '"tag_name": "postfixadmin-\K.*?(?=")')
fi
isInstalled=$(grep -E "${latestVersion}" "${PFA_WEB_LOC}/version" >>/dev/null 2>&1 && echo "1" || echo "0")
if [ $isInstalled -eq 1 ] ; then
echo " [I] Version $latestVersion is already installed"
return 1
else
echo " [I] Update to $latestVersion available"
fi
return 0
}
step_20_info() {
echoinfoArgs "[POSTFIXADMIN SRV ROOT]"
echo -n "Create a backup"
if [ $CONFIG -ne 0 ] ; then
echo " at $PFA_BACKUP"
else
echo
fi
}
step_20_alias() { ALIAS="backup"; }
step_20() {
shift
local tempRoot=
if [ $CONFIG -eq 0 ] ; then
echoerr " [E] No configuration file found"
return 1
fi
if [ ! -z $PFA_BACKUP ] ; then
exe mkdir -p "$PFA_BACKUP"
fi
if [ ! -z $1 ] ; then
tempRoot="$1"
else
tempRoot="$PFA_SRV_LOC"
fi
local srvBackup="$PFA_BACKUP/${toolName}_`date +%Y%m%d-%H%M%S`.tar.gz"
echo " [I] Backing up server directory to $srvBackup"
exe cd "$tempRoot/.."
exe tar czf "$srvBackup" $(basename "$tempRoot")
exe $WDIR/mysql.sh -qq backup "$PFA_DATABASE" "$PFA_BACKUP"
}
step_22_info() {
echoinfoArgs "[CUSTOM VERSION]"
shift
if [ $CONTEXT_EXE -ne 0 ]; then
echoinfo -n "Upgrade to version "
if [ -z "$1" ]; then
echo -n "$(curl --silent "$latestUrl" | grep -Po '"tag_name": "postfixadmin-\K.*?(?=")')"
else
echo -n "$1"
fi
echo " from github"
else
echo "Upgrade to latest or a custom version from github"
fi
}
step_22_alias() { ALIAS="upgrade"; }
step_22() {
shift # don't need step number
local latestVersion=
if [ ! -z $1 ] ; then
latestVersion="$1"
else
latestVersion=$(curl --silent "$latestUrl" | grep -Po '"tag_name": "postfixadmin-\K.*?(?=")')
fi
if [ -z $latestVersion ] ; then
echoerr " [E] Cannot determine latest version from github repository"
return 1
elif [ $QUIET -eq 0 ] ; then
echo
exe read -p "Install $latestVersion to $PFA_SRV_LOC [n]o/(y)es? " answer
case $answer in
[yY])
;;
*)
echoerr " [I] Upgrade aborted"
return 1
;;
esac
fi
# Major versions are stated in the CHANGELOG.TXT without the trailing minor version e.g. "3.3" for "3.3.0"
# Trailing ".0" is removed if exists
local isInstalled=$(grep -E "Version ${latestVersion%.0} " "${PFA_SRV_LOC}/CHANGELOG.TXT" >>/dev/null 2>&1 && echo "1" || echo "0")
if [ $isInstalled -eq 1 ] ; then
echoerr " [E] Version $latestVersion is already installed"
return 2
fi
# Download
local downUrl="https://github.com/$toolName/$toolName/archive/postfixadmin-${latestVersion}.tar.gz"
local tempExtract="$tempDown/postfixadmin-postfixadmin-$latestVersion"
if [ ! -e "$tempExtract" ] ; then
exe mkdir -p "$tempDown"
exe wget -O "$tempLoc" $downUrl
endReturn -o $? "Download failed: $downUrl"
exe cd "$tempDown"
exe tar -xf "$tempLoc"
endReturn -o $? "Extract failed: $tempLoc"
else
echo " [I] Found existing download: $tempExtract"
fi
# Installation
local tempBu="${PFA_SRV_LOC}_bu_`date +%Y%m%d-%H%M%S`"
if [ -e "$PFA_SRV_LOC" ] ; then
exe mv "$PFA_SRV_LOC" "$tempBu"
step backup "$tempBu"
endReturn -o $? "Backup failed; $PFA_SRV_LOC renamed!"
fi
echo " [I] Installing version $latestVersion to $PFA_SRV_LOC"
exe cp -ar "$tempExtract" "$PFA_SRV_LOC"
exe mkdir -p $(dirname "$PFA_WEB_LOC")
echo " [I] Create symlink to $PFA_WEB_LOC"
exe ln -fs "$PFA_SRV_LOC/public" "$PFA_WEB_LOC"
# Setting file permissions
exe chown -R www-data: "$PFA_SRV_LOC/public"
# Configuration
local webConf="$tempBu/$toolConfName"
if [ -e "$webConf" ] ; then
echo " [I] Copying configuration"
exe cp -ar "$webConf" "$PFA_SRV_LOC/"
else
echo " [I] Creating empty configuration file $PFA_SRV_LOC/$toolConfName"
exep "echo -e \"# Created gy $WDIR/$(basename $0)\\n\\n# Changeme\" > \"$PFA_SRV_LOC/$toolConfName\""
fi
# Templates
local templatesLoc="$tempBu/$toolTemplates"
if [ -e "$templatesLoc" ] ; then
echo " [I] Copying $toolTemplates"
exe cp -ar "$templatesLoc" "$toolTemplatesLoc"
else
echo " [I] Creating empty directory $toolTemplatesLoc"
exe mkdir -p "$toolTemplatesLoc"
exe chown -R www-data: "$toolTemplatesLoc"
fi
exe rm -rf "$tempBu"
}
tempDown="/tmp/${toolName}"
tempLoc="$tempDown/${toolName}.tar.gz"
step_23_info() { echo "Clean temporary files: $tempDown"; }
step_23_alias() { ALIAS="clean"; }
step_23() {
exe rm -rf "$tempDown"
}
step_100_info() {
echoinfoArgs "[OPTIONS]"
echo "Execute $toolName client script"
echoinfo "[OPTIONS] are passed on to $toolName-cli unmodified"
}
step_100_alias() { ALIAS="cli"; }
step_100() {
shift
exe ${PFA_SRV_LOC}/scripts/$toolName-cli $@
}
VERSION_SEQREV=14
. /usr/local/bin/sequencer.sh