The Script Updated (untested)


    if [[ -n "$(ss -Hltn "sport = :${NODE_PORT}")" ]]; then
        NODE_LISTENING="true"
        break
    fi

    sleep 1

done

if [[ "${NODE_LISTENING}" == "true" ]]; then
    echo "[+] Node is listening on ${NODE_PORT}/tcp."
else
    echo "[WARN] Node is not listening on ${NODE_PORT}/tcp yet." >&2
    echo "       This is expected during the initial database load. Monitor with:" >&2
    echo "       ss -ltn 'sport = :${NODE_PORT}'" >&2
    echo "       sudo tail -f ${WRAPPER_LOG}" >&2
fi

# ----------------------------------------------------
# 19b. Legacy Installation Removal
# ----------------------------------------------------

#
# Runs only after the new node is verified running, so a failed
# provisioning run leaves legacy installations (and their keystores) intact.
# Legacy directories and units are root-owned, so removal uses sudo.
#

if [[ "${LEGACY_UNIT_FOUND}" == "true" ]]; then

    echo -e "\n[*] Removing legacy systemd unit ${LEGACY_SERVICE_NAME}..."

    sudo systemctl disable "${LEGACY_SERVICE_NAME}" 2>/dev/null || true
    sudo rm -f "${LEGACY_SERVICE_FILE}"
    sudo systemctl daemon-reload
    sudo systemctl reset-failed "${LEGACY_SERVICE_NAME}" 2>/dev/null || true

    echo "[+] Legacy systemd unit removed."

fi

for LEGACY_DIR in "${LEGACY_FOUND_DIRS[@]}"; do

    echo -e "\n[*] Removing legacy installation at ${LEGACY_DIR}..."

    sudo rm -rf "${LEGACY_DIR}"

    #
    # Remove the parent directory only if nothing else lives there.
    #

    LEGACY_PARENT_DIR="$(dirname "${LEGACY_DIR}")"

    sudo rmdir "${LEGACY_PARENT_DIR}" 2>/dev/null || \
        echo "    - ${LEGACY_PARENT_DIR} not empty; left in place."

    #
    # Remove the persistent SELinux rules registered for the legacy paths.
    #

    if command -v semanage >/dev/null 2>&1 || sudo test -x /usr/sbin/semanage; then

        sudo semanage fcontext -d "${LEGACY_DIR}(/.*)?" 2>/dev/null || true

        if [[ ! -d "${LEGACY_PARENT_DIR}" ]]; then
            sudo semanage fcontext -d "${LEGACY_PARENT_DIR}" 2>/dev/null || true
        fi

        echo "    - Legacy SELinux file-context rules removed."

    fi

    echo "[+] Legacy installation removed: ${LEGACY_DIR}"

done

# ----------------------------------------------------
# 20. Final Service Status
# ----------------------------------------------------

echo -e "\n[*] Final systemd status..."

systemctl status "${SERVICE_NAME}" --no-pager -l || true

# ----------------------------------------------------
# 21. Completion Summary
# ----------------------------------------------------

echo
echo "=================================================="
echo " SERVICENOW APPLICATION NODE PROVISIONING COMPLETE"
echo "=================================================="
echo
echo " Node:                  ${NODE_ID}"
echo " Node Port:             ${NODE_PORT}/tcp (listening: ${NODE_LISTENING})"
echo " Application Directory: ${APP_DIR}"
echo " Provisioned By:        $(id -un) (sudo for system changes)"
echo " Application User:      ${APP_USER}"
echo " Application Group:     ${APP_GROUP}"
echo " Java Home:             ${JAVA_HOME}"
echo " Database:              ${DB_HOST}:${DB_PORT}/${DB_NAME}"
echo " Database User:         ${DB_USER}"
echo " KMF Keystore:          ${KMF_KEYSTORE}"
echo " KMF Alias:             ${KMF_ALIAS}"
echo " KMF Backup:            ${KEYS_DIR}"
if [[ "${KMF_PASSWORD_CHANGED}" == "true" ]]; then
echo " KMF Password:          differs from KMF_PASSWORD; copy it from ${KMF_PROPERTIES}"
fi
echo " HTTPS Keystore:        ${SSL_KEYSTORE}"
echo " DB mTLS Keystore:       ${DB_TLS_KEYSTORE}"
echo " DB mTLS Truststore:     ${DB_TLS_TRUSTSTORE}"
echo " JVM Overrides:         51-memory, 92-access, 93-legacy-jdk, 98-general"
echo " Service:               ${SERVICE_NAME}"
echo " Wrapper PID:           ${WRAPPER_PID}"
echo " JVM PID:               ${JAVA_PID}"
for LEGACY_DIR in "${LEGACY_FOUND_DIRS[@]}"; do
echo " Legacy Installation:   ${LEGACY_DIR} (removed)"
done
if [[ -n "${LEGACY_SOURCE_DIR}" ]]; then
echo " Keystores Migrated:    from ${LEGACY_SOURCE_DIR}"
fi
echo
echo "=================================================="

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *