后端双协议: 新增 anthropic.go(Complete/Stream/工具转换) + client.go 工厂按 Protocol 路由; llm.Client 加 Stream; llm_chat 三方法收敛复用 llm.Client; resolveProtocol 按 provider 路由; Agent CLI 注入协议。编码套餐(GLM-5.2/Kimi/MiMo/Anthropic)走 Anthropic /v1/messages。 SSH凭证修复: applyCredential 正确解析凭证 JSON 的 private_key(旧实现把整个 JSON 当私钥 → ssh.ParsePrivateKey 失败 → ssh-key 登录不通); AssignCredential/批量导入/单台 CRUD 三处统一; 单台补齐 credential_id 字段(原被忽略)。 容量预测: 新增 AI 深度解读(buildCapacityInsightPrompt + PredictiveCapacityTab 按钮, 复用 aiApi.chat)。 其他: DeepSeek 模型名更新为 deepseek-v4-flash/pro; 智能助手去掉重复模型配置按钮; LLMConfig 精简生成参数(Claude Code 式)。 Co-Authored-By: Claude <noreply@anthropic.com>
161 lines
4.4 KiB
Go
161 lines
4.4 KiB
Go
package controller
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"ezops/model"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// 批量操作域:批量导入 + 批量删除 // Logan.Li
|
||
|
||
// BatchCreateServers 批量导入服务器 // Logan.Li
|
||
// 接收 {servers:[...], region_id, auth_type, credential_id},逐条校验并创建,跳过重复(主机名或IP已存在)
|
||
func (h *ServerHandler) BatchCreateServers(c *gin.Context) {
|
||
var req model.BatchCreateServerRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, model.Response{
|
||
Code: 400,
|
||
Message: "请求参数错误: " + err.Error(),
|
||
})
|
||
return
|
||
}
|
||
|
||
authType := strings.TrimSpace(req.AuthType)
|
||
if authType == "" {
|
||
authType = "password"
|
||
}
|
||
|
||
// 如果指定了凭据,提前解密(plain 在循环内由 applyCredential 解析填充) // Logan.Li
|
||
var (
|
||
hasCred bool
|
||
cred model.Credential
|
||
credPlain string
|
||
)
|
||
if req.CredentialID != nil && *req.CredentialID > 0 {
|
||
if err := h.db.First(&cred, *req.CredentialID).Error; err != nil {
|
||
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "凭据不存在"})
|
||
return
|
||
}
|
||
p, err := h.crypt.Decrypt(cred.Content)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "凭据解密失败"})
|
||
return
|
||
}
|
||
credPlain = p
|
||
hasCred = true
|
||
}
|
||
|
||
result := model.BatchCreateResult{Errors: []string{}}
|
||
for i, item := range req.Servers {
|
||
hostname := strings.TrimSpace(item.Hostname)
|
||
ip := strings.TrimSpace(item.IP)
|
||
if hostname == "" || ip == "" {
|
||
result.Failed++
|
||
result.Errors = append(result.Errors, fmt.Sprintf("第%d行: 主机名和IP不能为空", i+2))
|
||
continue
|
||
}
|
||
|
||
// 重复检查(主机名或 IP 已存在则跳过)
|
||
var count int64
|
||
h.db.Model(&model.Server{}).Where("hostname = ? OR ip = ?", hostname, ip).Count(&count)
|
||
if count > 0 {
|
||
result.Skipped++
|
||
continue
|
||
}
|
||
|
||
port := item.Port
|
||
if port == 0 {
|
||
port = 22
|
||
}
|
||
username := strings.TrimSpace(item.Username)
|
||
if username == "" {
|
||
username = "root"
|
||
}
|
||
|
||
server := model.Server{
|
||
Hostname: hostname,
|
||
IP: ip,
|
||
Port: port,
|
||
Username: username,
|
||
AuthType: authType, // 无凭证时用请求 authType(默认 password)
|
||
OS: item.OS,
|
||
CPU: item.CPU,
|
||
Memory: item.Memory * 1024, // GB → MB
|
||
Disk: item.Disk,
|
||
Tags: item.Tags,
|
||
RegionID: req.RegionID,
|
||
Status: "stopped",
|
||
}
|
||
if hasCred {
|
||
applyCredential(cred, credPlain, &server) // 有凭证则覆盖认证字段(ssh→私钥/ssh_user/ssh_port) // Logan.Li
|
||
}
|
||
if err := h.db.Create(&server).Error; err != nil {
|
||
result.Failed++
|
||
result.Errors = append(result.Errors, fmt.Sprintf("%s(%s): %s", hostname, ip, err.Error()))
|
||
continue
|
||
}
|
||
result.Success++
|
||
}
|
||
|
||
msg := fmt.Sprintf("导入完成: 成功 %d, 跳过 %d, 失败 %d", result.Success, result.Skipped, result.Failed)
|
||
c.JSON(http.StatusOK, model.Response{
|
||
Code: 200,
|
||
Message: msg,
|
||
Data: result,
|
||
})
|
||
}
|
||
|
||
// BatchDeleteServers 批量删除服务器 // Logan.Li
|
||
// @Summary 批量删除服务器
|
||
// @Description 批量删除服务器
|
||
// @Tags 服务器
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Security Bearer
|
||
// @Param request body model.IDsRequest true "删除请求"
|
||
// @Success 200 {object} model.Response
|
||
// @Router /servers/batch-delete [post]
|
||
func (h *ServerHandler) BatchDeleteServers(c *gin.Context) {
|
||
var req model.IDsRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, model.Response{
|
||
Code: 400,
|
||
Message: "请求参数错误",
|
||
})
|
||
return
|
||
}
|
||
|
||
// 查询待删除服务器用于审计日志 // Logan.Li
|
||
var servers []model.Server
|
||
h.db.Where("id IN ?", req.IDs).Find(&servers)
|
||
|
||
// 硬删除:先清理每台服务器的 Agent 上报和聚合数据
|
||
for _, s := range servers {
|
||
h.db.Unscoped().Where("hostname = ?", s.Hostname).Delete(&model.AgentReport{})
|
||
h.db.Unscoped().Where("hostname = ?", s.Hostname).Delete(&model.MetricsAggregate{})
|
||
}
|
||
|
||
if err := h.db.Unscoped().Delete(&model.Server{}, req.IDs).Error; err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Response{
|
||
Code: 500,
|
||
Message: "删除失败",
|
||
})
|
||
return
|
||
}
|
||
|
||
// 记录批量删除审计日志
|
||
for _, s := range servers {
|
||
h.recordServerAudit(c, "delete", s.Hostname+" ("+s.IP+")", strconv.Itoa(int(s.ID)), 1, "")
|
||
}
|
||
|
||
c.JSON(http.StatusOK, model.Response{
|
||
Code: 200,
|
||
Message: "服务器已彻底删除",
|
||
})
|
||
}
|