This repository has been archived by the owner on May 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
containerCollection.go
204 lines (159 loc) · 4.97 KB
/
containerCollection.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
package containerCollection
import (
"github.com/marmelab/gaudi/container"
"github.com/marmelab/gaudi/util"
"os"
)
type ContainerCollection map[string]*container.Container
func Merge(c1, c2 ContainerCollection) ContainerCollection {
result := make(ContainerCollection)
for name, currentContainer := range c1 {
result[name] = currentContainer
}
for name, currentContainer := range c2 {
result[name] = currentContainer
}
return result
}
func (collection ContainerCollection) Init(relativePath string) bool {
hasGaudiManagedContainer := false
// Fill name & dependencies
for name, currentContainer := range collection {
currentContainer.Name = name
currentContainer.Init()
if currentContainer.IsGaudiManaged() {
hasGaudiManagedContainer = true
currentContainer.Image = "gaudi/" + name
}
for _, dependency := range currentContainer.Links {
if depContainer, exists := collection[dependency]; exists {
currentContainer.AddDependency(depContainer)
} else {
util.LogError(name + " references a non existing application : " + dependency)
}
}
// Add relative path to volumes
for volumeHost, volumeContainer := range currentContainer.Volumes {
// Relative volume host
if string(volumeHost[0]) != "/" {
delete(currentContainer.Volumes, volumeHost)
volumeHost = relativePath + "/" + volumeHost
currentContainer.Volumes[volumeHost] = volumeContainer
}
// Create directory if needed
if !util.IsDir(volumeHost) {
err := os.MkdirAll(volumeHost, 0755)
if err != nil {
util.LogError(err)
}
}
}
}
return hasGaudiManagedContainer
}
func (collection ContainerCollection) Get(args ...interface{}) *container.Container {
if c, ok := collection[args[0].(string)]; ok {
return c
} else if len(args) == 2 {
return args[1].(*container.Container)
}
return nil
}
func (collection ContainerCollection) GetType(containerType string) *container.Container {
for _, currentContainer := range collection {
if currentContainer.Type == containerType {
return currentContainer
}
}
return nil
}
func (collection ContainerCollection) Start(rebuild bool) {
collection.CheckIfNotEmpty()
if rebuild {
collection.Clean()
collection.Build()
}
startChans := make(map[string]chan bool, len(collection))
// Start all applications
for name, currentContainer := range collection {
startChans[name] = make(chan bool)
go startOne(currentContainer, rebuild, startChans)
}
// Waiting for all applications to start
for name, _ := range collection {
<-startChans[name]
}
}
func (collection ContainerCollection) Stop() {
collection.CheckIfNotEmpty()
nbContainers := len(collection)
killChans := make(chan bool, nbContainers)
for _, currentContainer := range collection {
go currentContainer.Kill(false, killChans)
}
// Waiting for all applications to stop
waitForIt(killChans)
}
func (collection ContainerCollection) CheckIfNotEmpty() {
// Check if there is at least a container
if collection == nil || len(collection) == 0 {
util.LogError("Gaudi requires at least an application to be defined to start anything")
}
}
func (collection ContainerCollection) Clean() {
nbContainers := len(collection)
cleanChans := make(chan bool, nbContainers)
// Clean all applications
for _, currentContainer := range collection {
go currentContainer.Clean(cleanChans)
}
waitForIt(cleanChans)
}
func (collection ContainerCollection) Build() {
nbContainers := len(collection)
buildChans := make(chan bool, nbContainers)
// Build all
for _, currentContainer := range collection {
go currentContainer.BuildOrPull(buildChans)
}
waitForIt(buildChans)
}
func (collection ContainerCollection) IsComponentDependingOf(container *container.Container, otherComponentType string) bool {
for _, currentContainer := range collection {
if currentContainer.Type == otherComponentType && currentContainer.DependsOf(container.Type) {
return true
}
}
return false
}
func waitForIt(channels chan bool) {
nbContainers := cap(channels)
for i := 0; i < nbContainers; i++ {
<-channels
}
}
func (collection ContainerCollection) AddAmbassadors() {
for name, currentContainer := range collection {
if currentContainer.Ambassador.Type == "" {
continue
}
// Add the ambassador
ambassadorName := "ambassador-" + name
ambassador := &container.Container{Name: ambassadorName, Type: "ambassador"}
ambassador.Init()
ambassador.Links = append(ambassador.Links, name)
ambassador.Ports[currentContainer.Ambassador.Port] = currentContainer.Ambassador.Port
collection[ambassadorName] = ambassador
}
}
func startOne(currentContainer *container.Container, rebuild bool, done map[string]chan bool) {
// Waiting for dependencies to be started
for _, dependency := range currentContainer.Dependencies {
if dependency.Name == currentContainer.Name {
util.LogError("Application " + currentContainer.Name + " can't be linked with itself.")
}
<-done[dependency.Name]
}
currentContainer.Start(rebuild)
close(done[currentContainer.Name])
}