后端: - models/automation.go: Playbook 模型新增 Category/Tags 字段; 新增 CreatePlaybookRequest - handlers/automation.go: 新增 runPlaybookLocal(写临时YML+ansible-playbook --connection=local执行) + randomCode(crypto/rand生成唯一code) + List/Get/Create/Update/Delete/Execute Playbook 完整CRUD Execute 创建TaskExecution(task_type=playbook)记录,采集stdout/stderr/退出码/耗时 - routes/routes.go: 注册 /playbooks 路由组(7个端点) 前端: - services/api.ts: ansibleApi 从mock改为真实 /playbooks 后端调用 - AnsibleManager.tsx 完全重写: - Playbook管理: 真实数据卡片列表(id/name/category/tags/run_count/timeout) + 搜索/分类筛选 - 新建/编辑对话框复用(编辑模式预填数据) — 新增编辑入口(原无编辑功能) - 查看内容: 终端风格YAML查看器 + 快捷编辑按钮 - 执行: ConfirmProvider确认 + ansible-playbook本地执行 + toast结果反馈 - 执行记录tab: 真实TaskExecution记录(任务名/状态/开始时间/耗时/日志查看) - 统计卡片: 总数/执行中/今日成功/今日失败(基于真实执行记录) - 移除原inventory占位tab(无后端支持,避免非功能按钮) - tags JSON数组与前端string[]互转映射
166 lines
6.8 KiB
Go
166 lines
6.8 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CronJob 定时任务模型
|
|
type CronJob struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:128;not null" json:"name"`
|
|
Description string `gorm:"size:500" json:"description"`
|
|
CronExpr string `gorm:"size:64;not null" json:"cron_expr"`
|
|
Type string `gorm:"size:32;not null" json:"type"` // script, command, playbook
|
|
Content string `gorm:"type:text" json:"content"`
|
|
ScriptID *uint `json:"script_id"`
|
|
TargetType string `gorm:"size:32;default:server" json:"target_type"` // server, group, tag
|
|
Targets string `gorm:"type:text" json:"targets"` // JSON
|
|
Timeout int `gorm:"default:300" json:"timeout"`
|
|
Enabled bool `gorm:"default:true" json:"enabled"`
|
|
LastRunAt *time.Time `json:"last_run_at"`
|
|
LastStatus string `gorm:"size:32" json:"last_status"`
|
|
RunCount int `gorm:"default:0" json:"run_count"`
|
|
SuccessCount int `gorm:"default:0" json:"success_count"`
|
|
FailCount int `gorm:"default:0" json:"fail_count"`
|
|
NotifyOn string `gorm:"size:32;default:failure" json:"notify_on"` // always, failure, success
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
|
|
// 关联
|
|
Script *Script `json:"script,omitempty"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (CronJob) TableName() string {
|
|
return "cron_jobs"
|
|
}
|
|
|
|
// Script 脚本库模型
|
|
type Script struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:128;not null" json:"name"`
|
|
Code string `gorm:"size:64;uniqueIndex;not null" json:"code"`
|
|
Description string `gorm:"size:500" json:"description"`
|
|
Type string `gorm:"size:32;default:shell" json:"type"` // shell, python, powershell, yaml
|
|
Content string `gorm:"type:text;not null" json:"content"`
|
|
Params string `gorm:"type:text" json:"params"` // JSON
|
|
Timeout int `gorm:"default:300" json:"timeout"`
|
|
Version int `gorm:"default:1" json:"version"`
|
|
RunCount int `gorm:"default:0" json:"run_count"`
|
|
CreatedBy uint `json:"created_by"`
|
|
UpdatedBy uint `json:"updated_by"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (Script) TableName() string {
|
|
return "scripts"
|
|
}
|
|
|
|
// TaskExecution 任务执行记录
|
|
type TaskExecution struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
TaskType string `gorm:"size:32;not null" json:"task_type"` // cron, script, playbook
|
|
TaskID uint `json:"task_id"`
|
|
TaskName string `gorm:"size:128" json:"task_name"`
|
|
Status string `gorm:"size:32;default:pending" json:"status"` // pending, running, success, failed, cancelled
|
|
TargetType string `gorm:"size:32" json:"target_type"`
|
|
Targets string `gorm:"type:text" json:"targets"`
|
|
Output string `gorm:"type:longtext" json:"output"`
|
|
Error string `gorm:"type:text" json:"error"`
|
|
Duration int `json:"duration"`
|
|
StartedAt *time.Time `json:"started_at"`
|
|
FinishedAt *time.Time `json:"finished_at"`
|
|
CreatedBy uint `json:"created_by"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (TaskExecution) TableName() string {
|
|
return "task_executions"
|
|
}
|
|
|
|
// Playbook Ansible Playbook模型
|
|
type Playbook struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:128;not null" json:"name"`
|
|
Code string `gorm:"size:64;uniqueIndex;not null" json:"code"`
|
|
Description string `gorm:"size:500" json:"description"`
|
|
Content string `gorm:"type:text;not null" json:"content"`
|
|
Category string `gorm:"size:64" json:"category"`
|
|
Tags string `gorm:"type:text" json:"tags"` // JSON 数组字符串,如 ["deploy","docker"]
|
|
Variables string `gorm:"type:text" json:"variables"`
|
|
Inventory string `gorm:"type:text" json:"inventory"`
|
|
Timeout int `gorm:"default:600" json:"timeout"`
|
|
RunCount int `gorm:"default:0" json:"run_count"`
|
|
CreatedBy uint `json:"created_by"`
|
|
UpdatedBy uint `json:"updated_by"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (Playbook) TableName() string {
|
|
return "playbooks"
|
|
}
|
|
|
|
// CreatePlaybookRequest 创建Playbook请求
|
|
type CreatePlaybookRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
Content string `json:"content" binding:"required"`
|
|
Category string `json:"category"`
|
|
Tags string `json:"tags"` // JSON 数组字符串
|
|
Variables string `json:"variables"`
|
|
Inventory string `json:"inventory"`
|
|
Timeout int `json:"timeout"`
|
|
}
|
|
|
|
// CreateCronJobRequest 创建定时任务请求
|
|
type CreateCronJobRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
CronExpr string `json:"cron_expr" binding:"required"`
|
|
Type string `json:"type" binding:"required,oneof=script command playbook"`
|
|
Content string `json:"content"`
|
|
ScriptID *uint `json:"script_id"`
|
|
TargetType string `json:"target_type" binding:"omitempty,oneof=server group tag"`
|
|
Targets []uint `json:"targets"`
|
|
Timeout int `json:"timeout"`
|
|
NotifyOn string `json:"notify_on" binding:"omitempty,oneof=always failure success"`
|
|
}
|
|
|
|
// CreateScriptRequest 创建脚本请求
|
|
type CreateScriptRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Code string `json:"code" binding:"required"`
|
|
Description string `json:"description"`
|
|
Type string `json:"type" binding:"omitempty,oneof=shell python powershell yaml"`
|
|
Content string `json:"content" binding:"required"`
|
|
Params string `json:"params"`
|
|
Timeout int `json:"timeout"`
|
|
}
|
|
|
|
// ExecuteScriptRequest 执行脚本请求
|
|
type ExecuteScriptRequest struct {
|
|
TargetType string `json:"target_type" binding:"required,oneof=server group tag"`
|
|
Targets []uint `json:"targets" binding:"required"`
|
|
Params map[string]string `json:"params"`
|
|
}
|
|
|
|
// AutomationStats 自动化统计
|
|
type AutomationStats struct {
|
|
CronJobCount int64 `json:"cron_job_count"`
|
|
ScriptCount int64 `json:"script_count"`
|
|
PlaybookCount int64 `json:"playbook_count"`
|
|
TodayExecutions int64 `json:"today_executions"`
|
|
SuccessRate float64 `json:"success_rate"`
|
|
}
|