go-edge-shell/goedge_add_node.sh

387 lines
12 KiB
Bash
Raw Normal View History

2024-12-28 16:26:27 +00:00
#!/usr/bin/env bash
set -e
###############################################################################
# 函数:检查并安装依赖
###############################################################################
function install_deps_if_needed() {
2025-01-03 14:32:52 +00:00
local packages=("curl" "jq" "base64" "unzip")
2024-12-28 16:26:27 +00:00
for pkg in "${packages[@]}"; do
if ! command -v "$pkg" &> /dev/null; then
echo "[INFO] 检测到命令 '$pkg' 不存在,需要安装..."
if command -v apt-get &> /dev/null; then
2025-01-03 14:32:52 +00:00
echo "[INFO] 检测到系统使用 apt-get开始安装 $pkg"
2024-12-28 16:26:27 +00:00
sudo apt-get update -y
2025-01-03 14:32:52 +00:00
sudo apt-get install -y curl jq coreutils unzip
2024-12-28 16:26:27 +00:00
elif command -v yum &> /dev/null; then
2025-01-03 14:32:52 +00:00
echo "[INFO] 检测到系统使用 yum开始安装 $pkg"
sudo yum install -y curl jq coreutils unzip
2024-12-28 16:26:27 +00:00
elif command -v dnf &> /dev/null; then
2025-01-03 14:32:52 +00:00
echo "[INFO] 检测到系统使用 dnf开始安装 $pkg"
sudo dnf install -y curl jq coreutils unzip
2024-12-28 16:26:27 +00:00
elif command -v zypper &> /dev/null; then
2025-01-03 14:32:52 +00:00
echo "[INFO] 检测到系统使用 zypper开始安装 $pkg"
sudo zypper install -y curl jq coreutils unzip
2025-01-03 15:27:27 +00:00
elif command -v apk &> /dev/null; then
echo "[INFO] 检测到系统使用 apk (Alpine),开始安装 $pkg"
sudo apk add --no-cache curl jq coreutils unzip
2024-12-28 16:26:27 +00:00
else
2025-01-03 14:32:52 +00:00
echo "[ERROR] 未识别的包管理器,无法自动安装 $pkg,请手动安装。"
2024-12-28 16:26:27 +00:00
exit 1
fi
if ! command -v "$pkg" &> /dev/null; then
echo "[ERROR] 安装 $pkg 失败,请手动安装后再试。"
exit 1
fi
fi
done
}
###############################################################################
2025-01-03 14:32:52 +00:00
# 主体脚本
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
install_deps_if_needed # 1. 安装依赖
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
# 2. 解析参数
2024-12-28 16:26:27 +00:00
###############################################################################
ACID=""
ACKEY=""
NAME=""
CLUSTER_ID=""
GROUP_ID=0
REGION_ID=0
DNS_ROUTES=""
2025-01-03 15:27:27 +00:00
API_HOSTS=() # 存放多个API地址
GRANT_ID=1 # 默认
WEB_INSTALL=0 # 是否使用 --webinstall
2024-12-28 16:26:27 +00:00
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)
2025-01-03 15:27:27 +00:00
# 将后续所有非开头带 "--" 的参数视为host(可多个)
shift
while [[ $# -gt 0 && ! $1 =~ ^-- ]]; do
API_HOSTS+=("$1")
shift
done
2024-12-28 16:26:27 +00:00
;;
2025-01-03 14:32:52 +00:00
--webinstall)
WEB_INSTALL=1
shift
;;
2024-12-28 16:26:27 +00:00
*)
echo "[ERROR] 未知参数: $1"
exit 1
;;
esac
done
2025-01-03 15:27:27 +00:00
# 校验必填: acid, ackey, clusterid, api-host
2024-12-28 16:26:27 +00:00
if [[ -z "$ACID" || -z "$ACKEY" || -z "$CLUSTER_ID" ]]; then
2025-01-03 15:27:27 +00:00
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]"
2024-12-28 16:26:27 +00:00
exit 1
fi
2025-01-03 15:27:27 +00:00
if [[ ${#API_HOSTS[@]} -eq 0 ]]; then
echo "[ERROR] 必须至少指定一个 --api-host"
exit 1
fi
# 假设脚本中要选第一个 API_HOSTS[0] 作为获取 Token 的地址
MAIN_API_HOST="${API_HOSTS[0]}"
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
# 3. 获取本机 IPv4, 拼装默认名称
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
echo "[INFO] 获取本机 IPv4..."
2024-12-28 16:26:27 +00:00
LOCAL_IPV4=$(curl -s 4.ipw.cn || true)
if [[ -z "$LOCAL_IPV4" ]]; then
LOCAL_IPV4="0.0.0.0"
2025-01-03 14:32:52 +00:00
echo "[WARN] 无法获取IPv4使用默认: $LOCAL_IPV4"
2024-12-28 16:26:27 +00:00
fi
echo "[INFO] 本机 IPv4: $LOCAL_IPV4"
if [[ -z "$NAME" ]]; then
2025-01-03 14:32:52 +00:00
DATE_STR=$(date +%Y-%m-%d)
2024-12-28 16:26:27 +00:00
NAME="${LOCAL_IPV4}(${DATE_STR})"
echo "[INFO] 未指定 --name自动设置 NAME=$NAME"
fi
###############################################################################
2025-01-03 14:32:52 +00:00
# 4. 获取 Token
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
echo "[INFO] 获取 Token..."
2024-12-28 16:26:27 +00:00
get_token_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
2025-01-03 15:27:27 +00:00
"$MAIN_API_HOST/APIAccessTokenService/getAPIAccessToken" \
2024-12-28 16:26:27 +00:00
-d '{
"type": "admin",
"accessKeyId": "'"$ACID"'",
"accessKey": "'"$ACKEY"'"
}')
get_token_code=$(echo "$get_token_resp" | jq -r '.code')
if [ "$get_token_code" != "200" ]; then
2025-01-03 14:32:52 +00:00
echo "[ERROR] 获取Token失败: $get_token_resp"
2024-12-28 16:26:27 +00:00
exit 1
fi
TOKEN=$(echo "$get_token_resp" | jq -r '.data.token')
echo "[INFO] 成功获取 Token"
###############################################################################
2025-01-03 14:32:52 +00:00
# 5. 创建节点 (createNode)
2024-12-28 16:26:27 +00:00
###############################################################################
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" \
2025-01-03 15:27:27 +00:00
"$MAIN_API_HOST/NodeService/createNode" \
2024-12-28 16:26:27 +00:00
-d "$create_node_payload"
)
create_node_code=$(echo "$create_node_resp" | jq -r '.code')
if [ "$create_node_code" != "200" ]; then
2025-01-03 14:32:52 +00:00
echo "[ERROR] 创建节点失败: $create_node_resp"
2024-12-28 16:26:27 +00:00
exit 1
fi
NODE_ID=$(echo "$create_node_resp" | jq -r '.data.nodeId')
2025-01-03 14:32:52 +00:00
echo "[INFO] 节点创建成功, nodeId=$NODE_ID"
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
# 6. 批量创建 IP (createNodeIPAddresses)
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
echo "[INFO] 添加节点IP地址..."
2024-12-28 16:26:27 +00:00
LOCAL_IPV6=$(curl -s 6.ipw.cn || true)
if [[ -z "$LOCAL_IPV6" ]]; then
2025-01-03 14:32:52 +00:00
echo "[WARN] 获取 IPv6 失败"
2024-12-28 16:26:27 +00:00
fi
2025-01-03 14:32:52 +00:00
echo "[INFO] IPv6: $LOCAL_IPV6"
2024-12-28 16:26:27 +00:00
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" \
2025-01-03 15:27:27 +00:00
"$MAIN_API_HOST/NodeIPAddressService/createNodeIPAddresses" \
2024-12-28 16:26:27 +00:00
-d "$create_ips_payload"
)
create_ips_code=$(echo "$create_ips_resp" | jq -r '.code')
if [ "$create_ips_code" != "200" ]; then
2025-01-03 14:32:52 +00:00
echo "[ERROR] 创建 IP 失败: $create_ips_resp"
2024-12-28 16:26:27 +00:00
exit 1
fi
ADDR_IDS=$(echo "$create_ips_resp" | jq -r '.data.nodeIPAddressIds')
2025-01-03 14:32:52 +00:00
echo "[INFO] 批量添加 IP 完成"
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
# 7. 获取节点信息 (findEnabledNode)
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
echo "[INFO] 获取节点信息..."
2024-12-28 16:26:27 +00:00
find_node_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \
2025-01-03 15:27:27 +00:00
"$MAIN_API_HOST/NodeService/findEnabledNode" \
2024-12-28 16:26:27 +00:00
-d '{
"nodeId": '"$NODE_ID"'
}'
)
find_node_code=$(echo "$find_node_resp" | jq -r '.code')
if [ "$find_node_code" != "200" ]; then
2025-01-03 14:32:52 +00:00
echo "[ERROR] 获取节点信息失败: $find_node_resp"
2024-12-28 16:26:27 +00:00
exit 1
fi
node_info=$(echo "$find_node_resp" | jq -r '.data.node')
2025-01-03 14:32:52 +00:00
NODE_UNIQUE_ID=$(echo "$node_info" | jq -r '.uniqueId')
NODE_SECRET=$(echo "$node_info" | jq -r '.secret')
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
# 8. 根据 --webinstall 判断自动/手动安装
2024-12-28 16:26:27 +00:00
###############################################################################
2025-01-03 14:32:52 +00:00
if [[ "$WEB_INSTALL" -eq 1 ]]; then
# -------------------- 自动(网页)安装 -------------------- #
echo "[INFO] --webinstall 已指定, 开始调用 /NodeService/installNode..."
install_node_payload=$(cat <<EOF
2024-12-28 16:26:27 +00:00
{
"nodeId": $NODE_ID
}
EOF
)
2025-01-03 14:32:52 +00:00
install_node_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \
2025-01-03 15:27:27 +00:00
"$MAIN_API_HOST/NodeService/installNode" \
2025-01-03 14:32:52 +00:00
-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
2024-12-28 16:26:27 +00:00
2025-01-03 14:32:52 +00:00
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
2025-01-03 15:27:27 +00:00
# 将多个 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+=']'
2025-01-03 14:32:52 +00:00
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
2025-01-03 15:05:49 +00:00
rpc.endpoints: [ "" ]
2025-01-03 14:32:52 +00:00
nodeId: ""
secret: ""
EOT
fi
2025-01-03 15:27:27 +00:00
# 将 rpc.endpoints 改为多地址数组
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
2025-01-03 14:32:52 +00:00
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
2025-01-03 15:27:27 +00:00
2025-01-03 14:32:52 +00:00
update_payload=$(cat <<EOF
{
"nodeId": $NODE_ID,
"isInstalled": true
}
EOF
2024-12-28 16:26:27 +00:00
)
2025-01-03 14:32:52 +00:00
update_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "X-Edge-Access-Token: $TOKEN" \
2025-01-03 15:27:27 +00:00
"$MAIN_API_HOST/NodeService/updateNodeIsInstalled" \
2025-01-03 14:32:52 +00:00
-d "$update_payload"
)
update_code=$(echo "$update_resp" | jq -r '.code')
if [ "$update_code" != "200" ]; then
echo "[ERROR] updateNodeIsInstalled 失败: $update_resp"
exit 1
fi
2024-12-28 16:26:27 +00:00
fi
2025-01-03 15:27:27 +00:00
2025-01-03 14:32:52 +00:00
echo "[INFO] 脚本执行完成。"
exit 0