-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
158 lines (137 loc) · 4.06 KB
/
client.go
File metadata and controls
158 lines (137 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"context"
"encoding/binary"
"io"
"log"
"sort"
"strings"
"github.com/moby/moby/api/types/events"
"github.com/moby/moby/client"
)
// Container represents an internal representation of docker containers
type Container struct {
ID string `json:"id"`
Names []string `json:"names"`
Name string `json:"name"`
Image string `json:"image"`
ImageID string `json:"imageId"`
Command string `json:"command"`
Created int64 `json:"created"`
State string `json:"state"`
Status string `json:"status"`
ComposeProject string `json:"compose_project"`
ComposeService string `json:"compose_service"`
}
type Client struct {
cli *client.Client
}
// NewClient creates a new instance of Client
func NewClient() *Client {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
log.Fatal(err)
}
return &Client{cli}
}
func (d *Client) ListContainers(ctx context.Context) ([]Container, error) {
list, err := d.cli.ContainerList(ctx, client.ContainerListOptions{})
if err != nil {
return nil, err
}
var containers []Container
for _, c := range list.Items {
container := Container{
ID: c.ID,
Names: c.Names,
Name: strings.TrimPrefix(c.Names[0], "/"),
Image: c.Image,
ImageID: c.ImageID,
Command: c.Command,
Created: c.Created,
State: string(c.State),
Status: c.Status,
ComposeProject: c.Labels["com.docker.compose.project"],
ComposeService: c.Labels["com.docker.compose.service"],
}
containers = append(containers, container)
}
sort.Slice(containers, func(i, j int) bool {
return containers[i].Name < containers[j].Name
})
if containers == nil {
containers = []Container{}
}
return containers, nil
}
func (d *Client) GetContainer(ctx context.Context, id string) (*Container, error) {
inspect, err := d.cli.ContainerInspect(ctx, id, client.ContainerInspectOptions{})
if err != nil {
return nil, err
}
return &Container{
ID: inspect.Container.ID,
Names: []string{inspect.Container.Name},
Name: strings.TrimPrefix(inspect.Container.Name, "/"),
Image: inspect.Container.Config.Image,
ImageID: inspect.Container.Image,
Command: strings.Join(inspect.Container.Config.Cmd, " "),
Created: 0,
State: string(inspect.Container.State.Status),
Status: string(inspect.Container.State.Status),
ComposeProject: inspect.Container.Config.Labels["com.docker.compose.project"],
ComposeService: inspect.Container.Config.Labels["com.docker.compose.service"],
}, nil
}
func (d *Client) ContainerLogs(ctx context.Context, id string, options client.ContainerLogsOptions) (<-chan string, <-chan error) {
reader, err := d.cli.ContainerLogs(ctx, id, options)
errChannel := make(chan error, 1)
if err != nil {
errChannel <- err
close(errChannel)
return nil, errChannel
}
messages := make(chan string)
go func() {
<-ctx.Done()
reader.Close()
}()
go func() {
defer close(messages)
defer close(errChannel)
defer reader.Close()
hdr := make([]byte, 8)
var buffer bytes.Buffer
for {
_, err := reader.Read(hdr)
if err != nil {
errChannel <- err
break
}
count := binary.BigEndian.Uint32(hdr[4:])
_, err = io.CopyN(&buffer, reader, int64(count))
if err != nil {
errChannel <- err
break
}
select {
case messages <- buffer.String():
case <-ctx.Done():
// return // TODO: maybe return here
}
buffer.Reset()
}
}()
return messages, errChannel
}
func (d *Client) Events(ctx context.Context) (<-chan events.Message, <-chan error) {
filters := make(client.Filters)
filters.Add("event", string(events.ActionStart))
filters.Add("event", string(events.ActionDie))
filters.Add("event", string(events.ActionKill))
filters.Add("event", string(events.ActionStop))
filters.Add("event", string(events.ActionDestroy))
res := d.cli.Events(ctx, client.EventsListOptions{Filters: filters})
return res.Messages, res.Err
}