Add basic support for agent

This commit is contained in:
cuigh
2021-12-17 20:13:58 +08:00
parent 94127504ff
commit cb2cb4ab86
23 changed files with 402 additions and 197 deletions

View File

@@ -33,6 +33,7 @@ func NewContainer(b biz.ContainerBiz) *ContainerHandler {
func containerSearch(b biz.ContainerBiz) web.HandlerFunc {
type Args struct {
Node string `json:"node" bind:"node"`
Name string `json:"name" bind:"name"`
Status string `json:"status" bind:"status"`
PageIndex int `json:"pageIndex" bind:"pageIndex"`
@@ -47,7 +48,7 @@ func containerSearch(b biz.ContainerBiz) web.HandlerFunc {
)
if err = ctx.Bind(args); err == nil {
containers, total, err = b.Search(args.Name, args.Status, args.PageIndex, args.PageSize)
containers, total, err = b.Search(args.Node, args.Name, args.Status, args.PageIndex, args.PageSize)
}
if err != nil {
@@ -63,8 +64,9 @@ func containerSearch(b biz.ContainerBiz) web.HandlerFunc {
func containerFind(b biz.ContainerBiz) web.HandlerFunc {
return func(ctx web.Context) error {
node := ctx.Query("node")
id := ctx.Query("id")
container, raw, err := b.Find(id)
container, raw, err := b.Find(node, id)
if err != nil {
return err
}
@@ -74,12 +76,13 @@ func containerFind(b biz.ContainerBiz) web.HandlerFunc {
func containerDelete(b biz.ContainerBiz) web.HandlerFunc {
type Args struct {
ID string `json:"id"`
Node string `json:"node"`
ID string `json:"id"`
}
return func(ctx web.Context) (err error) {
args := &Args{}
if err = ctx.Bind(args); err == nil {
err = b.Delete(args.ID, ctx.User())
err = b.Delete(args.Node, args.ID, ctx.User())
}
return ajax(ctx, err)
}
@@ -87,6 +90,7 @@ func containerDelete(b biz.ContainerBiz) web.HandlerFunc {
func containerFetchLogs(b biz.ContainerBiz) web.HandlerFunc {
type Args struct {
Node string `json:"node" bind:"node"`
ID string `json:"id" bind:"id"`
Lines int `json:"lines" bind:"lines"`
Timestamps bool `json:"timestamps" bind:"timestamps"`
@@ -98,7 +102,7 @@ func containerFetchLogs(b biz.ContainerBiz) web.HandlerFunc {
stdout, stderr string
)
if err = ctx.Bind(args); err == nil {
stdout, stderr, err = b.FetchLogs(args.ID, args.Lines, args.Timestamps)
stdout, stderr, err = b.FetchLogs(args.Node, args.ID, args.Lines, args.Timestamps)
}
if err != nil {
return err
@@ -110,11 +114,12 @@ func containerFetchLogs(b biz.ContainerBiz) web.HandlerFunc {
func containerConnect(b biz.ContainerBiz) web.HandlerFunc {
return func(ctx web.Context) error {
var (
id = ctx.Query("id")
cmd = ctx.Query("cmd")
node = ctx.Query("node")
id = ctx.Query("id")
cmd = ctx.Query("cmd")
)
_, _, err := b.Find(id)
_, _, err := b.Find(node, id)
if err != nil {
return err
}
@@ -124,17 +129,17 @@ func containerConnect(b biz.ContainerBiz) web.HandlerFunc {
return err
}
idResp, err := b.ExecCreate(id, cmd)
idResp, err := b.ExecCreate(node, id, cmd)
if err != nil {
return err
}
resp, err := b.ExecAttach(idResp.ID)
resp, err := b.ExecAttach(node, idResp.ID)
if err != nil {
return err
}
err = b.ExecStart(idResp.ID)
err = b.ExecStart(node, idResp.ID)
if err != nil {
return err
}

View File

@@ -8,6 +8,7 @@ import (
// NodeHandler encapsulates node related handlers.
type NodeHandler struct {
List web.HandlerFunc `path:"/list" auth:"node.view" desc:"list nodes"`
Search web.HandlerFunc `path:"/search" auth:"node.view" desc:"search nodes"`
Find web.HandlerFunc `path:"/find" auth:"node.view" desc:"find node by name"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"node.delete" desc:"delete node"`
@@ -17,6 +18,7 @@ type NodeHandler struct {
// NewNode creates an instance of NodeHandler
func NewNode(nb biz.NodeBiz) *NodeHandler {
return &NodeHandler{
List: nodeList(nb),
Search: nodeSearch(nb),
Find: nodeFind(nb),
Delete: nodeDelete(nb),
@@ -24,6 +26,17 @@ func NewNode(nb biz.NodeBiz) *NodeHandler {
}
}
func nodeList(nb biz.NodeBiz) web.HandlerFunc {
return func(ctx web.Context) error {
nodes, err := nb.List()
if err != nil {
return err
}
return success(ctx, nodes)
}
}
func nodeSearch(nb biz.NodeBiz) web.HandlerFunc {
return func(ctx web.Context) error {
nodes, err := nb.Search()

View File

@@ -14,8 +14,6 @@ import (
"github.com/cuigh/swirl/model"
)
//var ErrSystemInitialized = errors.New("system was already initialized")
// SystemHandler encapsulates system related handlers.
type SystemHandler struct {
CheckState web.HandlerFunc `path:"/check-state" auth:"*" desc:"check system state"`