swirl/biz/docker/task.go

122 lines
2.8 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package docker
import (
2018-02-23 03:06:45 +00:00
"bytes"
2017-09-26 12:50:09 +00:00
"context"
"io"
2017-10-19 12:40:05 +00:00
"math"
"sort"
2018-02-23 03:06:45 +00:00
"strconv"
2017-09-26 12:50:09 +00:00
2017-10-19 12:40:05 +00:00
"github.com/cuigh/swirl/misc"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/model"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
2018-02-23 03:06:45 +00:00
"github.com/docker/docker/pkg/stdcopy"
2017-09-26 12:50:09 +00:00
)
// TaskList return all running tasks of a service or a node.
2017-10-19 12:40:05 +00:00
func TaskList(args *model.TaskListArgs) (infos []*model.TaskInfo, totalCount int, err error) {
2017-09-26 12:50:09 +00:00
err = mgr.Do(func(ctx context.Context, cli *client.Client) (err error) {
var (
tasks []swarm.Task
2017-10-19 12:40:05 +00:00
opts = types.TaskListOptions{
Filters: filters.NewArgs(),
}
2017-09-26 12:50:09 +00:00
)
2017-10-19 12:40:05 +00:00
if args.PageIndex < 1 {
args.PageIndex = 1
2017-09-26 12:50:09 +00:00
}
2017-10-19 12:40:05 +00:00
if args.PageSize < 1 {
args.PageSize = math.MaxInt32
2017-09-26 12:50:09 +00:00
}
2017-10-19 12:40:05 +00:00
if args.Service != "" {
opts.Filters.Add("service", args.Service)
2017-09-26 12:50:09 +00:00
}
2017-10-19 12:40:05 +00:00
if args.Node != "" {
opts.Filters.Add("node", args.Node)
}
if args.Name != "" {
opts.Filters.Add("name", args.Name)
}
if args.State != "" {
opts.Filters.Add("desired-state", args.State)
}
2017-09-26 12:50:09 +00:00
tasks, err = cli.TaskList(ctx, opts)
2017-10-19 12:40:05 +00:00
totalCount = len(tasks)
if err == nil && totalCount > 0 {
2017-09-26 12:50:09 +00:00
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].UpdatedAt.After(tasks[j].UpdatedAt)
})
2017-10-19 12:40:05 +00:00
start, end := misc.Page(totalCount, args.PageIndex, args.PageSize)
tasks = tasks[start:end]
2017-09-26 12:50:09 +00:00
nodes := make(map[string]string)
for _, t := range tasks {
if _, ok := nodes[t.NodeID]; !ok {
if n, _, e := cli.NodeInspectWithRaw(ctx, t.NodeID); e == nil {
2017-11-08 10:36:13 +00:00
if n.Spec.Name == "" {
nodes[t.NodeID] = n.Description.Hostname
} else {
nodes[t.NodeID] = n.Spec.Name
}
2017-09-26 12:50:09 +00:00
} else {
nodes[t.NodeID] = ""
}
}
}
infos = make([]*model.TaskInfo, len(tasks))
for i, t := range tasks {
infos[i] = model.NewTaskInfo(t, nodes[t.NodeID])
}
}
return
})
return
}
// TaskInspect return detail information of a task.
func TaskInspect(id string) (task swarm.Task, raw []byte, err error) {
err = mgr.Do(func(ctx context.Context, cli *client.Client) (err error) {
task, raw, err = cli.TaskInspectWithRaw(ctx, id)
return
})
return
}
// TaskLogs returns the logs generated by a task in an io.ReadCloser.
// It's up to the caller to close the stream.
2018-02-23 03:06:45 +00:00
func TaskLogs(id string, line int, timestamps bool) (stdout, stderr *bytes.Buffer, err error) {
var (
ctx context.Context
cli *client.Client
rc io.ReadCloser
)
ctx, cli, err = mgr.Client()
if err != nil {
2017-09-26 12:50:09 +00:00
return
2018-02-23 03:06:45 +00:00
}
opts := types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Tail: strconv.Itoa(line),
Timestamps: timestamps,
//Since: (time.Hour * 24).String()
}
if rc, err = cli.TaskLogs(ctx, id, opts); err == nil {
defer rc.Close()
stdout = &bytes.Buffer{}
stderr = &bytes.Buffer{}
_, err = stdcopy.StdCopy(stdout, stderr, rc)
}
2017-09-26 12:50:09 +00:00
return
}