Refactor codes

This commit is contained in:
cuigh 2017-10-11 11:26:03 +08:00
parent 45f50d159f
commit 63d9b9bd53
18 changed files with 1220 additions and 1104 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// ConfigController is a controller of docker config
type ConfigController struct { type ConfigController struct {
List web.HandlerFunc `path:"/" name:"config.list" authorize:"!" desc:"config list page"` List web.HandlerFunc `path:"/" name:"config.list" authorize:"!" desc:"config list page"`
Delete web.HandlerFunc `path:"/delete" method:"post" name:"config.delete" authorize:"!" desc:"delete config"` Delete web.HandlerFunc `path:"/delete" method:"post" name:"config.delete" authorize:"!" desc:"delete config"`
@ -19,67 +20,73 @@ type ConfigController struct {
Update web.HandlerFunc `path:"/:id/update" method:"post" name:"config.update" authorize:"!" desc:"update config"` Update web.HandlerFunc `path:"/:id/update" method:"post" name:"config.update" authorize:"!" desc:"update config"`
} }
// Config creates an instance of ConfigController
func Config() (c *ConfigController) { func Config() (c *ConfigController) {
c = &ConfigController{} return &ConfigController{
List: configList,
c.List = func(ctx web.Context) error { Delete: configDelete,
name := ctx.Q("name") New: configNew,
page := cast.ToIntD(ctx.Q("page"), 1) Create: configCreate,
configs, totalCount, err := docker.ConfigList(name, page, model.PageSize) Edit: configEdit,
if err != nil { Update: configUpdate,
return err
}
m := newPagerModel(ctx, totalCount, model.PageSize, page).
Add("Name", name).
Add("Configs", configs)
return ctx.Render("config/list", m)
} }
}
c.Delete = func(ctx web.Context) error {
ids := strings.Split(ctx.F("ids"), ",") func configList(ctx web.Context) error {
err := docker.ConfigRemove(ids) name := ctx.Q("name")
return ajaxResult(ctx, err) page := cast.ToIntD(ctx.Q("page"), 1)
} configs, totalCount, err := docker.ConfigList(name, page, model.PageSize)
if err != nil {
c.New = func(ctx web.Context) error { return err
m := newModel(ctx) }
return ctx.Render("config/new", m)
} m := newPagerModel(ctx, totalCount, model.PageSize, page).
Add("Name", name).
c.Create = func(ctx web.Context) error { Add("Configs", configs)
v := &model.ConfigCreateInfo{} return ctx.Render("config/list", m)
err := ctx.Bind(v) }
if err == nil {
err = docker.ConfigCreate(v) func configDelete(ctx web.Context) error {
if err == nil { ids := strings.Split(ctx.F("ids"), ",")
biz.Event.CreateConfig(model.EventActionCreate, v.Name, ctx.User()) err := docker.ConfigRemove(ids)
} return ajaxResult(ctx, err)
} }
return ajaxResult(ctx, err)
} func configNew(ctx web.Context) error {
m := newModel(ctx)
c.Edit = func(ctx web.Context) error { return ctx.Render("config/new", m)
id := ctx.P("id") }
cfg, _, err := docker.ConfigInspect(id)
if err != nil { func configCreate(ctx web.Context) error {
return err v := &model.ConfigCreateInfo{}
} err := ctx.Bind(v)
m := newModel(ctx).Add("Config", cfg) if err == nil {
return ctx.Render("config/edit", m) err = docker.ConfigCreate(v)
} if err == nil {
biz.Event.CreateConfig(model.EventActionCreate, v.Name, ctx.User())
c.Update = func(ctx web.Context) error { }
v := &model.ConfigUpdateInfo{} }
err := ctx.Bind(v) return ajaxResult(ctx, err)
if err == nil { }
err = docker.ConfigUpdate(v)
if err == nil { func configEdit(ctx web.Context) error {
biz.Event.CreateConfig(model.EventActionUpdate, v.Name, ctx.User()) id := ctx.P("id")
} cfg, _, err := docker.ConfigInspect(id)
} if err != nil {
return ajaxResult(ctx, err) return err
} }
m := newModel(ctx).Add("Config", cfg)
return return ctx.Render("config/edit", m)
}
func configUpdate(ctx web.Context) error {
v := &model.ConfigUpdateInfo{}
err := ctx.Bind(v)
if err == nil {
err = docker.ConfigUpdate(v)
if err == nil {
biz.Event.CreateConfig(model.EventActionUpdate, v.Name, ctx.User())
}
}
return ajaxResult(ctx, err)
} }

View File

@ -10,6 +10,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// ContainerController is a controller of docker container
type ContainerController struct { type ContainerController struct {
List web.HandlerFunc `path:"/" name:"container.list" authorize:"!" desc:"container list page"` 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"` Detail web.HandlerFunc `path:"/:id/detail" name:"container.detail" authorize:"!" desc:"container detail page"`
@ -18,6 +19,7 @@ type ContainerController struct {
Delete web.HandlerFunc `path:"/delete" method:"post" name:"container.delete" authorize:"!" desc:"delete container"` Delete web.HandlerFunc `path:"/delete" method:"post" name:"container.delete" authorize:"!" desc:"delete container"`
} }
// Container creates an instance of ContainerController
func Container() (c *ContainerController) { func Container() (c *ContainerController) {
return &ContainerController{ return &ContainerController{
List: containerList, List: containerList,

View File

@ -6,33 +6,35 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// EventController is a controller of user events
type EventController struct { type EventController struct {
List web.HandlerFunc `path:"/" name:"event.list" authorize:"!" desc:"event list page"` List web.HandlerFunc `path:"/" name:"event.list" authorize:"!" desc:"event list page"`
} }
// Event creates an instance of EventController
func Event() (c *EventController) { func Event() (c *EventController) {
c = &EventController{} return &EventController{
List: eventList,
}
}
c.List = func(ctx web.Context) error { func eventList(ctx web.Context) error {
args := &model.EventListArgs{} args := &model.EventListArgs{}
err := ctx.Bind(args) err := ctx.Bind(args)
if err != nil { if err != nil {
return err return err
} }
args.PageSize = model.PageSize args.PageSize = model.PageSize
if args.PageIndex == 0 { if args.PageIndex == 0 {
args.PageIndex = 1 args.PageIndex = 1
}
events, totalCount, err := biz.Event.List(args)
if err != nil {
return err
}
m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
Add("Events", events).Add("Args", args)
return ctx.Render("system/event/list", m)
} }
return events, totalCount, err := biz.Event.List(args)
if err != nil {
return err
}
m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
Add("Events", events).Add("Args", args)
return ctx.Render("system/event/list", m)
} }

View File

@ -8,101 +8,108 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// HomeController is a basic controller of site
type HomeController struct { type HomeController struct {
Index web.HandlerFunc `path:"/" name:"index" authorize:"?" desc:"index page"` Index web.HandlerFunc `path:"/" name:"index" authorize:"?" desc:"index page"`
Error403 web.HandlerFunc `path:"/403" name:"403" authorize:"?" desc:"403 page"`
Error404 web.HandlerFunc `path:"/404" name:"404" authorize:"*" desc:"404 page"`
Login web.HandlerFunc `path:"/login" name:"login" authorize:"*" desc:"sign in page"` Login web.HandlerFunc `path:"/login" name:"login" authorize:"*" desc:"sign in page"`
InitGet web.HandlerFunc `path:"/init" name:"init" authorize:"*" desc:"initialize page"` InitGet web.HandlerFunc `path:"/init" name:"init" authorize:"*" desc:"initialize page"`
InitPost web.HandlerFunc `path:"/init" method:"post" name:"init" authorize:"*" desc:"initialize system"` InitPost web.HandlerFunc `path:"/init" method:"post" name:"init" authorize:"*" desc:"initialize system"`
Error403 web.HandlerFunc `path:"/403" name:"403" authorize:"?" desc:"403 page"`
Error404 web.HandlerFunc `path:"/404" name:"404" authorize:"*" desc:"404 page"`
} }
// Home creates an instance of HomeController
func Home() (c *HomeController) { func Home() (c *HomeController) {
c = &HomeController{} return &HomeController{
Index: homeIndex,
c.Index = func(ctx web.Context) (err error) { Login: homeLogin,
var ( InitGet: homeInitGet,
count int InitPost: homeInitPost,
m = newModel(ctx) Error403: homeError403,
) Error404: homeError404,
if count, err = docker.NodeCount(); err != nil {
return
}
m.Add("NodeCount", count)
if count, err = docker.NetworkCount(); err != nil {
return
}
m.Add("NetworkCount", count)
if count, err = docker.ServiceCount(); err != nil {
return
}
m.Add("ServiceCount", count)
if count, err = docker.StackCount(); err != nil {
return
}
m.Add("StackCount", count)
return ctx.Render("index", m)
} }
}
c.Login = func(ctx web.Context) error {
count, err := biz.User.Count() func homeIndex(ctx web.Context) (err error) {
if err != nil { var (
return err count int
} else if count == 0 { m = newModel(ctx)
return ctx.Redirect("init") )
}
if ctx.User() != nil { if count, err = docker.NodeCount(); err != nil {
u := ctx.Q("from") return
if u == "" { }
u = "/" m.Add("NodeCount", count)
}
return ctx.Redirect(u) if count, err = docker.NetworkCount(); err != nil {
} return
return ctx.Render("login", nil) }
} m.Add("NetworkCount", count)
c.InitGet = func(ctx web.Context) error { if count, err = docker.ServiceCount(); err != nil {
count, err := biz.User.Count() return
if err != nil { }
return err m.Add("ServiceCount", count)
} else if count > 0 {
return ctx.Redirect("login") if count, err = docker.StackCount(); err != nil {
} return
return ctx.Render("init", nil) }
} m.Add("StackCount", count)
c.InitPost = func(ctx web.Context) error { return ctx.Render("index", m)
count, err := biz.User.Count() }
if err != nil {
return err func homeLogin(ctx web.Context) error {
} else if count > 0 { count, err := biz.User.Count()
return errors.New("Swirl was already initialized") if err != nil {
} return err
} else if count == 0 {
user := &model.User{} return ctx.Redirect("init")
err = ctx.Bind(user) }
if err != nil { if ctx.User() != nil {
return err u := ctx.Q("from")
} if u == "" {
u = "/"
user.Admin = true }
user.Type = model.UserTypeInternal return ctx.Redirect(u)
err = biz.User.Create(user, nil) }
return ajaxResult(ctx, err) return ctx.Render("login", nil)
} }
c.Error403 = func(ctx web.Context) error { func homeInitGet(ctx web.Context) error {
return ctx.Render("403", nil) count, err := biz.User.Count()
} if err != nil {
return err
c.Error404 = func(ctx web.Context) error { } else if count > 0 {
return ctx.Render("404", nil) return ctx.Redirect("login")
} }
return ctx.Render("init", nil)
return }
func homeInitPost(ctx web.Context) error {
count, err := biz.User.Count()
if err != nil {
return err
} else if count > 0 {
return errors.New("Swirl was already initialized")
}
user := &model.User{}
err = ctx.Bind(user)
if err != nil {
return err
}
user.Admin = true
user.Type = model.UserTypeInternal
err = biz.User.Create(user, nil)
return ajaxResult(ctx, err)
}
func homeError403(ctx web.Context) error {
return ctx.Render("403", nil)
}
func homeError404(ctx web.Context) error {
return ctx.Render("404", nil)
} }

View File

@ -10,6 +10,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// ImageController is a controller of docker image
type ImageController struct { type ImageController struct {
List web.HandlerFunc `path:"/" name:"image.list" authorize:"!" desc:"image list page"` 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"` Detail web.HandlerFunc `path:"/:id/detail" name:"image.detail" authorize:"!" desc:"image detail page"`
@ -17,6 +18,7 @@ type ImageController struct {
Delete web.HandlerFunc `path:"/delete" method:"post" name:"image.delete" authorize:"!" desc:"delete image"` Delete web.HandlerFunc `path:"/delete" method:"post" name:"image.delete" authorize:"!" desc:"delete image"`
} }
// Image creates an instance of ImageController
func Image() (c *ImageController) { func Image() (c *ImageController) {
return &ImageController{ return &ImageController{
List: imageList, List: imageList,

View File

@ -8,6 +8,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// NetworkController is a controller of docker network
type NetworkController struct { type NetworkController struct {
List web.HandlerFunc `path:"/" name:"network.list" authorize:"!" desc:"network list page"` List web.HandlerFunc `path:"/" name:"network.list" authorize:"!" desc:"network list page"`
New web.HandlerFunc `path:"/new" name:"network.new" authorize:"!" desc:"new network page"` New web.HandlerFunc `path:"/new" name:"network.new" authorize:"!" desc:"new network page"`
@ -18,82 +19,88 @@ type NetworkController struct {
Raw web.HandlerFunc `path:"/:name/raw" name:"network.raw" authorize:"!" desc:"network raw page"` Raw web.HandlerFunc `path:"/:name/raw" name:"network.raw" authorize:"!" desc:"network raw page"`
} }
// Network create a NetworkController instance. // Network creates a NetworkController instance.
func Network() (c *NetworkController) { func Network() (c *NetworkController) {
c = &NetworkController{} return &NetworkController{
List: networkList,
c.List = func(ctx web.Context) error { New: networkNew,
networks, err := docker.NetworkList() Create: networkCreate,
if err != nil { Delete: networkDelete,
return err Disconnect: networkDisconnect,
} Detail: networkDetail,
Raw: networkRaw,
m := newModel(ctx).Add("Networks", networks)
return ctx.Render("network/list", m)
} }
}
c.New = func(ctx web.Context) error {
m := newModel(ctx) func networkList(ctx web.Context) error {
return ctx.Render("/network/new", m) networks, err := docker.NetworkList()
} if err != nil {
return err
c.Create = func(ctx web.Context) error { }
info := &model.NetworkCreateInfo{}
err := ctx.Bind(info) m := newModel(ctx).Add("Networks", networks)
if err != nil { return ctx.Render("network/list", m)
return err }
}
err = docker.NetworkCreate(info) func networkNew(ctx web.Context) error {
if err == nil { m := newModel(ctx)
biz.Event.CreateNetwork(model.EventActionCreate, info.Name, info.Name, ctx.User()) return ctx.Render("/network/new", m)
} }
return ajaxResult(ctx, err)
} func networkCreate(ctx web.Context) error {
info := &model.NetworkCreateInfo{}
c.Delete = func(ctx web.Context) error { err := ctx.Bind(info)
name := ctx.F("name") if err != nil {
err := docker.NetworkRemove(name) return err
if err == nil { }
biz.Event.CreateNetwork(model.EventActionDelete, name, name, ctx.User()) err = docker.NetworkCreate(info)
} if err == nil {
return ajaxResult(ctx, err) biz.Event.CreateNetwork(model.EventActionCreate, info.Name, info.Name, ctx.User())
} }
return ajaxResult(ctx, err)
c.Disconnect = func(ctx web.Context) error { }
name := ctx.P("name")
container := ctx.F("container") func networkDelete(ctx web.Context) error {
err := docker.NetworkDisconnect(name, container) name := ctx.F("name")
if err == nil { err := docker.NetworkRemove(name)
biz.Event.CreateNetwork(model.EventActionDisconnect, name, name+" <-> "+container, ctx.User()) if err == nil {
} biz.Event.CreateNetwork(model.EventActionDelete, name, name, ctx.User())
return ajaxResult(ctx, err) }
} return ajaxResult(ctx, err)
}
c.Detail = func(ctx web.Context) error {
name := ctx.P("name") func networkDisconnect(ctx web.Context) error {
network, err := docker.NetworkInspect(name) name := ctx.P("name")
if err != nil { container := ctx.F("container")
return err err := docker.NetworkDisconnect(name, container)
} if err == nil {
m := newModel(ctx).Add("Network", network) biz.Event.CreateNetwork(model.EventActionDisconnect, name, name+" <-> "+container, ctx.User())
return ctx.Render("network/detail", m) }
} return ajaxResult(ctx, err)
}
c.Raw = func(ctx web.Context) error {
name := ctx.P("name") func networkDetail(ctx web.Context) error {
raw, err := docker.NetworkInspectRaw(name) name := ctx.P("name")
if err != nil { network, err := docker.NetworkInspect(name)
return err if err != nil {
} return err
}
j, err := misc.JSONIndent(raw) m := newModel(ctx).Add("Network", network)
if err != nil { return ctx.Render("network/detail", m)
return err }
}
func networkRaw(ctx web.Context) error {
m := newModel(ctx).Add("Network", name).Add("Raw", j) name := ctx.P("name")
return ctx.Render("network/raw", m) raw, err := docker.NetworkInspectRaw(name)
} if err != nil {
return err
return }
j, err := misc.JSONIndent(raw)
if err != nil {
return err
}
m := newModel(ctx).Add("Network", name).Add("Raw", j)
return ctx.Render("network/raw", m)
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// NodeController is a controller of swarm node
type NodeController struct { type NodeController struct {
List web.HandlerFunc `path:"/" name:"node.list" authorize:"!" desc:"node list page"` 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"` Detail web.HandlerFunc `path:"/:id/detail" name:"node.detail" authorize:"!" desc:"node detail page"`
@ -16,77 +17,83 @@ type NodeController struct {
Update web.HandlerFunc `path:"/:id/update" method:"post" name:"node.update" authorize:"!" desc:"update node"` Update web.HandlerFunc `path:"/:id/update" method:"post" name:"node.update" authorize:"!" desc:"update node"`
} }
// Node creates an instance of NodeController
func Node() (c *NodeController) { func Node() (c *NodeController) {
c = &NodeController{} return &NodeController{
List: nodeList,
c.List = func(ctx web.Context) error { Detail: nodeDetail,
nodes, err := docker.NodeList() Raw: nodeRaw,
if err != nil { Delete: nodeDelete,
return err Edit: nodeEdit,
} Update: nodeUpdate,
m := newModel(ctx).Add("Nodes", nodes)
return ctx.Render("node/list", m)
} }
}
c.Delete = func(ctx web.Context) error {
id := ctx.F("id") func nodeList(ctx web.Context) error {
err := docker.NodeRemove(id) nodes, err := docker.NodeList()
return ajaxResult(ctx, err) if err != nil {
} return err
}
c.Detail = func(ctx web.Context) error {
id := ctx.P("id") m := newModel(ctx).Add("Nodes", nodes)
node, _, err := docker.NodeInspect(id) return ctx.Render("node/list", m)
if err != nil { }
return err
} func nodeDelete(ctx web.Context) error {
id := ctx.F("id")
tasks, err := docker.TaskList("", id) err := docker.NodeRemove(id)
if err != nil { return ajaxResult(ctx, err)
return err }
}
func nodeDetail(ctx web.Context) error {
m := newModel(ctx).Add("Node", node).Add("Tasks", tasks) id := ctx.P("id")
return ctx.Render("node/detail", m) node, _, err := docker.NodeInspect(id)
} if err != nil {
return err
c.Raw = func(ctx web.Context) error { }
id := ctx.P("id")
node, raw, err := docker.NodeInspect(id) tasks, err := docker.TaskList("", id)
if err != nil { if err != nil {
return err return err
} }
j, err := misc.JSONIndent(raw) m := newModel(ctx).Add("Node", node).Add("Tasks", tasks)
if err != nil { return ctx.Render("node/detail", m)
return err }
}
func nodeRaw(ctx web.Context) error {
m := newModel(ctx).Add("ID", id).Add("Node", node).Add("Raw", j) id := ctx.P("id")
return ctx.Render("node/raw", m) node, raw, err := docker.NodeInspect(id)
} if err != nil {
return err
c.Edit = func(ctx web.Context) error { }
id := ctx.P("id")
node, _, err := docker.NodeInspect(id) j, err := misc.JSONIndent(raw)
if err != nil { if err != nil {
return err return err
} }
m := newModel(ctx).Add("Node", node) m := newModel(ctx).Add("ID", id).Add("Node", node).Add("Raw", j)
return ctx.Render("node/edit", m) return ctx.Render("node/raw", m)
} }
c.Update = func(ctx web.Context) error { func nodeEdit(ctx web.Context) error {
id := ctx.P("id") id := ctx.P("id")
info := &model.NodeUpdateInfo{} node, _, err := docker.NodeInspect(id)
err := ctx.Bind(info) if err != nil {
if err == nil { return err
err = docker.NodeUpdate(id, info) }
}
return ajaxResult(ctx, err) m := newModel(ctx).Add("Node", node)
} return ctx.Render("node/edit", m)
}
return
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)
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// ProfileController is a controller of user profile
type ProfileController struct { type ProfileController struct {
Index web.HandlerFunc `path:"/" name:"profile.info" authorize:"?" desc:"profile info page"` Index web.HandlerFunc `path:"/" name:"profile.info" authorize:"?" desc:"profile info page"`
ModifyInfo web.HandlerFunc `path:"/" method:"post" name:"profile.info.modify" authorize:"?" desc:"modify info"` ModifyInfo web.HandlerFunc `path:"/" method:"post" name:"profile.info.modify" authorize:"?" desc:"modify info"`
@ -13,40 +14,44 @@ type ProfileController struct {
ModifyPassword web.HandlerFunc `path:"/password" method:"post" name:"profile.password.modify" authorize:"?" desc:"modify password"` ModifyPassword web.HandlerFunc `path:"/password" method:"post" name:"profile.password.modify" authorize:"?" desc:"modify password"`
} }
// Profile creates an instance of ProfileController
func Profile() (c *ProfileController) { func Profile() (c *ProfileController) {
c = &ProfileController{} return &ProfileController{
Index: profileIndex,
c.Index = func(ctx web.Context) error { ModifyInfo: profileModifyInfo,
user, err := biz.User.GetByID(ctx.User().ID()) Password: profilePassword,
if err != nil { ModifyPassword: profileModifyPassword,
return err
}
m := newModel(ctx).Add("User", user)
return ctx.Render("profile/index", m)
} }
}
c.ModifyInfo = func(ctx web.Context) error {
user := &model.User{} func profileIndex(ctx web.Context) error {
err := ctx.Bind(user) user, err := biz.User.GetByID(ctx.User().ID())
if err == nil { if err != nil {
user.ID = ctx.User().ID() return err
err = biz.User.UpdateInfo(user) }
}
return ajaxResult(ctx, err) m := newModel(ctx).Add("User", user)
} return ctx.Render("profile/index", m)
}
c.Password = func(ctx web.Context) error {
m := newModel(ctx) func profileModifyInfo(ctx web.Context) error {
return ctx.Render("profile/password", m) user := &model.User{}
} err := ctx.Bind(user)
if err == nil {
c.ModifyPassword = func(ctx web.Context) error { user.ID = ctx.User().ID()
old_pwd := ctx.F("password_old") err = biz.User.UpdateInfo(user)
new_pwd := ctx.F("password") }
err := biz.User.UpdatePassword(ctx.User().ID(), old_pwd, new_pwd) return ajaxResult(ctx, err)
return ajaxResult(ctx, err) }
}
func profilePassword(ctx web.Context) error {
return m := newModel(ctx)
return ctx.Render("profile/password", m)
}
func profileModifyPassword(ctx web.Context) error {
oldPwd := ctx.F("password_old")
newPwd := ctx.F("password")
err := biz.User.UpdatePassword(ctx.User().ID(), oldPwd, newPwd)
return ajaxResult(ctx, err)
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// RegistryController is a controller of docker registry
type RegistryController struct { type RegistryController struct {
List web.HandlerFunc `path:"/" name:"registry.list" authorize:"!" desc:"registry list page"` List web.HandlerFunc `path:"/" name:"registry.list" authorize:"!" desc:"registry list page"`
Create web.HandlerFunc `path:"/create" method:"post" name:"registry.create" authorize:"!" desc:"create registry"` Create web.HandlerFunc `path:"/create" method:"post" name:"registry.create" authorize:"!" desc:"create registry"`
@ -13,44 +14,48 @@ type RegistryController struct {
Update web.HandlerFunc `path:"/update" method:"post" name:"registry.update" authorize:"!" desc:"update registry"` Update web.HandlerFunc `path:"/update" method:"post" name:"registry.update" authorize:"!" desc:"update registry"`
} }
// Registry creates an instance of RegistryController
func Registry() (c *RegistryController) { func Registry() (c *RegistryController) {
c = &RegistryController{} return &RegistryController{
List: registryList,
c.List = func(ctx web.Context) error { Create: registryCreate,
registries, err := biz.Registry.List() Delete: registryDelete,
if err != nil { Update: registryUpdate,
return err
}
m := newModel(ctx).Add("Registries", registries)
return ctx.Render("registry/list", m)
} }
}
c.Create = func(ctx web.Context) error {
registry := &model.Registry{} func registryList(ctx web.Context) error {
err := ctx.Bind(registry) registries, err := biz.Registry.List()
if err != nil { if err != nil {
return err return err
} }
err = biz.Registry.Create(registry, ctx.User())
return ajaxResult(ctx, err) m := newModel(ctx).Add("Registries", registries)
} return ctx.Render("registry/list", m)
}
c.Delete = func(ctx web.Context) error {
id := ctx.F("id") func registryCreate(ctx web.Context) error {
err := biz.Registry.Delete(id, ctx.User()) registry := &model.Registry{}
return ajaxResult(ctx, err) err := ctx.Bind(registry)
} if err != nil {
return err
c.Update = func(ctx web.Context) error { }
registry := &model.Registry{} err = biz.Registry.Create(registry, ctx.User())
err := ctx.Bind(registry) return ajaxResult(ctx, err)
if err != nil { }
return err
} func registryDelete(ctx web.Context) error {
err = biz.Registry.Update(registry, ctx.User()) id := ctx.F("id")
return ajaxResult(ctx, err) err := biz.Registry.Delete(id, ctx.User())
} return ajaxResult(ctx, err)
}
return
func registryUpdate(ctx web.Context) error {
registry := &model.Registry{}
err := ctx.Bind(registry)
if err != nil {
return err
}
err = biz.Registry.Update(registry, ctx.User())
return ajaxResult(ctx, err)
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// RoleController is a controller of user role
type RoleController struct { type RoleController struct {
Index web.HandlerFunc `path:"/" name:"role.list" authorize:"!" desc:"role list page"` Index web.HandlerFunc `path:"/" name:"role.list" authorize:"!" desc:"role list page"`
New web.HandlerFunc `path:"/new" name:"role.new" authorize:"!" desc:"new role page"` New web.HandlerFunc `path:"/new" name:"role.new" authorize:"!" desc:"new role page"`
@ -17,84 +18,91 @@ type RoleController struct {
Update web.HandlerFunc `path:"/:id/update" method:"post" name:"role.update" authorize:"!" desc:"update role"` Update web.HandlerFunc `path:"/:id/update" method:"post" name:"role.update" authorize:"!" desc:"update role"`
} }
// Role creates an instance of RoleController
func Role() (c *RoleController) { func Role() (c *RoleController) {
c = &RoleController{} return &RoleController{
Index: roleIndex,
c.Index = func(ctx web.Context) error { New: roleNew,
roles, err := biz.Role.List() Create: roleCreate,
if err != nil { Delete: roleDelete,
return err Detail: roleDetail,
} Edit: roleEdit,
Update: roleUpdate,
m := newModel(ctx).Add("Roles", roles)
return ctx.Render("system/role/list", m)
} }
}
c.New = func(ctx web.Context) error {
m := newModel(ctx).Add("Perms", misc.Perms) func roleIndex(ctx web.Context) error {
return ctx.Render("system/role/new", m) roles, err := biz.Role.List()
} if err != nil {
return err
c.Create = func(ctx web.Context) error { }
role := &model.Role{}
err := ctx.Bind(role) m := newModel(ctx).Add("Roles", roles)
if err == nil { return ctx.Render("system/role/list", m)
err = biz.Role.Create(role, ctx.User()) }
}
return ajaxResult(ctx, err) func roleNew(ctx web.Context) error {
} m := newModel(ctx).Add("Perms", misc.Perms)
return ctx.Render("system/role/new", m)
c.Delete = func(ctx web.Context) error { }
id := ctx.F("id")
err := biz.Role.Delete(id, ctx.User()) func roleCreate(ctx web.Context) error {
return ajaxResult(ctx, err) role := &model.Role{}
} err := ctx.Bind(role)
if err == nil {
c.Detail = func(ctx web.Context) error { err = biz.Role.Create(role, ctx.User())
id := ctx.P("id") }
role, err := biz.Role.Get(id) return ajaxResult(ctx, err)
if err != nil { }
return err
} func roleDelete(ctx web.Context) error {
if role == nil { id := ctx.F("id")
return web.ErrNotFound err := biz.Role.Delete(id, ctx.User())
} return ajaxResult(ctx, err)
}
perms := make(map[string]struct{})
for _, p := range role.Perms { func roleDetail(ctx web.Context) error {
perms[p] = model.Placeholder id := ctx.P("id")
} role, err := biz.Role.Get(id)
m := newModel(ctx).Add("Role", role).Add("Perms", misc.Perms).Add("CheckedPerms", perms) if err != nil {
return ctx.Render("system/role/detail", m) return err
} }
if role == nil {
c.Edit = func(ctx web.Context) error { return web.ErrNotFound
id := ctx.P("id") }
role, err := biz.Role.Get(id)
if err != nil { perms := make(map[string]struct{})
return err for _, p := range role.Perms {
} perms[p] = model.Placeholder
if role == nil { }
return web.ErrNotFound m := newModel(ctx).Add("Role", role).Add("Perms", misc.Perms).Add("CheckedPerms", perms)
} return ctx.Render("system/role/detail", m)
}
perms := make(map[string]struct{})
for _, p := range role.Perms { func roleEdit(ctx web.Context) error {
perms[p] = model.Placeholder id := ctx.P("id")
} role, err := biz.Role.Get(id)
m := newModel(ctx).Add("Role", role).Add("Perms", misc.Perms).Add("CheckedPerms", perms) if err != nil {
return ctx.Render("system/role/edit", m) return err
} }
if role == nil {
c.Update = func(ctx web.Context) error { return web.ErrNotFound
role := &model.Role{} }
err := ctx.Bind(role)
if err == nil { perms := make(map[string]struct{})
role.ID = ctx.P("id") for _, p := range role.Perms {
err = biz.Role.Update(role, ctx.User()) perms[p] = model.Placeholder
} }
return ajaxResult(ctx, err) m := newModel(ctx).Add("Role", role).Add("Perms", misc.Perms).Add("CheckedPerms", perms)
} return ctx.Render("system/role/edit", m)
}
return
func roleUpdate(ctx web.Context) error {
role := &model.Role{}
err := ctx.Bind(role)
if err == nil {
role.ID = ctx.P("id")
err = biz.Role.Update(role, ctx.User())
}
return ajaxResult(ctx, err)
} }

View File

@ -10,6 +10,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// SecretController is a controller of docker secret
type SecretController struct { type SecretController struct {
List web.HandlerFunc `path:"/" name:"secret.list" authorize:"!" desc:"secret list page"` List web.HandlerFunc `path:"/" name:"secret.list" authorize:"!" desc:"secret list page"`
Delete web.HandlerFunc `path:"/delete" method:"post" name:"secret.delete" authorize:"!" desc:"delete secret"` Delete web.HandlerFunc `path:"/delete" method:"post" name:"secret.delete" authorize:"!" desc:"delete secret"`
@ -19,75 +20,80 @@ type SecretController struct {
Update web.HandlerFunc `path:"/:id/update" method:"post" name:"secret.update" authorize:"!" desc:"update secret"` Update web.HandlerFunc `path:"/:id/update" method:"post" name:"secret.update" authorize:"!" desc:"update secret"`
} }
// Secret creates an instance of SecretController
func Secret() (c *SecretController) { func Secret() (c *SecretController) {
c = &SecretController{} return &SecretController{
List: secretList,
c.List = func(ctx web.Context) error { Delete: secretDelete,
name := ctx.Q("name") New: secretNew,
page := cast.ToIntD(ctx.Q("page"), 1) Create: secretCreate,
secrets, totalCount, err := docker.SecretList(name, page, model.PageSize) Edit: secretEdit,
if err != nil { Update: secretUpdate,
return err
}
m := newPagerModel(ctx, totalCount, model.PageSize, page).
Add("Name", name).
Add("Secrets", secrets)
return ctx.Render("secret/list", m)
} }
}
c.Delete = func(ctx web.Context) error { func secretList(ctx web.Context) error {
ids := strings.Split(ctx.F("ids"), ",") name := ctx.Q("name")
for _, id := range ids { page := cast.ToIntD(ctx.Q("page"), 1)
err := docker.SecretRemove(id) secrets, totalCount, err := docker.SecretList(name, page, model.PageSize)
if err != nil { if err != nil {
return ajaxResult(ctx, err) return err
} else { }
// todo:
biz.Event.CreateSecret(model.EventActionDelete, id, ctx.User()) m := newPagerModel(ctx, totalCount, model.PageSize, page).
} Add("Name", name).
} Add("Secrets", secrets)
return ajaxSuccess(ctx, nil) return ctx.Render("secret/list", m)
} }
c.New = func(ctx web.Context) error { func secretDelete(ctx web.Context) error {
m := newModel(ctx) ids := strings.Split(ctx.F("ids"), ",")
return ctx.Render("secret/new", m) for _, id := range ids {
} err := docker.SecretRemove(id)
if err != nil {
c.Create = func(ctx web.Context) error { return ajaxResult(ctx, err)
v := &model.ConfigCreateInfo{} } else {
err := ctx.Bind(v) // todo:
if err == nil { biz.Event.CreateSecret(model.EventActionDelete, id, ctx.User())
err = docker.SecretCreate(v) }
if err == nil { }
biz.Event.CreateSecret(model.EventActionCreate, v.Name, ctx.User()) return ajaxSuccess(ctx, nil)
} }
}
return ajaxResult(ctx, err) func secretNew(ctx web.Context) error {
} m := newModel(ctx)
return ctx.Render("secret/new", m)
c.Edit = func(ctx web.Context) error { }
id := ctx.P("id")
secret, _, err := docker.SecretInspect(id) func secretCreate(ctx web.Context) error {
if err != nil { v := &model.ConfigCreateInfo{}
return err err := ctx.Bind(v)
} if err == nil {
m := newModel(ctx).Add("Secret", secret) err = docker.SecretCreate(v)
return ctx.Render("secret/edit", m) if err == nil {
} biz.Event.CreateSecret(model.EventActionCreate, v.Name, ctx.User())
}
c.Update = func(ctx web.Context) error { }
v := &model.ConfigUpdateInfo{} return ajaxResult(ctx, err)
err := ctx.Bind(v) }
if err == nil {
err = docker.SecretUpdate(v) func secretEdit(ctx web.Context) error {
if err == nil { id := ctx.P("id")
biz.Event.CreateSecret(model.EventActionUpdate, v.Name, ctx.User()) secret, _, err := docker.SecretInspect(id)
} if err != nil {
} return err
return ajaxResult(ctx, err) }
} m := newModel(ctx).Add("Secret", secret)
return ctx.Render("secret/edit", m)
return }
func secretUpdate(ctx web.Context) error {
v := &model.ConfigUpdateInfo{}
err := ctx.Bind(v)
if err == nil {
err = docker.SecretUpdate(v)
if err == nil {
biz.Event.CreateSecret(model.EventActionUpdate, v.Name, ctx.User())
}
}
return ajaxResult(ctx, err)
} }

View File

@ -15,6 +15,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// ServiceController is a controller of docker service
type ServiceController struct { type ServiceController struct {
List web.HandlerFunc `path:"/" name:"service.list" authorize:"!" desc:"service list page"` List web.HandlerFunc `path:"/" name:"service.list" authorize:"!" desc:"service list page"`
Detail web.HandlerFunc `path:"/:name/detail" name:"service.detail" authorize:"!" desc:"service detail page"` Detail web.HandlerFunc `path:"/:name/detail" name:"service.detail" authorize:"!" desc:"service detail page"`
@ -28,218 +29,231 @@ type ServiceController struct {
Update web.HandlerFunc `path:"/:name/edit" method:"post" name:"service.update" authorize:"!" desc:"update service"` Update web.HandlerFunc `path:"/:name/edit" method:"post" name:"service.update" authorize:"!" desc:"update service"`
} }
// Service creates an instance of ServiceController
func Service() (c *ServiceController) { func Service() (c *ServiceController) {
c = &ServiceController{} return &ServiceController{
List: serviceList,
Detail: serviceDetail,
Raw: serviceRaw,
Logs: serviceLogs,
Delete: serviceDelete,
New: serviceNew,
Create: serviceCreate,
Edit: serviceEdit,
Update: serviceUpdate,
Scale: serviceScale,
}
}
c.List = func(ctx web.Context) error { func serviceList(ctx web.Context) error {
name := ctx.Q("name") name := ctx.Q("name")
page := cast.ToIntD(ctx.Q("page"), 1) page := cast.ToIntD(ctx.Q("page"), 1)
services, totalCount, err := docker.ServiceList(name, page, model.PageSize) services, totalCount, err := docker.ServiceList(name, page, model.PageSize)
if err != nil { if err != nil {
return err return err
}
m := newPagerModel(ctx, totalCount, model.PageSize, page).
Add("Name", name).
Add("Services", services)
return ctx.Render("service/list", m)
} }
c.Detail = func(ctx web.Context) error { m := newPagerModel(ctx, totalCount, model.PageSize, page).
name := ctx.P("name") Add("Name", name).
service, _, err := docker.ServiceInspect(name) Add("Services", services)
if err != nil { return ctx.Render("service/list", m)
return err }
}
info := model.NewServiceDetailInfo(service) func serviceDetail(ctx web.Context) error {
for _, vip := range service.Endpoint.VirtualIPs { name := ctx.P("name")
n, err := docker.NetworkInspect(vip.NetworkID) service, _, err := docker.ServiceInspect(name)
if err != nil { if err != nil {
return err return err
}
info.Networks = append(info.Networks, model.Network{ID: vip.NetworkID, Name: n.Name, Address: vip.Addr})
}
tasks, err := docker.TaskList(name, "")
if err != nil {
return err
}
m := newModel(ctx).Add("Service", info).Add("Tasks", tasks)
return ctx.Render("service/detail", m)
} }
c.Raw = func(ctx web.Context) error { info := model.NewServiceDetailInfo(service)
name := ctx.P("name") for _, vip := range service.Endpoint.VirtualIPs {
_, raw, err := docker.ServiceInspect(name) n, e := docker.NetworkInspect(vip.NetworkID)
if e != nil {
return e
}
info.Networks = append(info.Networks, model.Network{ID: vip.NetworkID, Name: n.Name, Address: vip.Addr})
}
tasks, err := docker.TaskList(name, "")
if err != nil {
return err
}
m := newModel(ctx).Add("Service", info).Add("Tasks", tasks)
return ctx.Render("service/detail", m)
}
func serviceRaw(ctx web.Context) error {
name := ctx.P("name")
_, raw, err := docker.ServiceInspect(name)
if err != nil {
return err
}
j, err := misc.JSONIndent(raw)
if err != nil {
return err
}
m := newModel(ctx).Add("Service", name).Add("Raw", j)
return ctx.Render("service/raw", m)
}
func serviceLogs(ctx web.Context) error {
name := ctx.P("name")
line := cast.ToIntD(ctx.Q("line"), 500)
timestamps := cast.ToBoolD(ctx.Q("timestamps"), false)
stdout, stderr, err := docker.ServiceLogs(name, line, timestamps)
if err != nil {
return err
}
m := newModel(ctx).Add("Service", name).Add("Line", line).Add("Timestamps", timestamps).
Add("Stdout", stdout.String()).Add("Stderr", stderr.String())
return ctx.Render("service/logs", m)
}
func serviceDelete(ctx web.Context) error {
names := strings.Split(ctx.F("names"), ",")
for _, name := range names {
if err := docker.ServiceRemove(name); err != nil {
return ajaxResult(ctx, err)
} else {
biz.Event.CreateService(model.EventActionDelete, name, ctx.User())
}
}
return ajaxSuccess(ctx, nil)
}
func serviceNew(ctx web.Context) error {
service := &model.ServiceInfo{}
tid := ctx.Q("template")
if tid != "" {
tpl, err := biz.Template.Get(tid)
if err != nil { if err != nil {
return err return err
} }
j, err := misc.JSONIndent(raw) if tpl != nil {
if err != nil { err = json.Unmarshal([]byte(tpl.Content), service)
return err
}
m := newModel(ctx).Add("Service", name).Add("Raw", j)
return ctx.Render("service/raw", m)
}
c.Logs = func(ctx web.Context) error {
name := ctx.P("name")
line := cast.ToIntD(ctx.Q("line"), 500)
timestamps := cast.ToBoolD(ctx.Q("timestamps"), false)
stdout, stderr, err := docker.ServiceLogs(name, line, timestamps)
if err != nil {
return err
}
m := newModel(ctx).Add("Service", name).Add("Line", line).Add("Timestamps", timestamps).
Add("Stdout", stdout.String()).Add("Stderr", stderr.String())
return ctx.Render("service/logs", m)
}
c.Delete = func(ctx web.Context) error {
names := strings.Split(ctx.F("names"), ",")
for _, name := range names {
if err := docker.ServiceRemove(name); err != nil {
return ajaxResult(ctx, err)
} else {
biz.Event.CreateService(model.EventActionDelete, name, ctx.User())
}
}
return ajaxSuccess(ctx, nil)
}
c.New = func(ctx web.Context) error {
service := &model.ServiceInfo{}
tid := ctx.Q("template")
if tid != "" {
tpl, err := biz.Template.Get(tid)
if err != nil { if err != nil {
return err return err
} }
if tpl != nil { if service.Registry != "" {
err = json.Unmarshal([]byte(tpl.Content), service) var registry *model.Registry
registry, err = biz.Registry.Get(service.Registry)
if err != nil { if err != nil {
return err return err
} }
service.RegistryURL = registry.URL
if service.Registry != "" {
registry, err := biz.Registry.Get(service.Registry)
if err != nil {
return err
}
service.RegistryURL = registry.URL
}
} }
} }
networks, err := docker.NetworkList()
if err != nil {
return err
}
secrets, _, err := docker.SecretList("", 1, 100)
if err != nil {
return err
}
configs, _, err := docker.ConfigList("", 1, 100)
if err != nil {
return err
}
registries, err := biz.Registry.List()
if err != nil {
return err
}
checkedNetworks := set.FromSlice(service.Networks, func(i int) interface{} { return service.Networks[i] })
m := newModel(ctx).Add("Service", service).Add("Registries", registries).
Add("Networks", networks).Add("CheckedNetworks", checkedNetworks).
Add("Secrets", secrets).Add("Configs", configs)
return ctx.Render("service/new", m)
} }
c.Create = func(ctx web.Context) error { networks, err := docker.NetworkList()
info := &model.ServiceInfo{} if err != nil {
err := ctx.Bind(info) return err
if err == nil {
if info.Registry != "" {
registry, err := biz.Registry.Get(info.Registry)
if err != nil {
return errors.Wrap("Load registry info failed", err)
} else if registry == nil {
return errors.New("Can't load registry info")
}
info.Image = registry.URL + "/" + info.Image
info.RegistryAuth = registry.GetEncodedAuth()
}
err = docker.ServiceCreate(info)
}
if err == nil {
biz.Event.CreateService(model.EventActionCreate, info.Name, ctx.User())
}
return ajaxResult(ctx, err)
} }
secrets, _, err := docker.SecretList("", 1, 100)
c.Edit = func(ctx web.Context) error { if err != nil {
name := ctx.P("name") return err
service, _, err := docker.ServiceInspect(name)
if err != nil {
return err
}
networks, err := docker.NetworkList()
if err != nil {
return err
}
secrets, _, err := docker.SecretList("", 1, 100)
if err != nil {
return err
}
configs, _, err := docker.ConfigList("", 1, 100)
if err != nil {
return err
}
checkedNetworks := set.FromSlice(service.Endpoint.VirtualIPs, func(i int) interface{} { return service.Endpoint.VirtualIPs[i].NetworkID })
m := newModel(ctx).Add("Service", model.NewServiceInfo(service)).
Add("Networks", networks).Add("CheckedNetworks", checkedNetworks).
Add("Secrets", secrets).Add("Configs", configs)
return ctx.Render("service/edit", m)
} }
configs, _, err := docker.ConfigList("", 1, 100)
c.Update = func(ctx web.Context) error { if err != nil {
info := &model.ServiceInfo{} return err
err := ctx.Bind(info)
if err == nil {
info.Name = ctx.P("name")
err = docker.ServiceUpdate(info)
}
if err == nil {
biz.Event.CreateService(model.EventActionUpdate, info.Name, ctx.User())
}
return ajaxResult(ctx, err)
} }
registries, err := biz.Registry.List()
c.Scale = func(ctx web.Context) error { if err != nil {
name := ctx.F("name") return err
count, err := strconv.Atoi(ctx.F("count"))
if err != nil {
return err
}
err = docker.ServiceScale(name, uint64(count))
if err == nil {
biz.Event.CreateService(model.EventActionScale, name, ctx.User())
}
return ajaxResult(ctx, err)
} }
checkedNetworks := set.FromSlice(service.Networks, func(i int) interface{} { return service.Networks[i] })
return m := newModel(ctx).Add("Service", service).Add("Registries", registries).
Add("Networks", networks).Add("CheckedNetworks", checkedNetworks).
Add("Secrets", secrets).Add("Configs", configs)
return ctx.Render("service/new", m)
}
func serviceCreate(ctx web.Context) error {
info := &model.ServiceInfo{}
err := ctx.Bind(info)
if err != nil {
return err
}
if info.Registry != "" {
var registry *model.Registry
registry, err = biz.Registry.Get(info.Registry)
if err != nil {
return errors.Wrap("Load registry info failed", err)
} else if registry == nil {
return errors.New("Can't load registry info")
}
info.Image = registry.URL + "/" + info.Image
info.RegistryAuth = registry.GetEncodedAuth()
}
if err = docker.ServiceCreate(info); err == nil {
biz.Event.CreateService(model.EventActionCreate, info.Name, ctx.User())
}
return ajaxResult(ctx, err)
}
func serviceEdit(ctx web.Context) error {
name := ctx.P("name")
service, _, err := docker.ServiceInspect(name)
if err != nil {
return err
}
networks, err := docker.NetworkList()
if err != nil {
return err
}
secrets, _, err := docker.SecretList("", 1, 100)
if err != nil {
return err
}
configs, _, err := docker.ConfigList("", 1, 100)
if err != nil {
return err
}
checkedNetworks := set.FromSlice(service.Endpoint.VirtualIPs, func(i int) interface{} { return service.Endpoint.VirtualIPs[i].NetworkID })
m := newModel(ctx).Add("Service", model.NewServiceInfo(service)).
Add("Networks", networks).Add("CheckedNetworks", checkedNetworks).
Add("Secrets", secrets).Add("Configs", configs)
return ctx.Render("service/edit", m)
}
func serviceUpdate(ctx web.Context) error {
info := &model.ServiceInfo{}
err := ctx.Bind(info)
if err == nil {
info.Name = ctx.P("name")
err = docker.ServiceUpdate(info)
}
if err == nil {
biz.Event.CreateService(model.EventActionUpdate, info.Name, ctx.User())
}
return ajaxResult(ctx, err)
}
func serviceScale(ctx web.Context) error {
name := ctx.F("name")
count, err := strconv.Atoi(ctx.F("count"))
if err != nil {
return err
}
err = docker.ServiceScale(name, uint64(count))
if err == nil {
biz.Event.CreateService(model.EventActionScale, name, ctx.User())
}
return ajaxResult(ctx, err)
} }

View File

@ -6,32 +6,35 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// SettingController is a controller of system setting
type SettingController struct { type SettingController struct {
Index web.HandlerFunc `path:"/" name:"setting.edit" authorize:"!" desc:"settings edit page"` Index web.HandlerFunc `path:"/" name:"setting.edit" authorize:"!" desc:"settings edit page"`
Update web.HandlerFunc `path:"/" name:"setting.update" method:"post" authorize:"!" desc:"update settings"` Update web.HandlerFunc `path:"/" name:"setting.update" method:"post" authorize:"!" desc:"update settings"`
} }
// Setting creates an instance of SettingController
func Setting() (c *SettingController) { func Setting() (c *SettingController) {
c = &SettingController{} return &SettingController{
Index: settingIndex,
c.Index = func(ctx web.Context) error { Update: settingUpdate,
setting, err := biz.Setting.Get()
if err != nil {
return err
}
m := newModel(ctx).Add("Setting", setting)
return ctx.Render("system/setting/index", m)
} }
}
c.Update = func(ctx web.Context) error {
setting := &model.Setting{} func settingIndex(ctx web.Context) error {
err := ctx.Bind(setting) setting, err := biz.Setting.Get()
if err == nil { if err != nil {
err = biz.Setting.Update(setting, ctx.User()) return err
} }
return ajaxResult(ctx, err)
} m := newModel(ctx).Add("Setting", setting)
return ctx.Render("system/setting/index", m)
return }
func settingUpdate(ctx web.Context) error {
setting := &model.Setting{}
err := ctx.Bind(setting)
if err == nil {
err = biz.Setting.Update(setting, ctx.User())
}
return ajaxResult(ctx, err)
} }

View File

@ -8,6 +8,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// StackController is a controller of docker stack(compose)
type StackController struct { type StackController struct {
TaskList web.HandlerFunc `path:"/task/" name:"stack.task.list" authorize:"!" desc:"stack task list page"` TaskList web.HandlerFunc `path:"/task/" name:"stack.task.list" authorize:"!" desc:"stack task list page"`
TaskDelete web.HandlerFunc `path:"/task/delete" method:"post" name:"stack.task.delete" authorize:"!" desc:"delete stack task"` TaskDelete web.HandlerFunc `path:"/task/delete" method:"post" name:"stack.task.delete" authorize:"!" desc:"delete stack task"`
@ -21,157 +22,167 @@ type StackController struct {
ArchiveCreate web.HandlerFunc `path:"/archive/new" method:"post" name:"stack.archive.create" authorize:"!" desc:"create stack.archive"` ArchiveCreate web.HandlerFunc `path:"/archive/new" method:"post" name:"stack.archive.create" authorize:"!" desc:"create stack.archive"`
} }
// Stack creates an instance of StackController
func Stack() (c *StackController) { func Stack() (c *StackController) {
c = &StackController{} return &StackController{
TaskList: stackTaskList,
TaskDelete: stackTaskDelete,
ArchiveList: stackArchiveList,
ArchiveDetail: stackArchiveDetail,
ArchiveEdit: stackArchiveEdit,
ArchiveUpdate: stackArchiveUpdate,
ArchiveDelete: stackArchiveDelete,
ArchiveDeploy: stackArchiveDeploy,
ArchiveNew: stackArchiveNew,
ArchiveCreate: stackArchiveCreate,
}
}
c.TaskList = func(ctx web.Context) error { func stackTaskList(ctx web.Context) error {
stacks, err := docker.StackList() stacks, err := docker.StackList()
if err != nil { if err != nil {
return err return err
}
m := newModel(ctx).Add("Stacks", stacks)
return ctx.Render("stack/task/list", m)
} }
c.TaskDelete = func(ctx web.Context) error { m := newModel(ctx).Add("Stacks", stacks)
name := ctx.F("name") return ctx.Render("stack/task/list", m)
err := docker.StackRemove(name) }
if err == nil {
biz.Event.CreateStackTask(model.EventActionDelete, name, ctx.User()) func stackTaskDelete(ctx web.Context) error {
} name := ctx.F("name")
return ajaxResult(ctx, err) err := docker.StackRemove(name)
if err == nil {
biz.Event.CreateStackTask(model.EventActionDelete, name, ctx.User())
}
return ajaxResult(ctx, err)
}
func stackArchiveList(ctx web.Context) error {
args := &model.ArchiveListArgs{}
err := ctx.Bind(args)
if err != nil {
return err
}
args.PageSize = model.PageSize
if args.PageIndex == 0 {
args.PageIndex = 1
} }
c.ArchiveList = func(ctx web.Context) error { archives, totalCount, err := biz.Archive.List(args)
args := &model.ArchiveListArgs{} if err != nil {
err := ctx.Bind(args) return err
if err != nil {
return err
}
args.PageSize = model.PageSize
if args.PageIndex == 0 {
args.PageIndex = 1
}
archives, totalCount, err := biz.Archive.List(args)
if err != nil {
return err
}
m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
Add("Name", args.Name).
Add("Archives", archives)
return ctx.Render("stack/archive/list", m)
} }
c.ArchiveDetail = func(ctx web.Context) error { m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
id := ctx.P("id") Add("Name", args.Name).
archive, err := biz.Archive.Get(id) Add("Archives", archives)
if err != nil { return ctx.Render("stack/archive/list", m)
return err }
}
if archive == nil {
return web.ErrNotFound
}
m := newModel(ctx).Add("Archive", archive) func stackArchiveDetail(ctx web.Context) error {
return ctx.Render("stack/archive/detail", m) id := ctx.P("id")
archive, err := biz.Archive.Get(id)
if err != nil {
return err
}
if archive == nil {
return web.ErrNotFound
} }
c.ArchiveEdit = func(ctx web.Context) error { m := newModel(ctx).Add("Archive", archive)
id := ctx.P("id") return ctx.Render("stack/archive/detail", m)
archive, err := biz.Archive.Get(id) }
if err != nil {
return err
}
if archive == nil {
return web.ErrNotFound
}
m := newModel(ctx).Add("Archive", archive) func stackArchiveEdit(ctx web.Context) error {
return ctx.Render("stack/archive/edit", m) id := ctx.P("id")
archive, err := biz.Archive.Get(id)
if err != nil {
return err
}
if archive == nil {
return web.ErrNotFound
} }
c.ArchiveUpdate = func(ctx web.Context) error { m := newModel(ctx).Add("Archive", archive)
archive := &model.Archive{} return ctx.Render("stack/archive/edit", m)
err := ctx.Bind(archive) }
if err == nil {
// Validate format
_, err = compose.Parse(archive.Name, archive.Content)
if err != nil {
return err
}
archive.UpdatedBy = ctx.User().ID() func stackArchiveUpdate(ctx web.Context) error {
err = biz.Archive.Update(archive) archive := &model.Archive{}
err := ctx.Bind(archive)
if err == nil {
// Validate format
_, err = compose.Parse(archive.Name, archive.Content)
if err != nil {
return err
} }
if err == nil {
biz.Event.CreateStackArchive(model.EventActionUpdate, archive.ID, archive.Name, ctx.User()) archive.UpdatedBy = ctx.User().ID()
} err = biz.Archive.Update(archive)
return ajaxResult(ctx, err) }
if err == nil {
biz.Event.CreateStackArchive(model.EventActionUpdate, archive.ID, archive.Name, ctx.User())
}
return ajaxResult(ctx, err)
}
func stackArchiveDelete(ctx web.Context) error {
id := ctx.F("id")
err := biz.Archive.Delete(id, ctx.User())
return ajaxResult(ctx, err)
}
func stackArchiveDeploy(ctx web.Context) error {
id := ctx.F("id")
archive, err := biz.Archive.Get(id)
if err != nil {
return err
} }
c.ArchiveDelete = func(ctx web.Context) error { cfg, err := compose.Parse(archive.Name, archive.Content)
id := ctx.F("id") if err != nil {
err := biz.Archive.Delete(id, ctx.User()) return err
return ajaxResult(ctx, err)
} }
c.ArchiveDeploy = func(ctx web.Context) error { registries, err := biz.Registry.List()
id := ctx.F("id") if err != nil {
archive, err := biz.Archive.Get(id) return err
if err != nil { }
return err
}
cfg, err := compose.Parse(archive.Name, archive.Content) // Find auth info from registry
if err != nil { authes := map[string]string{}
return err for _, sc := range cfg.Services {
} if _, ok := authes[sc.Image]; !ok {
for _, r := range registries {
registries, err := biz.Registry.List() if r.Match(sc.Image) {
if err != nil { authes[sc.Image] = r.GetEncodedAuth()
return err
}
// Find auth info from registry
authes := map[string]string{}
for _, sc := range cfg.Services {
if _, ok := authes[sc.Image]; !ok {
for _, r := range registries {
if r.Match(sc.Image) {
authes[sc.Image] = r.GetEncodedAuth()
}
} }
} }
} }
err = docker.StackDeploy(archive.Name, archive.Content, authes)
return ajaxResult(ctx, err)
} }
c.ArchiveNew = func(ctx web.Context) error { err = docker.StackDeploy(archive.Name, archive.Content, authes)
m := newModel(ctx) return ajaxResult(ctx, err)
return ctx.Render("stack/archive/new", m) }
}
func stackArchiveNew(ctx web.Context) error {
c.ArchiveCreate = func(ctx web.Context) error { m := newModel(ctx)
archive := &model.Archive{} return ctx.Render("stack/archive/new", m)
err := ctx.Bind(archive) }
if err == nil {
// Validate format func stackArchiveCreate(ctx web.Context) error {
_, err = compose.Parse(archive.Name, archive.Content) archive := &model.Archive{}
if err != nil { err := ctx.Bind(archive)
return err if err == nil {
} // Validate format
_, err = compose.Parse(archive.Name, archive.Content)
archive.CreatedBy = ctx.User().ID() if err != nil {
archive.UpdatedBy = archive.CreatedBy return err
err = biz.Archive.Create(archive) }
}
return ajaxResult(ctx, err) archive.CreatedBy = ctx.User().ID()
} archive.UpdatedBy = archive.CreatedBy
err = biz.Archive.Create(archive)
return }
return ajaxResult(ctx, err)
} }

View File

@ -6,40 +6,43 @@ import (
"github.com/cuigh/swirl/misc" "github.com/cuigh/swirl/misc"
) )
// TaskController is a controller of swarm task
type TaskController struct { type TaskController struct {
Detail web.HandlerFunc `path:"/:id/detail" name:"task.detail" authorize:"!" desc:"task detail page"` 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"` Raw web.HandlerFunc `path:"/:id/raw" name:"task.raw" authorize:"!" desc:"task raw page"`
} }
// Task creates an instance of TaskController
func Task() (c *TaskController) { func Task() (c *TaskController) {
c = &TaskController{} return &TaskController{
Detail: taskDetail,
c.Detail = func(ctx web.Context) error { Raw: taskRaw,
id := ctx.P("id")
task, _, err := docker.TaskInspect(id)
if err != nil {
return err
}
m := newModel(ctx).Add("Task", task)
return ctx.Render("task/detail", m)
} }
}
c.Raw = func(ctx web.Context) error {
id := ctx.P("id") func taskDetail(ctx web.Context) error {
task, raw, err := docker.TaskInspect(id) id := ctx.P("id")
if err != nil { task, _, err := docker.TaskInspect(id)
return err if err != nil {
} return err
}
j, err := misc.JSONIndent(raw)
if err != nil { m := newModel(ctx).Add("Task", task)
return err return ctx.Render("task/detail", m)
} }
m := newModel(ctx).Add("Task", task).Add("Raw", j) func taskRaw(ctx web.Context) error {
return ctx.Render("task/raw", m) id := ctx.P("id")
} task, raw, err := docker.TaskInspect(id)
if err != nil {
return return err
}
j, err := misc.JSONIndent(raw)
if err != nil {
return err
}
m := newModel(ctx).Add("Task", task).Add("Raw", j)
return ctx.Render("task/raw", m)
} }

View File

@ -10,6 +10,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// TemplateController is a controller of service template
type TemplateController struct { type TemplateController struct {
List web.HandlerFunc `path:"/" name:"template.list" authorize:"!" desc:"service template list page"` List web.HandlerFunc `path:"/" name:"template.list" authorize:"!" desc:"service template list page"`
New web.HandlerFunc `path:"/new" name:"template.new" authorize:"!" desc:"new service template page"` New web.HandlerFunc `path:"/new" name:"template.new" authorize:"!" desc:"new service template page"`
@ -19,6 +20,7 @@ type TemplateController struct {
Delete web.HandlerFunc `path:"/delete" method:"post" name:"template.delete" authorize:"!" desc:"delete service template"` Delete web.HandlerFunc `path:"/delete" method:"post" name:"template.delete" authorize:"!" desc:"delete service template"`
} }
// Template creates an instance of TemplateController
func Template() (c *TemplateController) { func Template() (c *TemplateController) {
return &TemplateController{ return &TemplateController{
List: templateList, List: templateList,
@ -80,10 +82,13 @@ func templateCreate(ctx web.Context) error {
info := &model.ServiceInfo{} info := &model.ServiceInfo{}
err := ctx.Bind(info) err := ctx.Bind(info)
if err == nil { if err == nil {
tpl := &model.Template{Name: info.Name} var (
content []byte
tpl = &model.Template{Name: info.Name}
)
info.Name = "" info.Name = ""
content, err := json.Marshal(info) content, err = json.Marshal(info)
if err != nil { if err != nil {
return err return err
} }
@ -111,7 +116,8 @@ func templateEdit(ctx web.Context) error {
service.Name = tpl.Name service.Name = tpl.Name
if service.Registry != "" { if service.Registry != "" {
registry, err := biz.Registry.Get(service.Registry) var registry *model.Registry
registry, err = biz.Registry.Get(service.Registry)
if err != nil { if err != nil {
return err return err
} }
@ -146,13 +152,16 @@ func templateUpdate(ctx web.Context) error {
info := &model.ServiceInfo{} info := &model.ServiceInfo{}
err := ctx.Bind(info) err := ctx.Bind(info)
if err == nil { if err == nil {
tpl := &model.Template{ var (
ID: ctx.P("id"), content []byte
Name: info.Name, tpl = &model.Template{
} ID: ctx.P("id"),
Name: info.Name,
}
)
info.Name = "" info.Name = ""
content, err := json.Marshal(info) content, err = json.Marshal(info)
if err != nil { if err != nil {
return err return err
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// UserController is a controller of user
type UserController struct { type UserController struct {
Index web.HandlerFunc `path:"/" name:"user.list" authorize:"!" desc:"user list page"` Index web.HandlerFunc `path:"/" name:"user.list" authorize:"!" desc:"user list page"`
New web.HandlerFunc `path:"/new" name:"user.new" authorize:"!" desc:"new user page"` New web.HandlerFunc `path:"/new" name:"user.new" authorize:"!" desc:"new user page"`
@ -19,133 +20,142 @@ type UserController struct {
Delete web.HandlerFunc `path:"/delete" method:"post" name:"user.delete" authorize:"!" desc:"delete user"` Delete web.HandlerFunc `path:"/delete" method:"post" name:"user.delete" authorize:"!" desc:"delete user"`
} }
// User creates an instance of UserController
func User() (c *UserController) { func User() (c *UserController) {
c = &UserController{} return &UserController{
Index: userIndex,
New: userNew,
Create: userCreate,
Detail: userDetail,
Edit: userEdit,
Update: userUpdate,
Block: userBlock,
Unblock: userUnblock,
Delete: userDelete,
}
}
c.Index = func(ctx web.Context) error { func userIndex(ctx web.Context) error {
args := &model.UserListArgs{} args := &model.UserListArgs{}
err := ctx.Bind(args) err := ctx.Bind(args)
if err != nil { if err != nil {
return err return err
} }
args.PageSize = model.PageSize args.PageSize = model.PageSize
if args.PageIndex == 0 { if args.PageIndex == 0 {
args.PageIndex = 1 args.PageIndex = 1
}
users, totalCount, err := biz.User.List(args)
if err != nil {
return err
}
m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
Add("Query", args.Query).
Add("Filter", args.Filter).
Add("Users", users)
return ctx.Render("system/user/list", m)
} }
c.New = func(ctx web.Context) error { users, totalCount, err := biz.User.List(args)
roles, err := biz.Role.List() if err != nil {
if err != nil { return err
return err
}
m := newModel(ctx).Add("Roles", roles)
return ctx.Render("system/user/new", m)
} }
c.Create = func(ctx web.Context) error { m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
user := &model.User{} Add("Query", args.Query).
err := ctx.Bind(user) Add("Filter", args.Filter).
if err == nil { Add("Users", users)
user.Type = model.UserTypeInternal return ctx.Render("system/user/list", m)
err = biz.User.Create(user, ctx.User()) }
}
return ajaxResult(ctx, err) func userNew(ctx web.Context) error {
roles, err := biz.Role.List()
if err != nil {
return err
} }
c.Detail = func(ctx web.Context) error { m := newModel(ctx).Add("Roles", roles)
name := ctx.P("name") return ctx.Render("system/user/new", m)
user, err := biz.User.GetByName(name) }
if err != nil {
return err
}
if user == nil {
return web.ErrNotFound
}
var ( func userCreate(ctx web.Context) error {
roles map[string]string user := &model.User{}
role *model.Role err := ctx.Bind(user)
) if err == nil {
if len(user.Roles) > 0 { user.Type = model.UserTypeInternal
roles = map[string]string{} err = biz.User.Create(user, ctx.User())
for _, id := range user.Roles { }
role, err = biz.Role.Get(id) return ajaxResult(ctx, err)
if err != nil { }
return err
} else if role != nil { func userDetail(ctx web.Context) error {
roles[id] = role.Name name := ctx.P("name")
} else { user, err := biz.User.GetByName(name)
log.Get("user").Warnf("Role %v is invalid", id) if err != nil {
} return err
}
if user == nil {
return web.ErrNotFound
}
var (
roles map[string]string
role *model.Role
)
if len(user.Roles) > 0 {
roles = map[string]string{}
for _, id := range user.Roles {
role, err = biz.Role.Get(id)
if err != nil {
return err
} else if role != nil {
roles[id] = role.Name
} else {
log.Get("user").Warnf("Role %v is invalid", id)
} }
} }
m := newModel(ctx).Add("User", user).Add("Roles", roles)
return ctx.Render("system/user/detail", m)
} }
c.Edit = func(ctx web.Context) error { m := newModel(ctx).Add("User", user).Add("Roles", roles)
name := ctx.P("name") return ctx.Render("system/user/detail", m)
user, err := biz.User.GetByName(name) }
if err != nil {
return err func userEdit(ctx web.Context) error {
} name := ctx.P("name")
if user == nil { user, err := biz.User.GetByName(name)
return web.ErrNotFound if err != nil {
} return err
}
roles, err := biz.Role.List() if user == nil {
if err != nil { return web.ErrNotFound
return err }
}
roles, err := biz.Role.List()
userRoles := make(map[string]struct{}) if err != nil {
for _, id := range user.Roles { return err
userRoles[id] = model.Placeholder }
}
m := newModel(ctx).Add("User", user).Add("Roles", roles).Add("UserRoles", userRoles) userRoles := make(map[string]struct{})
return ctx.Render("system/user/edit", m) for _, id := range user.Roles {
} userRoles[id] = model.Placeholder
}
c.Update = func(ctx web.Context) error { m := newModel(ctx).Add("User", user).Add("Roles", roles).Add("UserRoles", userRoles)
user := &model.User{} return ctx.Render("system/user/edit", m)
err := ctx.Bind(user) }
if err == nil {
err = biz.User.Update(user, ctx.User()) func userUpdate(ctx web.Context) error {
} user := &model.User{}
return ajaxResult(ctx, err) err := ctx.Bind(user)
} if err == nil {
err = biz.User.Update(user, ctx.User())
c.Block = func(ctx web.Context) error { }
id := ctx.F("id") return ajaxResult(ctx, err)
err := biz.User.Block(id) }
return ajaxResult(ctx, err)
} func userBlock(ctx web.Context) error {
id := ctx.F("id")
c.Unblock = func(ctx web.Context) error { err := biz.User.Block(id)
id := ctx.F("id") return ajaxResult(ctx, err)
err := biz.User.Unblock(id) }
return ajaxResult(ctx, err)
} func userUnblock(ctx web.Context) error {
id := ctx.F("id")
c.Delete = func(ctx web.Context) error { err := biz.User.Unblock(id)
id := ctx.F("id") return ajaxResult(ctx, err)
err := biz.User.Delete(id) }
return ajaxResult(ctx, err)
} func userDelete(ctx web.Context) error {
id := ctx.F("id")
return err := biz.User.Delete(id)
return ajaxResult(ctx, err)
} }

View File

@ -11,6 +11,7 @@ import (
"github.com/cuigh/swirl/model" "github.com/cuigh/swirl/model"
) )
// VolumeController is a controller of docker volume
type VolumeController struct { type VolumeController struct {
List web.HandlerFunc `path:"/" name:"volume.list" authorize:"!" desc:"volume list page"` List web.HandlerFunc `path:"/" name:"volume.list" authorize:"!" desc:"volume list page"`
New web.HandlerFunc `path:"/new" name:"volume.new" authorize:"!" desc:"new volume page"` New web.HandlerFunc `path:"/new" name:"volume.new" authorize:"!" desc:"new volume page"`
@ -21,86 +22,93 @@ type VolumeController struct {
Raw web.HandlerFunc `path:"/:name/raw" name:"volume.raw" authorize:"!" desc:"volume raw page"` Raw web.HandlerFunc `path:"/:name/raw" name:"volume.raw" authorize:"!" desc:"volume raw page"`
} }
// Volume creates an instance of VolumeController
func Volume() (c *VolumeController) { func Volume() (c *VolumeController) {
c = &VolumeController{} return &VolumeController{
List: volumeList,
c.List = func(ctx web.Context) error { New: volumeNew,
//name := ctx.Q("name") Create: volumeCreate,
page := cast.ToIntD(ctx.Q("page"), 1) Delete: volumeDelete,
volumes, totalCount, err := docker.VolumeList(page, model.PageSize) Prune: volumePrune,
if err != nil { Detail: volumeDetail,
return err Raw: volumeRaw,
}
m := newPagerModel(ctx, totalCount, model.PageSize, page).
//Add("Name", name).
Add("Volumes", volumes)
return ctx.Render("volume/list", m)
} }
}
c.New = func(ctx web.Context) error {
m := newModel(ctx) func volumeList(ctx web.Context) error {
return ctx.Render("/volume/new", m) //name := ctx.Q("name")
} page := cast.ToIntD(ctx.Q("page"), 1)
volumes, totalCount, err := docker.VolumeList(page, model.PageSize)
c.Create = func(ctx web.Context) error { if err != nil {
info := &model.VolumeCreateInfo{} return err
err := ctx.Bind(info) }
if err != nil {
return err m := newPagerModel(ctx, totalCount, model.PageSize, page).
} //Add("Name", name).
err = docker.VolumeCreate(info) Add("Volumes", volumes)
if err == nil { return ctx.Render("volume/list", m)
biz.Event.CreateVolume(model.EventActionCreate, info.Name, ctx.User()) }
}
return ajaxResult(ctx, err) func volumeNew(ctx web.Context) error {
} m := newModel(ctx)
return ctx.Render("/volume/new", m)
c.Delete = func(ctx web.Context) error { }
names := strings.Split(ctx.F("names"), ",")
for _, name := range names { func volumeCreate(ctx web.Context) error {
if err := docker.VolumeRemove(name); err != nil { info := &model.VolumeCreateInfo{}
return ajaxResult(ctx, err) err := ctx.Bind(info)
} else { if err != nil {
biz.Event.CreateVolume(model.EventActionDelete, name, ctx.User()) return err
} }
} err = docker.VolumeCreate(info)
return ajaxSuccess(ctx, nil) if err == nil {
} biz.Event.CreateVolume(model.EventActionCreate, info.Name, ctx.User())
}
c.Prune = func(ctx web.Context) error { return ajaxResult(ctx, err)
report, err := docker.VolumePrune() }
if err == nil {
return ajaxSuccess(ctx, report) func volumeDelete(ctx web.Context) error {
} names := strings.Split(ctx.F("names"), ",")
return ajaxResult(ctx, err) for _, name := range names {
} if err := docker.VolumeRemove(name); err != nil {
return ajaxResult(ctx, err)
c.Detail = func(ctx web.Context) error { } else {
name := ctx.P("name") biz.Event.CreateVolume(model.EventActionDelete, name, ctx.User())
volume, _, err := docker.VolumeInspectRaw(name) }
if err != nil { }
return err return ajaxSuccess(ctx, nil)
} }
m := newModel(ctx).Add("Volume", volume)
return ctx.Render("volume/detail", m) func volumePrune(ctx web.Context) error {
} report, err := docker.VolumePrune()
if err == nil {
c.Raw = func(ctx web.Context) error { return ajaxSuccess(ctx, report)
name := ctx.P("name") }
_, raw, err := docker.VolumeInspectRaw(name) return ajaxResult(ctx, err)
if err != nil { }
return err
} func volumeDetail(ctx web.Context) error {
name := ctx.P("name")
j, err := misc.JSONIndent(raw) volume, _, err := docker.VolumeInspectRaw(name)
if err != nil { if err != nil {
return err return err
} }
m := newModel(ctx).Add("Volume", volume)
m := newModel(ctx).Add("Volume", name).Add("Raw", j) return ctx.Render("volume/detail", m)
return ctx.Render("volume/raw", m) }
}
func volumeRaw(ctx web.Context) error {
return name := ctx.P("name")
_, raw, err := docker.VolumeInspectRaw(name)
if err != nil {
return err
}
j, err := misc.JSONIndent(raw)
if err != nil {
return err
}
m := newModel(ctx).Add("Volume", name).Add("Raw", j)
return ctx.Render("volume/raw", m)
} }