Your IP : 10.128.30.70


Current Path : /var/lib/dpkg/info/
Upload File :
Current File : //var/lib/dpkg/info/rmmagent.prerm

#!/bin/sh

# Reference: https://wiki.debian.org/MaintainerScripts

set -e

if [ -n "${RMM_DEBUG}" ]; then
    echo "Running: $0 $@"
    set -x
fi

RMM_BASE="/usr/local/rmmagent"
SVC_NAME="rmmagent"
LOG_DIR="/var/log/rmmagent"

SYSTEM_CTL="$(command -v systemctl 2>/dev/null || true)"
UPDATE_RC="$(command -v update-rc.d 2>/dev/null || true)"
INVOKE_RC="$(command -v invoke-rc.d 2>/dev/null || true)"

# Init logs, if not available
mkdir -p "${LOG_DIR}"

logInfo() # message
{
    logMessage "$1" false
    if [ -n "${RMM_DEBUG}" ]; then
        echo "$1"
    fi
}

logWarning() # message
{
    logMessage "$1" true
    echo "$1"
}

logMessage() # message, isWarning
{
    if [ -d "${LOG_DIR}" ]; then
        Prefix="I"
        if ${2}; then
            Prefix="W"
        fi
        echo "[`date '+%Y-%m-%d %H:%M:%S'`] [${Prefix}] $1" >> "${LOG_DIR}/package_script.log"
    fi
}

unregisterAgent()
{
    logInfo "Unregister Agent"
    if [ -f "${RMM_BASE}/agentconfig.xml" ]; then
        logInfo "Unregister"
        cd "${RMM_BASE}"
        "${RMM_BASE}/rmmagentd" unregister || logWarning "Error during unregister"
    fi
}

stopService()
{
    logInfo "Stop service"
    if [ -x "${SYSTEM_CTL}" ]; then
        logInfo "Stopping with systemd"
        "${SYSTEM_CTL}" stop "${SVC_NAME}.service"
    elif [ -x "/etc/init.d/${SVC_NAME}" ]; then
        logInfo "Stopping with init.d"
        if [ -x "${INVOKE_RC}" ]; then
            "${INVOKE_RC}" "${SVC_NAME}" stop
        else
            "/etc/init.d/${SVC_NAME}" stop
        fi
    fi
}

disableService()
{
    logInfo "Disable service"
    if [ -x "${SYSTEM_CTL}" ]; then
        logInfo "Disabling with systemd"
        "${SYSTEM_CTL}" disable "${SVC_NAME}.service"

        # Putting back file, removed in "pre" script
        touch "/etc/init.d/${SVC_NAME}" || true
    elif [ -x "${UPDATE_RC}" ]; then
        logInfo "Disabling with init.d"
        "${UPDATE_RC}" "${SVC_NAME}" remove
    else
        logInfo "Disabling manually"
        rm -f /etc/rc?.d/???"${SVC_NAME}" || logWarning "Error removing rc.d script"
    fi
}

startService()
{
    logInfo "Rollback - start service"
    if [ -x "${SYSTEM_CTL}" ]; then
        logInfo "Starting with systemd"
        "${SYSTEM_CTL}" start "${SVC_NAME}.service"
    elif [ -x "/etc/init.d/${SVC_NAME}" ]; then
        logInfo "Starting with init.d"
        if [ -x "${INVOKE_RC}" ]; then
            "${INVOKE_RC}" "${SVC_NAME}" start
        else
            "/etc/init.d/${SVC_NAME}" start
        fi
    fi
}

logInfo "prerm script is executing: $1 command"

case "$1" in
    remove|deconfigure|remove-in-favour|deconfigure-in-favour)
        unregisterAgent
        stopService
        disableService
        ;;
    upgrade)
        ;;
    failed-upgrade)
        startService
        ;;
    *)
        logWarning "prerm called with unknown argument '$1'" >&2
        exit 1
        ;;
esac

exit 0