Compare commits
No commits in common. "main" and "edge-dns" have entirely different histories.
71
README.md
71
README.md
@ -1,72 +1,3 @@
|
||||
# go-edge-shell
|
||||
|
||||
本脚本用于在 GoEdgeCDN 上自动完成以下操作:
|
||||
|
||||
1. 获取 Token(调用 /APIAccessTokenService/getAPIAccessToken)
|
||||
2. 创建节点(调用 /NodeService/createNode),其中 nodeLogin.params 等 bytes 类型字段会自动做 Base64 编码
|
||||
3. 批量添加 IP(调用 /NodeIPAddressService/createNodeIPAddresses)
|
||||
4. 获取节点信息(调用 /NodeService/findEnabledNode)
|
||||
5. 安装节点(调用 /NodeService/installNode)
|
||||
6. 新增安装节点时默认采用从本仓库下载edge-node,如果失败请在最后加上 `--webinstall` 采用webapi进行从你的edgecdn管理站拉取安装脚本
|
||||
|
||||
如果调用脚本时没有指定节点名称 --name,则会自动使用 “<本机IPv4>(YYYY-MM-DD)” 作为名称。
|
||||
另外,支持通过 --grantid 参数来自定义 grantId,若不指定则默认为 1。
|
||||
|
||||
## 使用方法
|
||||
|
||||
```bash
|
||||
curl -o goedge_add_node.sh https://git.randallanjie.com/Randall/go-edge-shell/raw/branch/main/goedge_add_node.sh
|
||||
chmod +x goedge_add_node.sh
|
||||
./goedge_add_node.sh \
|
||||
--acid 12345 \
|
||||
--ackey 1234567890 \
|
||||
--clusterid 2 \
|
||||
--name "测试节点" \
|
||||
--grantid 5 \
|
||||
--http-api https://api.randallanjie.com \
|
||||
--api-host https://rpc1.randallanjie.com https://rpc2.randallanjie.com
|
||||
```
|
||||
- --http-api:必填,脚本本身通过此地址与后端进行 HTTP 调用
|
||||
- --api-host:可多填,用于节点自身的 rpc.endpoints 配置写入 api_node.yaml。节点启动后将自行与这些 RPC 地址通信
|
||||
|
||||
若需要使用后端 WebAPI 来安装 edge-node,可在以上命令后面加上 --webinstall,即:
|
||||
```bash
|
||||
./goedge_add_node.sh ... --webinstall
|
||||
```
|
||||
这样脚本会跳过本地解压步骤,直接调用后端 /NodeService/installNode。
|
||||
|
||||
## 参数说明
|
||||
|
||||
| **参数名** | **是否必填** | **说明** | **示例** |
|
||||
|:-----------:|:--------:|:--------------------------------------------------------------------------------:|:--------------------------------------------:|
|
||||
| --acid | 是 | Access Key ID | --acid 12345 |
|
||||
| --ackey | 是 | Access Key | --ackey 1234567890 |
|
||||
| --clusterid | 是 | 节点所属的集群 ID | --clusterid 2 |
|
||||
| --http-api | 是 | 脚本本身调用HTTP接口时使用的主地址 | --http-api https://api.randallanjie.com |
|
||||
| --api-host | 否 | 节点自身 RPC endpoints 配置,可多填,用空格分隔,写入 rpc.endpoints (如 --api-host https://rpc1 ...) | --api-host https://rpc1 ... https://rpc2 ... |
|
||||
| --grantid | 否 | SSH 的 grantId,用于 nodeLogin.params;默认 1 | --grantid 5 |
|
||||
| --name | 否 | 节点名称,若不指定则自动使用「<本机IPv4>(YYYY-MM-DD)」格式 | --name MyNode |
|
||||
|
||||
|
||||
## 特性
|
||||
|
||||
- 自动安装依赖
|
||||
如 curl, jq, base64, unzip 等命令不存在,脚本会自动尝试通过 apt-get / yum / dnf / zypper / apk 安装它们(Alpine适配)。
|
||||
|
||||
- Base64 编码
|
||||
脚本会自动对 nodeLogin.params 中的 JSON 内容进行 Base64 编码,以适配 GoEdge 后端对 bytes 字段的要求。
|
||||
|
||||
- 自动命名
|
||||
当不指定 --name 时,脚本会自动以 “本机IPv4 + 当前日期” 组合作为节点名称。
|
||||
|
||||
- 多地址支持
|
||||
可通过 --api-host 传多个 RPC 地址(用空格分隔),写入节点配置文件 api_node.yaml。
|
||||
|
||||
- 一站式调用
|
||||
本脚本包含创建节点、批量添加 IP、安装节点等操作,一次执行完成
|
||||
|
||||
## 适用环境
|
||||
|
||||
- Linux 发行版:如 Ubuntu、Debian、CentOS、Rocky Linux、Alma Linux、OpenSUSE、Alpine 等
|
||||
- Shell:需要常见的 Bash 环境
|
||||
- 依赖命令:curl, jq, base64, unzip。脚本中已实现自动安装逻辑,但需具备相应的包管理器(apt-get, yum, dnf, zypper 或 apk)
|
||||
GoEdge自动化添加节点脚本
|
@ -1,392 +0,0 @@
|
||||
#!/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
|
Loading…
Reference in New Issue
Block a user