92 lines
2.2 KiB
Bash
Executable File
92 lines
2.2 KiB
Bash
Executable File
#!/bin/bash
|
|
# Script to list all services from MiniDiscovery service
|
|
|
|
# Default values
|
|
BASE_URL="http://localhost:8500"
|
|
SERVICE_NAME=""
|
|
PASSING_ONLY=false
|
|
|
|
# Parse command line arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
-t|--token)
|
|
API_TOKEN="$2"
|
|
shift 2
|
|
;;
|
|
-u|--url)
|
|
BASE_URL="$2"
|
|
shift 2
|
|
;;
|
|
-s|--service)
|
|
SERVICE_NAME="$2"
|
|
shift 2
|
|
;;
|
|
-p|--passing)
|
|
PASSING_ONLY=true
|
|
shift
|
|
;;
|
|
-h|--help)
|
|
echo "Usage: $0 -t API_TOKEN [options]"
|
|
echo "Options:"
|
|
echo " -t, --token TOKEN API token for authentication (required)"
|
|
echo " -u, --url URL Base URL of the API (default: $BASE_URL)"
|
|
echo " -s, --service NAME List instances of a specific service"
|
|
echo " -p, --passing Only show instances passing health checks"
|
|
echo " -h, --help Show this help message"
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Unknown option: $1"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Check if API token is provided
|
|
if [ -z "$API_TOKEN" ]; then
|
|
echo "Error: API token is required"
|
|
echo "Use -t or --token to provide the API token"
|
|
exit 1
|
|
fi
|
|
|
|
# Function to format JSON output
|
|
format_json() {
|
|
# Check if jq is available
|
|
if command -v jq &> /dev/null; then
|
|
echo "$1" | jq .
|
|
else
|
|
echo "$1" | python3 -m json.tool 2>/dev/null || echo "$1"
|
|
fi
|
|
}
|
|
|
|
# If service name is provided, list instances of that service
|
|
if [ -n "$SERVICE_NAME" ]; then
|
|
echo "Listing instances of service: $SERVICE_NAME"
|
|
|
|
if [ "$PASSING_ONLY" = true ]; then
|
|
echo "Showing only instances passing health checks..."
|
|
ENDPOINT="/v1/health/service/${SERVICE_NAME}?passing=true"
|
|
else
|
|
ENDPOINT="/v1/catalog/service/${SERVICE_NAME}"
|
|
fi
|
|
|
|
RESPONSE=$(curl -s -X GET \
|
|
-H "X-API-Token: $API_TOKEN" \
|
|
-H "Accept: application/json" \
|
|
"${BASE_URL}${ENDPOINT}")
|
|
|
|
echo "Found $(echo "$RESPONSE" | grep -o '"ID"' | wc -l) instance(s):"
|
|
format_json "$RESPONSE"
|
|
else
|
|
# List all services
|
|
echo "Listing all services..."
|
|
|
|
RESPONSE=$(curl -s -X GET \
|
|
-H "X-API-Token: $API_TOKEN" \
|
|
-H "Accept: application/json" \
|
|
"${BASE_URL}/v1/catalog/services")
|
|
|
|
echo "Services found:"
|
|
format_json "$RESPONSE"
|
|
fi
|