2021-12-06 12:24:22 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-12-24 03:50:21 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
"github.com/cuigh/auxo/data"
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/biz"
|
2022-01-06 08:54:14 +00:00
|
|
|
"github.com/cuigh/swirl/misc"
|
2021-12-06 12:24:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServiceHandler encapsulates service related handlers.
|
|
|
|
type ServiceHandler struct {
|
|
|
|
Search web.HandlerFunc `path:"/search" auth:"service.view" desc:"search services"`
|
|
|
|
Find web.HandlerFunc `path:"/find" auth:"service.view" desc:"find service by name"`
|
|
|
|
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"service.delete" desc:"delete service"`
|
|
|
|
Restart web.HandlerFunc `path:"/restart" method:"post" auth:"service.restart" desc:"restart service"`
|
|
|
|
Rollback web.HandlerFunc `path:"/rollback" method:"post" auth:"service.rollback" desc:"rollback service"`
|
|
|
|
Scale web.HandlerFunc `path:"/scale" method:"post" auth:"service.edit" desc:"scale service"`
|
|
|
|
Save web.HandlerFunc `path:"/save" method:"post" auth:"service.edit" desc:"create or update service"`
|
|
|
|
Deploy web.HandlerFunc `path:"/deploy" method:"post" auth:"service.deploy" desc:"deploy service"`
|
|
|
|
FetchLogs web.HandlerFunc `path:"/fetch-logs" auth:"service.logs" desc:"fetch logs of service"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates an instance of ServiceHandler
|
|
|
|
func NewService(b biz.ServiceBiz) *ServiceHandler {
|
|
|
|
return &ServiceHandler{
|
|
|
|
Search: serviceSearch(b),
|
|
|
|
Find: serviceFind(b),
|
|
|
|
Delete: serviceDelete(b),
|
|
|
|
Restart: serviceRestart(b),
|
|
|
|
Rollback: serviceRollback(b),
|
|
|
|
Scale: serviceScale(b),
|
|
|
|
Save: serviceSave(b),
|
|
|
|
Deploy: serviceDeploy(b),
|
|
|
|
FetchLogs: serviceFetchLogs(b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceSearch(b biz.ServiceBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
Name string `json:"name" bind:"name"`
|
|
|
|
Mode string `json:"mode" bind:"mode"`
|
|
|
|
PageIndex int `json:"pageIndex" bind:"pageIndex"`
|
|
|
|
PageSize int `json:"pageSize" bind:"pageSize"`
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
var (
|
|
|
|
args = &Args{}
|
|
|
|
services []*biz.ServiceBase
|
|
|
|
total int
|
|
|
|
)
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
services, total, err = b.Search(ctx, args.Name, args.Mode, args.PageIndex, args.PageSize)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, data.Map{
|
2021-12-06 12:24:22 +00:00
|
|
|
"items": services,
|
|
|
|
"total": total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceFind(b biz.ServiceBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
name := c.Query("name")
|
|
|
|
status := c.Query("status") == "true"
|
|
|
|
service, raw, err := b.Find(ctx, name, status)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-12-24 11:52:29 +00:00
|
|
|
} else if service == nil {
|
|
|
|
return web.NewError(http.StatusNotFound)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, data.Map{"service": service, "raw": raw})
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceDelete(b biz.ServiceBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
args := &Args{}
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = b.Delete(ctx, args.Name, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceRestart(b biz.ServiceBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
args := &Args{}
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = b.Restart(ctx, args.Name, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceRollback(b biz.ServiceBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
args := &Args{}
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = b.Rollback(ctx, args.Name, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceScale(b biz.ServiceBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Count uint64 `json:"count"`
|
|
|
|
Version uint64 `json:"version"`
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
args := &Args{}
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = b.Scale(ctx, args.Name, args.Count, args.Version, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceSave(b biz.ServiceBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
2021-12-06 12:24:22 +00:00
|
|
|
s := &biz.Service{}
|
2022-01-06 08:54:14 +00:00
|
|
|
err := c.Bind(s, true)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
if s.ID == "" {
|
2022-01-06 08:54:14 +00:00
|
|
|
err = b.Create(ctx, s, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
} else {
|
2022-01-06 08:54:14 +00:00
|
|
|
err = b.Update(ctx, s, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceDeploy(b biz.ServiceBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
//mode := c.Query("mode") // update/replace
|
2021-12-06 12:24:22 +00:00
|
|
|
service := &biz.Service{}
|
2022-01-06 08:54:14 +00:00
|
|
|
err := c.Bind(service, true)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
var s *biz.Service
|
2022-01-06 08:54:14 +00:00
|
|
|
if s, _, err = b.Find(ctx, service.Name, false); err != nil {
|
2021-12-06 12:24:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s == nil {
|
2022-01-06 08:54:14 +00:00
|
|
|
err = b.Create(ctx, service, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
} else {
|
2022-01-04 03:04:46 +00:00
|
|
|
// Only the image field is allowed to be changed when updating.
|
|
|
|
s.Image = service.Image
|
2022-01-06 08:54:14 +00:00
|
|
|
err = b.Update(ctx, s, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceFetchLogs(b biz.ServiceBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
ID string `json:"id" bind:"id"`
|
|
|
|
Lines int `json:"lines" bind:"lines"`
|
|
|
|
Timestamps bool `json:"timestamps" bind:"timestamps"`
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
var (
|
|
|
|
args = &Args{}
|
|
|
|
stdout, stderr string
|
|
|
|
)
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
stdout, stderr, err = b.FetchLogs(ctx, args.ID, args.Lines, args.Timestamps)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, data.Map{"stdout": stdout, "stderr": stderr})
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|