Initial commit. DNS queries are broken. But most of the stuff seems to work.
This commit is contained in:
commit
26e9454ba9
1350
MiniDiscovery.py
Normal file
1350
MiniDiscovery.py
Normal file
File diff suppressed because it is too large
Load Diff
19
Pipfile
Normal file
19
Pipfile
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
[[source]]
|
||||||
|
url = "https://pypi.org/simple"
|
||||||
|
verify_ssl = true
|
||||||
|
name = "pypi"
|
||||||
|
|
||||||
|
[packages]
|
||||||
|
fastapi = "*"
|
||||||
|
twisted = "*"
|
||||||
|
pytest = "*"
|
||||||
|
pydantic = "*"
|
||||||
|
uvicorn = "*"
|
||||||
|
black = "*"
|
||||||
|
pytest-mock = "*"
|
||||||
|
requests = "*"
|
||||||
|
|
||||||
|
[dev-packages]
|
||||||
|
|
||||||
|
[requires]
|
||||||
|
python_version = "3.13"
|
213
demo_client.py
Normal file
213
demo_client.py
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import uuid
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# --- Configuration ---
|
||||||
|
DEFAULT_BASE_URL = "http://localhost:8500" # Adjust if running elsewhere
|
||||||
|
|
||||||
|
# --- Helper Functions ---
|
||||||
|
|
||||||
|
|
||||||
|
def make_request(
|
||||||
|
method: str,
|
||||||
|
endpoint: str,
|
||||||
|
api_token: str,
|
||||||
|
base_url: str = DEFAULT_BASE_URL,
|
||||||
|
params: dict = None,
|
||||||
|
json_data: dict = None,
|
||||||
|
):
|
||||||
|
"""Helper function to make authenticated requests."""
|
||||||
|
headers = {
|
||||||
|
"X-API-Token": api_token,
|
||||||
|
"Accept": "application/json", # Indicate we prefer JSON responses
|
||||||
|
}
|
||||||
|
if json_data:
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
|
||||||
|
url = f"{base_url}{endpoint}"
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = requests.request(
|
||||||
|
method,
|
||||||
|
url,
|
||||||
|
headers=headers,
|
||||||
|
params=params,
|
||||||
|
json=json_data,
|
||||||
|
timeout=10, # Add a timeout
|
||||||
|
)
|
||||||
|
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
|
||||||
|
# Try to parse JSON, but handle cases where response might be empty (like 200 OK on deregister)
|
||||||
|
try:
|
||||||
|
return response.json()
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
return {
|
||||||
|
"status_code": response.status_code,
|
||||||
|
"content": response.text,
|
||||||
|
} # Return status/text if no JSON body
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
print(f"Error making request to {method} {url}: {e}", file=sys.stderr)
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
print(f"An unexpected error occurred: {e}", file=sys.stderr)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def register_service(
|
||||||
|
api_token: str,
|
||||||
|
service_name: str,
|
||||||
|
address: str,
|
||||||
|
port: int,
|
||||||
|
tags: list = None,
|
||||||
|
metadata: dict = None,
|
||||||
|
service_id: str = None,
|
||||||
|
):
|
||||||
|
"""Registers a service instance."""
|
||||||
|
if service_id is None:
|
||||||
|
service_id = f"{service_name}-{uuid.uuid4()}" # Generate a unique ID
|
||||||
|
|
||||||
|
instance_data = {
|
||||||
|
"id": service_id,
|
||||||
|
"name": service_name,
|
||||||
|
"address": address,
|
||||||
|
"port": port,
|
||||||
|
"tags": tags if tags is not None else [],
|
||||||
|
"metadata": metadata if metadata is not None else {},
|
||||||
|
# Health defaults to 'passing' on the server side if not provided
|
||||||
|
}
|
||||||
|
|
||||||
|
print(f"[*] Registering service: {json.dumps(instance_data)}")
|
||||||
|
result = make_request(
|
||||||
|
"POST",
|
||||||
|
"/v1/agent/service/register",
|
||||||
|
api_token=api_token,
|
||||||
|
json_data=instance_data,
|
||||||
|
)
|
||||||
|
if result:
|
||||||
|
print(f"[+] Registration successful: {result}")
|
||||||
|
return service_id # Return the ID used
|
||||||
|
else:
|
||||||
|
print(f"[-] Registration failed.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def deregister_service(api_token: str, service_id: str):
|
||||||
|
"""Deregisters a service instance."""
|
||||||
|
print(f"[*] Deregistering service ID: {service_id}")
|
||||||
|
endpoint = f"/v1/agent/service/deregister/{service_id}"
|
||||||
|
result = make_request(
|
||||||
|
"PUT", endpoint, api_token=api_token # Note: Consul uses PUT for deregister
|
||||||
|
)
|
||||||
|
if result:
|
||||||
|
# Check status code as PUT might return 200 OK with no body on success
|
||||||
|
if isinstance(result, dict) and result.get("status_code") == 200:
|
||||||
|
print(f"[+] Deregistration successful (Status Code 200).")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print(f"[+] Deregistration response: {result}")
|
||||||
|
return True # Assume success if no exception raised
|
||||||
|
else:
|
||||||
|
print(f"[-] Deregistration failed.")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_service_instances(
|
||||||
|
api_token: str, service_name: str, passing_only: bool = False
|
||||||
|
):
|
||||||
|
"""Gets instances for a specific service."""
|
||||||
|
print(f"[*] Querying service '{service_name}' (Passing only: {passing_only})")
|
||||||
|
if passing_only:
|
||||||
|
endpoint = f"/v1/health/service/{service_name}"
|
||||||
|
params = {"passing": "true"}
|
||||||
|
else:
|
||||||
|
endpoint = f"/v1/catalog/service/{service_name}"
|
||||||
|
params = None
|
||||||
|
|
||||||
|
result = make_request("GET", endpoint, api_token=api_token, params=params)
|
||||||
|
|
||||||
|
if result is not None: # Result could be an empty list [] which is valid
|
||||||
|
print(f"[+] Found {len(result)} instance(s):")
|
||||||
|
print(json.dumps(result, indent=2))
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
print(f"[-] Failed to query service '{service_name}'.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def list_all_services(api_token: str):
|
||||||
|
"""Lists all known service names."""
|
||||||
|
print("[*] Listing all known service names...")
|
||||||
|
endpoint = "/v1/catalog/services"
|
||||||
|
result = make_request("GET", endpoint, api_token=api_token)
|
||||||
|
if result:
|
||||||
|
print("[+] Known services:")
|
||||||
|
print(json.dumps(result, indent=2))
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
print("[-] Failed to list services.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# --- Main Execution ---
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description="MiniDiscovery Client Example")
|
||||||
|
parser.add_argument(
|
||||||
|
"-t",
|
||||||
|
"--token",
|
||||||
|
required=True,
|
||||||
|
help="API Token for MiniDiscovery authentication",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--base-url",
|
||||||
|
default=DEFAULT_BASE_URL,
|
||||||
|
help=f"Base URL of the MiniDiscovery API (default: {DEFAULT_BASE_URL})",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
print(f"--- MiniDiscovery Client Demo (Using API at {args.base_url}) ---")
|
||||||
|
|
||||||
|
# 1. Register a couple of services
|
||||||
|
print("\n--- Step 1: Register Services ---")
|
||||||
|
web_id_1 = register_service(
|
||||||
|
args.token,
|
||||||
|
"web",
|
||||||
|
"192.168.1.10",
|
||||||
|
8080,
|
||||||
|
tags=["frontend", "prod"],
|
||||||
|
metadata={"version": "1.2"},
|
||||||
|
)
|
||||||
|
web_id_2 = register_service(
|
||||||
|
args.token,
|
||||||
|
"web",
|
||||||
|
"192.168.1.11",
|
||||||
|
8080,
|
||||||
|
tags=["frontend", "prod"],
|
||||||
|
metadata={"version": "1.2"},
|
||||||
|
)
|
||||||
|
db_id_1 = register_service(
|
||||||
|
args.token, "database", "10.0.0.5", 5432, tags=["backend", "prod-db"]
|
||||||
|
)
|
||||||
|
|
||||||
|
# 2. List all service names
|
||||||
|
print("\n--- Step 2: List All Service Names ---")
|
||||||
|
list_all_services(args.token)
|
||||||
|
|
||||||
|
# 3. Query specific services
|
||||||
|
print("\n--- Step 3: Query Services ---")
|
||||||
|
get_service_instances(args.token, "web")
|
||||||
|
get_service_instances(
|
||||||
|
args.token, "database", passing_only=True
|
||||||
|
) # Assume health check runs
|
||||||
|
get_service_instances(args.token, "nonexistent-service")
|
||||||
|
|
||||||
|
# 4. Deregister one instance
|
||||||
|
print("\n--- Step 4: Deregister an Instance ---")
|
||||||
|
if web_id_1:
|
||||||
|
deregister_service(args.token, web_id_1)
|
||||||
|
|
||||||
|
# 5. Query again to see the change
|
||||||
|
print("\n--- Step 5: Query 'web' Service Again ---")
|
||||||
|
get_service_instances(args.token, "web")
|
||||||
|
|
||||||
|
print("\n--- Demo Complete ---")
|
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
|
Loading…
x
Reference in New Issue
Block a user