/
docker.go
268 lines (233 loc) · 7.96 KB
/
docker.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package connector
import (
"context"
"fmt"
"sync"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/events"
dockerclient "github.com/docker/docker/client"
"github.com/shellhub-io/shellhub/pkg/agent"
log "github.com/sirupsen/logrus"
)
var _ Connector = new(DockerConnector)
// DockerConnector is a struct that represents a connector that uses Docker as the container runtime.
type DockerConnector struct {
mu sync.Mutex
// server is the ShellHub address of the server that the agent will connect to.
server string
// tenant is the tenant ID of the namespace that the agent belongs to.
tenant string
// cli is the Docker client.
cli *dockerclient.Client
// privateKeys is the path to the directory that contains the private keys for the containers.
privateKeys string
// cancels is a map that contains the cancel functions for each container.
// This is used to stop the agent for a container, marking as done its context and closing the agent.
cancels map[string]context.CancelFunc
}
// NewDockerConnector creates a new [Connector] that uses Docker as the container runtime.
func NewDockerConnector(server string, tenant string, privateKey string) (Connector, error) {
cli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv, dockerclient.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
return &DockerConnector{
server: server,
tenant: tenant,
cli: cli,
privateKeys: privateKey,
cancels: make(map[string]context.CancelFunc),
}, nil
}
// events returns the docker events.
func (d *DockerConnector) events(ctx context.Context) (<-chan events.Message, <-chan error) {
return d.cli.Events(ctx, types.EventsOptions{})
}
func (d *DockerConnector) List(ctx context.Context) ([]Container, error) {
containers, err := d.cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
return nil, err
}
list := make([]Container, len(containers))
for i, container := range containers {
list[i].ID = container.ID
name, err := d.getContainerNameFromID(ctx, container.ID)
if err != nil {
return nil, err
}
list[i].Name = name
}
return list, nil
}
// Start starts the agent for the container with the given ID.
func (d *DockerConnector) Start(ctx context.Context, id string, name string) {
id = id[:12]
d.mu.Lock()
ctx, d.cancels[id] = context.WithCancel(ctx)
d.mu.Unlock()
privateKey := fmt.Sprintf("%s/%s.key", d.privateKeys, id)
go initContainerAgent(ctx, d.cli, Container{
ID: id,
Name: name,
ServerAddress: d.server,
Tenant: d.tenant,
PrivateKey: privateKey,
Cancel: d.cancels[id],
})
}
// Stop stops the agent for the container with the given ID.
func (d *DockerConnector) Stop(_ context.Context, id string) {
id = id[:12]
d.mu.Lock()
defer d.mu.Unlock()
cancel, ok := d.cancels[id]
if ok {
cancel()
delete(d.cancels, id)
}
}
func (d *DockerConnector) getContainerNameFromID(ctx context.Context, id string) (string, error) {
container, err := d.cli.ContainerInspect(ctx, id)
if err != nil {
return "", err
}
// NOTICE: It removes the first character on container's name that is a `/`.
return container.Name[1:], nil
}
// Listen listens for events and starts or stops the agent for the containers.
func (d *DockerConnector) Listen(ctx context.Context) error {
containers, err := d.List(ctx)
if err != nil {
return err
}
for _, container := range containers {
d.Start(ctx, container.ID, container.Name)
}
events, errs := d.events(ctx)
for {
select {
case <-ctx.Done():
return nil
case err := <-errs:
return err
case container := <-events:
// NOTICE: "start" and "die" Docker's events are call every time a new container start or stop,
// independently how the command was run. For example, if a container was started with `docker run -d`, the
// "start" event will be called, but if the same container was started with `docker start <container-id>`,
// the "start" event will be called too. The same happens with the "die" event.
switch container.Action {
case "start":
name, err := d.getContainerNameFromID(ctx, container.ID)
if err != nil {
return err
}
d.Start(ctx, container.ID, name)
case "die":
d.Stop(ctx, container.ID)
}
}
}
}
// initContainerAgent initializes the agent for a container.
func initContainerAgent(ctx context.Context, cli *dockerclient.Client, container Container) {
agent.AgentPlatform = "connector"
agent.AgentVersion = ConnectorVersion
cfg := &agent.Config{
ServerAddress: container.ServerAddress,
TenantID: container.Tenant,
PrivateKey: container.PrivateKey,
PreferredIdentity: container.ID,
PreferredHostname: container.Name,
KeepAliveInterval: 30,
}
log.WithFields(log.Fields{
"id": container.ID,
"identity": cfg.PreferredIdentity,
"hostname": cfg.PreferredHostname,
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Info("Connector container started")
mode, err := agent.NewConnectorMode(cli, container.ID)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"id": container.ID,
"identity": cfg.PreferredIdentity,
"hostname": cfg.PreferredHostname,
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Fatal("Failed to create connector mode")
}
ag, err := agent.NewAgentWithConfig(cfg, mode)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"id": container.ID,
"configuration": cfg,
"version": agent.AgentVersion,
}).Fatal("Failed to create agent")
}
if err := ag.Initialize(); err != nil {
log.WithError(err).WithFields(log.Fields{
"id": container.ID,
"configuration": cfg,
"version": agent.AgentVersion,
}).Fatal("Failed to initialize agent")
}
go func() {
if err := ag.Ping(ctx, 0); err != nil {
log.WithError(err).WithFields(log.Fields{
"id": container.ID,
"identity": cfg.PreferredIdentity,
"hostname": cfg.PreferredHostname,
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Fatal("Failed to ping server")
}
log.WithFields(log.Fields{
"id": container.ID,
"identity": cfg.PreferredIdentity,
"hostname": cfg.PreferredHostname,
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Info("Stopped pinging server")
}()
log.WithFields(log.Fields{
"id": container.ID,
"identity": cfg.PreferredIdentity,
"hostname": cfg.PreferredHostname,
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Info("Listening for connections")
// NOTICE(r): listing for connection and wait for a channel message to close the agent. It will receives
// this mensagem when something out of this goroutine send a `done`, what will cause the agent closes
// and no more connection to be allowed until it be started again.
if err := ag.Listen(ctx); err != nil {
log.WithError(err).WithFields(log.Fields{
"id": container.ID,
"identity": cfg.PreferredIdentity,
"hostname": cfg.PreferredHostname,
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Fatal("Failed to listen for connections")
}
log.WithFields(log.Fields{
"id": container.ID,
"identity": cfg.PreferredIdentity,
"hostname": cfg.PreferredHostname,
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"version": agent.AgentVersion,
}).Info("Connector container done")
}