swirl/controller/stack.go

178 lines
4.3 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"
"github.com/cuigh/swirl/biz/docker"
"github.com/cuigh/swirl/biz/docker/compose"
"github.com/cuigh/swirl/model"
)
2017-10-11 03:26:03 +00:00
// StackController is a controller of docker stack(compose)
2017-09-26 12:50:09 +00:00
type StackController struct {
2018-04-16 09:21:20 +00:00
List web.HandlerFunc `path:"/" name:"stack.list" authorize:"!" desc:"stack list page"`
New web.HandlerFunc `path:"/new" name:"stack.new" authorize:"!" desc:"new stack page"`
Create web.HandlerFunc `path:"/new" method:"post" name:"stack.create" authorize:"!" desc:"create stack"`
Detail web.HandlerFunc `path:"/:name/detail" name:"stack.detail" authorize:"!" desc:"stack detail page"`
Edit web.HandlerFunc `path:"/:name/edit" name:"stack.edit" authorize:"!" desc:"stack edit page"`
Update web.HandlerFunc `path:"/:name/update" method:"post" name:"stack.update" authorize:"!" desc:"update stack"`
Deploy web.HandlerFunc `path:"/:name/deploy" method:"post" name:"stack.deploy" authorize:"!" desc:"deploy stack"`
Shutdown web.HandlerFunc `path:"/:name/shutdown" method:"post" name:"stack.shutdown" authorize:"!" desc:"shutdown stack"`
Delete web.HandlerFunc `path:"/:name/delete" method:"post" name:"stack.delete" authorize:"!" desc:"delete stack"`
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
// Stack creates an instance of StackController
2017-09-26 12:50:09 +00:00
func Stack() (c *StackController) {
2017-10-11 03:26:03 +00:00
return &StackController{
2018-04-16 09:21:20 +00:00
List: stackList,
New: stackNew,
Create: stackCreate,
Detail: stackDetail,
Edit: stackEdit,
Update: stackUpdate,
Deploy: stackDeploy,
Shutdown: stackShutdown,
Delete: stackDelete,
2017-10-11 03:26:03 +00:00
}
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func stackList(ctx web.Context) error {
args := &model.StackListArgs{}
err := ctx.Bind(args)
if err != nil {
return err
}
stacks, err := biz.Stack.List(args)
2017-10-11 03:26:03 +00:00
if err != nil {
return err
}
2018-04-16 09:21:20 +00:00
m := newModel(ctx).Set("Stacks", stacks).
Set("Name", args.Name).
Set("Filter", args.Filter)
return ctx.Render("stack/list", m)
2017-10-11 03:26:03 +00:00
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func stackNew(ctx web.Context) error {
m := newModel(ctx)
return ctx.Render("stack/new", m)
2017-10-11 03:26:03 +00:00
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func stackCreate(ctx web.Context) error {
stack := &model.Stack{}
err := ctx.Bind(stack)
2017-10-11 03:26:03 +00:00
if err != nil {
return err
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
// Validate format
_, err = compose.Parse(stack.Name, stack.Content)
2017-10-11 03:26:03 +00:00
if err != nil {
return err
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
stack.CreatedBy = ctx.User().ID()
stack.UpdatedBy = stack.CreatedBy
err = biz.Stack.Create(stack, ctx.User())
return ajaxResult(ctx, err)
2017-10-11 03:26:03 +00:00
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func stackDetail(ctx web.Context) error {
name := ctx.P("name")
stack, err := biz.Stack.Get(name)
2017-10-11 03:26:03 +00:00
if err != nil {
return err
}
2018-04-16 09:21:20 +00:00
if stack == nil {
2017-10-11 03:26:03 +00:00
return web.ErrNotFound
2017-09-26 12:50:09 +00:00
}
2018-04-16 09:21:20 +00:00
m := newModel(ctx).Set("Stack", stack)
return ctx.Render("stack/detail", m)
2017-10-11 03:26:03 +00:00
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func stackEdit(ctx web.Context) error {
name := ctx.P("name")
stack, err := biz.Stack.Get(name)
2017-10-11 03:26:03 +00:00
if err != nil {
return err
2017-09-26 12:50:09 +00:00
}
2018-04-16 09:21:20 +00:00
if stack == nil {
2017-10-11 03:26:03 +00:00
return web.ErrNotFound
}
2018-04-16 09:21:20 +00:00
m := newModel(ctx).Set("Stack", stack)
return ctx.Render("stack/edit", m)
2017-10-11 03:26:03 +00:00
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func stackUpdate(ctx web.Context) error {
stack := &model.Stack{}
err := ctx.Bind(stack)
2017-10-11 03:26:03 +00:00
if err == nil {
// Validate format
2018-04-16 09:21:20 +00:00
_, err = compose.Parse(stack.Name, stack.Content)
2017-09-26 12:50:09 +00:00
if err != nil {
return err
}
2018-04-16 09:21:20 +00:00
stack.UpdatedBy = ctx.User().ID()
err = biz.Stack.Update(stack, ctx.User())
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
return ajaxResult(ctx, err)
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func stackDeploy(ctx web.Context) error {
name := ctx.P("name")
stack, err := biz.Stack.Get(name)
2017-10-11 03:26:03 +00:00
if err != nil {
return err
2017-09-26 12:50:09 +00:00
}
2018-04-16 09:21:20 +00:00
cfg, err := compose.Parse(stack.Name, stack.Content)
2017-10-11 03:26:03 +00:00
if err != nil {
return err
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
registries, err := biz.Registry.List()
if err != nil {
return err
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
// 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()
2017-09-26 12:50:09 +00:00
}
}
}
}
2018-04-16 09:21:20 +00:00
err = docker.StackDeploy(stack.Name, stack.Content, authes)
if err == nil {
biz.Event.CreateStack(model.EventActionDeploy, name, ctx.User())
}
2017-10-11 03:26:03 +00:00
return ajaxResult(ctx, err)
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func stackShutdown(ctx web.Context) error {
name := ctx.P("name")
err := docker.StackRemove(name)
if err == nil {
biz.Event.CreateStack(model.EventActionShutdown, name, ctx.User())
}
2018-04-16 09:21:20 +00:00
return ajaxResult(ctx, err)
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func stackDelete(ctx web.Context) error {
name := ctx.P("name")
err := docker.StackRemove(name)
if err == nil {
err = biz.Stack.Delete(name, ctx.User())
2017-10-11 03:26:03 +00:00
}
return ajaxResult(ctx, err)
2017-09-26 12:50:09 +00:00
}