- 支持 Windows Agent 构建、安装脚本和统一上报协议 - 增强 RDP 剪贴板、虚拟盘和文件传输能力 - 新增 AIOps 上下文工具、告警 RCA trace 落库和回放接口 - 重构 docs 模块文档并补充终端、堡垒机、监控、AI Agent 设计
308 lines
7.5 KiB
Go
308 lines
7.5 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"ezops/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ListCronJobs 获取定时任务列表
|
|
// @Summary 获取定时任务列表
|
|
// @Description 获取所有定时任务列表
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param page query int false "页码"
|
|
// @Param page_size query int false "每页数量"
|
|
// @Success 200 {object} model.Response{data=model.ListResponse}
|
|
// @Router /cron-jobs [get]
|
|
func (h *AutomationHandler) ListCronJobs(c *gin.Context) {
|
|
var query model.PageQuery
|
|
c.ShouldBindQuery(&query)
|
|
|
|
var total int64
|
|
h.db.Model(&model.CronJob{}).Count(&total)
|
|
|
|
var jobs []model.CronJob
|
|
h.db.Preload("Script").Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&jobs)
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: model.ListResponse{
|
|
List: jobs,
|
|
Total: total,
|
|
},
|
|
})
|
|
}
|
|
|
|
// GetCronJob 获取定时任务详情
|
|
// @Summary 获取定时任务详情
|
|
// @Description 获取指定定时任务的详细信息
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} model.Response{data=model.CronJob}
|
|
// @Router /cron-jobs/{id} [get]
|
|
func (h *AutomationHandler) GetCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var job model.CronJob
|
|
if err := h.db.Preload("Script").First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{
|
|
Code: 404,
|
|
Message: "定时任务不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// CreateCronJob 创建定时任务
|
|
// @Summary 创建定时任务
|
|
// @Description 创建新定时任务
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body model.CreateCronJobRequest true "创建请求"
|
|
// @Success 200 {object} model.Response{data=model.CronJob}
|
|
// @Router /cron-jobs [post]
|
|
func (h *AutomationHandler) CreateCronJob(c *gin.Context) {
|
|
var req model.CreateCronJobRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
job := model.CronJob{
|
|
Name: req.Name,
|
|
Description: req.Description,
|
|
CronExpr: req.CronExpr,
|
|
Type: req.Type,
|
|
Content: req.Content,
|
|
ScriptID: req.ScriptID,
|
|
TargetType: req.TargetType,
|
|
Timeout: req.Timeout,
|
|
NotifyOn: req.NotifyOn,
|
|
Enabled: true,
|
|
}
|
|
|
|
if job.Timeout == 0 {
|
|
job.Timeout = 300
|
|
}
|
|
if job.NotifyOn == "" {
|
|
job.NotifyOn = "failure"
|
|
}
|
|
|
|
if err := h.db.Create(&job).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "创建失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "创建成功",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// UpdateCronJob 更新定时任务
|
|
// @Summary 更新定时任务
|
|
// @Description 更新指定定时任务
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Param request body model.CreateCronJobRequest true "更新请求"
|
|
// @Success 200 {object} model.Response{data=model.CronJob}
|
|
// @Router /cron-jobs/{id} [put]
|
|
func (h *AutomationHandler) UpdateCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var req model.CreateCronJobRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var job model.CronJob
|
|
if err := h.db.First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{
|
|
Code: 404,
|
|
Message: "定时任务不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
updates := map[string]interface{}{
|
|
"name": req.Name,
|
|
"description": req.Description,
|
|
"cron_expr": req.CronExpr,
|
|
"type": req.Type,
|
|
"content": req.Content,
|
|
"script_id": req.ScriptID,
|
|
"target_type": req.TargetType,
|
|
"timeout": req.Timeout,
|
|
"notify_on": req.NotifyOn,
|
|
}
|
|
|
|
if err := h.db.Model(&job).Updates(updates).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "更新失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
h.db.Preload("Script").First(&job, id)
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "更新成功",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// DeleteCronJob 删除定时任务
|
|
// @Summary 删除定时任务
|
|
// @Description 删除指定定时任务
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} model.Response
|
|
// @Router /cron-jobs/{id} [delete]
|
|
func (h *AutomationHandler) DeleteCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
if err := h.db.Delete(&model.CronJob{}, id).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "删除失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "删除成功",
|
|
})
|
|
}
|
|
|
|
// BatchDeleteCronJobs 批量删除定时任务
|
|
// @Summary 批量删除定时任务
|
|
// @Description 批量删除定时任务
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body model.IDsRequest true "删除请求"
|
|
// @Success 200 {object} model.Response
|
|
// @Router /cron-jobs/batch-delete [delete]
|
|
func (h *AutomationHandler) BatchDeleteCronJobs(c *gin.Context) {
|
|
var req model.IDsRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误",
|
|
})
|
|
return
|
|
}
|
|
|
|
if err := h.db.Delete(&model.CronJob{}, req.IDs).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "删除失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "删除成功",
|
|
})
|
|
}
|
|
|
|
// ToggleCronJob 切换定时任务状态
|
|
// @Summary 切换定时任务状态
|
|
// @Description 启用或禁用定时任务
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} model.Response{data=model.CronJob}
|
|
// @Router /cron-jobs/{id}/toggle [post]
|
|
func (h *AutomationHandler) ToggleCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var job model.CronJob
|
|
if err := h.db.First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{
|
|
Code: 404,
|
|
Message: "定时任务不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
job.Enabled = !job.Enabled
|
|
if err := h.db.Save(&job).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "操作失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "操作成功",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// ExecuteCronJob 执行定时任务
|
|
// @Summary 执行定时任务
|
|
// @Description 手动执行定时任务
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} model.Response{data=model.TaskExecution}
|
|
// @Router /cron-jobs/{id}/execute [post]
|
|
func (h *AutomationHandler) ExecuteCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
execution, err := QueueCronJobByID(h.db, uint(id))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{
|
|
Code: 404,
|
|
Message: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "已提交后台执行",
|
|
Data: execution,
|
|
})
|
|
}
|