/
unit_of_work.go
350 lines (311 loc) · 8.13 KB
/
unit_of_work.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
package crane
import (
"fmt"
"os"
"strings"
"text/template"
)
type UnitOfWork struct {
targeted []string
containers []string
order []string
requireStarted []string
}
func NewUnitOfWork(dependencyMap map[string]*Dependencies, targeted []string) (uow *UnitOfWork, err error) {
uow = &UnitOfWork{
targeted: targeted,
containers: targeted,
order: []string{},
requireStarted: []string{},
}
// select all containers which we care about
for {
c := uow.containers
initialLenContainers := len(c)
for _, name := range c {
dependencies := dependencyMap[name]
if dependencies == nil {
err = fmt.Errorf("Container %s referenced, but not defined.", name)
return
}
for _, dep := range dependencies.All {
uow.ensureInContainers(dep)
if dependencies.requireStarted(dep) {
uow.ensureInRequireStarted(dep)
}
}
}
if len(uow.containers) == initialLenContainers {
break
}
}
// bring containers into order
for {
initialLenOrdered := len(uow.order)
for _, name := range uow.containers {
if dependencies, ok := dependencyMap[name]; ok {
if dependencies.satisfied() {
uow.order = append(uow.order, name)
delete(dependencyMap, name)
for _, dependencies := range dependencyMap {
dependencies.remove(name)
}
}
}
}
if len(uow.order) == initialLenOrdered {
break
}
}
if len(uow.order) < len(uow.containers) {
err = fmt.Errorf("Dependencies for container(s) %s could not be resolved.", uow.targeted)
} else if len(uow.containers) == 0 {
err = fmt.Errorf("Command cannot be applied to any container.")
}
return
}
func (uow *UnitOfWork) Run(cmds []string, excluded []string) {
uow.prepareRequirements()
for _, container := range uow.Containers() {
if includes(uow.targeted, container.Name()) {
container.Run(cmds, excluded)
} else if includes(uow.requireStarted, container.Name()) || !container.Exists() {
container.Start(excluded)
}
}
}
func (uow *UnitOfWork) Lift(cmds []string, excluded []string, noCache bool, parallel int) {
uow.Targeted().Provision(noCache, parallel)
uow.prepareRequirements()
for _, container := range uow.Containers() {
if includes(uow.targeted, container.Name()) {
container.Run(cmds, excluded)
} else if includes(uow.requireStarted, container.Name()) || !container.Exists() {
container.Start(excluded)
}
}
}
func (uow *UnitOfWork) Stats() {
args := []string{"stats"}
for _, container := range uow.Targeted() {
for _, name := range container.InstancesOfStatus("running") {
args = append(args, name)
}
}
if len(args) > 1 {
executeCommand("docker", args, os.Stdout, os.Stderr)
} else {
printNoticef("None of the targeted container is running.\n")
}
}
func (uow *UnitOfWork) Status(noTrunc bool) {
uow.Targeted().Status(noTrunc)
}
// Push containers.
func (uow *UnitOfWork) Push() {
for _, container := range uow.Targeted() {
container.Push()
}
}
// Unpause containers.
func (uow *UnitOfWork) Unpause() {
for _, container := range uow.Targeted() {
container.Unpause()
}
}
// Pause containers.
func (uow *UnitOfWork) Pause() {
for _, container := range uow.Targeted().Reversed() {
container.Pause()
}
}
// Start containers.
func (uow *UnitOfWork) Start() {
uow.prepareRequirements()
for _, container := range uow.Containers() {
if includes(uow.targeted, container.Name()) {
container.Start(excluded)
} else if includes(uow.requireStarted, container.Name()) || !container.Exists() {
container.Start(excluded)
}
}
}
// Stop containers.
func (uow *UnitOfWork) Stop() {
for _, container := range uow.Targeted().Reversed() {
container.Stop()
}
}
// Kill containers.
func (uow *UnitOfWork) Kill() {
for _, container := range uow.Targeted().Reversed() {
container.Kill()
}
}
func (uow *UnitOfWork) Exec(cmds []string) {
for _, container := range uow.Containers() {
if includes(uow.targeted, container.Name()) {
container.Exec(cmds)
} else if includes(uow.requireStarted, container.Name()) || !container.Exists() {
container.Start(excluded)
}
}
}
// Rm containers.
func (uow *UnitOfWork) Rm(force bool) {
for _, container := range uow.Targeted().Reversed() {
container.Rm(force)
}
}
// Create containers.
func (uow *UnitOfWork) Create(cmds []string, excluded []string) {
uow.prepareRequirements()
for _, container := range uow.Containers() {
if includes(uow.targeted, container.Name()) {
container.Create(cmds, excluded)
} else if includes(uow.requireStarted, container.Name()) || !container.Exists() {
container.Start(excluded)
}
}
}
// Provision containers.
func (uow *UnitOfWork) Provision(noCache bool, parallel int) {
uow.Targeted().Provision(noCache, parallel)
}
// Pull containers.
func (uow *UnitOfWork) PullImage() {
for _, container := range uow.Targeted() {
if len(container.BuildParams().Context()) == 0 {
container.PullImage()
}
}
}
// Log containers.
func (uow *UnitOfWork) Logs(follow bool, timestamps bool, tail string, colorize bool, since string) {
uow.Targeted().Logs(follow, timestamps, tail, colorize, since)
}
// Generate files.
func (uow *UnitOfWork) Generate(templateFile string, output string) {
templateFileParts := strings.Split(templateFile, "/")
templateName := templateFileParts[len(templateFileParts)-1]
tmpl, err := template.New(templateName).ParseFiles(templateFile)
if err != nil {
printErrorf("ERROR: %s\n", err)
return
}
executeTemplate := func(outputFile string, templateInfo interface{}) {
writer := os.Stdout
if len(outputFile) > 0 {
writer, _ = os.Create(outputFile)
}
err = tmpl.Execute(writer, templateInfo)
if err != nil {
printErrorf("ERROR: %s\n", err)
}
}
if strings.Contains(output, "%s") {
for _, container := range uow.TargetedInfo() {
executeTemplate(fmt.Sprintf(output, container.PrefixedName()), container)
}
} else {
tmplInfo := struct {
Containers []ContainerInfo
}{
Containers: uow.TargetedInfo(),
}
executeTemplate(output, tmplInfo)
}
}
func (uow *UnitOfWork) Containers() Containers {
c := []Container{}
for _, name := range uow.order {
c = append(c, cfg.Container(name))
}
return c
}
func (uow *UnitOfWork) Targeted() Containers {
c := []Container{}
for _, name := range uow.order {
if includes(uow.targeted, name) {
c = append(c, cfg.Container(name))
}
}
return c
}
func (uow *UnitOfWork) TargetedInfo() []ContainerInfo {
c := []ContainerInfo{}
for _, name := range uow.order {
if includes(uow.targeted, name) {
c = append([]ContainerInfo{cfg.ContainerInfo(name)}, c...)
}
}
return c
}
func (uow *UnitOfWork) Associated() []string {
c := []string{}
for _, name := range uow.order {
if !includes(uow.targeted, name) {
c = append(c, name)
}
}
return c
}
func (uow *UnitOfWork) RequiredNetworks() []string {
required := []string{}
networks := cfg.NetworkNames()
if len(networks) == 0 {
return required
}
for _, container := range uow.Containers() {
net := container.RunParams().Net()
if includes(networks, net) && !includes(required, net) {
required = append(required, net)
}
}
return required
}
func (uow *UnitOfWork) RequiredVolumes() []string {
required := []string{}
volumes := cfg.VolumeNames()
if len(volumes) == 0 {
return required
}
for _, container := range uow.Containers() {
for _, volumeSource := range container.RunParams().VolumeSources() {
if includes(volumes, volumeSource) && !includes(required, volumeSource) {
required = append(required, volumeSource)
}
}
}
return required
}
func (uow *UnitOfWork) ensureInContainers(name string) {
if !includes(uow.containers, name) {
uow.containers = append(uow.containers, name)
}
}
func (uow *UnitOfWork) ensureInRequireStarted(name string) {
if !includes(uow.requireStarted, name) {
uow.requireStarted = append(uow.requireStarted, name)
}
}
func (uow *UnitOfWork) prepareRequirements() {
uow.prepareNetworks()
uow.prepareVolumes()
}
func (uow *UnitOfWork) prepareNetworks() {
for _, n := range uow.RequiredNetworks() {
net := cfg.Network(n)
if !net.Exists() {
net.Create()
}
}
}
func (uow *UnitOfWork) prepareVolumes() {
for _, v := range uow.RequiredVolumes() {
vol := cfg.Volume(v)
if !vol.Exists() {
vol.Create()
}
}
}