|
| 1 | +#! /usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Configuration |
| 5 | +PLATFORM="docker-compose" # Change to "helm" or "cdk" as needed |
| 6 | +VERSION="unknown" |
| 7 | +STATUS="" |
| 8 | +SERVER_BASE_URL="http://hyperswitch-server:8080" |
| 9 | +HYPERSWITCH_HEALTH_URL="${SERVER_BASE_URL}/health" |
| 10 | +HYPERSWITCH_DEEP_HEALTH_URL="${SERVER_BASE_URL}/health/ready" |
| 11 | +WEBHOOK_URL="https://hyperswitch.gateway.scarf.sh/${PLATFORM}" |
| 12 | + |
| 13 | +# Fetch health status |
| 14 | +echo "Fetching app server health status..." |
| 15 | +HEALTH_RESPONSE=$(curl --silent --fail "${HYPERSWITCH_HEALTH_URL}") || HEALTH_RESPONSE="connection_error" |
| 16 | + |
| 17 | +if [[ "${HEALTH_RESPONSE}" == "connection_error" ]]; then |
| 18 | + STATUS="error" |
| 19 | + ERROR_MESSAGE="404 response" |
| 20 | + |
| 21 | + curl --get "${WEBHOOK_URL}" \ |
| 22 | + --data-urlencode "version=${VERSION}" \ |
| 23 | + --data-urlencode "status=${STATUS}" \ |
| 24 | + --data-urlencode "error_message=${ERROR_MESSAGE}" |
| 25 | + |
| 26 | + echo "Webhook sent with connection error." |
| 27 | + exit 0 |
| 28 | +fi |
| 29 | + |
| 30 | +# Fetch Hyperswitch version |
| 31 | +VERSION=$(curl --silent --output /dev/null --request GET --write-out '%header{x-hyperswitch-version}' "${HYPERSWITCH_DEEP_HEALTH_URL}" | sed 's/-dirty$//') |
| 32 | + |
| 33 | +echo "Fetching Hyperswitch health status..." |
| 34 | +HEALTH_RESPONSE=$(curl --silent "${HYPERSWITCH_DEEP_HEALTH_URL}") |
| 35 | + |
| 36 | +# Prepare curl command |
| 37 | +CURL_COMMAND=("curl" "--get" "${WEBHOOK_URL}" "--data-urlencode" "version=${VERSION}") |
| 38 | + |
| 39 | +# Check if the response contains an error |
| 40 | +if [[ "$(echo "${HEALTH_RESPONSE}" | jq --raw-output '.error')" != 'null' ]]; then |
| 41 | + STATUS="error" |
| 42 | + ERROR_TYPE=$(echo "${HEALTH_RESPONSE}" | jq --raw-output '.error.type') |
| 43 | + ERROR_MESSAGE=$(echo "${HEALTH_RESPONSE}" | jq --raw-output '.error.message') |
| 44 | + ERROR_CODE=$(echo "${HEALTH_RESPONSE}" | jq --raw-output '.error.code') |
| 45 | + |
| 46 | + CURL_COMMAND+=( |
| 47 | + "--data-urlencode" "status=${STATUS}" |
| 48 | + "--data-urlencode" "error_type=${ERROR_TYPE}" |
| 49 | + "--data-urlencode" "error_message=${ERROR_MESSAGE}" |
| 50 | + "--data-urlencode" "error_code=${ERROR_CODE}" |
| 51 | + ) |
| 52 | +else |
| 53 | + STATUS="success" |
| 54 | + CURL_COMMAND+=("--data-urlencode" "status=${STATUS}") |
| 55 | + |
| 56 | + for key in $(echo "${HEALTH_RESPONSE}" | jq --raw-output 'keys_unsorted[]'); do |
| 57 | + value=$(echo "${HEALTH_RESPONSE}" | jq --raw-output --arg key "${key}" '.[$key]') |
| 58 | + CURL_COMMAND+=("--data-urlencode" "'${key}=${value}'") |
| 59 | + done |
| 60 | +fi |
| 61 | + |
| 62 | +# Send the webhook request |
| 63 | +bash -c "${CURL_COMMAND[*]}" |
| 64 | + |
| 65 | +echo "Webhook notification sent." |
0 commit comments