swirl/controller/task.go

96 lines
2.3 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package controller
import (
"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 {
2017-10-19 12:40:05 +00:00
List web.HandlerFunc `path:"/" name:"task.list" authorize:"!" desc:"task list page"`
2017-09-26 12:50:09 +00:00
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"`
2018-02-23 03:06:45 +00:00
Logs web.HandlerFunc `path:"/:id/logs" name:"task.logs" authorize:"!" desc:"task logs page"`
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{
2017-10-19 12:40:05 +00:00
List: taskList,
2017-10-11 03:26:03 +00:00
Detail: taskDetail,
Raw: taskRaw,
2018-02-23 03:06:45 +00:00
Logs: taskLogs,
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
}
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 {
return err
}
m := newModel(ctx).Set("Task", task).Set("Line", line).Set("Timestamps", timestamps).
Set("Stdout", stdout.String()).Set("Stderr", stderr.String())
return ctx.Render("task/logs", m)
}