2017-09-26 12:50:09 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2018-06-14 10:41:22 +00:00
|
|
|
"io"
|
2017-10-10 08:26:53 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-05-07 12:58:32 +00:00
|
|
|
"github.com/cuigh/auxo/data"
|
2018-06-14 10:41:22 +00:00
|
|
|
"github.com/cuigh/auxo/log"
|
2017-09-26 12:50:09 +00:00
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/auxo/util/cast"
|
|
|
|
"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"
|
2018-06-14 10:41:22 +00:00
|
|
|
"github.com/gobwas/ws"
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
2017-09-26 12:50:09 +00:00
|
|
|
)
|
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
// ContainerController is a controller of docker container
|
2017-09-26 12:50:09 +00:00
|
|
|
type ContainerController struct {
|
2018-05-07 12:58:32 +00:00
|
|
|
List web.HandlerFunc `path:"/" name:"container.list" authorize:"!" desc:"container list page"`
|
|
|
|
Detail web.HandlerFunc `path:"/:id/detail" name:"container.detail" authorize:"!" desc:"container detail page"`
|
|
|
|
Raw web.HandlerFunc `path:"/:id/raw" name:"container.raw" authorize:"!" desc:"container raw page"`
|
|
|
|
Logs web.HandlerFunc `path:"/:id/logs" name:"container.logs" authorize:"!" desc:"container logs page"`
|
|
|
|
FetchLogs web.HandlerFunc `path:"/:id/fetch_logs" name:"container.fetch_logs" authorize:"?" desc:"fetch container logs"`
|
|
|
|
Delete web.HandlerFunc `path:"/delete" method:"post" name:"container.delete" authorize:"!" desc:"delete container"`
|
2018-06-14 10:41:22 +00:00
|
|
|
Exec web.HandlerFunc `path:"/:id/exec" name:"container.exec" authorize:"!" desc:"run a command in a running container"`
|
|
|
|
Connect web.HandlerFunc `path:"/:id/connect" name:"container.connect" authorize:"!" desc:"connect to a running container"`
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 03:26:03 +00:00
|
|
|
// Container creates an instance of ContainerController
|
2017-09-26 12:50:09 +00:00
|
|
|
func Container() (c *ContainerController) {
|
2017-10-10 08:26:53 +00:00
|
|
|
return &ContainerController{
|
2018-05-07 12:58:32 +00:00
|
|
|
List: containerList,
|
|
|
|
Detail: containerDetail,
|
|
|
|
Raw: containerRaw,
|
|
|
|
Logs: containerLogs,
|
|
|
|
FetchLogs: containerFetchLogs,
|
2018-06-14 10:41:22 +00:00
|
|
|
Delete: containerDelete,
|
|
|
|
Exec: containerExec,
|
|
|
|
Connect: containerConnect,
|
2017-10-10 08:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-10 08:26:53 +00:00
|
|
|
func containerList(ctx web.Context) error {
|
|
|
|
args := &model.ContainerListArgs{}
|
|
|
|
err := ctx.Bind(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args.PageSize = model.PageSize
|
|
|
|
if args.PageIndex == 0 {
|
|
|
|
args.PageIndex = 1
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 08:26:53 +00:00
|
|
|
containers, totalCount, err := docker.ContainerList(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
|
2017-11-08 10:36:13 +00:00
|
|
|
Set("Name", args.Name).
|
|
|
|
Set("Filter", args.Filter).
|
|
|
|
Set("Containers", containers)
|
2017-10-10 08:26:53 +00:00
|
|
|
return ctx.Render("container/list", m)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-10 08:26:53 +00:00
|
|
|
func containerDetail(ctx web.Context) error {
|
|
|
|
id := ctx.P("id")
|
|
|
|
container, err := docker.ContainerInspect(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("Container", container)
|
2017-10-10 08:26:53 +00:00
|
|
|
return ctx.Render("container/detail", m)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2017-10-10 08:26:53 +00:00
|
|
|
func containerRaw(ctx web.Context) error {
|
|
|
|
id := ctx.P("id")
|
|
|
|
container, raw, err := docker.ContainerInspectRaw(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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("Container", container).Set("Raw", j)
|
2017-10-10 08:26:53 +00:00
|
|
|
return ctx.Render("container/raw", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerLogs(ctx web.Context) error {
|
|
|
|
id := ctx.P("id")
|
|
|
|
container, _, err := docker.ContainerInspectRaw(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 12:58:32 +00:00
|
|
|
m := newModel(ctx).Set("Container", container)
|
|
|
|
return ctx.Render("container/logs", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerFetchLogs(ctx web.Context) error {
|
|
|
|
id := ctx.P("id")
|
2017-10-19 06:24:38 +00:00
|
|
|
line := cast.ToInt(ctx.Q("line"), 500)
|
|
|
|
timestamps := cast.ToBool(ctx.Q("timestamps"), false)
|
2017-10-10 08:26:53 +00:00
|
|
|
stdout, stderr, err := docker.ContainerLogs(id, line, timestamps)
|
|
|
|
if err != nil {
|
2018-05-07 12:58:32 +00:00
|
|
|
return ajaxResult(ctx, err)
|
2017-10-10 08:26:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 12:58:32 +00:00
|
|
|
return ctx.JSON(data.Map{
|
|
|
|
"stdout": stdout.String(),
|
|
|
|
"stderr": stderr.String(),
|
|
|
|
})
|
2017-10-10 08:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func containerDelete(ctx web.Context) error {
|
|
|
|
ids := strings.Split(ctx.F("ids"), ",")
|
|
|
|
for _, id := range ids {
|
|
|
|
if err := docker.ContainerRemove(id); err != nil {
|
|
|
|
return ajaxResult(ctx, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ajaxSuccess(ctx, nil)
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
2018-06-14 10:41:22 +00:00
|
|
|
|
|
|
|
func containerExec(ctx web.Context) error {
|
|
|
|
id := ctx.P("id")
|
|
|
|
container, _, err := docker.ContainerInspectRaw(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := newModel(ctx).Set("Container", container)
|
|
|
|
return ctx.Render("container/exec", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerConnect(ctx web.Context) error {
|
|
|
|
id := ctx.P("id")
|
|
|
|
_, _, err := docker.ContainerInspectRaw(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-11 08:36:54 +00:00
|
|
|
conn, _, _, err := ws.UpgradeHTTP(ctx.Request(), ctx.Response())
|
2018-06-14 10:41:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := ctx.Q("cmd")
|
|
|
|
idResp, err := docker.ContainerExecCreate(id, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := docker.ContainerExecAttach(idResp.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = docker.ContainerExecStart(idResp.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
closed = false
|
|
|
|
logger = log.Get("exec")
|
|
|
|
disposer = func() {
|
|
|
|
if !closed {
|
|
|
|
closed = true
|
|
|
|
conn.Close()
|
|
|
|
resp.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// input
|
|
|
|
go func() {
|
|
|
|
defer disposer()
|
|
|
|
|
|
|
|
for {
|
|
|
|
msg, op, err := wsutil.ReadClientData(conn)
|
|
|
|
if err != nil {
|
|
|
|
if !closed {
|
|
|
|
logger.Error("Failed to read data from client: ", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if op == ws.OpClose {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = resp.Conn.Write(msg)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to write data to container: ", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// output
|
|
|
|
go func() {
|
|
|
|
defer disposer()
|
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
n, err := resp.Reader.Read(buf)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
logger.Error("Failed to read data from container: ", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
err = wsutil.WriteServerMessage(conn, ws.OpText, buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to write data to client: ", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|