go-edge-shell/goedge_add_node.sh

364 lines
11 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
#
# 当未指定 --name 参数时,自动使用 "<本机IPv4>(YYYY-MM-DD-HH-mm)" 作为节点名称。
#
# 使用 --webinstall 参数时,使用 /NodeService/installNode 进行“网页自动安装”;
# 否则执行手动安装流程,并在安装完成后调用 /NodeService/updateNodeIsInstalled 设置节点已安装。
#
# 注:在手动安装时, /usr/local/goedge/edge-node/configs/api_node.yaml 的 nodeId 字段
# 要使用后台返回的 uniqueId而非 numeric id。
set -e
###############################################################################
# 函数:检查并安装依赖
###############################################################################
function install_deps_if_needed() {
local packages=("curl" "jq" "base64" "unzip")
for pkg in "${packages[@]}"; do
if ! command -v "$pkg" &> /dev/null; then
echo "[INFO] 检测到命令 '$pkg' 不存在,需要安装..."
if command -v apt-get &> /dev/null; then
echo "[INFO] 检测到系统使用 apt-get开始安装 $pkg"
sudo apt-get update -y
sudo apt-get install -y curl jq coreutils unzip
elif command -v yum &> /dev/null; then
echo "[INFO] 检测到系统使用 yum开始安装 $pkg"
sudo yum install -y curl jq coreutils unzip
elif command -v dnf &> /dev/null; then
echo "[INFO] 检测到系统使用 dnf开始安装 $pkg"
sudo dnf install -y curl jq coreutils unzip
elif command -v zypper &> /dev/null; then
echo "[INFO] 检测到系统使用 zypper开始安装 $pkg"
sudo zypper install -y curl jq coreutils unzip
else
echo "[ERROR] 未识别的包管理器,无法自动安装 $pkg,请手动安装。"
exit 1
fi
if ! command -v "$pkg" &> /dev/null; then
echo "[ERROR] 安装 $pkg 失败,请手动安装后再试。"
exit 1
fi
fi
done
}
###############################################################################
# 主体脚本
###############################################################################
install_deps_if_needed # 1. 安装依赖
###############################################################################
# 2. 解析参数
###############################################################################
ACID=""
ACKEY=""
NAME=""
CLUSTER_ID=""
GROUP_ID=0
REGION_ID=0
DNS_ROUTES=""
API_HOST=""
GRANT_ID=1 # 默认
WEB_INSTALL=0 # 是否使用 --webinstall
while [[ $# -gt 0 ]]; do
case $1 in
--acid)
ACID="$2"
shift 2
;;
--ackey)
ACKEY="$2"
shift 2
;;
--name)
NAME="$2"
shift 2
;;
--clusterid)
CLUSTER_ID="$2"
shift 2
;;
--grantid)
GRANT_ID="$2"
shift 2
;;
--groupid)
GROUP_ID="$2"
shift 2
;;
--regionid)
REGION_ID="$2"
shift 2
;;
--dnsroutes)
DNS_ROUTES="$2"
shift 2
;;
--api-host)
API_HOST="$2"
shift 2
;;
--webinstall)
WEB_INSTALL=1
shift
;;
*)
echo "[ERROR] 未知参数: $1"
exit 1
;;
esac
done
# 校验必填
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]"
exit 1
fi
###############################################################################
# 3. 获取本机 IPv4, 拼装默认名称
###############################################################################
echo "[INFO] 获取本机 IPv4..."
LOCAL_IPV4=$(curl -s 4.ipw.cn || true)
if [[ -z "$LOCAL_IPV4" ]]; then
LOCAL_IPV4="0.0.0.0"
echo "[WARN] 无法获取IPv4使用默认: $LOCAL_IPV4"
fi
echo "[INFO] 本机 IPv4: $LOCAL_IPV4"
if [[ -z "$NAME" ]]; then
DATE_STR=$(date +%Y-%m-%d)
NAME="${LOCAL_IPV4}(${DATE_STR})"
echo "[INFO] 未指定 --name自动设置 NAME=$NAME"
fi
###############################################################################
# 4. 获取 Token
###############################################################################
echo "[INFO] 获取 Token..."
get_token_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
"$API_HOST/APIAccessTokenService/getAPIAccessToken" \
-d '{
"type": "admin",
"accessKeyId": "'"$ACID"'",
"accessKey": "'"$ACKEY"'"
}')
get_token_code=$(echo "$get_token_resp" | jq -r '.code')
if [ "$get_token_code" != "200" ]; then
echo "[ERROR] 获取Token失败: $get_token_resp"
exit 1
fi
TOKEN=$(echo "$get_token_resp" | jq -r '.data.token')
echo "[INFO] 成功获取 Token"
###############################################################################
# 5. 创建节点 (createNode)
###############################################################################
echo "[INFO] 正在创建节点..."
if [[ -n "$DNS_ROUTES" ]]; then
dns_routes_json_array=$(echo "$DNS_ROUTES" | sed 's/ /","/g')
dns_routes_json_array="[\"$dns_routes_json_array\"]"
else
dns_routes_json_array="[]"
fi
node_login_json=$(cat <<EOF
{
"host": "$LOCAL_IPV4",
"port": 22,
"grantId": $GRANT_ID
}
EOF
)
BASE64_PARAMS=$(echo -n "$node_login_json" | base64 | tr -d '\n')
create_node_payload=$(cat <<EOF
{
"name": "$NAME",
"nodeClusterId": $CLUSTER_ID,
"nodeLogin": {
"id": 3,
"name": "SSH",
"type": "ssh",
"params": "$BASE64_PARAMS"
},
"nodeGroupId": $GROUP_ID,
"dnsRoutes": $dns_routes_json_array,
"nodeRegionId": $REGION_ID
}
EOF
)
create_node_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeService/createNode" \
-d "$create_node_payload"
)
create_node_code=$(echo "$create_node_resp" | jq -r '.code')
if [ "$create_node_code" != "200" ]; then
echo "[ERROR] 创建节点失败: $create_node_resp"
exit 1
fi
NODE_ID=$(echo "$create_node_resp" | jq -r '.data.nodeId')
echo "[INFO] 节点创建成功, nodeId=$NODE_ID"
###############################################################################
# 6. 批量创建 IP (createNodeIPAddresses)
###############################################################################
echo "[INFO] 添加节点IP地址..."
LOCAL_IPV6=$(curl -s 6.ipw.cn || true)
if [[ -z "$LOCAL_IPV6" ]]; then
echo "[WARN] 获取 IPv6 失败"
fi
echo "[INFO] IPv6: $LOCAL_IPV6"
ip_array='["'"$LOCAL_IPV4"'"]'
if [[ -n "$LOCAL_IPV6" ]]; then
ip_array='["'"$LOCAL_IPV4"'","'"$LOCAL_IPV6"'"]'
fi
create_ips_payload=$(cat <<EOF
{
"nodeId": $NODE_ID,
"role": "node",
"name": "auto-ips",
"ipList": $ip_array,
"canAccess": true,
"isUp": true,
"groupValue": "",
"nodeClusterIds": []
}
EOF
)
create_ips_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeIPAddressService/createNodeIPAddresses" \
-d "$create_ips_payload"
)
create_ips_code=$(echo "$create_ips_resp" | jq -r '.code')
if [ "$create_ips_code" != "200" ]; then
echo "[ERROR] 创建 IP 失败: $create_ips_resp"
exit 1
fi
ADDR_IDS=$(echo "$create_ips_resp" | jq -r '.data.nodeIPAddressIds')
echo "[INFO] 批量添加 IP 完成"
###############################################################################
# 7. 获取节点信息 (findEnabledNode)
###############################################################################
echo "[INFO] 获取节点信息..."
find_node_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeService/findEnabledNode" \
-d '{
"nodeId": '"$NODE_ID"'
}'
)
find_node_code=$(echo "$find_node_resp" | jq -r '.code')
if [ "$find_node_code" != "200" ]; then
echo "[ERROR] 获取节点信息失败: $find_node_resp"
exit 1
fi
node_info=$(echo "$find_node_resp" | jq -r '.data.node')
# !!! 重点修正nodeId 在手动安装时应使用 uniqueId
NODE_UNIQUE_ID=$(echo "$node_info" | jq -r '.uniqueId')
NODE_SECRET=$(echo "$node_info" | jq -r '.secret')
###############################################################################
# 8. 根据 --webinstall 判断自动/手动安装
###############################################################################
if [[ "$WEB_INSTALL" -eq 1 ]]; then
# -------------------- 自动(网页)安装 -------------------- #
echo "[INFO] --webinstall 已指定, 开始调用 /NodeService/installNode..."
install_node_payload=$(cat <<EOF
{
"nodeId": $NODE_ID
}
EOF
)
install_node_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeService/installNode" \
-d "$install_node_payload"
)
install_node_code=$(echo "$install_node_resp" | jq -r '.code')
if [ "$install_node_code" != "200" ]; then
echo "[ERROR] installNode 失败: $install_node_resp"
exit 1
fi
else
# -------------------- 非web面板安装 -------------------- #
echo "[INFO] 未指定 --webinstall进入非web面板安装流程..."
if [[ ! -d /usr/local/goedge ]]; then
sudo mkdir -p /usr/local/goedge
fi
cd /usr/local/goedge
ARCH=$(uname -m)
if [[ "$ARCH" == "x86_64" ]]; then
echo "[INFO] x86_64 -> 下载 amd64"
sudo curl -L -o edge-node.zip "https://git.randallanjie.com/Randall/go-edge-shell/releases/download/edge-node/edge-node-linux-amd64-plus-v1.3.9.zip"
elif [[ "$ARCH" == "aarch64" ]]; then
echo "[INFO] aarch64 -> 下载 arm64"
sudo curl -L -o edge-node.zip "https://git.randallanjie.com/Randall/go-edge-shell/releases/download/edge-node/edge-node-linux-arm64-plus-v1.3.9.zip"
else
echo "[WARN] 未知架构 $ARCH -> 默认amd64"
sudo curl -L -o edge-node.zip "https://git.randallanjie.com/Randall/go-edge-shell/releases/download/edge-node/edge-node-linux-amd64-plus-v1.3.9.zip"
fi
sudo unzip -o edge-node.zip
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
rpc.endpoints: [ "http://server1.randallanjie.com:8001" ]
nodeId: ""
secret: ""
EOT
fi
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
sudo ./edge-node service
sudo systemctl daemon-reload
sudo systemctl start edge-node.service
sudo systemctl enable edge-node.service
update_payload=$(cat <<EOF
{
"nodeId": $NODE_ID,
"isInstalled": true
}
EOF
)
update_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \
"$API_HOST/NodeService/updateNodeIsInstalled" \
-d "$update_payload"
)
update_code=$(echo "$update_resp" | jq -r '.code')
if [ "$update_code" != "200" ]; then
echo "[ERROR] updateNodeIsInstalled 失败: $update_resp"
exit 1
fi
fi
echo "[INFO] 脚本执行完成。"
exit 0