更新 goedge_add_node.sh

This commit is contained in:
Randall 2025-01-03 15:27:27 +00:00
parent df8da9a4ec
commit 8ec57d1ad3

View File

@ -25,6 +25,9 @@ function install_deps_if_needed() {
elif command -v zypper &> /dev/null; then elif command -v zypper &> /dev/null; then
echo "[INFO] 检测到系统使用 zypper开始安装 $pkg" echo "[INFO] 检测到系统使用 zypper开始安装 $pkg"
sudo zypper install -y curl jq coreutils unzip 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 else
echo "[ERROR] 未识别的包管理器,无法自动安装 $pkg,请手动安装。" echo "[ERROR] 未识别的包管理器,无法自动安装 $pkg,请手动安装。"
exit 1 exit 1
@ -53,9 +56,9 @@ CLUSTER_ID=""
GROUP_ID=0 GROUP_ID=0
REGION_ID=0 REGION_ID=0
DNS_ROUTES="" DNS_ROUTES=""
API_HOST="" API_HOSTS=() # 存放多个API地址
GRANT_ID=1 # 默认 GRANT_ID=1 # 默认
WEB_INSTALL=0 # 是否使用 --webinstall WEB_INSTALL=0 # 是否使用 --webinstall
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
@ -92,8 +95,12 @@ while [[ $# -gt 0 ]]; do
shift 2 shift 2
;; ;;
--api-host) --api-host)
API_HOST="$2" # 将后续所有非开头带 "--" 的参数视为host(可多个)
shift 2 shift
while [[ $# -gt 0 && ! $1 =~ ^-- ]]; do
API_HOSTS+=("$1")
shift
done
;; ;;
--webinstall) --webinstall)
WEB_INSTALL=1 WEB_INSTALL=1
@ -106,12 +113,20 @@ while [[ $# -gt 0 ]]; do
esac esac
done done
# 校验必填 # 校验必填: acid, ackey, clusterid, api-host
if [[ -z "$ACID" || -z "$ACKEY" || -z "$CLUSTER_ID" ]]; then if [[ -z "$ACID" || -z "$ACKEY" || -z "$CLUSTER_ID" ]]; then
echo "[ERROR] 用法: $0 --acid <ACCESS_KEY_ID> --ackey <ACCESS_KEY> --clusterid <CLUSTER_ID> [--grantid <GRANT_ID>] [--name <NAME>] [--groupid <GROUP_ID>] [--regionid <REGION_ID>] [--dnsroutes <DNS_ROUTES>] [--api-host <API_HOST>] [--webinstall]" echo "[ERROR] 用法: $0 --acid <ACCESS_KEY_ID> --ackey <ACCESS_KEY> --clusterid <CLUSTER_ID> [--grantid <GRANT_ID>] [--name <NAME>] [--groupid <GROUP_ID>] [--regionid <REGION_ID>] [--dnsroutes <DNS_ROUTES>] [--api-host <HOST1> [HOST2]...] [--webinstall]"
exit 1 exit 1
fi 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, 拼装默认名称 # 3. 获取本机 IPv4, 拼装默认名称
############################################################################### ###############################################################################
@ -135,7 +150,7 @@ fi
echo "[INFO] 获取 Token..." echo "[INFO] 获取 Token..."
get_token_resp=$(curl -s -X POST \ get_token_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
"$API_HOST/APIAccessTokenService/getAPIAccessToken" \ "$MAIN_API_HOST/APIAccessTokenService/getAPIAccessToken" \
-d '{ -d '{
"type": "admin", "type": "admin",
"accessKeyId": "'"$ACID"'", "accessKeyId": "'"$ACID"'",
@ -191,7 +206,7 @@ EOF
create_node_resp=$(curl -s -X POST \ create_node_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \ -H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeService/createNode" \ "$MAIN_API_HOST/NodeService/createNode" \
-d "$create_node_payload" -d "$create_node_payload"
) )
create_node_code=$(echo "$create_node_resp" | jq -r '.code') create_node_code=$(echo "$create_node_resp" | jq -r '.code')
@ -234,7 +249,7 @@ EOF
create_ips_resp=$(curl -s -X POST \ create_ips_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \ -H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeIPAddressService/createNodeIPAddresses" \ "$MAIN_API_HOST/NodeIPAddressService/createNodeIPAddresses" \
-d "$create_ips_payload" -d "$create_ips_payload"
) )
create_ips_code=$(echo "$create_ips_resp" | jq -r '.code') create_ips_code=$(echo "$create_ips_resp" | jq -r '.code')
@ -252,7 +267,7 @@ echo "[INFO] 获取节点信息..."
find_node_resp=$(curl -s -X POST \ find_node_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \ -H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeService/findEnabledNode" \ "$MAIN_API_HOST/NodeService/findEnabledNode" \
-d '{ -d '{
"nodeId": '"$NODE_ID"' "nodeId": '"$NODE_ID"'
}' }'
@ -283,7 +298,7 @@ EOF
install_node_resp=$(curl -s -X POST \ install_node_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \ -H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeService/installNode" \ "$MAIN_API_HOST/NodeService/installNode" \
-d "$install_node_payload" -d "$install_node_payload"
) )
install_node_code=$(echo "$install_node_resp" | jq -r '.code') install_node_code=$(echo "$install_node_resp" | jq -r '.code')
@ -316,6 +331,19 @@ else
sudo unzip -o edge-node.zip 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 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 <<EOT sudo tee /usr/local/goedge/edge-node/configs/api_node.yaml >/dev/null <<EOT
rpc.endpoints: [ "" ] rpc.endpoints: [ "" ]
@ -323,14 +351,18 @@ nodeId: ""
secret: "" secret: ""
EOT EOT
fi fi
sudo sed -i "s|^rpc.endpoints:.*|rpc.endpoints: [ \"$API_HOST\" ]|g" /usr/local/goedge/edge-node/configs/api_node.yaml
sudo sed -i "s/^nodeId:.*/nodeId: \"$NODE_UNIQUE_ID\"/g" /usr/local/goedge/edge-node/configs/api_node.yaml # 将 rpc.endpoints 改为多地址数组
sudo sed -i "s/^secret:.*/secret: \"$NODE_SECRET\"/g" /usr/local/goedge/edge-node/configs/api_node.yaml sudo sed -i "s|^rpc.endpoints:.*|rpc.endpoints: $RPC_ENDPOINTS_JSON|g" /usr/local/goedge/edge-node/configs/api_node.yaml
sudo sed -i "s|^nodeId:.*|nodeId: \"$NODE_UNIQUE_ID\"|g" /usr/local/goedge/edge-node/configs/api_node.yaml
sudo sed -i "s|^secret:.*|secret: \"$NODE_SECRET\"|g" /usr/local/goedge/edge-node/configs/api_node.yaml
cd /usr/local/goedge/edge-node/bin cd /usr/local/goedge/edge-node/bin
sudo ./edge-node service sudo ./edge-node service
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl start edge-node.service sudo systemctl start edge-node.service
sudo systemctl enable edge-node.service sudo systemctl enable edge-node.service
update_payload=$(cat <<EOF update_payload=$(cat <<EOF
{ {
"nodeId": $NODE_ID, "nodeId": $NODE_ID,
@ -341,7 +373,7 @@ EOF
update_resp=$(curl -s -X POST \ update_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \ -H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeService/updateNodeIsInstalled" \ "$MAIN_API_HOST/NodeService/updateNodeIsInstalled" \
-d "$update_payload" -d "$update_payload"
) )
update_code=$(echo "$update_resp" | jq -r '.code') update_code=$(echo "$update_resp" | jq -r '.code')
@ -350,5 +382,6 @@ EOF
exit 1 exit 1
fi fi
fi fi
echo "[INFO] 脚本执行完成。" echo "[INFO] 脚本执行完成。"
exit 0 exit 0