This repository was archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteps.go
135 lines (113 loc) · 3.2 KB
/
steps.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
package brewery
import (
"context"
"io"
"io/ioutil"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
var containerTimeout = 10 * time.Second
// Step defines an action to run in or with containers in a provided environment.
type Step interface {
Run(context.Context, Environment) error
}
// StepFunc is a function that can act as a Step.
type StepFunc func(context.Context, Environment) error
// Run the step function
func (s StepFunc) Run(ctx context.Context, env Environment) error {
return s(ctx, env)
}
// EnsureContainer is a step which ensures that the provided brews containers are running.
//
// Will create new containers if the provided brew has no created containers.
type EnsureContainer struct {
Containers []*Brew
}
// Run will boot every brews containers
func (e *EnsureContainer) Run(ctx context.Context, env Environment) error {
g, ctx := errgroup.WithContext(ctx)
for _, cn := range e.Containers {
cn := cn
g.Go(func() error {
return e.run(ctx, env, cn)
})
}
return g.Wait()
}
func (e *EnsureContainer) run(ctx context.Context, env Environment, brew *Brew) error {
containerName := env.ContainerName(brew.Key)
cs, err := env.Client.ContainerInspect(ctx, containerName)
if err != nil && !client.IsErrContainerNotFound(err) {
return err
}
switch {
case err != nil:
return e.create(ctx, env, brew)
case cs.State.Running:
return nil
default:
return e.start(ctx, env, cs.ID)
}
}
func (e *EnsureContainer) build(ctx context.Context, env Environment, brew *Brew) error {
return errors.Errorf("Currently not able to build from dockerfiles")
}
func (e *EnsureContainer) create(ctx context.Context, env Environment, brew *Brew) error {
if brew.Build != "" {
return e.build(ctx, env, brew)
}
rc, err := env.Client.ImagePull(ctx, brew.Image, types.ImagePullOptions{})
if err != nil {
return err
}
if _, err = io.Copy(ioutil.Discard, rc); err != nil {
return err
}
if err = rc.Close(); err != nil {
return err
}
containerName := env.ContainerName(brew.Key)
config := &container.Config{
AttachStdout: true,
AttachStderr: true,
Env: brew.Envs,
Image: brew.Image,
}
hostConfig := &container.HostConfig{
AutoRemove: true,
}
nwConfig := &network.NetworkingConfig{}
cbody, err := env.Client.ContainerCreate(ctx, config, hostConfig, nwConfig, containerName)
if err != nil {
return err
}
return e.start(ctx, env, cbody.ID)
}
func (e *EnsureContainer) start(ctx context.Context, env Environment, cid string) error {
return env.Client.ContainerRestart(ctx, cid, &containerTimeout)
}
// EnsureContainers creates a new EnsureContainer step with the provided containers.
func EnsureContainers(containers ...*Brew) *EnsureContainer {
return &EnsureContainer{
Containers: containers,
}
}
type StepBundle struct {
Steps []Step
}
func (s StepBundle) Run(ctx context.Context, env Environment) error {
for _, step := range s.Steps {
if err := step.Run(ctx, env); err != nil {
return err
}
}
return nil
}
func (s *StepBundle) add(step Step) {
s.Steps = append(s.Steps, step)
}