refactor: unify kubernetes_ table prefix, merge CI/CD models, LLM session support

- Rename container_clusters → kubernetes_clusters, container_topology_layouts → kubernetes_topology_layouts
- LLM: add session_id to chat history for multi-turn conversation support
- CI/CD: add pipeline_id, deploy_type, artifact_url, k8s_cluster_id to AppRelease
- Cleanup: remove deprecated playbook handler, simplify automation models
- Database: optimize auto-migrate model list
- Frontend: AIOps assistant improvements, api.ts updates
This commit is contained in:
attacker committed 2026-06-19 15:01:54 +00:00
1 parent 9365c1e9db
commit 732974d152
14 files changed
+121 -474

No files matched your search

@@ -304,7 +304,10 @@ func (h *AutomationHandler) ExecuteCronJob(c *gin.Context) {
// Resolve the content to execute. For type=script, load the linked Script.
content := job.Content
scriptType := "shell"
scriptType := job.Type // shell | python | command | script
if scriptType == "command" {
scriptType = "shell" // one-liner commands run via bash
}
if job.Type == "script" && job.ScriptID != nil {
var script models.Script
if err := h.db.First(&script, *job.ScriptID).Error; err == nil {
@@ -1,358 +0,0 @@
package handlers
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"time"
"ezops/internal/models"
"github.com/gin-gonic/gin"
)
// runPlaybookLocal executes an Ansible playbook YAML locally on the AIOps host.
// It writes the content to a temporary file and invokes `ansible-playbook`
// against localhost with a local connection. If the ansible-playbook binary is
// missing the exec error is captured in the output. Returns combined
// stdout+stderr, the exit code (-1 on failure) and duration in milliseconds.
func runPlaybookLocal(content, inventory string, timeoutSec int) (output string, exitCode int, durationMs int) {
start := time.Now()
timeout := time.Duration(timeoutSec) * time.Second
if timeout <= 0 {
timeout = 600 * time.Second
}
// Write the playbook YAML to a temporary file.
tmpFile, err := os.CreateTemp("", "ezops-playbook-*.yml")
if err != nil {
return fmt.Sprintf("创建临时文件失败: %v", err), -1, 0
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.WriteString(content); err != nil {
tmpFile.Close()
return fmt.Sprintf("写入临时文件失败: %v", err), -1, 0
}
tmpFile.Close()
// Write an optional inventory file.
inventoryArg := "localhost,"
if inventory != "" {
invFile, err := os.CreateTemp("", "ezops-inventory-*.ini")
if err == nil {
if _, werr := invFile.WriteString(inventory); werr == nil {
inventoryArg = invFile.Name()
defer os.Remove(invFile.Name())
}
invFile.Close()
}
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
args := []string{"-i", inventoryArg}
if inventoryArg == "localhost," {
args = append(args, "--connection=local")
}
args = append(args, tmpFile.Name())
cmd := exec.CommandContext(ctx, "ansible-playbook", args...)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
runErr := cmd.Run()
durationMs = int(time.Since(start).Milliseconds())
output = out.String()
if ctx.Err() == context.DeadlineExceeded {
output += fmt.Sprintf("\n[超时] 执行超过 %d 秒被终止", timeoutSec)
return output, -1, durationMs
}
if runErr != nil {
if exitError, ok := runErr.(*exec.ExitError); ok {
exitCode = exitError.ExitCode()
} else {
// Binary not found or other exec error (e.g. ansible-playbook not
// installed). Provide a clear hint instead of an empty output.
exitCode = -1
if output == "" {
output = fmt.Sprintf("无法执行 ansible-playbook:%v\n(请确认 AIOps 主机已安装 ansible:yum install -y ansible / pip install ansible)", runErr)
}
}
return output, exitCode, durationMs
}
return output, 0, durationMs
}
// ListPlaybooks 获取Playbook列表
// @Summary 获取Playbook列表
// @Description 获取所有Ansible Playbook列表
// @Tags 自动化运维
// @Produce json
// @Security Bearer
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Success 200 {object} models.Response{data=models.ListResponse}
// @Router /playbooks [get]
func (h *AutomationHandler) ListPlaybooks(c *gin.Context) {
var query models.PageQuery
c.ShouldBindQuery(&query)
var total int64
h.db.Model(&models.Playbook{}).Count(&total)
var playbooks []models.Playbook
h.db.Order("created_at DESC").Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&playbooks)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "success",
Data: models.ListResponse{
List: playbooks,
Total: total,
},
})
}
// GetPlaybook 获取Playbook详情
// @Summary 获取Playbook详情
// @Description 获取指定Playbook的详细信息
// @Tags 自动化运维
// @Produce json
// @Security Bearer
// @Param id path int true "Playbook ID"
// @Success 200 {object} models.Response{data=models.Playbook}
// @Router /playbooks/{id} [get]
func (h *AutomationHandler) GetPlaybook(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var playbook models.Playbook
if err := h.db.First(&playbook, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.Response{
Code: 404,
Message: "Playbook不存在",
})
return
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "success",
Data: playbook,
})
}
// CreatePlaybook 创建Playbook
// @Summary 创建Playbook
// @Description 创建新的Ansible Playbook
// @Tags 自动化运维
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body models.CreatePlaybookRequest true "创建请求"
// @Success 200 {object} models.Response{data=models.Playbook}
// @Router /playbooks [post]
func (h *AutomationHandler) CreatePlaybook(c *gin.Context) {
var req models.CreatePlaybookRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
playbook := models.Playbook{
Name: req.Name,
Code: randomCode("pb"),
Description: req.Description,
Content: req.Content,
Category: req.Category,
Tags: req.Tags,
Variables: req.Variables,
Inventory: req.Inventory,
Timeout: req.Timeout,
}
if playbook.Category == "" {
playbook.Category = "deployment"
}
if playbook.Timeout == 0 {
playbook.Timeout = 600
}
if err := h.db.Create(&playbook).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "创建失败: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "创建成功",
Data: playbook,
})
}
// UpdatePlaybook 更新Playbook
// @Summary 更新Playbook
// @Description 更新指定Playbook
// @Tags 自动化运维
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "Playbook ID"
// @Param request body models.CreatePlaybookRequest true "更新请求"
// @Success 200 {object} models.Response{data=models.Playbook}
// @Router /playbooks/{id} [put]
func (h *AutomationHandler) UpdatePlaybook(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var req models.CreatePlaybookRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
var playbook models.Playbook
if err := h.db.First(&playbook, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.Response{
Code: 404,
Message: "Playbook不存在",
})
return
}
updates := map[string]interface{}{
"name": req.Name,
"description": req.Description,
"content": req.Content,
"category": req.Category,
"tags": req.Tags,
"variables": req.Variables,
"inventory": req.Inventory,
"timeout": req.Timeout,
}
if err := h.db.Model(&playbook).Updates(updates).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "更新失败",
})
return
}
h.db.First(&playbook, id)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "更新成功",
Data: playbook,
})
}
// DeletePlaybook 删除Playbook
// @Summary 删除Playbook
// @Description 删除指定Playbook
// @Tags 自动化运维
// @Produce json
// @Security Bearer
// @Param id path int true "Playbook ID"
// @Success 200 {object} models.Response
// @Router /playbooks/{id} [delete]
func (h *AutomationHandler) DeletePlaybook(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
if err := h.db.Delete(&models.Playbook{}, id).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "删除失败",
})
return
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "删除成功",
})
}
// ExecutePlaybook 执行Playbook
// @Summary 执行Playbook
// @Description 在AIOps主机上本地执行Ansible Playbook
// @Tags 自动化运维
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "Playbook ID"
// @Success 200 {object} models.Response{data=models.TaskExecution}
// @Router /playbooks/{id}/execute [post]
func (h *AutomationHandler) ExecutePlaybook(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var playbook models.Playbook
if err := h.db.First(&playbook, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.Response{
Code: 404,
Message: "Playbook不存在",
})
return
}
now := time.Now()
execution := models.TaskExecution{
TaskType: "playbook",
TaskID: uint(id),
TaskName: playbook.Name,
Status: "running",
StartedAt: &now,
}
if err := h.db.Create(&execution).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "创建执行记录失败",
})
return
}
// Execute the playbook locally on the AIOps host.
output, exitCode, duration := runPlaybookLocal(playbook.Content, playbook.Inventory, playbook.Timeout)
finishedAt := time.Now()
status := "success"
if exitCode != 0 {
status = "failed"
}
h.db.Model(&execution).Updates(map[string]interface{}{
"status": status,
"output": output,
"duration": duration,
"finished_at": finishedAt,
})
h.db.Model(&playbook).UpdateColumn("run_count", playbook.RunCount+1)
execution.Output = output
execution.Status = status
execution.Duration = duration
execution.FinishedAt = &finishedAt
msg := "执行成功"
if status == "failed" {
msg = "执行失败"
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: msg,
Data: execution,
})
}
+14 -4
View File
@@ -90,9 +90,14 @@ func (h *LLMHandler) Chat(c *gin.Context) {
// 取最后一条用户消息(前端已附加当前提问)
lastUser := req.Messages[len(req.Messages)-1]
now := time.Now()
// session_id:前端传入或后端生成(兼容旧版客户端) // Logan.Li
sid := req.SessionID
if sid == "" {
sid = fmt.Sprintf("sess-%d", now.UnixMilli())
}
h.db.Create(&[]models.ChatHistory{
{UserID: uid, Role: "user", Content: lastUser.Content, CreatedAt: now},
{UserID: uid, Role: "assistant", Content: replyContent, Model: cfg.Model, CreatedAt: now.Add(time.Millisecond)},
{UserID: uid, SessionID: sid, Role: "user", Content: lastUser.Content, CreatedAt: now},
{UserID: uid, SessionID: sid, Role: "assistant", Content: replyContent, Model: cfg.Model, CreatedAt: now.Add(time.Millisecond)},
})
}
@@ -251,9 +256,14 @@ func (h *LLMHandler) ChatStream(c *gin.Context) {
if uid != 0 {
lastUser := req.Messages[len(req.Messages)-1]
now := time.Now()
// session_id:前端传入或后端生成(兼容旧版客户端) // Logan.Li
sid := req.SessionID
if sid == "" {
sid = fmt.Sprintf("sess-%d", now.UnixMilli())
}
h.db.Create(&[]models.ChatHistory{
{UserID: uid, Role: "user", Content: lastUser.Content, CreatedAt: now},
{UserID: uid, Role: "assistant", Content: fullContent, Model: cfg.Model, CreatedAt: now.Add(time.Millisecond)},
{UserID: uid, SessionID: sid, Role: "user", Content: lastUser.Content, CreatedAt: now},
{UserID: uid, SessionID: sid, Role: "assistant", Content: fullContent, Model: cfg.Model, CreatedAt: now.Add(time.Millisecond)},
})
}
// 发送结束标志
+15 -7
View File
@@ -9,16 +9,24 @@ import (
"github.com/gin-gonic/gin"
)
// GetChatHistory 获取当前用户的对话历史(按时间正序,仅返回最近 100 条避免过大) // Logan.Li
// GET /api/v1/aiops/chat-history
// GetChatHistory 获取当前用户的对话历史(按 session_id 过滤,默认返回最新 session) // Logan.Li
// GET /api/v1/aiops/chat-history?session_id=xxx
func (h *LLMHandler) GetChatHistory(c *gin.Context) {
uid := middleware.GetUserID(c)
sid := c.Query("session_id")
var rows []models.ChatHistory
// 子查询取最近 100 条后再正序返回,保证前端渲染顺序正确 // Logan.Li
h.db.Where("user_id = ?", uid).
Order("id DESC").
Limit(100).
Find(&rows)
q := h.db.Where("user_id = ?", uid)
// 指定 session_id 直接过滤;未指定则查最新 session // Logan.Li
if sid != "" {
q = q.Where("session_id = ?", sid)
} else {
// 取该用户最新的 session_id(id 最大那条的 session_id)
var latest models.ChatHistory
if err := h.db.Where("user_id = ?", uid).Order("id DESC").First(&latest).Error; err == nil && latest.SessionID != "" {
q = q.Where("session_id = ?", latest.SessionID)
}
}
q.Order("id DESC").Limit(100).Find(&rows)
// 反转为时间正序(最早在前)
for i, j := 0, len(rows)-1; i < j; i, j = i+1, j-1 {
rows[i], rows[j] = rows[j], rows[i]
@@ -1,6 +1,7 @@
package handlers
import (
"encoding/json"
"net/http"
"strconv"
@@ -65,14 +66,28 @@ func (h *PipelineHandler) CreateRelease(c *gin.Context) {
}
release := models.AppRelease{
AppName: req.AppName,
Version: req.Version,
Environment: req.Environment,
Status: "pending",
CommitID: req.CommitID,
CommitMsg: req.CommitMsg,
Servers: "[]",
Description: req.Description,
PipelineID: req.PipelineID,
PipelineRunID: req.PipelineRunID,
AppName: req.AppName,
Version: req.Version,
Environment: req.Environment,
DeployType: req.DeployType,
Status: "pending",
CommitID: req.CommitID,
CommitMsg: req.CommitMsg,
ImageTag: req.ImageTag,
K8sClusterID: req.K8sClusterID,
K8sNamespace: req.K8sNamespace,
Servers: "[]",
Description: req.Description,
}
if req.DeployType == "" {
release.DeployType = "server"
}
if len(req.Servers) > 0 {
data, _ := json.Marshal(req.Servers)
release.Servers = string(data)
}
if err := h.db.Create(&release).Error; err != nil {
+3 -13
View File
@@ -419,19 +419,6 @@ func Register(r *gin.Engine, db *gorm.DB, log *zap.Logger, cfg *config.Config, c
scripts.POST("/:id/execute", automationHandler.ExecuteScript)
}
playbooks := authorized.Group("/playbooks")
{
playbooks.GET("", automationHandler.ListPlaybooks)
playbooks.POST("", automationHandler.CreatePlaybook)
playbooks.GET("/:id", automationHandler.GetPlaybook)
playbooks.PUT("/:id", automationHandler.UpdatePlaybook)
playbooks.DELETE("/:id", automationHandler.DeletePlaybook)
playbooks.POST("/:id/execute", automationHandler.ExecutePlaybook)
}
// 任务执行记录(脚本/定时任务执行历史)
authorized.GET("/task-executions", automationHandler.ListExecutions)
// Ansible 自动化路由 // Logan.Li
ansibleHandler := handlers.NewAnsibleHandler(db)
ansible := authorized.Group("/ansible")
@@ -457,6 +444,9 @@ func Register(r *gin.Engine, db *gorm.DB, log *zap.Logger, cfg *config.Config, c
ansible.GET("/match-hosts", ansibleHandler.MatchHosts)
}
// 任务执行记录(脚本/定时任务执行历史,不含 Ansible)
authorized.GET("/task-executions", automationHandler.ListExecutions)
// 日志监控路由 // Logan.Li
logHandler := handlers.NewLogHandler(db)
logs := authorized.Group("/logs")