Your IP : 10.128.30.70


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

#!/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
}

enableService()
{
    logInfo "Enable service"
    if [ -x "${SYSTEM_CTL}" ]; then
        logInfo "Enabling with systemd"

        # Remove init.d scripts, because this distro uses systemd
        rm -rf "/etc/init.d/${SVC_NAME}" || true

        "${SYSTEM_CTL}" daemon-reload
        "${SYSTEM_CTL}" disable "${SVC_NAME}.service"
        "${SYSTEM_CTL}" enable "${SVC_NAME}.service"
    elif [ -x "${UPDATE_RC}" ]; then
        logInfo "Enabling with init.d"
        "${UPDATE_RC}" "${SVC_NAME}" defaults
    else
        logInfo "Enabling manually"
        for i in 2 3 4 5; do
            ln -sf "/etc/init.d/${SVC_NAME}" "/etc/rc${i}.d/S90${SVC_NAME}"
        done
        for i in 1 6; do
            ln -sf "/etc/init.d/${SVC_NAME}" "/etc/rc${i}.d/K10${SVC_NAME}"
        done
    fi
}

startService()
{
    logInfo "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
}

setPermissionsInRmmagent()
{
    # Set default permissions
    chmod -R go-w "${RMM_BASE}"
    for dir in ${RMM_BASE}/*; do
        if [ -d "${dir}" ]; then
            chmod -R go-rwx "${dir}"
        fi
    done

    logInfo "Permissions are fixed for ${RMM_BASE}"
}

logInfo "postinst script is executing: $1 command"

case "$1" in
    configure)
        enableService
        startService
        setPermissionsInRmmagent
        ;;
    abort-remove|abort-deconfigure)
        if [ -f "${RMM_BASE}/rmmagentd" ]; then
            logInfo "Agent files are in place. Restarting service."
            enableService
            startService
            exit 0
        fi
        logWarning "Action can't be undone. Daemon was already removed"
        exit 1
        ;;
    abort-upgrade)
        ;;
    *)
        logWarning "postinst called with unknown argument '$1'" >&2
        exit 1
esac

exit 0