This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathaction.go
222 lines (190 loc) · 5.71 KB
/
action.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
/*-
* Copyright 2015 Grammarly, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package compose
import (
"bytes"
"fmt"
"sync"
)
// Action interface describes action that can be done by rocker-compose docker client
type Action interface {
Execute(client Client) error
String() string
}
type action struct {
container *Container
}
type ensureContainerExist action
type ensureContainerState action
type runContainer action
type removeContainer action
type noAction action
type waitContainerAction action
// NoAction is an empty action which does nothing
var NoAction = &noAction{}
type stepAction struct {
actions []Action
async bool
}
// NewStepAction makes a "step" wrapper which holds the list of actions that may run in parallel.
// Multiple steps can only run one by one. Steps can be nested.
func NewStepAction(async bool, actions ...Action) Action {
len := len(actions)
if len == 0 {
return NoAction
}
if len == 1 {
return actions[0]
}
//filter NoAction elements
acts := []Action{}
for _, a := range actions {
if a != NoAction {
acts = append(acts, a)
}
}
return &stepAction{
actions: acts,
async: async,
}
}
// NewWaitContainerAction makes action that waits for container
func NewWaitContainerAction(c *Container) Action {
return &waitContainerAction{container: c}
}
// NewEnsureContainerExistAction makes action that ensures that container exists
func NewEnsureContainerExistAction(c *Container) Action {
return &ensureContainerExist{container: c}
}
// NewEnsureContainerStateAction makes action that ensures that container
// state is a desired one
func NewEnsureContainerStateAction(c *Container) Action {
return &ensureContainerState{container: c}
}
// NewRunContainerAction makes action that runs a container
func NewRunContainerAction(c *Container) Action {
return &runContainer{container: c}
}
// NewRemoveContainerAction makes action that removes a container
func NewRemoveContainerAction(c *Container) Action {
return &removeContainer{container: c}
}
// Execute runs the step
func (a *stepAction) Execute(client Client) (err error) {
if a.async {
err = a.executeAsync(client)
} else {
err = a.executeSync(client)
}
return
}
func (a *stepAction) executeAsync(client Client) (err error) {
var wg sync.WaitGroup
len := len(a.actions)
errors := make(chan error, len)
wg.Add(len)
for _, a := range a.actions {
go func(action Action) {
defer wg.Done()
if err := action.Execute(client); err != nil {
errors <- err
}
}(a)
}
wg.Wait()
select {
case err = <-errors:
default:
}
return
}
func (a *stepAction) executeSync(client Client) (err error) {
for _, a := range a.actions {
if err = a.Execute(client); err != nil {
return
}
}
return
}
// String returns the printable string representation of the step.
func (a *stepAction) String() string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("Running in concurrency mode = %t:\n", a.async))
for _, a := range a.actions {
buffer.WriteString(fmt.Sprintf(" - %s\n", a))
}
return buffer.String()
}
// Execute runs a container
func (a *runContainer) Execute(client Client) (err error) {
err = client.RunContainer(a.container)
return
}
// String returns the printable string representation of the runContainer action.
func (a *runContainer) String() string {
return fmt.Sprintf("Creating container '%s'", a.container.Name)
}
// Execute removes a container
func (a *removeContainer) Execute(client Client) (err error) {
err = client.RemoveContainer(a.container)
return
}
// String returns the printable string representation of the removeContainer action.
func (a *removeContainer) String() string {
return fmt.Sprintf("Removing container '%s'", a.container.Name)
}
// Execute waits for a container
func (a *waitContainerAction) Execute(client Client) (err error) {
return client.WaitForContainer(a.container)
}
// String returns the printable string representation of the waitContainer action.
func (a *waitContainerAction) String() string {
return fmt.Sprintf("Waiting for container '%s'", a.container.Name)
}
// Execute does nothing
func (a *noAction) Execute(client Client) (err error) {
return
}
// String returns "noop"
func (a *noAction) String() string {
return "noop"
}
// Execute ensures container exists
func (c *ensureContainerExist) Execute(client Client) (err error) {
return client.EnsureContainerExist(c.container)
}
// String returns the printable string representation of the ensureContainerExist action.
func (c *ensureContainerExist) String() string {
return fmt.Sprintf("Ensuring container '%s'", c.container.Name)
}
// Execute ensures container state is what we want in a spec
func (c *ensureContainerState) Execute(client Client) (err error) {
return client.EnsureContainerState(c.container)
}
// String returns the printable string representation of the ensureContainerState action.
func (c *ensureContainerState) String() string {
return fmt.Sprintf("Ensuring container state '%s'", c.container.Name)
}
// WalkActions recursively though all action and applies given function to every action.
func WalkActions(actions []Action, fn func(action Action)) {
for _, a := range actions {
if step, ok := a.(*stepAction); ok {
WalkActions(step.actions, fn)
} else {
fn(a)
}
}
}