-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.go
64 lines (50 loc) · 1.63 KB
/
start.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
// SPDX-FileCopyrightText: 2022 Mario Román Dono <[email protected]>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package labsoncontainers
import (
"context"
"fmt"
"golang.org/x/sync/errgroup"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
// StartEnvironment starts all the containers of the provided lab environment.
func StartEnvironment(labName string) error {
containers, err := GetEnvironmentContainers(labName)
if err != nil {
return fmt.Errorf("error while starting environment: %w", err)
}
err = startContainers(containers)
if err != nil {
return fmt.Errorf("error while starting environment: %w", err)
}
return nil
}
// stopsContainers concurrently starts (using errgroups) all the specified containers.
func startContainers(containers []LabContainer) error {
g, ctx := errgroup.WithContext(context.Background())
for _, container := range containers {
id := container.ID
g.Go(func() error {
return startContainer(ctx, id)
})
}
if err := g.Wait(); err != nil {
return fmt.Errorf("error while starting containers: %w", err)
}
return nil
}
// startContainer starts the specified container.
func startContainer(errGroupCtx context.Context, containerID string) error {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return fmt.Errorf("error while starting container %v: %v", containerID, err)
}
err = cli.ContainerStart(ctx, containerID, types.ContainerStartOptions{})
if err != nil {
return fmt.Errorf("error while starting container %v: %v", containerID, err)
}
return nil
}