diff --git a/src/modules/job/http/router.go b/src/modules/job/http/router.go index ad069e68..fdadc6f7 100644 --- a/src/modules/job/http/router.go +++ b/src/modules/job/http/router.go @@ -36,5 +36,8 @@ func Config(r *gin.Engine) { userLogin.GET("/task/:id", taskView) userLogin.PUT("/task/:id/action", taskActionPut) userLogin.PUT("/task/:id/host", taskHostPut) + + // 专门针对工单系统开发的接口 + userLogin.POST("/run/:id", taskRunForTT) } } diff --git a/src/modules/job/http/router_funcs.go b/src/modules/job/http/router_funcs.go index 70a3312d..db72e396 100644 --- a/src/modules/job/http/router_funcs.go +++ b/src/modules/job/http/router_funcs.go @@ -256,10 +256,5 @@ func cleanHosts(formHosts []string) []string { arr = append(arr, item) } - cnt = len(arr) - if cnt == 0 { - bomb("arg[hosts] empty") - } - return arr } diff --git a/src/modules/job/http/router_task.go b/src/modules/job/http/router_task.go index 5cdd67ef..6f15ca33 100644 --- a/src/modules/job/http/router_task.go +++ b/src/modules/job/http/router_task.go @@ -34,6 +34,9 @@ func taskPost(c *gin.Context) { var f taskForm bind(c, &f) hosts := cleanHosts(f.Hosts) + if len(hosts) == 0 { + bomb("arg[hosts] empty") + } checkTaskPerm(hosts, user, f.Account) @@ -557,3 +560,65 @@ func taskCallback(c *gin.Context) { renderMessage(c, nil) } + +// 这个数据结构是tt回调的时候使用的通用数据结构,里边既有工单基本信息,也有结构化数据,job这里只需要从中解析出结构化数据 +type ttForm struct { + +} + +// /api/job-ce/run/:id?hosts=10.3.4.5,10.4.5.6 +func taskRunForTT(c *gin.Context) { + var f ttForm + bind(c, &f) + + tpl := TaskTpl(urlParamInt64(c, "id")) + arr, err := tpl.Hosts() + dangerous(err) + + // 如果QueryString里带有hosts参数,就用QueryString里的机器列表 + // 否则就从结构化数据中解析hosts + // 如果结构化数据中也没有,那只能有模板里的,模板里也没有就报错 + hosts := queryStr(c, "hosts", "") + + if hosts != "" { + // 使用QueryString传过来的hosts + tmp := cleanHosts(strings.Split(hosts, ",")) + if len(tmp) > 0 { + arr = tmp + } + } else { + // TODO 从结构化数据中取hosts + hosts = "xyz" + hosts = strings.ReplaceAll(hosts, "\r", ",") + hosts = strings.ReplaceAll(hosts, "\n", ",") + tmp := cleanHosts(strings.Split(hosts, ",")) + if len(tmp) > 0 { + arr = tmp + } + } + + if len(arr) == 0 { + bomb("hosts empty") + } + + // 校验权限 + user := loginUser(c) + checkTaskPerm(arr, user, tpl.Account) + + task := &models.TaskMeta{ + Title: tpl.Title, + Account: tpl.Account, + Batch: tpl.Batch, + Tolerance: tpl.Tolerance, + Timeout: tpl.Timeout, + Pause: tpl.Pause, + Script: tpl.Script, + Creator: user.Username, + } + + // TODO 把结构化数据转换为脚本命令行参数 + task.Args = "" + + dangerous(task.Save(arr, "start")) + renderData(c, task.Id, nil) +} diff --git a/src/modules/job/http/router_task_tpl.go b/src/modules/job/http/router_task_tpl.go index baf93a3a..495713be 100644 --- a/src/modules/job/http/router_task_tpl.go +++ b/src/modules/job/http/router_task_tpl.go @@ -293,6 +293,9 @@ func taskTplRun(c *gin.Context) { f.Overwrite(tpl) hosts := cleanHosts(f.Hosts) + if len(hosts) == 0 { + bomb("arg[hosts] empty") + } checkTaskPerm(hosts, user, f.Account)