swirl/controller/task.go

106 lines
2.5 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package controller
import (
2018-05-07 12:58:32 +00:00
"github.com/cuigh/auxo/data"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/auxo/net/web"
2018-02-23 03:06:45 +00:00
"github.com/cuigh/auxo/util/cast"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/biz/docker"
2017-10-10 07:26:07 +00:00
"github.com/cuigh/swirl/misc"
2017-10-19 12:40:05 +00:00
"github.com/cuigh/swirl/model"
2017-09-26 12:50:09 +00:00
)
2017-10-11 03:26:03 +00:00
// TaskController is a controller of swarm task
2017-09-26 12:50:09 +00:00
type TaskController struct {
2018-05-07 12:58:32 +00:00
List web.HandlerFunc `path:"/" name:"task.list" authorize:"!" desc:"task list page"`
Detail web.HandlerFunc `path:"/:id/detail" name:"task.detail" authorize:"!" desc:"task detail page"`
Raw web.HandlerFunc `path:"/:id/raw" name:"task.raw" authorize:"!" desc:"task raw page"`
Logs web.HandlerFunc `path:"/:id/logs" name:"task.logs" authorize:"!" desc:"task logs page"`
FetchLogs web.HandlerFunc `path:"/:id/fetch_logs" name:"task.fetch_logs" authorize:"?" desc:"fetch task logs"`
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
// Task creates an instance of TaskController
2017-09-26 12:50:09 +00:00
func Task() (c *TaskController) {
2017-10-11 03:26:03 +00:00
return &TaskController{
2018-05-07 12:58:32 +00:00
List: taskList,
Detail: taskDetail,
Raw: taskRaw,
Logs: taskLogs,
FetchLogs: taskFetchLogs,
2017-10-11 03:26:03 +00:00
}
}
2017-09-26 12:50:09 +00:00
2017-10-19 12:40:05 +00:00
func taskList(ctx web.Context) error {
args := &model.TaskListArgs{}
err := ctx.Bind(args)
if err != nil {
return err
}
args.PageSize = model.PageSize
if args.PageIndex == 0 {
args.PageIndex = 1
}
tasks, totalCount, err := docker.TaskList(args)
if err != nil {
return err
}
m := newPagerModel(ctx, totalCount, args.PageSize, args.PageIndex).
2017-11-08 10:36:13 +00:00
Set("Args", args).
Set("Tasks", tasks)
2017-10-19 12:40:05 +00:00
return ctx.Render("task/list", m)
}
2017-10-11 03:26:03 +00:00
func taskDetail(ctx web.Context) error {
id := ctx.P("id")
task, _, err := docker.TaskInspect(id)
if err != nil {
return err
2017-09-26 12:50:09 +00:00
}
2017-11-08 10:36:13 +00:00
m := newModel(ctx).Set("Task", task)
2017-10-11 03:26:03 +00:00
return ctx.Render("task/detail", m)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func taskRaw(ctx web.Context) error {
id := ctx.P("id")
task, raw, err := docker.TaskInspect(id)
if err != nil {
return err
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
j, err := misc.JSONIndent(raw)
if err != nil {
return err
2017-09-26 12:50:09 +00:00
}
2017-11-08 10:36:13 +00:00
m := newModel(ctx).Set("Task", task).Set("Raw", j)
2017-10-11 03:26:03 +00:00
return ctx.Render("task/raw", m)
2017-09-26 12:50:09 +00:00
}
2018-02-23 03:06:45 +00:00
func taskLogs(ctx web.Context) error {
id := ctx.P("id")
task, _, err := docker.TaskInspect(id)
if err != nil {
return err
}
2018-05-07 12:58:32 +00:00
m := newModel(ctx).Set("Task", task)
return ctx.Render("task/logs", m)
}
func taskFetchLogs(ctx web.Context) error {
id := ctx.P("id")
2018-02-23 03:06:45 +00:00
line := cast.ToInt(ctx.Q("line"), 500)
timestamps := cast.ToBool(ctx.Q("timestamps"), false)
stdout, stderr, err := docker.TaskLogs(id, line, timestamps)
if err != nil {
2018-05-07 12:58:32 +00:00
return ajaxResult(ctx, err)
2018-02-23 03:06:45 +00:00
}
2018-05-07 12:58:32 +00:00
return ctx.JSON(data.Map{
"stdout": stdout.String(),
"stderr": stderr.String(),
})
2018-02-23 03:06:45 +00:00
}