- 前端: React 18 + TypeScript + Vite + Tailwind CSS + shadcn/ui - 后端: Go + Gin + GORM + JWT (golang-jwt/jwt/v5) - 功能: 服务器管理、K8s集群、监控告警、CI/CD、自动化运维、审批流程、审计日志 - 文档: 设计文档(ROADMAP/PDD/架构/模块)
554 lines
13 KiB
Go
554 lines
13 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"ezops/internal/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// AutomationHandler 自动化运维处理器
|
|
type AutomationHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAutomationHandler 创建自动化运维处理器
|
|
func NewAutomationHandler(db *gorm.DB) *AutomationHandler {
|
|
return &AutomationHandler{db: db}
|
|
}
|
|
|
|
// ListCronJobs 获取定时任务列表
|
|
// @Summary 获取定时任务列表
|
|
// @Description 获取所有定时任务列表
|
|
// @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 /cron-jobs [get]
|
|
func (h *AutomationHandler) ListCronJobs(c *gin.Context) {
|
|
var query models.PageQuery
|
|
c.ShouldBindQuery(&query)
|
|
|
|
var total int64
|
|
h.db.Model(&models.CronJob{}).Count(&total)
|
|
|
|
var jobs []models.CronJob
|
|
h.db.Preload("Script").Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&jobs)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: models.ListResponse{
|
|
List: jobs,
|
|
Total: total,
|
|
},
|
|
})
|
|
}
|
|
|
|
// GetCronJob 获取定时任务详情
|
|
// @Summary 获取定时任务详情
|
|
// @Description 获取指定定时任务的详细信息
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} models.Response{data=models.CronJob}
|
|
// @Router /cron-jobs/{id} [get]
|
|
func (h *AutomationHandler) GetCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var job models.CronJob
|
|
if err := h.db.Preload("Script").First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "定时任务不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// CreateCronJob 创建定时任务
|
|
// @Summary 创建定时任务
|
|
// @Description 创建新定时任务
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body models.CreateCronJobRequest true "创建请求"
|
|
// @Success 200 {object} models.Response{data=models.CronJob}
|
|
// @Router /cron-jobs [post]
|
|
func (h *AutomationHandler) CreateCronJob(c *gin.Context) {
|
|
var req models.CreateCronJobRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
job := models.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, models.Response{
|
|
Code: 500,
|
|
Message: "创建失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.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 models.CreateCronJobRequest true "更新请求"
|
|
// @Success 200 {object} models.Response{data=models.CronJob}
|
|
// @Router /cron-jobs/{id} [put]
|
|
func (h *AutomationHandler) UpdateCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var req models.CreateCronJobRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var job models.CronJob
|
|
if err := h.db.First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.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, models.Response{
|
|
Code: 500,
|
|
Message: "更新失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
h.db.Preload("Script").First(&job, id)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "更新成功",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// DeleteCronJob 删除定时任务
|
|
// @Summary 删除定时任务
|
|
// @Description 删除指定定时任务
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} models.Response
|
|
// @Router /cron-jobs/{id} [delete]
|
|
func (h *AutomationHandler) DeleteCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
if err := h.db.Delete(&models.CronJob{}, id).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "删除失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "删除成功",
|
|
})
|
|
}
|
|
|
|
// ToggleCronJob 切换定时任务状态
|
|
// @Summary 切换定时任务状态
|
|
// @Description 启用或禁用定时任务
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} models.Response{data=models.CronJob}
|
|
// @Router /cron-jobs/{id}/toggle [post]
|
|
func (h *AutomationHandler) ToggleCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var job models.CronJob
|
|
if err := h.db.First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "定时任务不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
job.Enabled = !job.Enabled
|
|
if err := h.db.Save(&job).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "操作失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "操作成功",
|
|
Data: job,
|
|
})
|
|
}
|
|
|
|
// ExecuteCronJob 执行定时任务
|
|
// @Summary 执行定时任务
|
|
// @Description 手动执行定时任务
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "任务ID"
|
|
// @Success 200 {object} models.Response{data=models.TaskExecution}
|
|
// @Router /cron-jobs/{id}/execute [post]
|
|
func (h *AutomationHandler) ExecuteCronJob(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var job models.CronJob
|
|
if err := h.db.First(&job, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "定时任务不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
execution := models.TaskExecution{
|
|
TaskType: "cron",
|
|
TaskID: uint(id),
|
|
TaskName: job.Name,
|
|
Status: "pending",
|
|
TargetType: job.TargetType,
|
|
Targets: job.Targets,
|
|
}
|
|
|
|
if err := h.db.Create(&execution).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "创建执行记录失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "执行中",
|
|
Data: execution,
|
|
})
|
|
}
|
|
|
|
// ListScripts 获取脚本列表
|
|
// @Summary 获取脚本列表
|
|
// @Description 获取所有脚本列表
|
|
// @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 /scripts [get]
|
|
func (h *AutomationHandler) ListScripts(c *gin.Context) {
|
|
var query models.PageQuery
|
|
c.ShouldBindQuery(&query)
|
|
|
|
var total int64
|
|
h.db.Model(&models.Script{}).Count(&total)
|
|
|
|
var scripts []models.Script
|
|
h.db.Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&scripts)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: models.ListResponse{
|
|
List: scripts,
|
|
Total: total,
|
|
},
|
|
})
|
|
}
|
|
|
|
// GetScript 获取脚本详情
|
|
// @Summary 获取脚本详情
|
|
// @Description 获取指定脚本的详细信息
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "脚本ID"
|
|
// @Success 200 {object} models.Response{data=models.Script}
|
|
// @Router /scripts/{id} [get]
|
|
func (h *AutomationHandler) GetScript(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var script models.Script
|
|
if err := h.db.First(&script, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "脚本不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: script,
|
|
})
|
|
}
|
|
|
|
// CreateScript 创建脚本
|
|
// @Summary 创建脚本
|
|
// @Description 创建新脚本
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body models.CreateScriptRequest true "创建请求"
|
|
// @Success 200 {object} models.Response{data=models.Script}
|
|
// @Router /scripts [post]
|
|
func (h *AutomationHandler) CreateScript(c *gin.Context) {
|
|
var req models.CreateScriptRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
script := models.Script{
|
|
Name: req.Name,
|
|
Code: req.Code,
|
|
Description: req.Description,
|
|
Type: req.Type,
|
|
Content: req.Content,
|
|
Params: req.Params,
|
|
Timeout: req.Timeout,
|
|
}
|
|
|
|
if script.Type == "" {
|
|
script.Type = "shell"
|
|
}
|
|
if script.Timeout == 0 {
|
|
script.Timeout = 300
|
|
}
|
|
|
|
if err := h.db.Create(&script).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "创建失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "创建成功",
|
|
Data: script,
|
|
})
|
|
}
|
|
|
|
// UpdateScript 更新脚本
|
|
// @Summary 更新脚本
|
|
// @Description 更新指定脚本
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "脚本ID"
|
|
// @Param request body models.CreateScriptRequest true "更新请求"
|
|
// @Success 200 {object} models.Response{data=models.Script}
|
|
// @Router /scripts/{id} [put]
|
|
func (h *AutomationHandler) UpdateScript(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var req models.CreateScriptRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var script models.Script
|
|
if err := h.db.First(&script, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "脚本不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
updates := map[string]interface{}{
|
|
"name": req.Name,
|
|
"description": req.Description,
|
|
"type": req.Type,
|
|
"content": req.Content,
|
|
"params": req.Params,
|
|
"timeout": req.Timeout,
|
|
"version": script.Version + 1,
|
|
}
|
|
|
|
if err := h.db.Model(&script).Updates(updates).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "更新失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
h.db.First(&script, id)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "更新成功",
|
|
Data: script,
|
|
})
|
|
}
|
|
|
|
// DeleteScript 删除脚本
|
|
// @Summary 删除脚本
|
|
// @Description 删除指定脚本
|
|
// @Tags 自动化运维
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "脚本ID"
|
|
// @Success 200 {object} models.Response
|
|
// @Router /scripts/{id} [delete]
|
|
func (h *AutomationHandler) DeleteScript(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
if err := h.db.Delete(&models.Script{}, id).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "删除失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "删除成功",
|
|
})
|
|
}
|
|
|
|
// ExecuteScript 执行脚本
|
|
// @Summary 执行脚本
|
|
// @Description 在目标服务器上执行脚本
|
|
// @Tags 自动化运维
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "脚本ID"
|
|
// @Param request body models.ExecuteScriptRequest true "执行请求"
|
|
// @Success 200 {object} models.Response{data=models.TaskExecution}
|
|
// @Router /scripts/{id}/execute [post]
|
|
func (h *AutomationHandler) ExecuteScript(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var script models.Script
|
|
if err := h.db.First(&script, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "脚本不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
var req models.ExecuteScriptRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
execution := models.TaskExecution{
|
|
TaskType: "script",
|
|
TaskID: uint(id),
|
|
TaskName: script.Name,
|
|
Status: "pending",
|
|
TargetType: req.TargetType,
|
|
}
|
|
|
|
if err := h.db.Create(&execution).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "创建执行记录失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "执行中",
|
|
Data: execution,
|
|
})
|
|
}
|