92 lines
2.6 KiB
Bash
Executable File
92 lines
2.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
API_BASE_URL="${1:-http://127.0.0.1:8100}"
|
|
NODE_CODE="${2:-}"
|
|
NODE_REGION="${3:-mainland}"
|
|
NODE_ROLE="${4:-worker}"
|
|
CONTROL_PLANE_BASE_URL="${5:-${API_BASE_URL}}"
|
|
ROOT_DIR="${6:-/opt/domaincheck}"
|
|
ISSUED_BY="${ISSUED_BY:-cli-bootstrap}"
|
|
EXPIRES_IN_HOURS="${EXPIRES_IN_HOURS:-72}"
|
|
|
|
if [[ -z "${NODE_CODE}" ]]; then
|
|
echo "usage: $0 <api_base_url> <node_code> [node_region] [node_role] [control_plane_base_url] [root_dir]" >&2
|
|
exit 1
|
|
fi
|
|
|
|
NORMALIZED_API_BASE_URL="${API_BASE_URL%/}"
|
|
NORMALIZED_CONTROL_PLANE_BASE_URL="${CONTROL_PLANE_BASE_URL%/}"
|
|
if [[ "${NORMALIZED_API_BASE_URL}" != */api/v1 ]]; then
|
|
API_ENDPOINT="${NORMALIZED_API_BASE_URL}/api/v1/ops/agent/bootstrap-plan"
|
|
else
|
|
API_ENDPOINT="${NORMALIZED_API_BASE_URL}/ops/agent/bootstrap-plan"
|
|
fi
|
|
|
|
TMP_RESPONSE_FILE="$(mktemp)"
|
|
cleanup() {
|
|
rm -f "${TMP_RESPONSE_FILE}"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
python3 - "${NODE_CODE}" "${NODE_REGION}" "${NODE_ROLE}" "${ISSUED_BY}" "${EXPIRES_IN_HOURS}" "${NORMALIZED_CONTROL_PLANE_BASE_URL}" "${ROOT_DIR}" >"${TMP_RESPONSE_FILE}.payload" <<'PY'
|
|
import json
|
|
import sys
|
|
|
|
payload = {
|
|
"node_code": sys.argv[1],
|
|
"node_region": sys.argv[2],
|
|
"node_role": sys.argv[3],
|
|
"issued_by": sys.argv[4],
|
|
"expires_in_hours": int(sys.argv[5]),
|
|
"control_plane_base_url": sys.argv[6],
|
|
"root_dir": sys.argv[7],
|
|
"metadata": {
|
|
"issued_from": "build_node_agent_bootstrap_plan.sh",
|
|
},
|
|
}
|
|
print(json.dumps(payload, ensure_ascii=False))
|
|
PY
|
|
|
|
curl -fsS -X POST \
|
|
-H 'Content-Type: application/json' \
|
|
--data-binary @"${TMP_RESPONSE_FILE}.payload" \
|
|
"${API_ENDPOINT}" >"${TMP_RESPONSE_FILE}"
|
|
|
|
python3 - "${TMP_RESPONSE_FILE}" <<'PY'
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
response = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
|
|
response_code = response.get("code", 0)
|
|
if int(response_code if response_code is not None else 0) != 0:
|
|
raise SystemExit(response.get("message") or "bootstrap plan request failed")
|
|
|
|
data = response.get("data") or {}
|
|
plan = data.get("bootstrap_plan") or {}
|
|
|
|
print("[token]")
|
|
print(f"node_code={data.get('node_code', '')}")
|
|
print(f"token_preview={data.get('token_preview', '')}")
|
|
print(f"expires_at={data.get('expires_at', '')}")
|
|
print()
|
|
print("[env]")
|
|
print(plan.get("env_content") or "")
|
|
print()
|
|
print("[commands]")
|
|
print(plan.get("command_block") or "")
|
|
print()
|
|
print("[bootstrap-script]")
|
|
print(plan.get("bootstrap_script_content") or "")
|
|
print()
|
|
print("[bootstrap-run-block]")
|
|
print(plan.get("bootstrap_run_script_block") or "")
|
|
print()
|
|
print("[health-checks]")
|
|
for item in plan.get("health_checks") or []:
|
|
print(item)
|
|
PY
|
|
|
|
rm -f "${TMP_RESPONSE_FILE}.payload"
|