Files
Fabric/scripts/register-guild-node.sh

72 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Usage:
# scripts/register-guild-node.sh <node_id> <name> <endpoint>
# Example:
# scripts/register-guild-node.sh guild-node-1 "Guild Node 1" "http://backend-guild:7002"
if [[ $# -ne 3 ]]; then
echo "Usage: $0 <node_id> <name> <endpoint>" >&2
exit 1
fi
NODE_ID="$1"
NODE_NAME="$2"
NODE_ENDPOINT="$3"
if ! command -v docker >/dev/null 2>&1; then
echo "docker not found" >&2
exit 1
fi
if ! docker compose ps backend-center >/dev/null 2>&1; then
echo "backend-center service is not available in current docker compose project" >&2
exit 1
fi
RESULT_JSON=$(docker compose exec -T \
-e REG_NODE_ID="$NODE_ID" \
-e REG_NODE_NAME="$NODE_NAME" \
-e REG_NODE_ENDPOINT="$NODE_ENDPOINT" \
backend-center node -e "
(async () => {
const nodeId = process.env.REG_NODE_ID;
const name = process.env.REG_NODE_NAME;
const endpoint = process.env.REG_NODE_ENDPOINT;
const res = await fetch('http://127.0.0.1:7001/api/nodes/register', {
method: 'POST',
headers: {
'content-type': 'application/json',
'x-fabric-version': '1',
},
body: JSON.stringify({ nodeId, name, endpoint }),
});
const text = await res.text();
let data;
try { data = JSON.parse(text); } catch { data = { raw: text }; }
process.stdout.write(JSON.stringify({ status: res.status, data }));
})();
" )
STATUS=$(node -e "const o=JSON.parse(process.argv[1]);process.stdout.write(String(o.status));" "$RESULT_JSON")
if [[ "$STATUS" != "201" ]]; then
echo "registration failed: $RESULT_JSON" >&2
exit 1
fi
API_KEY=$(node -e "const o=JSON.parse(process.argv[1]);process.stdout.write(o.data?.apiKey||'');" "$RESULT_JSON")
NODE_ID_OUT=$(node -e "const o=JSON.parse(process.argv[1]);process.stdout.write(o.data?.node?.nodeId||'');" "$RESULT_JSON")
if [[ -z "$API_KEY" ]]; then
echo "registration succeeded but no apiKey returned: $RESULT_JSON" >&2
exit 1
fi
echo "Node registered: $NODE_ID_OUT"
echo "FABRIC_BACKEND_GUILD_CENTER_API_KEY=$API_KEY"