- core/module: 命令模板、门控(feature flag/confirm/dry-run)、ecode 错误标签、ResultFormatter - runbook executor: http.get/http.post/assert.json(expect_success 严格断言)、目标主机管理、任务落库、最终 stdout 直接回传 - cf 模块: /cf dns list 支持域名自动解析 zone_id;dns list/zones/workers list 格式化回传;API success:false 判真失败 - 全部 CF/CPA runbook 接入严格断言,杜绝假阳性 success - 新增 cpa/cfdav/mail/sub 模块与 runbooks;QQ 订阅到期提醒调度 - Web 后台: 模块设置(CF/CPA/cfdav/AI)、OPS 任务、渠道管理、审计页
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package module
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"ops-assistant/internal/core/ecode"
|
|
"ops-assistant/internal/core/policy"
|
|
"ops-assistant/internal/core/runbook"
|
|
)
|
|
|
|
type Runner struct {
|
|
db *gorm.DB
|
|
exec *runbook.Executor
|
|
}
|
|
|
|
func NewRunner(db *gorm.DB, exec *runbook.Executor) *Runner {
|
|
return &Runner{db: db, exec: exec}
|
|
}
|
|
|
|
func (r *Runner) Run(commandText string, operator int64, req Request) (uint, string, error) {
|
|
if strings.TrimSpace(req.RunbookName) == "" {
|
|
return 0, "", fmt.Errorf(ecode.Tag(ecode.ErrStepFailed, "runbook 不能为空"))
|
|
}
|
|
if req.DryRun {
|
|
if !req.Gate.AllowDryRun {
|
|
return 0, "", fmt.Errorf(ecode.Tag(ecode.ErrStepFailed, "当前命令不允许 dry-run"))
|
|
}
|
|
return 0, "dry-run", nil
|
|
}
|
|
if err := policy.CheckGate(r.db, policy.GateRequest{
|
|
NeedFlag: req.Gate.NeedFlag,
|
|
RequireConfirm: req.Gate.RequireConfirm,
|
|
ConfirmToken: req.ConfirmToken,
|
|
ExpectedToken: req.Gate.ExpectedToken,
|
|
AllowDryRun: req.Gate.AllowDryRun,
|
|
DryRun: req.DryRun,
|
|
}); err != nil {
|
|
code := ecode.ErrFeatureDisabled
|
|
if strings.Contains(err.Error(), "confirm") || strings.Contains(err.Error(), "确认") {
|
|
code = ecode.ErrConfirmRequired
|
|
}
|
|
return 0, "", fmt.Errorf(ecode.Tag(code, err.Error()))
|
|
}
|
|
return r.exec.RunWithInputsAndMeta(commandText, req.RunbookName, operator, req.Inputs, req.Meta)
|
|
}
|