go-edge-shell/goedge_add_node.sh

392 lines
11 KiB
Bash
Raw Permalink 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
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
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
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=""
# HTTP_API: 必填用于脚本调用HTTP接口
HTTP_API=""
# RPC endpoints: 用于写入 api_node.yaml可多地址
API_HOSTS=()
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
;;
--http-api)
HTTP_API="$2"
shift 2
;;
--api-host)
# 将后续所有非开头带 "--" 的参数视为RPC地址(可多个)
shift
while [[ $# -gt 0 && ! $1 =~ ^-- ]]; do
API_HOSTS+=("$1")
shift
done
;;
--webinstall)
WEB_INSTALL=1
shift
;;
*)
echo "[ERROR] 未知参数: $1"
exit 1
;;
esac
done
# 校验必填: acid, ackey, clusterid, http-api
if [[ -z "$ACID" || -z "$ACKEY" || -z "$CLUSTER_ID" ]]; then
echo "[ERROR] 用法: $0 --acid <ACCESS_KEY_ID> --ackey <ACCESS_KEY> --clusterid <CLUSTER_ID> --http-api <HTTP_API> [--grantid <GRANT_ID>] [--name <NAME>] [--groupid <GROUP_ID>] [--regionid <REGION_ID>] [--dnsroutes <DNS_ROUTES>] [--api-host <RPC1> [RPC2]...] [--webinstall]"
exit 1
fi
if [[ -z "$HTTP_API" ]]; then
echo "[ERROR] 必须指定 --http-api <HTTP_API>"
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 (使用 HTTP_API)
###############################################################################
echo "[INFO] 获取 Token..."
get_token_resp=$(curl -s -X POST \
-H "Content-Type: application/json" \
"$HTTP_API/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" \
"$HTTP_API/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" \
"$HTTP_API/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" \
"$HTTP_API/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')
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" \
"$HTTP_API/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
# -------------------- 手动安装模式 -------------------- #
echo "[INFO] 未指定 --webinstall进入手动安装流程..."
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
RPC_ENDPOINTS_JSON='[]'
if [[ ${#API_HOSTS[@]} -gt 0 ]]; then
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+=']'
fi
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: [ "" ]
nodeId: ""
secret: ""
EOT
fi
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
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" \
"$HTTP_API/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