326 lines
9.3 KiB
Bash
Executable File
326 lines
9.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
readonly toolName=postfixadmin
|
|
# since php8.0 json is always available
|
|
readonly toolPhpDeps=(bz2 curl fpm gmp imap intl mbstring mysql xml zip)
|
|
readonly toolConfName="config.local.php"
|
|
readonly toolTemplates="templates_c"
|
|
readonly latestUrl="https://api.github.com/repos/$toolName/$toolName/releases/latest"
|
|
readonly fetchmailUser="fetchmail"
|
|
readonly fetchmailDeps="fetchmail liblockfile-simple-perl"
|
|
|
|
toolTemplatesLoc=
|
|
toolAdditionsLoc=
|
|
sq_aptOpt=
|
|
sq_config=0
|
|
|
|
seq_config() {
|
|
if initSeqConfig "${seq_configName:?}" "${seq_configTemplate:?}" ; then
|
|
info "${toolName} path: ${PFA_WEB_LOC:-}"
|
|
info -a "${toolName} backup: ${PFA_BACKUP:-}"
|
|
info -a " php Version: ${PFA_PHP_VERSION:-}"
|
|
sq_config=1
|
|
toolTemplatesLoc="$PFA_SRV_LOC/$toolTemplates"
|
|
toolAdditionsLoc="$PFA_SRV_LOC/ADDITIONS"
|
|
else
|
|
dry || return 1
|
|
fi
|
|
|
|
## Apt cmdline option to suppress user interaction
|
|
interactive || sq_aptOpt="-y"
|
|
|
|
## Return of non zero value will abort the sequence
|
|
return 0
|
|
}
|
|
|
|
step_1_info() {
|
|
# eval needed to expand sourced configuration variables
|
|
echo "Install $toolName dependencies:"
|
|
echoinfo "${toolPhpDeps[@]/#/php${PFA_PHP_VERSION:-}-}"
|
|
}
|
|
step_1_alias() { echo "install"; }
|
|
step_1() {
|
|
exe apt update
|
|
exe apt install "${toolPhpDeps[@]/#/php${PFA_PHP_VERSION:?}-}" ${sq_aptOpt:-}
|
|
}
|
|
|
|
step_2_info() { echo "Install $toolName to ${PFA_SRV_LOC:-}"; }
|
|
step_2() {
|
|
step upgrade
|
|
}
|
|
|
|
step_3_info() { echo "Install fetchmail"; }
|
|
step_3_alias() { echo "install_fetchmail"; }
|
|
step_3() {
|
|
exe apt install $fetchmailDeps ${sq_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 " [/etc/mail/postfixadmin/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() { echo "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() { echo "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() { echo "updatecheck"; }
|
|
step_18() {
|
|
shift
|
|
local isInstalled=
|
|
local latestVersion=
|
|
if [ -n "${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 [ $sq_config -ne 0 ] ; then
|
|
echo " at $PFA_BACKUP"
|
|
else
|
|
echo
|
|
fi
|
|
}
|
|
step_20_alias() { echo "backup"; }
|
|
step_20() {
|
|
shift
|
|
local tempRoot=
|
|
if [ $sq_config -eq 0 ] ; then
|
|
error -e "No configuration file found"
|
|
return 1
|
|
fi
|
|
if [ ! -z $PFA_BACKUP ] ; then
|
|
exe mkdir -p "$PFA_BACKUP"
|
|
fi
|
|
if [ -n "${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 ${seq_origin:?}/mysql.sh -qq backup "$PFA_DATABASE" "$PFA_BACKUP"
|
|
}
|
|
|
|
step_22_info() {
|
|
echoinfoArgs "[CUSTOM VERSION]"
|
|
shift
|
|
if contextExe; 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() { echo "upgrade"; }
|
|
step_22() {
|
|
shift # don't need step number
|
|
local latestVersion=
|
|
if [ -n "${1:-}" ] ; then
|
|
latestVersion="$1"
|
|
else
|
|
latestVersion=$(curl --silent "$latestUrl" | grep -Po '"tag_name": "postfixadmin-\K.*?(?=")')
|
|
fi
|
|
|
|
if [ -z $latestVersion ] ; then
|
|
error -e "Cannot determine latest version from github repository"
|
|
return 1
|
|
elif interactive ; then
|
|
echo
|
|
exe read -p "Install $latestVersion to $PFA_SRV_LOC [n]o/(y)es? " answer
|
|
case $answer in
|
|
[yY])
|
|
;;
|
|
*)
|
|
info -e "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
|
|
error -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 by ${seq_origin:?}/$(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() { echo "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() { echo "cli"; }
|
|
step_100() {
|
|
shift
|
|
exe ${PFA_SRV_LOC}/scripts/$toolName-cli $@
|
|
}
|
|
|
|
# shellcheck disable=SC2034 # Appears unused
|
|
readonly sqr_minVersion=16
|
|
# shellcheck disable=SC1091 # Don't follow this source
|
|
. /usr/local/bin/sequencer.sh
|