From 8ec57d1ad3940e45ce4ae6152963aadf089f111d Mon Sep 17 00:00:00 2001 From: Randall Date: Fri, 3 Jan 2025 15:27:27 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20goedge=5Fadd=5Fnode.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- goedge_add_node.sh | 65 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/goedge_add_node.sh b/goedge_add_node.sh index e17e510..c2e63aa 100644 --- a/goedge_add_node.sh +++ b/goedge_add_node.sh @@ -25,6 +25,9 @@ function install_deps_if_needed() { elif command -v zypper &> /dev/null; then echo "[INFO] 检测到系统使用 zypper,开始安装 $pkg" sudo zypper install -y curl jq coreutils unzip + elif command -v apk &> /dev/null; then + echo "[INFO] 检测到系统使用 apk (Alpine),开始安装 $pkg" + sudo apk add --no-cache curl jq coreutils unzip else echo "[ERROR] 未识别的包管理器,无法自动安装 $pkg,请手动安装。" exit 1 @@ -53,9 +56,9 @@ CLUSTER_ID="" GROUP_ID=0 REGION_ID=0 DNS_ROUTES="" -API_HOST="" -GRANT_ID=1 # 默认 -WEB_INSTALL=0 # 是否使用 --webinstall +API_HOSTS=() # 存放多个API地址 +GRANT_ID=1 # 默认 +WEB_INSTALL=0 # 是否使用 --webinstall while [[ $# -gt 0 ]]; do case $1 in @@ -92,8 +95,12 @@ while [[ $# -gt 0 ]]; do shift 2 ;; --api-host) - API_HOST="$2" - shift 2 + # 将后续所有非开头带 "--" 的参数视为host(可多个) + shift + while [[ $# -gt 0 && ! $1 =~ ^-- ]]; do + API_HOSTS+=("$1") + shift + done ;; --webinstall) WEB_INSTALL=1 @@ -106,12 +113,20 @@ while [[ $# -gt 0 ]]; do esac done -# 校验必填 +# 校验必填: acid, ackey, clusterid, api-host if [[ -z "$ACID" || -z "$ACKEY" || -z "$CLUSTER_ID" ]]; then - echo "[ERROR] 用法: $0 --acid --ackey --clusterid [--grantid ] [--name ] [--groupid ] [--regionid ] [--dnsroutes ] [--api-host ] [--webinstall]" + echo "[ERROR] 用法: $0 --acid --ackey --clusterid [--grantid ] [--name ] [--groupid ] [--regionid ] [--dnsroutes ] [--api-host [HOST2]...] [--webinstall]" exit 1 fi +if [[ ${#API_HOSTS[@]} -eq 0 ]]; then + echo "[ERROR] 必须至少指定一个 --api-host" + exit 1 +fi + +# 假设脚本中要选第一个 API_HOSTS[0] 作为获取 Token 的地址 +MAIN_API_HOST="${API_HOSTS[0]}" + ############################################################################### # 3. 获取本机 IPv4, 拼装默认名称 ############################################################################### @@ -135,7 +150,7 @@ fi echo "[INFO] 获取 Token..." get_token_resp=$(curl -s -X POST \ -H "Content-Type: application/json" \ - "$API_HOST/APIAccessTokenService/getAPIAccessToken" \ + "$MAIN_API_HOST/APIAccessTokenService/getAPIAccessToken" \ -d '{ "type": "admin", "accessKeyId": "'"$ACID"'", @@ -191,7 +206,7 @@ EOF create_node_resp=$(curl -s -X POST \ -H "Content-Type: application/json" \ -H "X-Edge-Access-Token: $TOKEN" \ - "$API_HOST/NodeService/createNode" \ + "$MAIN_API_HOST/NodeService/createNode" \ -d "$create_node_payload" ) create_node_code=$(echo "$create_node_resp" | jq -r '.code') @@ -234,7 +249,7 @@ EOF create_ips_resp=$(curl -s -X POST \ -H "Content-Type: application/json" \ -H "X-Edge-Access-Token: $TOKEN" \ - "$API_HOST/NodeIPAddressService/createNodeIPAddresses" \ + "$MAIN_API_HOST/NodeIPAddressService/createNodeIPAddresses" \ -d "$create_ips_payload" ) create_ips_code=$(echo "$create_ips_resp" | jq -r '.code') @@ -252,7 +267,7 @@ echo "[INFO] 获取节点信息..." find_node_resp=$(curl -s -X POST \ -H "Content-Type: application/json" \ -H "X-Edge-Access-Token: $TOKEN" \ - "$API_HOST/NodeService/findEnabledNode" \ + "$MAIN_API_HOST/NodeService/findEnabledNode" \ -d '{ "nodeId": '"$NODE_ID"' }' @@ -283,7 +298,7 @@ EOF install_node_resp=$(curl -s -X POST \ -H "Content-Type: application/json" \ -H "X-Edge-Access-Token: $TOKEN" \ - "$API_HOST/NodeService/installNode" \ + "$MAIN_API_HOST/NodeService/installNode" \ -d "$install_node_payload" ) install_node_code=$(echo "$install_node_resp" | jq -r '.code') @@ -316,6 +331,19 @@ else sudo unzip -o edge-node.zip + # 将多个 API_HOSTS 拼成 JSON 数组(如果需要) + # 例如:["https://1.2.3.4:8001","https://edge2.xxx.com:8001"] + # 这里只把它们转成空格->"",""简单写入 + # 当然也可保留只写API_HOSTS[0] + RPC_ENDPOINTS_JSON='[' + for ((i=0; i<${#API_HOSTS[@]}; i++)); do + RPC_ENDPOINTS_JSON+="\"${API_HOSTS[i]}\"" + if [[ $i -lt $(( ${#API_HOSTS[@]} - 1 )) ]]; then + RPC_ENDPOINTS_JSON+="," + fi + done + RPC_ENDPOINTS_JSON+=']' + if [[ ! -f /usr/local/goedge/edge-node/configs/api_node.yaml ]]; then sudo tee /usr/local/goedge/edge-node/configs/api_node.yaml >/dev/null <