更新 goedge_add_node.sh
This commit is contained in:
parent
c2d3b8d2dc
commit
6bce58c151
@ -1,6 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
echo "[INFO] Powered By Randallanjie.com"
|
||||
#
|
||||
# 当未指定 --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
|
||||
|
||||
@ -8,34 +14,30 @@ set -e
|
||||
# 函数:检查并安装依赖
|
||||
###############################################################################
|
||||
function install_deps_if_needed() {
|
||||
local packages=("curl" "jq" "base64")
|
||||
local packages=("curl" "jq" "base64" "unzip")
|
||||
|
||||
# 判断命令是否存在,若不存在,则需要安装
|
||||
for pkg in "${packages[@]}"; do
|
||||
if ! command -v "$pkg" &> /dev/null; then
|
||||
echo "[INFO] 检测到命令 '$pkg' 不存在,需要安装..."
|
||||
|
||||
# 尝试判断系统使用 apt-get 或 yum 安装
|
||||
if command -v apt-get &> /dev/null; then
|
||||
echo "[INFO] 检测到系统支持 apt-get,开始安装: $pkg"
|
||||
echo "[INFO] 检测到系统使用 apt-get,开始安装 $pkg"
|
||||
sudo apt-get update -y
|
||||
# base64 通常在 coreutils 中,jq 单独包
|
||||
sudo apt-get install -y curl jq coreutils
|
||||
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
|
||||
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
|
||||
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
|
||||
echo "[INFO] 检测到系统使用 zypper,开始安装 $pkg"
|
||||
sudo zypper install -y curl jq coreutils unzip
|
||||
else
|
||||
echo "[ERROR] 不支持的包管理器,无法自动安装 $pkg,请手动安装。"
|
||||
echo "[ERROR] 未识别的包管理器,无法自动安装 $pkg,请手动安装。"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 再次检测命令是否成功安装
|
||||
if ! command -v "$pkg" &> /dev/null; then
|
||||
echo "[ERROR] 安装 $pkg 失败,请手动安装后再试。"
|
||||
exit 1
|
||||
@ -45,13 +47,12 @@ function install_deps_if_needed() {
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# 开始脚本逻辑
|
||||
# 主体脚本
|
||||
###############################################################################
|
||||
# 1. 检查依赖
|
||||
install_deps_if_needed
|
||||
install_deps_if_needed # 1. 安装依赖
|
||||
|
||||
###############################################################################
|
||||
# 解析命令行参数
|
||||
# 2. 解析参数
|
||||
###############################################################################
|
||||
ACID=""
|
||||
ACKEY=""
|
||||
@ -61,7 +62,8 @@ GROUP_ID=0
|
||||
REGION_ID=0
|
||||
DNS_ROUTES=""
|
||||
API_HOST=""
|
||||
GRANT_ID=1 # 默认值
|
||||
GRANT_ID=1 # 默认
|
||||
WEB_INSTALL=0 # 是否使用 --webinstall
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
@ -101,6 +103,10 @@ while [[ $# -gt 0 ]]; do
|
||||
API_HOST="$2"
|
||||
shift 2
|
||||
;;
|
||||
--webinstall)
|
||||
WEB_INSTALL=1
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
echo "[ERROR] 未知参数: $1"
|
||||
exit 1
|
||||
@ -108,36 +114,33 @@ while [[ $# -gt 0 ]]; do
|
||||
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>]"
|
||||
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
|
||||
|
||||
###############################################################################
|
||||
# 先获取本机 IPv4,用于创建节点时的 nodeLogin 和 “自动名称”逻辑
|
||||
# 3. 获取本机 IPv4, 拼装默认名称
|
||||
###############################################################################
|
||||
echo "[INFO] 获取本机 IPv4 地址..."
|
||||
echo "[INFO] 获取本机 IPv4..."
|
||||
LOCAL_IPV4=$(curl -s 4.ipw.cn || true)
|
||||
if [[ -z "$LOCAL_IPV4" ]]; then
|
||||
echo "[WARN] 获取IPv4失败,使用0.0.0.0"
|
||||
LOCAL_IPV4="0.0.0.0"
|
||||
echo "[WARN] 无法获取IPv4,使用默认: $LOCAL_IPV4"
|
||||
fi
|
||||
echo "[INFO] 本机 IPv4: $LOCAL_IPV4"
|
||||
|
||||
# 如果没指定 --name,就自动生成一个 "<本机IPv4>(YYYY-MM-DD-HH-mm)" 格式名称
|
||||
if [[ -z "$NAME" ]]; then
|
||||
DATE_STR=$(date +%Y-%m-%d-%H-%M)
|
||||
DATE_STR=$(date +%Y-%m-%d)
|
||||
NAME="${LOCAL_IPV4}(${DATE_STR})"
|
||||
echo "[INFO] 未指定 --name,自动设置 NAME=$NAME"
|
||||
fi
|
||||
|
||||
|
||||
###############################################################################
|
||||
# 2. 获取 Token
|
||||
# 4. 获取 Token
|
||||
###############################################################################
|
||||
echo "[INFO] 正在获取 Token..."
|
||||
|
||||
echo "[INFO] 获取 Token..."
|
||||
get_token_resp=$(curl -s -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
"$API_HOST/APIAccessTokenService/getAPIAccessToken" \
|
||||
@ -148,18 +151,17 @@ get_token_resp=$(curl -s -X POST \
|
||||
}')
|
||||
get_token_code=$(echo "$get_token_resp" | jq -r '.code')
|
||||
if [ "$get_token_code" != "200" ]; then
|
||||
echo "[ERROR] 获取Token失败,响应: $get_token_resp"
|
||||
echo "[ERROR] 获取Token失败: $get_token_resp"
|
||||
exit 1
|
||||
fi
|
||||
TOKEN=$(echo "$get_token_resp" | jq -r '.data.token')
|
||||
echo "[INFO] 成功获取 Token"
|
||||
|
||||
###############################################################################
|
||||
# 3. 创建节点 (NodeService/createNode)
|
||||
# 5. 创建节点 (createNode)
|
||||
###############################################################################
|
||||
echo "[INFO] 正在创建节点..."
|
||||
|
||||
# 处理 DNS_ROUTES => JSON 数组
|
||||
if [[ -n "$DNS_ROUTES" ]]; then
|
||||
dns_routes_json_array=$(echo "$DNS_ROUTES" | sed 's/ /","/g')
|
||||
dns_routes_json_array="[\"$dns_routes_json_array\"]"
|
||||
@ -167,7 +169,6 @@ else
|
||||
dns_routes_json_array="[]"
|
||||
fi
|
||||
|
||||
# nodeLogin.params 的原始 JSON
|
||||
node_login_json=$(cat <<EOF
|
||||
{
|
||||
"host": "$LOCAL_IPV4",
|
||||
@ -176,8 +177,6 @@ node_login_json=$(cat <<EOF
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# 用 base64 编码
|
||||
BASE64_PARAMS=$(echo -n "$node_login_json" | base64 | tr -d '\n')
|
||||
|
||||
create_node_payload=$(cat <<EOF
|
||||
@ -205,24 +204,22 @@ create_node_resp=$(curl -s -X POST \
|
||||
)
|
||||
create_node_code=$(echo "$create_node_resp" | jq -r '.code')
|
||||
if [ "$create_node_code" != "200" ]; then
|
||||
echo "[ERROR] 创建节点失败,响应: $create_node_resp"
|
||||
echo "[ERROR] 创建节点失败: $create_node_resp"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
NODE_ID=$(echo "$create_node_resp" | jq -r '.data.nodeId')
|
||||
echo "[INFO] 节点创建成功,nodeId = $NODE_ID"
|
||||
echo "[INFO] 节点创建成功, nodeId=$NODE_ID"
|
||||
|
||||
###############################################################################
|
||||
# 4. 批量创建 IP (NodeIPAddressService/createNodeIPAddresses)
|
||||
# 6. 批量创建 IP (createNodeIPAddresses)
|
||||
###############################################################################
|
||||
echo "[INFO] 正在获取本机 IPv6 地址..."
|
||||
echo "[INFO] 添加节点IP地址..."
|
||||
LOCAL_IPV6=$(curl -s 6.ipw.cn || true)
|
||||
if [[ -z "$LOCAL_IPV6" ]]; then
|
||||
echo "[WARN] 本机 IPv6 获取失败或不存在"
|
||||
echo "[WARN] 获取 IPv6 失败"
|
||||
fi
|
||||
echo "[INFO] 本机 IPv6: $LOCAL_IPV6"
|
||||
echo "[INFO] IPv6: $LOCAL_IPV6"
|
||||
|
||||
echo "[INFO] 批量创建 IP 地址 (createNodeIPAddresses) ..."
|
||||
ip_array='["'"$LOCAL_IPV4"'"]'
|
||||
if [[ -n "$LOCAL_IPV6" ]]; then
|
||||
ip_array='["'"$LOCAL_IPV4"'","'"$LOCAL_IPV6"'"]'
|
||||
@ -250,17 +247,16 @@ create_ips_resp=$(curl -s -X POST \
|
||||
)
|
||||
create_ips_code=$(echo "$create_ips_resp" | jq -r '.code')
|
||||
if [ "$create_ips_code" != "200" ]; then
|
||||
echo "[ERROR] 创建 IP 地址失败,响应: $create_ips_resp"
|
||||
echo "[ERROR] 创建 IP 失败: $create_ips_resp"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ADDR_IDS=$(echo "$create_ips_resp" | jq -r '.data.nodeIPAddressIds')
|
||||
echo "[INFO] 已为节点 $NODE_ID 批量创建 IP 地址, nodeIPAddressIds = $ADDR_IDS"
|
||||
echo "[INFO] 批量添加 IP 完成"
|
||||
|
||||
###############################################################################
|
||||
# 5. 获取节点信息 (findEnabledNode)
|
||||
# 7. 获取节点信息 (findEnabledNode)
|
||||
###############################################################################
|
||||
echo "[INFO] 查找节点信息 (findEnabledNode)..."
|
||||
echo "[INFO] 获取节点信息..."
|
||||
find_node_resp=$(curl -s -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "X-Edge-Access-Token: $TOKEN" \
|
||||
@ -269,26 +265,31 @@ find_node_resp=$(curl -s -X POST \
|
||||
"nodeId": '"$NODE_ID"'
|
||||
}'
|
||||
)
|
||||
|
||||
find_node_code=$(echo "$find_node_resp" | jq -r '.code')
|
||||
if [ "$find_node_code" != "200" ]; then
|
||||
echo "[ERROR] 获取节点信息失败, 响应: $find_node_resp"
|
||||
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')
|
||||
|
||||
###############################################################################
|
||||
# 6. 安装节点 (NodeService/installNode)
|
||||
# 8. 根据 --webinstall 判断自动/手动安装
|
||||
###############################################################################
|
||||
echo "[INFO] 正在安装节点..."
|
||||
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" \
|
||||
@ -297,10 +298,67 @@ install_node_resp=$(curl -s -X POST \
|
||||
)
|
||||
install_node_code=$(echo "$install_node_resp" | jq -r '.code')
|
||||
if [ "$install_node_code" != "200" ]; then
|
||||
echo "[ERROR] 安装节点失败, 响应: $install_node_resp"
|
||||
echo "[ERROR] installNode 失败: $install_node_resp"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "[INFO] 已请求节点安装,请等待节点安装完成后上线。"
|
||||
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
|
Loading…
Reference in New Issue
Block a user