Initial fleet-api skill
This commit is contained in:
131
lib/machines.sh
Normal file
131
lib/machines.sh
Normal file
@@ -0,0 +1,131 @@
|
||||
#!/bin/bash
|
||||
# Machine registry logic
|
||||
|
||||
MACHINES_FILE="${DATA_DIR:-/data/fleet}/machines.json"
|
||||
STALE_THRESHOLD=60 # seconds
|
||||
|
||||
# Get current timestamp
|
||||
now() {
|
||||
date +%s
|
||||
}
|
||||
|
||||
# Read machines file with lock
|
||||
read_machines() {
|
||||
if [ -f "$MACHINES_FILE" ]; then
|
||||
cat "$MACHINES_FILE"
|
||||
else
|
||||
echo '{}'
|
||||
fi
|
||||
}
|
||||
|
||||
# Write machines file with lock
|
||||
write_machines() {
|
||||
local data="$1"
|
||||
echo "$data" > "$MACHINES_FILE.tmp"
|
||||
mv "$MACHINES_FILE.tmp" "$MACHINES_FILE"
|
||||
}
|
||||
|
||||
# Update machine status based on lastSeen
|
||||
update_status() {
|
||||
local machines="$1"
|
||||
local current_time=$(now)
|
||||
|
||||
echo "$machines" | jq --arg now "$current_time" --arg threshold "$STALE_THRESHOLD" '
|
||||
to_entries | map(
|
||||
.value.status = (
|
||||
if (.value.lastSeen | tonumber) < (($now | tonumber) - ($threshold | tonumber))
|
||||
then "unknown"
|
||||
else .value.status
|
||||
end
|
||||
)
|
||||
) | from_entries
|
||||
'
|
||||
}
|
||||
|
||||
# Handle POST /api/machines/register
|
||||
handle_register() {
|
||||
local body="$1"
|
||||
|
||||
# Validate JSON
|
||||
if ! echo "$body" | jq -e . >/dev/null 2>&1; then
|
||||
send_error "400 Bad Request" "Invalid JSON"
|
||||
return
|
||||
fi
|
||||
|
||||
# Extract required fields
|
||||
local id=$(echo "$body" | jq -r '.id // empty')
|
||||
local name=$(echo "$body" | jq -r '.name // empty')
|
||||
local url=$(echo "$body" | jq -r '.url // empty')
|
||||
|
||||
if [ -z "$id" ] || [ -z "$name" ] || [ -z "$url" ]; then
|
||||
send_error "400 Bad Request" "Missing required fields: id, name, url"
|
||||
return
|
||||
fi
|
||||
|
||||
# Read current machines
|
||||
local machines=$(read_machines)
|
||||
local current_time=$(now)
|
||||
|
||||
# Create/update machine entry
|
||||
local machine=$(echo "$body" | jq --arg ts "$current_time" '. + {lastSeen: ($ts | tonumber)}')
|
||||
|
||||
# Add to machines
|
||||
machines=$(echo "$machines" | jq --arg id "$id" --argjson machine "$machine" '.[$id] = $machine')
|
||||
|
||||
# Write back
|
||||
write_machines "$machines"
|
||||
|
||||
send_json "200 OK" "$machine"
|
||||
}
|
||||
|
||||
# Handle GET /api/machines
|
||||
handle_list_machines() {
|
||||
local machines=$(read_machines)
|
||||
|
||||
# Update status based on lastSeen
|
||||
machines=$(update_status "$machines")
|
||||
|
||||
# Convert to array
|
||||
local result=$(echo "$machines" | jq '[to_entries[] | .value]')
|
||||
|
||||
send_json "200 OK" "$result"
|
||||
}
|
||||
|
||||
# Handle GET /api/machines/:id
|
||||
handle_get_machine() {
|
||||
local id="$1"
|
||||
local machines=$(read_machines)
|
||||
|
||||
# Update status
|
||||
machines=$(update_status "$machines")
|
||||
|
||||
# Get machine
|
||||
local machine=$(echo "$machines" | jq --arg id "$id" '.[$id] // null')
|
||||
|
||||
if [ "$machine" = "null" ]; then
|
||||
send_error "404 Not Found" "Machine not found"
|
||||
return
|
||||
fi
|
||||
|
||||
send_json "200 OK" "$machine"
|
||||
}
|
||||
|
||||
# Handle DELETE /api/machines/:id
|
||||
handle_delete_machine() {
|
||||
local id="$1"
|
||||
local machines=$(read_machines)
|
||||
|
||||
# Check if exists
|
||||
local exists=$(echo "$machines" | jq --arg id "$id" 'has($id)')
|
||||
|
||||
if [ "$exists" = "false" ]; then
|
||||
send_error "404 Not Found" "Machine not found"
|
||||
return
|
||||
fi
|
||||
|
||||
# Remove machine
|
||||
machines=$(echo "$machines" | jq --arg id "$id" 'del(.[$id])')
|
||||
write_machines "$machines"
|
||||
|
||||
send_json "200 OK" '{"deleted":true}'
|
||||
}
|
||||
Reference in New Issue
Block a user