Initial commit. DNS queries are broken. But most of the stuff seems to work.
This commit is contained in:
71
scripts/README.md
Normal file
71
scripts/README.md
Normal file
@ -0,0 +1,71 @@
|
||||
# How to Use These Scripts
|
||||
|
||||
1. Register SSH Server
|
||||
|
||||
The first script registers an SSH server with the MiniDiscovery service:
|
||||
|
||||
bash
|
||||
|
||||
chmod +x register_ssh_server.sh
|
||||
./register_ssh_server.sh -t YOUR_API_TOKEN
|
||||
|
||||
Options:
|
||||
|
||||
-t, --token TOKEN - API token for authentication (required)
|
||||
-u, --url URL - Base URL of the API (default: http://localhost:8500)
|
||||
-n, --name NAME - Server name (default: ssh-server)
|
||||
-a, --address ADDRESS - Server address (default: 127.0.0.1)
|
||||
-p, --port PORT - SSH port (default: 22)
|
||||
--tags TAGS - Comma-separated tags (default: ssh,secure)
|
||||
|
||||
Example with custom values:
|
||||
|
||||
bash
|
||||
|
||||
./register_ssh_server.sh -t YOUR_API_TOKEN -n prod-ssh -a 192.168.1.50 -p 2222 --tags "ssh,secure,production"
|
||||
|
||||
2. List Services
|
||||
|
||||
The second script lists all services or instances of a specific service:
|
||||
|
||||
bash
|
||||
|
||||
chmod +x list_services.sh
|
||||
./list_services.sh -t YOUR_API_TOKEN
|
||||
|
||||
Options:
|
||||
|
||||
-t, --token TOKEN - API token for authentication (required)
|
||||
-u, --url URL - Base URL of the API
|
||||
-s, --service NAME - List instances of a specific service
|
||||
-p, --passing - Only show instances passing health checks
|
||||
|
||||
Example to list specific service:
|
||||
|
||||
bash
|
||||
|
||||
./list_services.sh -t YOUR_API_TOKEN -s ssh-server
|
||||
|
||||
3. Deregister Service
|
||||
|
||||
The third script removes a service from the discovery service:
|
||||
|
||||
bash
|
||||
|
||||
chmod +x deregister_service.sh
|
||||
./deregister_service.sh -t YOUR_API_TOKEN -i SERVICE_ID
|
||||
|
||||
Options:
|
||||
|
||||
-t, --token TOKEN - API token for authentication (required)
|
||||
-u, --url URL - Base URL of the API
|
||||
-i, --id SERVICE_ID - Service ID to deregister
|
||||
--last - Deregister the last registered service (uses stored ID)
|
||||
|
||||
Example using the last registered service:
|
||||
|
||||
bash
|
||||
|
||||
./deregister_service.sh -t YOUR_API_TOKEN --last
|
||||
|
||||
Note: The registration script saves the service ID to a file called .last_registered_service_id, which the deregistration script can use with the --last option.
|
88
scripts/deregister_service.sh
Normal file
88
scripts/deregister_service.sh
Normal file
@ -0,0 +1,88 @@
|
||||
#!/bin/bash
|
||||
# Script to deregister a service from MiniDiscovery service
|
||||
|
||||
# Default values
|
||||
BASE_URL="http://localhost:8500"
|
||||
SERVICE_ID=""
|
||||
|
||||
# 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
|
||||
;;
|
||||
-i|--id)
|
||||
SERVICE_ID="$2"
|
||||
shift 2
|
||||
;;
|
||||
--last)
|
||||
# Try to read the service ID from the file created during registration
|
||||
if [ -f .last_registered_service_id ]; then
|
||||
SERVICE_ID=$(cat .last_registered_service_id)
|
||||
else
|
||||
echo "Error: No previously registered service ID found."
|
||||
exit 1
|
||||
fi
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
echo "Usage: $0 -t API_TOKEN (-i SERVICE_ID | --last) [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 " -i, --id SERVICE_ID Service ID to deregister (required unless --last is used)"
|
||||
echo " --last Deregister the last registered service"
|
||||
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
|
||||
|
||||
# Check if service ID is provided
|
||||
if [ -z "$SERVICE_ID" ]; then
|
||||
echo "Error: Service ID is required"
|
||||
echo "Use -i or --id to provide the service ID, or --last to use the last registered service"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Make API request to deregister the service
|
||||
echo "Deregistering service ID: $SERVICE_ID"
|
||||
|
||||
RESPONSE=$(curl -s -X PUT \
|
||||
-H "X-API-Token: $API_TOKEN" \
|
||||
-H "Accept: application/json" \
|
||||
"${BASE_URL}/v1/agent/service/deregister/${SERVICE_ID}")
|
||||
|
||||
# Check if the request was successful
|
||||
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X PUT \
|
||||
-H "X-API-Token: $API_TOKEN" \
|
||||
-H "Accept: application/json" \
|
||||
"${BASE_URL}/v1/agent/service/deregister/${SERVICE_ID}")
|
||||
|
||||
if [ "$HTTP_CODE" -eq 200 ]; then
|
||||
echo "Deregistration successful!"
|
||||
# Remove the stored service ID if --last was used
|
||||
if [ -f .last_registered_service_id ] && [ "$(cat .last_registered_service_id)" = "$SERVICE_ID" ]; then
|
||||
rm .last_registered_service_id
|
||||
fi
|
||||
else
|
||||
echo "Deregistration failed. HTTP code: $HTTP_CODE"
|
||||
echo "Response: $RESPONSE"
|
||||
exit 1
|
||||
fi
|
91
scripts/list_services.sh
Executable file
91
scripts/list_services.sh
Executable file
@ -0,0 +1,91 @@
|
||||
#!/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
|
110
scripts/register_ssh_server.sh
Executable file
110
scripts/register_ssh_server.sh
Executable file
@ -0,0 +1,110 @@
|
||||
#!/bin/bash
|
||||
# Script to register an SSH server with MiniDiscovery service
|
||||
|
||||
# Default values
|
||||
BASE_URL="http://localhost:8500"
|
||||
SERVER_NAME="ssh-server"
|
||||
SERVER_ADDRESS="127.0.0.1"
|
||||
SERVER_PORT=22
|
||||
TAGS="ssh,secure"
|
||||
|
||||
# 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
|
||||
;;
|
||||
-n|--name)
|
||||
SERVER_NAME="$2"
|
||||
shift 2
|
||||
;;
|
||||
-a|--address)
|
||||
SERVER_ADDRESS="$2"
|
||||
shift 2
|
||||
;;
|
||||
-p|--port)
|
||||
SERVER_PORT="$2"
|
||||
shift 2
|
||||
;;
|
||||
--tags)
|
||||
TAGS="$2"
|
||||
shift 2
|
||||
;;
|
||||
-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 " -n, --name NAME Server name (default: $SERVER_NAME)"
|
||||
echo " -a, --address ADDRESS Server address (default: $SERVER_ADDRESS)"
|
||||
echo " -p, --port PORT SSH port (default: $SERVER_PORT)"
|
||||
echo " --tags TAGS Comma-separated tags (default: $TAGS)"
|
||||
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
|
||||
|
||||
# Generate a unique service ID
|
||||
SERVICE_ID="${SERVER_NAME}-$(uuidgen || cat /proc/sys/kernel/random/uuid)"
|
||||
|
||||
# Convert comma-separated tags to JSON array
|
||||
IFS=',' read -ra TAG_ARRAY <<< "$TAGS"
|
||||
TAGS_JSON=$(printf '"%s",' "${TAG_ARRAY[@]}" | sed 's/,$//')
|
||||
TAGS_JSON="[$TAGS_JSON]"
|
||||
|
||||
# Prepare JSON payload
|
||||
JSON_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"id": "$SERVICE_ID",
|
||||
"name": "$SERVER_NAME",
|
||||
"address": "$SERVER_ADDRESS",
|
||||
"port": $SERVER_PORT,
|
||||
"tags": $TAGS_JSON,
|
||||
"metadata": {
|
||||
"protocol": "ssh",
|
||||
"registered_at": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
|
||||
}
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Make API request to register the service
|
||||
echo "Registering SSH server with MiniDiscovery service..."
|
||||
echo "Service ID: $SERVICE_ID"
|
||||
|
||||
RESPONSE=$(curl -s -X POST \
|
||||
-H "X-API-Token: $API_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json" \
|
||||
-d "$JSON_PAYLOAD" \
|
||||
"${BASE_URL}/v1/agent/service/register")
|
||||
|
||||
# Check if the request was successful
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Registration successful!"
|
||||
echo "Service ID: $SERVICE_ID"
|
||||
echo "Remember this Service ID to deregister the service later."
|
||||
# Save the service ID to a file for later use
|
||||
echo "$SERVICE_ID" > .last_registered_service_id
|
||||
else
|
||||
echo "Registration failed."
|
||||
echo "Response: $RESPONSE"
|
||||
exit 1
|
||||
fi
|
Reference in New Issue
Block a user