Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
343708eebe | ||
|
|
583b27fb5c |
@@ -177,6 +177,28 @@ Agent 只主动连接 Hub,不需要公网入站端口。
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## 🚀 一键安装与升级
|
||||||
|
|
||||||
|
### 一键安装
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/kyaring/vps-commander/main/deploy/install.sh | sudo bash
|
||||||
|
```
|
||||||
|
|
||||||
|
安装脚本自动识别 `amd64/arm64` 并进入 Hub / Agent 安装流程。
|
||||||
|
|
||||||
|
### 一键升级 Agent
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/kyaring/vps-commander/main/deploy/upgrade-agent.sh | sudo bash
|
||||||
|
```
|
||||||
|
|
||||||
|
升级脚本下载目标 Release、校验文件大小、备份旧版本并在启动失败时自动回滚。
|
||||||
|
|
||||||
|
生产环境的 Agent 凭据以独立设备凭据为准;不要将 Cluster Secret 作为长期生产凭据重新启用。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 📄 文档索引
|
## 📄 文档索引
|
||||||
|
|
||||||
- [文档基线与全局索引](docs/DOCUMENTATION_BASELINE_20261002.md)
|
- [文档基线与全局索引](docs/DOCUMENTATION_BASELINE_20261002.md)
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ func main() {
|
|||||||
AdminToken: adminToken,
|
AdminToken: adminToken,
|
||||||
ExecLimiter: make(chan struct{}, 32),
|
ExecLimiter: make(chan struct{}, 32),
|
||||||
SearchLimiter: make(chan struct{}, 8),
|
SearchLimiter: make(chan struct{}, 8),
|
||||||
|
LocalSessions: executor.NewSessionManager(0),
|
||||||
}
|
}
|
||||||
if err := s.LoadSecurityPolicySnapshot(); err != nil {
|
if err := s.LoadSecurityPolicySnapshot(); err != nil {
|
||||||
log.Fatalf("load security policy snapshot: %v", err)
|
log.Fatalf("load security policy snapshot: %v", err)
|
||||||
@@ -121,6 +122,13 @@ func main() {
|
|||||||
IdleTimeout: 90 * time.Second,
|
IdleTimeout: 90 * time.Second,
|
||||||
}
|
}
|
||||||
log.Printf("vps-commander-hub listening on %s as %s", *addr, local)
|
log.Printf("vps-commander-hub listening on %s as %s", *addr, local)
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(time.Minute)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for range ticker.C {
|
||||||
|
s.LocalSessions.Cleanup()
|
||||||
|
}
|
||||||
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
+61
-17
@@ -47,9 +47,53 @@ chmod 0600 /etc/vps-commander/agent.env
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 三、Hub 服务端部署步骤
|
## 三、一键安装与升级
|
||||||
|
|
||||||
### 3.1 编译
|
### 3.1 一键安装
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/kyaring/vps-commander/main/deploy/install.sh | sudo bash
|
||||||
|
```
|
||||||
|
|
||||||
|
脚本自动识别 `amd64/arm64`,并进入 Hub / Agent 安装流程。
|
||||||
|
|
||||||
|
- Hub:自动生成 API Key 与 Web 面板密码。
|
||||||
|
- Agent:输入 Hub WSS 地址、节点名称和独立 Agent Token。
|
||||||
|
- 生产环境不再要求 Cluster Secret 作为长期凭据。
|
||||||
|
|
||||||
|
### 3.2 一键升级 Agent
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/kyaring/vps-commander/main/deploy/upgrade-agent.sh | sudo bash
|
||||||
|
```
|
||||||
|
|
||||||
|
升级流程:
|
||||||
|
|
||||||
|
```text
|
||||||
|
下载 Release
|
||||||
|
↓
|
||||||
|
临时文件完整性检查
|
||||||
|
↓
|
||||||
|
备份当前 Agent
|
||||||
|
↓
|
||||||
|
原子替换
|
||||||
|
↓
|
||||||
|
systemd restart
|
||||||
|
↓
|
||||||
|
健康检查
|
||||||
|
├─ 成功 → 删除备份
|
||||||
|
└─ 失败 → 自动恢复旧版本
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.3 手工安装
|
||||||
|
|
||||||
|
如果需要固定版本、离线部署或灰度验证,再使用下面的手工安装流程。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 四、Hub 服务端部署步骤
|
||||||
|
|
||||||
|
### 4.1 编译
|
||||||
```bash
|
```bash
|
||||||
cd /root/.openclaw/workspace/vps-commander
|
cd /root/.openclaw/workspace/vps-commander
|
||||||
go test ./...
|
go test ./...
|
||||||
@@ -65,13 +109,13 @@ vcs.modified=false
|
|||||||
vcs.revision=<release commit>
|
vcs.revision=<release commit>
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3.2 安装
|
### 4.2 安装
|
||||||
```bash
|
```bash
|
||||||
install -d -m 0750 /opt/vps-commander/data /etc/vps-commander
|
install -d -m 0750 /opt/vps-commander/data /etc/vps-commander
|
||||||
install -m 0755 bin/vps-commander-hub-linux-amd64 /opt/vps-commander/vps-commander-hub
|
install -m 0755 bin/vps-commander-hub-linux-amd64 /opt/vps-commander/vps-commander-hub
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3.3 配置
|
### 4.3 配置
|
||||||
```bash
|
```bash
|
||||||
cat << 'ENV' > /etc/vps-commander/hub.env
|
cat << 'ENV' > /etc/vps-commander/hub.env
|
||||||
VPS_COMMANDER_API_KEY=your_secure_api_key
|
VPS_COMMANDER_API_KEY=your_secure_api_key
|
||||||
@@ -80,7 +124,7 @@ ENV
|
|||||||
chmod 0600 /etc/vps-commander/hub.env
|
chmod 0600 /etc/vps-commander/hub.env
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3.4 Systemd
|
### 4.4 Systemd
|
||||||
```ini
|
```ini
|
||||||
[Service]
|
[Service]
|
||||||
EnvironmentFile=-/etc/vps-commander/hub.env
|
EnvironmentFile=-/etc/vps-commander/hub.env
|
||||||
@@ -89,7 +133,7 @@ Restart=always
|
|||||||
RestartSec=3s
|
RestartSec=3s
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3.5 启动
|
### 4.5 启动
|
||||||
```bash
|
```bash
|
||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
systemctl enable --now vps-commander-hub
|
systemctl enable --now vps-commander-hub
|
||||||
@@ -98,15 +142,15 @@ systemctl status vps-commander-hub
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 四、远程 Agent 节点部署步骤
|
## 五、远程 Agent 节点部署步骤
|
||||||
|
|
||||||
### 4.1 安装二进制
|
### 5.1 安装二进制
|
||||||
```bash
|
```bash
|
||||||
install -d -m 0750 /etc/vps-commander /opt/vps-commander
|
install -d -m 0750 /etc/vps-commander /opt/vps-commander
|
||||||
install -m 0755 vps-commander-agent-linux-amd64 /opt/vps-commander/vps-commander-agent
|
install -m 0755 vps-commander-agent-linux-amd64 /opt/vps-commander/vps-commander-agent
|
||||||
```
|
```
|
||||||
|
|
||||||
### 4.2 配置
|
### 5.2 配置
|
||||||
```bash
|
```bash
|
||||||
cat << 'ENV' > /etc/vps-commander/agent.env
|
cat << 'ENV' > /etc/vps-commander/agent.env
|
||||||
VPS_COMMANDER_HUB_WS_URL=wss://vpstool.kory.kdns.fr/agent/ws
|
VPS_COMMANDER_HUB_WS_URL=wss://vpstool.kory.kdns.fr/agent/ws
|
||||||
@@ -116,7 +160,7 @@ ENV
|
|||||||
chmod 0600 /etc/vps-commander/agent.env
|
chmod 0600 /etc/vps-commander/agent.env
|
||||||
```
|
```
|
||||||
|
|
||||||
### 4.3 Systemd
|
### 5.3 Systemd
|
||||||
```ini
|
```ini
|
||||||
[Service]
|
[Service]
|
||||||
EnvironmentFile=-/etc/vps-commander/agent.env
|
EnvironmentFile=-/etc/vps-commander/agent.env
|
||||||
@@ -127,7 +171,7 @@ RestartSec=3s
|
|||||||
|
|
||||||
当前 v2.0 生产迁移阶段可兼容显式 token;v2.1 发布后必须改为不出现在 ExecStart 的方式。
|
当前 v2.0 生产迁移阶段可兼容显式 token;v2.1 发布后必须改为不出现在 ExecStart 的方式。
|
||||||
|
|
||||||
### 4.4 启动
|
### 5.4 启动
|
||||||
```bash
|
```bash
|
||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
systemctl enable --now vps-commander-agent
|
systemctl enable --now vps-commander-agent
|
||||||
@@ -138,14 +182,14 @@ Agent 启动后主动连接 Hub,并在设备列表中显示为 `online`。
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 五、控制面板与 MCP 使用
|
## 六、控制面板与 MCP 使用
|
||||||
|
|
||||||
### 5.1 管理面板
|
### 6.1 管理面板
|
||||||
`https://vpstool.kory.kdns.fr/panel/login`
|
`https://vpstool.kory.kdns.fr/panel/login`
|
||||||
|
|
||||||
面板包含设备状态、安全策略、操作控制、审计和相关管理功能。
|
面板包含设备状态、安全策略、操作控制、审计和相关管理功能。
|
||||||
|
|
||||||
### 5.2 MCP
|
### 6.2 MCP
|
||||||
远程入口:
|
远程入口:
|
||||||
`https://vpstool.kory.kdns.fr/mcp/sse`
|
`https://vpstool.kory.kdns.fr/mcp/sse`
|
||||||
|
|
||||||
@@ -155,7 +199,7 @@ Agent 启动后主动连接 Hub,并在设备列表中显示为 `online`。
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 六、生产升级流程
|
## 七、生产升级流程
|
||||||
|
|
||||||
```text
|
```text
|
||||||
Backup
|
Backup
|
||||||
@@ -177,7 +221,7 @@ Full Acceptance
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 七、日常运维检查
|
## 八、日常运维检查
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
systemctl status vps-commander-hub
|
systemctl status vps-commander-hub
|
||||||
@@ -189,7 +233,7 @@ curl -fsS http://127.0.0.1:9521/healthz
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 八、当前生产审计提示
|
## 九、当前生产审计提示
|
||||||
|
|
||||||
2026-10-02 审计发现生产 Hub / Agent 曾由 `vcs.modified=true` 的工作树构建。
|
2026-10-02 审计发现生产 Hub / Agent 曾由 `vcs.modified=true` 的工作树构建。
|
||||||
因此生产现场二进制不能直接视为当前 HEAD 的可复现产物。
|
因此生产现场二进制不能直接视为当前 HEAD 的可复现产物。
|
||||||
|
|||||||
+9
-14
@@ -72,15 +72,15 @@ install_agent() {
|
|||||||
read -rp "请输入本节点名称 (默认: ${DEFAULT_NAME}): " AGENT_NAME
|
read -rp "请输入本节点名称 (默认: ${DEFAULT_NAME}): " AGENT_NAME
|
||||||
AGENT_NAME=${AGENT_NAME:-${DEFAULT_NAME}}
|
AGENT_NAME=${AGENT_NAME:-${DEFAULT_NAME}}
|
||||||
|
|
||||||
read -rp "请输入 Cluster Secret (集群通信密钥): " CLUSTER_SECRET
|
read -rp "请输入 Agent 独立凭据 Token: " AGENT_TOKEN
|
||||||
while [ -z "$CLUSTER_SECRET" ]; do
|
while [ -z "$AGENT_TOKEN" ]; do
|
||||||
read -rp "Cluster Secret 不能为空,请重新输入: " CLUSTER_SECRET
|
read -rp "Agent Token 不能为空,请重新输入: " AGENT_TOKEN
|
||||||
done
|
done
|
||||||
|
|
||||||
cat << ENV > "${CONF_DIR}/agent.env"
|
cat << ENV > "${CONF_DIR}/agent.env"
|
||||||
VPS_COMMANDER_HUB_WS_URL=${HUB_WS}
|
VPS_COMMANDER_HUB_WS_URL=${HUB_WS}
|
||||||
VPS_COMMANDER_AGENT_NAME=${AGENT_NAME}
|
VPS_COMMANDER_AGENT_NAME=${AGENT_NAME}
|
||||||
VPS_COMMANDER_CLUSTER_SECRET=***
|
VPS_COMMANDER_AGENT_TOKEN=${AGENT_TOKEN}
|
||||||
ENV
|
ENV
|
||||||
chmod 0600 "${CONF_DIR}/agent.env"
|
chmod 0600 "${CONF_DIR}/agent.env"
|
||||||
|
|
||||||
@@ -133,23 +133,19 @@ install_hub() {
|
|||||||
read -rp "请输入监听端口 (默认 9521): " HUB_PORT
|
read -rp "请输入监听端口 (默认 9521): " HUB_PORT
|
||||||
HUB_PORT=${HUB_PORT:-9521}
|
HUB_PORT=${HUB_PORT:-9521}
|
||||||
|
|
||||||
GEN_API_KEY=*** -c 24 /dev/urandom | xxd -p)
|
GEN_API_KEY=$(head -c 24 /dev/urandom | xxd -p)
|
||||||
GEN_SECRET=*** -c 24 /dev/urandom | xxd -p)
|
|
||||||
GEN_PASS=$(head -c 16 /dev/urandom | xxd -p)
|
GEN_PASS=$(head -c 16 /dev/urandom | xxd -p)
|
||||||
|
|
||||||
read -rp "请输入 API Key (默认随机生成): " API_KEY
|
read -rp "请输入 API Key (默认随机生成): " API_KEY
|
||||||
API_KEY=***
|
API_KEY=${API_KEY:-$GEN_API_KEY}
|
||||||
|
|
||||||
read -rp "请输入 Cluster Secret (默认随机生成): " CLUSTER_SECRET
|
|
||||||
CLUSTER_SECRET=${CLUS…RET}
|
|
||||||
|
|
||||||
read -rp "请输入 Web 管理面板密码 (默认随机生成): " WEB_PASS
|
read -rp "请输入 Web 管理面板密码 (默认随机生成): " WEB_PASS
|
||||||
WEB_PASS=${WEB_PASS:-$GEN_PASS}
|
WEB_PASS=${WEB_PASS:-$GEN_PASS}
|
||||||
|
|
||||||
cat << ENV > "${CONF_DIR}/hub.env"
|
cat << ENV > "${CONF_DIR}/hub.env"
|
||||||
VPS_COMMANDER_API_KEY=***
|
VPS_COMMANDER_API_KEY=${API_KEY}
|
||||||
VPS_COMMANDER_CLUSTER_SECRET=***
|
VPS_COMMANDER_WEB_PASSWORD=${WEB_PASS}
|
||||||
VPS_COMMANDER_WEB_PASSWORD=***
|
ENV
|
||||||
ENV
|
ENV
|
||||||
chmod 0600 "${CONF_DIR}/hub.env"
|
chmod 0600 "${CONF_DIR}/hub.env"
|
||||||
|
|
||||||
@@ -185,7 +181,6 @@ SVC
|
|||||||
echo -e "\n================== Hub 配置凭据 =================="
|
echo -e "\n================== Hub 配置凭据 =================="
|
||||||
echo -e "监听地址: 127.0.0.1:${HUB_PORT}"
|
echo -e "监听地址: 127.0.0.1:${HUB_PORT}"
|
||||||
echo -e "API Key: ${API_KEY}"
|
echo -e "API Key: ${API_KEY}"
|
||||||
echo -e "Cluster Secret: ${CLUSTER_SECRET}"
|
|
||||||
echo -e "Web 面板密码: ${WEB_PASS}"
|
echo -e "Web 面板密码: ${WEB_PASS}"
|
||||||
echo -e "==================================================\n"
|
echo -e "==================================================\n"
|
||||||
warn "请妥善保管上述凭据,反代请将域名反代至 127.0.0.1:${HUB_PORT}"
|
warn "请妥善保管上述凭据,反代请将域名反代至 127.0.0.1:${HUB_PORT}"
|
||||||
|
|||||||
+25
-1
@@ -162,7 +162,31 @@ API Key 不应提交到 Git 仓库。
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 七、Agent MCP
|
## 七、一键安装与升级
|
||||||
|
|
||||||
|
### 7.1 一键安装
|
||||||
|
|
||||||
|
仓库已经提供统一安装脚本,支持 Linux `amd64` 与 `arm64`,安装时自动识别架构并从 GitHub Release 获取对应二进制。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/kyaring/vps-commander/main/deploy/install.sh | sudo bash
|
||||||
|
```
|
||||||
|
|
||||||
|
脚本会进入交互式安装流程,可选择 Hub 或 Agent。生产环境使用时,Agent 应使用独立设备凭据,不应重新启用全局 Shared Secret。
|
||||||
|
|
||||||
|
### 7.2 一键升级 Agent
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/kyaring/vps-commander/main/deploy/upgrade-agent.sh | sudo bash
|
||||||
|
```
|
||||||
|
|
||||||
|
升级采用临时文件下载 → 基础完整性检查 → 旧二进制备份 → 原子替换 → systemd 重启 → 健康检查;启动失败时自动回滚旧版本。
|
||||||
|
|
||||||
|
> Hub 的生产升级建议继续采用滚动发布:先完成 Release 与构建校验,再替换 Hub,确认节点重新上线后再继续后续节点。避免把 Hub 升级做成无保护的“一键重启”。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 八、Agent MCP
|
||||||
|
|
||||||
Hub 可以发现远程 Agent 暴露的 MCP Service。
|
Hub 可以发现远程 Agent 暴露的 MCP Service。
|
||||||
|
|
||||||
|
|||||||
+101
-28
@@ -49,6 +49,7 @@ type Server struct {
|
|||||||
AdminToken string
|
AdminToken string
|
||||||
ExecLimiter chan struct{}
|
ExecLimiter chan struct{}
|
||||||
SearchLimiter chan struct{}
|
SearchLimiter chan struct{}
|
||||||
|
LocalSessions *executor.SessionManager
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -782,9 +783,13 @@ func (s *Server) processSession(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var req struct {
|
var req struct {
|
||||||
Device, SessionID, Command, Workdir, Data string
|
Device string `json:"device"`
|
||||||
Force bool
|
SessionID string `json:"session_id"`
|
||||||
RingCapacity int `json:"ring_capacity"`
|
Command string `json:"command"`
|
||||||
|
Workdir string `json:"workdir"`
|
||||||
|
Data string `json:"data"`
|
||||||
|
Force bool `json:"force"`
|
||||||
|
RingCapacity int `json:"ring_capacity"`
|
||||||
}
|
}
|
||||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 256*1024)).Decode(&req); err != nil {
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 256*1024)).Decode(&req); err != nil {
|
||||||
http.Error(w, "bad json", 400)
|
http.Error(w, "bad json", 400)
|
||||||
@@ -796,38 +801,106 @@ func (s *Server) processSession(w http.ResponseWriter, r *http.Request) {
|
|||||||
if !s.requireSecurity(w, r, req.Device, "exec", req.SessionID) {
|
if !s.requireSecurity(w, r, req.Device, "exec", req.SessionID) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if s.Cluster == nil {
|
|
||||||
http.Error(w, "cluster unavailable", 503)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
action := r.URL.Query().Get("action")
|
action := r.URL.Query().Get("action")
|
||||||
|
owner := sessionOwner(r)
|
||||||
var result any
|
var result any
|
||||||
var err error
|
var err error
|
||||||
switch action {
|
if s.isRemote(req.Device) {
|
||||||
case "start":
|
if s.Cluster == nil || !s.Cluster.Online(req.Device) {
|
||||||
if req.SessionID == "" {
|
http.Error(w, "device offline", 409)
|
||||||
req.SessionID, err = newSessionID()
|
return
|
||||||
if err != nil {
|
}
|
||||||
http.Error(w, "session id generation failed", 500)
|
switch action {
|
||||||
return
|
case "start":
|
||||||
|
if req.SessionID == "" {
|
||||||
|
req.SessionID, err = newSessionID()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "session id generation failed", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
result, err = s.Cluster.StartProcess(ctx, req.Device, cluster.StartProcessPayload{Command: req.Command, Workdir: req.Workdir, SessionID: req.SessionID, Owner: owner, RingCapacity: req.RingCapacity})
|
||||||
|
case "read":
|
||||||
|
result, err = s.Cluster.ReadProcessOutput(ctx, req.Device, req.SessionID, owner)
|
||||||
|
case "input":
|
||||||
|
err = s.Cluster.InteractProcess(ctx, req.Device, req.SessionID, req.Data, owner)
|
||||||
|
result = map[string]any{"ok": true}
|
||||||
|
case "stop":
|
||||||
|
err = s.Cluster.ForceTerminate(ctx, req.Device, req.SessionID, owner)
|
||||||
|
result = map[string]any{"ok": true}
|
||||||
|
case "list":
|
||||||
|
result, err = s.Cluster.ListSessions(ctx, req.Device)
|
||||||
|
default:
|
||||||
|
http.Error(w, "unknown action", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if s.LocalSessions == nil {
|
||||||
|
http.Error(w, "local session manager unavailable", 503)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch action {
|
||||||
|
case "start":
|
||||||
|
if req.SessionID == "" {
|
||||||
|
req.SessionID, err = newSessionID()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "session id generation failed", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var sess *executor.Session
|
||||||
|
sess, err = s.LocalSessions.Start(ctx, req.SessionID, req.Command, req.Workdir, owner, req.RingCapacity)
|
||||||
|
if err == nil {
|
||||||
|
result = map[string]any{"session_id": sess.ID, "started_at": sess.StartedAt.Unix()}
|
||||||
|
}
|
||||||
|
case "read":
|
||||||
|
var sess *executor.Session
|
||||||
|
sess, ok := s.LocalSessions.Get(req.SessionID)
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("session not found")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if !sess.Authorized(owner) {
|
||||||
|
err = fmt.Errorf("session ownership denied")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
code, done, _ := sess.Status()
|
||||||
|
stdout, stderr := sess.Output()
|
||||||
|
result = cluster.ProcessOutputPayload{SessionID: req.SessionID, Stdout: stdout, Stderr: stderr, ExitCode: code, Running: !done}
|
||||||
|
case "input":
|
||||||
|
var sess *executor.Session
|
||||||
|
sess, ok := s.LocalSessions.Get(req.SessionID)
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("session not found")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if !sess.Authorized(owner) {
|
||||||
|
err = fmt.Errorf("session ownership denied")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = sess.Stdin([]byte(req.Data))
|
||||||
|
result = map[string]any{"ok": true}
|
||||||
|
case "stop":
|
||||||
|
var sess *executor.Session
|
||||||
|
sess, ok := s.LocalSessions.Get(req.SessionID)
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("session not found")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if !sess.Authorized(owner) {
|
||||||
|
err = fmt.Errorf("session ownership denied")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = s.LocalSessions.Stop(req.SessionID, true)
|
||||||
|
result = map[string]any{"ok": true}
|
||||||
|
case "list":
|
||||||
|
result = s.LocalSessions.List()
|
||||||
|
default:
|
||||||
|
http.Error(w, "unknown action", 400)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
result, err = s.Cluster.StartProcess(ctx, req.Device, cluster.StartProcessPayload{Command: req.Command, Workdir: req.Workdir, SessionID: req.SessionID, Owner: sessionOwner(r), RingCapacity: req.RingCapacity})
|
|
||||||
case "read":
|
|
||||||
result, err = s.Cluster.ReadProcessOutput(ctx, req.Device, req.SessionID, sessionOwner(r))
|
|
||||||
case "input":
|
|
||||||
err = s.Cluster.InteractProcess(ctx, req.Device, req.SessionID, req.Data, sessionOwner(r))
|
|
||||||
result = map[string]any{"ok": true}
|
|
||||||
case "stop":
|
|
||||||
err = s.Cluster.ForceTerminate(ctx, req.Device, req.SessionID, sessionOwner(r))
|
|
||||||
result = map[string]any{"ok": true}
|
|
||||||
case "list":
|
|
||||||
result, err = s.Cluster.ListSessions(ctx, req.Device)
|
|
||||||
default:
|
|
||||||
http.Error(w, "unknown action", 400)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 409)
|
http.Error(w, err.Error(), 409)
|
||||||
|
|||||||
@@ -286,6 +286,7 @@ func (s *Server) handleRequest(ctx context.Context, r *http.Request, req JSONRPC
|
|||||||
InputSchema: InputSchema{Type: "object", Properties: map[string]PropertyDef{"device": {Type: "string", Description: "目标节点名称"}, "path": {Type: "string", Description: "目标文件路径"}, "old_text": {Type: "string", Description: "必须唯一匹配的原文本块"}, "new_text": {Type: "string", Description: "替换后的文本块"}}, Required: []string{"path", "old_text", "new_text"}},
|
InputSchema: InputSchema{Type: "object", Properties: map[string]PropertyDef{"device": {Type: "string", Description: "目标节点名称"}, "path": {Type: "string", Description: "目标文件路径"}, "old_text": {Type: "string", Description: "必须唯一匹配的原文本块"}, "new_text": {Type: "string", Description: "替换后的文本块"}}, Required: []string{"path", "old_text", "new_text"}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Name: "write_file",
|
||||||
Description: "向指定受控节点创建或覆写文件",
|
Description: "向指定受控节点创建或覆写文件",
|
||||||
InputSchema: InputSchema{
|
InputSchema: InputSchema{
|
||||||
Type: "object",
|
Type: "object",
|
||||||
|
|||||||
@@ -82,6 +82,23 @@ func TestMCPInitializeAndListTools(t *testing.T) {
|
|||||||
if !ok || len(tools) != 11 {
|
if !ok || len(tools) != 11 {
|
||||||
t.Fatalf("expected 11 tools, got %d", len(tools))
|
t.Fatalf("expected 11 tools, got %d", len(tools))
|
||||||
}
|
}
|
||||||
|
foundWriteFile := false
|
||||||
|
for _, raw := range tools {
|
||||||
|
tool, ok := raw.(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("tool entry is not an object: %#v", raw)
|
||||||
|
}
|
||||||
|
name, _ := tool["name"].(string)
|
||||||
|
if name == "" {
|
||||||
|
t.Fatalf("tool entry has empty name: %#v", tool)
|
||||||
|
}
|
||||||
|
if name == "write_file" {
|
||||||
|
foundWriteFile = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !foundWriteFile {
|
||||||
|
t.Fatalf("write_file missing from tools/list")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMCPCallTool(t *testing.T) {
|
func TestMCPCallTool(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user