#!/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" 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 echo " [I] Create lock folder" exe mkdir -p "$fetchmailLockDir" exe chown ${fetchmail}: "$fetchmailLockDir" } fetchmailUser="fetchmail" fetchmailLockDir="/var/lock/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 echo " [$toolAdditionsLoc/fetchmail.pl]" echo " # Change path to fetchmail.conf (see above)" echo echo " [I] Run step cron when configuration is done" } step_6_info() { echo "Create postfixadmin fetchmail plugin cron"; } step_6_alias() { ALIAS="cron"; } step_6() { addConf -s "$fetchPluginCron" "$fetchPluginCronLoc" } fetchPluginCronLoc="/etc/cron.d/fetchmailplugin" fetchPluginCron="*/1 * * * * fetchmail /srv/postfixadmin/ADDITIONS/fetchmail.pl > /dev/null" step_18_info() { 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() { echo -n "Create a backup [POSTFIXADMIN SRV ROOT]" 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() { shift if [ -z $1 ] ; then echo -n "Get latest version from github" if [ $CONTEXT_HELP -eq 0 ] ; then echo ": $(curl --silent "$latestUrl" | grep -Po '"tag_name": "postfixadmin-\K.*?(?=")')" else echo " [CUSTOM VERSION]" fi else echo "Get version $1 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() { echo "$toolName client script [OPTIONS]" 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=11 . /usr/local/bin/sequencer.sh