-
-
Notifications
You must be signed in to change notification settings - Fork 407
/
docker.go
executable file
·520 lines (440 loc) · 14.9 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Package docker provides functionality for working with Docker containers.
package docker
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
nativeDockerClient "github.com/docker/docker/client"
"go.keploy.io/server/v2/utils"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types"
dockerContainerPkg "github.com/docker/docker/api/types/container"
)
const (
defaultTimeoutForDockerQuery = 1 * time.Minute
)
type Impl struct {
nativeDockerClient.APIClient
timeoutForDockerQuery time.Duration
logger *zap.Logger
containerID string
}
func New(logger *zap.Logger) (Client, error) {
dockerClient, err := nativeDockerClient.NewClientWithOpts(nativeDockerClient.FromEnv,
nativeDockerClient.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
return &Impl{
APIClient: dockerClient,
timeoutForDockerQuery: defaultTimeoutForDockerQuery,
logger: logger,
}, nil
}
// GetContainerID is a Getter function for containerID
func (idc *Impl) GetContainerID() string {
return idc.containerID
}
// SetContainerID is a Setter function for containerID
func (idc *Impl) SetContainerID(containerID string) {
idc.containerID = containerID
}
// ExtractNetworksForContainer returns the list of all the networks that the container is a part of.
// Note that if a user did not explicitly attach the container to a network, the Docker daemon attaches it
// to a network called "bridge".
func (idc *Impl) ExtractNetworksForContainer(containerName string) (map[string]*network.EndpointSettings, error) {
ctx, cancel := context.WithTimeout(context.Background(), idc.timeoutForDockerQuery)
defer cancel()
containerJSON, err := idc.ContainerInspect(ctx, containerName)
if err != nil {
utils.LogError(idc.logger, err, "couldn't inspect container via the Docker API", zap.String("containerName", containerName))
return nil, err
}
if settings := containerJSON.NetworkSettings; settings != nil {
return settings.Networks, nil
}
// Docker attaches the container to "bridge" network by default.
// If the network list is empty, the docker daemon is possibly misbehaving,
// or the container is in a bad state.
utils.LogError(idc.logger, nil, "The network list for the given container is empty. This is unexpected.", zap.String("containerName", containerName))
return nil, fmt.Errorf("the container is not attached to any network")
}
func (idc *Impl) ConnectContainerToNetworks(containerName string, settings map[string]*network.EndpointSettings) error {
if settings == nil {
return fmt.Errorf("provided network settings is empty")
}
existingNetworks, err := idc.ExtractNetworksForContainer(containerName)
if err != nil {
return fmt.Errorf("could not get existing networks for container %s", containerName)
}
ctx, cancel := context.WithTimeout(context.Background(), idc.timeoutForDockerQuery)
defer cancel()
for networkName, setting := range settings {
// If the container is already part of this network, skip it.
_, ok := existingNetworks[networkName]
if ok {
continue
}
err := idc.NetworkConnect(ctx, networkName, containerName, setting)
if err != nil {
return err
}
}
return nil
}
func (idc *Impl) AttachNetwork(containerName string, networkNames []string) error {
if len(networkNames) == 0 {
return fmt.Errorf("provided network names list is empty")
}
existingNetworks, err := idc.ExtractNetworksForContainer(containerName)
if err != nil {
return fmt.Errorf("could not get existing networks for container %s", containerName)
}
ctx, cancel := context.WithTimeout(context.Background(), idc.timeoutForDockerQuery)
defer cancel()
for _, networkName := range networkNames {
// If the container is already part of this network, skip it.
_, ok := existingNetworks[networkName]
if ok {
continue
}
// As there are no specific settings, use nil for the settings parameter.
err := idc.NetworkConnect(ctx, networkName, containerName, nil)
if err != nil {
return err
}
}
return nil
}
// StopAndRemoveDockerContainer will Stop and Remove the docker container
func (idc *Impl) StopAndRemoveDockerContainer() error {
dockerClient := idc
containerID := idc.containerID
container, err := dockerClient.ContainerInspect(context.Background(), containerID)
if err != nil {
return fmt.Errorf("failed to inspect the docker container: %w", err)
}
if container.State.Status == "running" {
err = dockerClient.ContainerStop(context.Background(), containerID, dockerContainerPkg.StopOptions{})
if err != nil {
return fmt.Errorf("failed to stop the docker container: %w", err)
}
}
removeOptions := types.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
}
err = dockerClient.ContainerRemove(context.Background(), containerID, removeOptions)
if err != nil {
return fmt.Errorf("failed to remove the docker container: %w", err)
}
idc.logger.Debug("Docker Container stopped and removed successfully.")
return nil
}
// NetworkExists checks if the given network exists locally or not
func (idc *Impl) NetworkExists(networkName string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), idc.timeoutForDockerQuery)
defer cancel()
// Retrieve all networks.
networks, err := idc.NetworkList(ctx, types.NetworkListOptions{})
if err != nil {
return false, fmt.Errorf("error retrieving networks: %v", err)
}
// Check if the specified network is in the list.
for _, net := range networks {
if net.Name == networkName {
return true, nil
}
}
return false, nil
}
// CreateNetwork creates a custom docker network of type bridge.
func (idc *Impl) CreateNetwork(networkName string) error {
ctx, cancel := context.WithTimeout(context.Background(), idc.timeoutForDockerQuery)
defer cancel()
_, err := idc.NetworkCreate(ctx, networkName, types.NetworkCreate{
Driver: "bridge",
})
return err
}
// Compose structure to represent all the fields of a Docker Compose file
type Compose struct {
Version string `yaml:"version,omitempty"`
Services yaml.Node `yaml:"services,omitempty"`
Networks yaml.Node `yaml:"networks,omitempty"`
Volumes yaml.Node `yaml:"volumes,omitempty"`
Configs yaml.Node `yaml:"configs,omitempty"`
Secrets yaml.Node `yaml:"secrets,omitempty"`
}
func (idc *Impl) ReadComposeFile(filePath string) (*Compose, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
var compose Compose
err = yaml.Unmarshal(data, &compose)
if err != nil {
return nil, err
}
return &compose, nil
}
func (idc *Impl) WriteComposeFile(compose *Compose, path string) error {
data, err := yaml.Marshal(compose)
if err != nil {
return err
}
// write data to file
err = os.WriteFile(path, data, 0644)
if err != nil {
return err
}
return nil
}
// HasRelativePath returns information about whether bind mounts if they are being used contain relative file names or not
func (idc *Impl) HasRelativePath(compose *Compose) bool {
if compose.Services.Content == nil {
return false
}
for _, service := range compose.Services.Content {
for i, item := range service.Content {
if i+1 >= len(service.Content) {
break
}
if item.Value == "volumes" {
// volumeKeyNode := service.Content[i] or item
volumeValueNode := service.Content[i+1]
// Loop over all the volume mounts
for _, volumeMount := range volumeValueNode.Content {
// If volume mount starts with ./ or ../ then it as a relative path so return true
if volumeMount.Kind == yaml.ScalarNode && (volumeMount.Value[:2] == "./" || volumeMount.Value[:3] == "../") {
return true
}
}
}
}
}
return false
}
// GetNetworkInfo CheckNetworkInfo returns information about network name and also about whether the network is external or not in a docker-compose file.
func (idc *Impl) GetNetworkInfo(compose *Compose) *NetworkInfo {
if compose.Networks.Content == nil {
return nil
}
var defaultNetwork string
for i := 0; i < len(compose.Networks.Content); i += 2 {
if i+1 >= len(compose.Networks.Content) {
break
}
networkKeyNode := compose.Networks.Content[i]
networkValueNode := compose.Networks.Content[i+1]
if defaultNetwork == "" {
defaultNetwork = networkKeyNode.Value
}
isExternal := false
var externalName string
for j := 0; j < len(networkValueNode.Content); j += 2 {
if j+1 >= len(networkValueNode.Content) {
break
}
propertyNode := networkValueNode.Content[j]
valueNode := networkValueNode.Content[j+1]
if propertyNode.Value == "external" {
if valueNode.Kind == yaml.ScalarNode && valueNode.Value == "true" {
isExternal = true
break
} else if valueNode.Kind == yaml.MappingNode {
for k := 0; k < len(valueNode.Content); k += 2 {
if k+1 >= len(valueNode.Content) {
break
}
subPropertyNode := valueNode.Content[k]
subValueNode := valueNode.Content[k+1]
if subPropertyNode.Value == "name" {
isExternal = true
externalName = subValueNode.Value
break
}
}
}
break
}
}
if isExternal {
n := &NetworkInfo{External: true, Name: networkKeyNode.Value}
if externalName != "" {
n.Name = externalName
}
return n
}
}
if defaultNetwork != "" {
return &NetworkInfo{External: false, Name: defaultNetwork}
}
return nil
}
// GetHostWorkingDirectory Inspects Keploy docker container to get bind mount for current directory
func (idc *Impl) GetHostWorkingDirectory() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), idc.timeoutForDockerQuery)
defer cancel()
curDir, err := os.Getwd()
if err != nil {
utils.LogError(idc.logger, err, "failed to get current working directory")
return "", err
}
container, err := idc.ContainerInspect(ctx, "keploy-v2")
if err != nil {
utils.LogError(idc.logger, err, "error inspecting keploy-v2 container")
return "", err
}
containerMounts := container.Mounts
// Loop through container mounts and find the mount for current directory in the container
for _, mount := range containerMounts {
if mount.Destination == curDir {
idc.logger.Debug(fmt.Sprintf("found mount for %s in keploy-v2 container", curDir), zap.Any("mount", mount))
return mount.Source, nil
}
}
return "", fmt.Errorf(fmt.Sprintf("could not find mount for %s in keploy-v2 container", curDir))
}
// ForceAbsolutePath replaces relative paths in bind mounts with absolute paths
func (idc *Impl) ForceAbsolutePath(c *Compose, basePath string) error {
hostWorkingDirectory, err := idc.GetHostWorkingDirectory()
if err != nil {
return err
}
dockerComposeContext, err := filepath.Abs(filepath.Join(hostWorkingDirectory, basePath))
if err != nil {
utils.LogError(idc.logger, err, "error getting absolute path for docker compose file")
return err
}
dockerComposeContext = filepath.Dir(dockerComposeContext)
idc.logger.Debug("docker compose file location in host filesystem", zap.Any("dockerComposeContext", dockerComposeContext))
// Loop through all services in compose file
for _, service := range c.Services.Content {
for i, item := range service.Content {
if i+1 >= len(service.Content) {
break
}
if item.Value == "volumes" {
// volumeKeyNode := service.Content[i] or item
volumeValueNode := service.Content[i+1]
// Loop over all the volume mounts
for _, volumeMount := range volumeValueNode.Content {
// If volume mount starts with ./ or ../ then it is a relative path
if volumeMount.Kind == yaml.ScalarNode && (volumeMount.Value[:2] == "./" || volumeMount.Value[:3] == "../") {
// Replace the relative path with absolute path
absPath, err := filepath.Abs(filepath.Join(dockerComposeContext, volumeMount.Value))
if err != nil {
return err
}
volumeMount.Value = absPath
}
}
}
}
}
return nil
}
// MakeNetworkExternal makes the existing network of the user docker compose file external and save it to a new file
func (idc *Impl) MakeNetworkExternal(c *Compose) error {
// Iterate over all networks and check the 'external' flag.
if c.Networks.Content != nil {
for i := 0; i < len(c.Networks.Content); i += 2 {
if i+1 >= len(c.Networks.Content) {
break
}
// networkKeyNode := compose.Networks.Content[i]
networkValueNode := c.Networks.Content[i+1]
// If it's a shorthand notation or null value, initialize it as an empty mapping node
if (networkValueNode.Kind == yaml.ScalarNode && networkValueNode.Value == "") || networkValueNode.Tag == "!!null" {
networkValueNode.Kind = yaml.MappingNode
networkValueNode.Tag = ""
networkValueNode.Content = make([]*yaml.Node, 0)
}
externalFound := false
for index, propertyNode := range networkValueNode.Content {
if index+1 >= len(networkValueNode.Content) {
break
}
if propertyNode.Value == "external" {
externalFound = true
valueNode := networkValueNode.Content[index+1]
if valueNode.Kind == yaml.ScalarNode && (valueNode.Value == "false" || valueNode.Value == "") {
valueNode.Value = "true"
}
break
}
}
if !externalFound {
networkValueNode.Content = append(networkValueNode.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: "external"},
&yaml.Node{Kind: yaml.ScalarNode, Value: "true"},
)
}
}
}
return nil
}
// SetKeployNetwork adds the keploy-network network to the new docker compose file and copy rest of the contents from
// existing user docker compose file
func (idc *Impl) SetKeployNetwork(c *Compose) (*NetworkInfo, error) {
// Ensure that the top-level networks mapping exists.
if c.Networks.Content == nil {
c.Networks.Kind = yaml.MappingNode
c.Networks.Content = make([]*yaml.Node, 0)
}
networkInfo := &NetworkInfo{
Name: "keploy-network",
External: true,
}
// Check if "keploy-network" already exists
exists := false
for i := 0; i < len(c.Networks.Content); i += 2 {
if c.Networks.Content[i].Value == "keploy-network" {
exists = true
break
}
}
if !exists {
// Add the keploy-network with external: true
c.Networks.Content = append(c.Networks.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: "keploy-network"},
&yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "external"},
{Kind: yaml.ScalarNode, Value: "true"},
}},
)
}
// Add or modify network for each service
for _, service := range c.Services.Content {
networksFound := false
for _, item := range service.Content {
if item.Value == "networks" {
networksFound = true
break
}
}
if !networksFound {
service.Content = append(service.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: "networks"},
&yaml.Node{
Kind: yaml.SequenceNode,
Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "keploy-network"},
},
},
)
} else {
for _, item := range service.Content {
if item.Value == "networks" {
item.Content = append(item.Content, &yaml.Node{Kind: yaml.ScalarNode, Value: "keploy-network"})
}
}
}
}
return networkInfo, nil
}