swirl/controller/image.go

86 lines
2.0 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package controller
import (
2017-10-10 07:26:07 +00:00
"strings"
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"
)
2017-10-11 03:26:03 +00:00
// ImageController is a controller of docker image
2017-09-26 12:50:09 +00:00
type ImageController struct {
2017-10-10 07:26:07 +00:00
List web.HandlerFunc `path:"/" name:"image.list" authorize:"!" desc:"image list page"`
Detail web.HandlerFunc `path:"/:id/detail" name:"image.detail" authorize:"!" desc:"image detail page"`
Raw web.HandlerFunc `path:"/:id/raw" name:"image.raw" authorize:"!" desc:"image raw page"`
Delete web.HandlerFunc `path:"/delete" method:"post" name:"image.delete" authorize:"!" desc:"delete image"`
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
// Image creates an instance of ImageController
2017-09-26 12:50:09 +00:00
func Image() (c *ImageController) {
2017-10-10 07:26:07 +00:00
return &ImageController{
List: imageList,
Detail: imageDetail,
Raw: imageRaw,
Delete: imageDelete,
}
}
func imageList(ctx web.Context) error {
name := ctx.Q("name")
2017-10-19 06:24:38 +00:00
page := cast.ToInt(ctx.Q("page"), 1)
2017-10-10 07:26:07 +00:00
images, totalCount, err := docker.ImageList(name, page, model.PageSize)
if err != nil {
return err
}
m := newPagerModel(ctx, totalCount, model.PageSize, page).
2017-11-08 10:36:13 +00:00
Set("Name", name).
Set("Images", images)
2017-10-10 07:26:07 +00:00
return ctx.Render("image/list", m)
}
func imageDetail(ctx web.Context) error {
id := ctx.P("id")
image, _, err := docker.ImageInspect(id)
if err != nil {
return err
}
2017-09-26 12:50:09 +00:00
2017-10-10 07:26:07 +00:00
histories, err := docker.ImageHistory(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("Image", image).Set("Histories", histories)
2017-10-10 07:26:07 +00:00
return ctx.Render("image/detail", m)
}
func imageRaw(ctx web.Context) error {
id := ctx.P("id")
image, raw, err := docker.ImageInspect(id)
if err != nil {
return err
}
j, err := misc.JSONIndent(raw)
if err != nil {
return err
}
2017-11-08 10:36:13 +00:00
m := newModel(ctx).Set("Image", image).Set("Raw", j)
2017-10-10 07:26:07 +00:00
return ctx.Render("image/raw", m)
}
func imageDelete(ctx web.Context) error {
ids := strings.Split(ctx.F("ids"), ",")
for _, id := range ids {
if err := docker.ImageRemove(id); err != nil {
return ajaxResult(ctx, err)
}
}
return ajaxSuccess(ctx, nil)
2017-09-26 12:50:09 +00:00
}