111 lines
2.7 KiB
Bash
Executable File
111 lines
2.7 KiB
Bash
Executable File
#!/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
|