Files
aiops/backend/controller/server_batch.go
T
LoganandClaude 76ec9387f0 feat(rdp): 新增 RDP 远程桌面/SMB/账单同步能力 + 清理编译产物
主要变更:
- RDP 远程桌面:新增 backend/pkg/rdp(RDP 引擎 + credssp/display/rle/trace)、
  backend/controller/rdp*(连接/凭证/文件/会话/实时)、frontend RDP 组件与 API
- Windows SMB 文件传输:backend/pkg/winsmb
- 云账单同步:backend/pkg/billingsync + billing_sync/aliyun_billing,前端账单详情
- Agent CLI Kubernetes 工具:kubernetes_action/get、pod_execute/logs
- 服务器导入助手:server_import_helpers、ImportCredentialSettings
- 国际化与组件重构(cloud/deploy/monitor/server/system 等多模块)
- 清理编译产物二进制:移除 backend/server、backend/agent/bin/aiops-agent-*
- .gitignore:新增 backend/agent/ 与 .cache/(避免 334MB go-build 缓存误入库)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-04 01:50:36 +08:00

177 lines
4.7 KiB
Go
Raw 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.
package controller
import (
"fmt"
"net/http"
"strconv"
"strings"
"ezops/model"
"github.com/gin-gonic/gin"
)
// 批量操作域:批量导入 + 批量删除 // Logan.Li
// BatchCreateServers 批量导入服务器,Linux 默认 SSH 凭据,Windows 默认 RDP 凭据 // Logan.Li
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"
}
sshCredentialID := req.SSHCredentialID
if sshCredentialID == nil {
sshCredentialID = req.CredentialID
}
sshCred, sshPlain, hasSSHCred, err := h.loadImportCredential(sshCredentialID, "ssh")
if err != nil {
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: err.Error()})
return
}
rdpCred, rdpPlain, hasRDPCred, err := h.loadImportCredential(req.RDPCredentialID, "rdp")
if err != nil {
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: err.Error()})
return
}
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
}
osType := normalizeImportOSType(item.OSType, item.OS)
protocol := strings.TrimSpace(strings.ToLower(item.Protocol))
if protocol == "" {
if osType == "windows" {
protocol = "rdp"
} else {
protocol = "ssh"
}
}
port := item.Port
if port == 0 {
port = 22
}
rdpPort := item.RDPPort
if rdpPort == 0 {
rdpPort = 3389
}
username := strings.TrimSpace(item.Username)
if username == "" {
if protocol == "rdp" {
username = "Administrator"
} else {
username = "root"
}
}
server := model.Server{
Hostname: hostname,
IP: ip,
Port: port,
Username: username,
AuthType: authType, // 无凭证时用请求 authType(默认 password)
OSType: osType,
Protocol: protocol,
RDPPort: rdpPort,
OS: item.OS,
CPU: item.CPU,
Memory: item.Memory * 1024, // GB → MB
Disk: item.Disk,
Tags: item.Tags,
RegionID: req.RegionID,
Status: "stopped",
}
if protocol == "rdp" && hasRDPCred {
applyCredential(rdpCred, rdpPlain, &server)
} else if protocol != "rdp" && hasSSHCred {
applyCredential(sshCred, sshPlain, &server)
}
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: "服务器已彻底删除",
})
}