-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinspect.go
82 lines (65 loc) · 2.49 KB
/
inspect.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
// SPDX-FileCopyrightText: 2022 Mario Román Dono <mario.romandono@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package labsoncontainers
import (
"context"
"encoding/json"
"fmt"
"golang.org/x/sync/errgroup"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
// InspectEnvironment returns low-level information of all the containers of the provided lab environment.
// On success, it returns a map of the containers' names as keys and their information as values.
func InspectEnvironment(labName string) (map[string][]byte, error) {
containers, err := GetEnvironmentContainers(labName)
if err != nil {
return nil, fmt.Errorf("error while inspecting environment: %w", err)
}
inspectMap, err := inspectContainers(containers)
if err != nil {
return nil, fmt.Errorf("error while inspecting environment: %w", err)
}
inspectJSONMap := make(map[string][]byte, len(inspectMap))
for container, inspectInfo := range inspectMap {
inspectJSON, err := json.MarshalIndent(inspectInfo, "", " ")
if err != nil {
return nil, fmt.Errorf("error while inspecting environment: %w", err)
}
inspectJSONMap[container] = inspectJSON
}
return inspectJSONMap, nil
}
// inspectContainers concurrently inspects (using errgroups) all the specified containers.
func inspectContainers(containers []LabContainer) (map[string]types.ContainerJSON, error) {
g, ctx := errgroup.WithContext(context.Background())
inspectMap := make(map[string]types.ContainerJSON, len(containers))
for _, container := range containers {
name, id := container.Name, container.ID
g.Go(func() error {
inspectInfo, err := inspectContainer(ctx, id)
if err == nil {
inspectMap[name] = inspectInfo
}
return err
})
}
if err := g.Wait(); err != nil {
return nil, fmt.Errorf("error while inspecting containers: %w", err)
}
return inspectMap, nil
}
// inspectContainer inspects the specified container and returns the required information.
func inspectContainer(errGroupCtx context.Context, containerID string) (types.ContainerJSON, error) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return types.ContainerJSON{}, fmt.Errorf("error while inspecting container %v: %v", containerID, err)
}
inspectJSON, err := cli.ContainerInspect(ctx, containerID)
if err != nil {
return types.ContainerJSON{}, fmt.Errorf("error while inspecting container %v: %v", containerID, err)
}
return inspectJSON, nil
}