swirl/controller/node.go

100 lines
2.4 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package controller
import (
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz/docker"
2017-10-10 07:26:07 +00:00
"github.com/cuigh/swirl/misc"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/model"
)
2017-10-11 03:26:03 +00:00
// NodeController is a controller of swarm node
2017-09-26 12:50:09 +00:00
type NodeController struct {
List web.HandlerFunc `path:"/" name:"node.list" authorize:"!" desc:"node list page"`
Detail web.HandlerFunc `path:"/:id/detail" name:"node.detail" authorize:"!" desc:"node detail page"`
Raw web.HandlerFunc `path:"/:id/raw" name:"node.raw" authorize:"!" desc:"node raw page"`
Delete web.HandlerFunc `path:"/delete" method:"post" name:"node.delete" authorize:"!" desc:"delete node"`
Edit web.HandlerFunc `path:"/:id/edit" name:"node.edit" authorize:"!" desc:"node edit page"`
Update web.HandlerFunc `path:"/:id/update" method:"post" name:"node.update" authorize:"!" desc:"update node"`
}
2017-10-11 03:26:03 +00:00
// Node creates an instance of NodeController
2017-09-26 12:50:09 +00:00
func Node() (c *NodeController) {
2017-10-11 03:26:03 +00:00
return &NodeController{
List: nodeList,
Detail: nodeDetail,
Raw: nodeRaw,
Delete: nodeDelete,
Edit: nodeEdit,
Update: nodeUpdate,
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func nodeList(ctx web.Context) error {
nodes, err := docker.NodeList()
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("Nodes", nodes)
2017-10-11 03:26:03 +00:00
return ctx.Render("node/list", m)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func nodeDelete(ctx web.Context) error {
id := ctx.F("id")
err := docker.NodeRemove(id)
return ajaxResult(ctx, err)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func nodeDetail(ctx web.Context) error {
id := ctx.P("id")
node, _, err := docker.NodeInspect(id)
if err != nil {
return err
2017-09-26 12:50:09 +00:00
}
2017-10-19 12:40:05 +00:00
tasks, _, err := docker.TaskList(&model.TaskListArgs{Node: id})
2017-10-11 03:26:03 +00:00
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("Node", node).Set("Tasks", tasks)
2017-10-11 03:26:03 +00:00
return ctx.Render("node/detail", m)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func nodeRaw(ctx web.Context) error {
id := ctx.P("id")
node, raw, err := docker.NodeInspect(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("ID", id).Set("Node", node).Set("Raw", j)
2017-10-11 03:26:03 +00:00
return ctx.Render("node/raw", m)
}
func nodeEdit(ctx web.Context) error {
id := ctx.P("id")
node, _, err := docker.NodeInspect(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("Node", node)
2017-10-11 03:26:03 +00:00
return ctx.Render("node/edit", m)
}
func nodeUpdate(ctx web.Context) error {
id := ctx.P("id")
info := &model.NodeUpdateInfo{}
err := ctx.Bind(info)
if err == nil {
err = docker.NodeUpdate(id, info)
}
return ajaxResult(ctx, err)
2017-09-26 12:50:09 +00:00
}