swirl/biz/stack.go

128 lines
2.5 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package biz
import (
2018-04-16 09:21:20 +00:00
"strings"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/auxo/net/web"
2018-04-16 09:21:20 +00:00
"github.com/cuigh/swirl/biz/docker"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/dao"
"github.com/cuigh/swirl/model"
)
// Stack return a stack biz instance.
2018-04-16 09:21:20 +00:00
var Stack = &stackBiz{}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
type stackBiz struct {
2017-09-26 12:50:09 +00:00
}
2018-04-16 09:21:20 +00:00
func (b *stackBiz) List(args *model.StackListArgs) (stacks []*model.Stack, err error) {
var (
upStacks, internalStacks []*model.Stack
upMap = make(map[string]*model.Stack)
)
// load real stacks
upStacks, err = docker.StackList()
if err != nil {
return
}
for _, stack := range upStacks {
upMap[stack.Name] = stack
}
// load stack definitions
2017-09-26 12:50:09 +00:00
do(func(d dao.Interface) {
2018-04-16 09:21:20 +00:00
internalStacks, err = d.StackList()
2017-09-26 12:50:09 +00:00
})
2018-04-16 09:21:20 +00:00
if err != nil {
return
}
// merge stacks and definitions
for _, stack := range internalStacks {
stack.Internal = true
if s, ok := upMap[stack.Name]; ok {
stack.Services = s.Services
delete(upMap, stack.Name)
}
if !b.filter(stack, args) {
stacks = append(stacks, stack)
}
}
for _, stack := range upMap {
if !b.filter(stack, args) {
stacks = append(stacks, stack)
}
}
2017-09-26 12:50:09 +00:00
return
}
2018-04-16 09:21:20 +00:00
func (b *stackBiz) filter(stack *model.Stack, args *model.StackListArgs) bool {
if args.Name != "" {
if !strings.Contains(strings.ToLower(stack.Name), strings.ToLower(args.Name)) {
return true
}
}
switch args.Filter {
case "up":
if len(stack.Services) == 0 {
return true
}
case "internal":
if !stack.Internal {
return true
}
case "external":
if stack.Internal {
return true
}
}
return false
}
func (b *stackBiz) Create(stack *model.Stack, user web.User) (err error) {
2017-09-26 12:50:09 +00:00
do(func(d dao.Interface) {
2018-04-16 09:21:20 +00:00
err = d.StackCreate(stack)
if err == nil {
Event.CreateStack(model.EventActionCreate, stack.Name, user)
}
2017-09-26 12:50:09 +00:00
})
return
}
2018-04-16 09:21:20 +00:00
func (b *stackBiz) Get(name string) (stack *model.Stack, err error) {
2017-09-26 12:50:09 +00:00
do(func(d dao.Interface) {
2018-04-16 09:21:20 +00:00
stack, err = d.StackGet(name)
})
return
}
2017-09-26 12:50:09 +00:00
2018-04-16 09:21:20 +00:00
func (b *stackBiz) Update(stack *model.Stack, user web.User) (err error) {
do(func(d dao.Interface) {
err = d.StackUpdate(stack)
2017-09-26 12:50:09 +00:00
if err == nil {
2018-04-16 09:21:20 +00:00
Event.CreateStack(model.EventActionUpdate, stack.Name, user)
2017-09-26 12:50:09 +00:00
}
})
return
}
2018-04-16 09:21:20 +00:00
func (b *stackBiz) Delete(name string, user web.User) (err error) {
2017-09-26 12:50:09 +00:00
do(func(d dao.Interface) {
2018-04-16 09:21:20 +00:00
err = d.StackDelete(name)
if err == nil {
Event.CreateStack(model.EventActionDelete, name, user)
}
2017-09-26 12:50:09 +00:00
})
return
}
2018-04-16 09:21:20 +00:00
// Migrate migrates old archives to stack collection.
func (b *stackBiz) Migrate() {
2017-09-26 12:50:09 +00:00
do(func(d dao.Interface) {
2018-04-16 09:21:20 +00:00
d.StackMigrate()
2017-09-26 12:50:09 +00:00
})
}