/
config.go
287 lines (263 loc) · 7.86 KB
/
config.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
package crane
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"gopkg.in/v1/yaml"
"io/ioutil"
"os"
"path/filepath"
"sort"
)
type Config interface {
TargetedContainers() Containers
DependencyGraph() DependencyGraph
}
type config struct {
RawContainerMap map[string]*container `json:"containers" yaml:"containers"`
RawGroups map[string][]string `json:"groups" yaml:"groups"`
containerMap ContainerMap
dependencyGraph DependencyGraph
target Target
order []string
groups map[string][]string
}
// ContainerMap maps the container name
// to its configuration
type ContainerMap map[string]Container
type Target []string
// configFiles returns a slice of
// files to read the config from.
// If the --config option was given,
// it will only use the given file.
func configFiles(options Options) []string {
if len(options.config) > 0 {
return []string{options.config}
} else {
return []string{"crane.json", "crane.yaml", "crane.yml"}
}
}
// readConfig will read the config file
// and return the created config.
func readConfig(filename string) *config {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(StatusError{err, 74})
}
ext := filepath.Ext(filename)
return unmarshal(data, ext)
}
// displaySyntaxError will display more information
// such as line and error type given an error and
// the data that was unmarshalled.
// Thanks to https://github.com/markpeek/packer/commit/5bf33a0e91b2318a40c42e9bf855dcc8dd4cdec5
func displaySyntaxError(data []byte, syntaxError error) (err error) {
syntax, ok := syntaxError.(*json.SyntaxError)
if !ok {
err = syntaxError
return
}
newline := []byte{'\x0a'}
space := []byte{' '}
start, end := bytes.LastIndex(data[:syntax.Offset], newline)+1, len(data)
if idx := bytes.Index(data[start:], newline); idx >= 0 {
end = start + idx
}
line, pos := bytes.Count(data[:start], newline)+1, int(syntax.Offset)-start-1
err = fmt.Errorf("\nError in line %d: %s \n%s\n%s^", line, syntaxError, data[start:end], bytes.Repeat(space, pos))
return
}
// unmarshal converts either JSON
// or YAML into a config object.
func unmarshal(data []byte, ext string) *config {
var config *config
var err error
if ext == ".json" {
err = json.Unmarshal(data, &config)
} else if ext == ".yml" || ext == ".yaml" {
err = yaml.Unmarshal(data, &config)
} else {
panic(StatusError{errors.New("Unrecognized file extension"), 65})
}
if err != nil {
err = displaySyntaxError(data, err)
panic(StatusError{err, 65})
}
return config
}
// NewConfig retus a new config based on given
// options.
// Containers will be ordered so that they can be
// brought up and down with Docker.
func NewConfig(options Options, forceOrder bool) Config {
var config *config
for _, f := range configFiles(options) {
if _, err := os.Stat(f); err == nil {
config = readConfig(f)
break
}
}
if config == nil {
panic(StatusError{fmt.Errorf("No configuration found %v", configFiles(options)), 78})
}
config.expandEnv()
config.dependencyGraph = config.DependencyGraph()
config.determineTarget(options.target, options.cascadeDependencies, options.cascadeAffected)
var err error
config.order, err = config.dependencyGraph.order(config.target, forceOrder)
if err != nil {
panic(StatusError{err, 78})
}
return config
}
// Containers returns the containers of the config in order
func (c *config) TargetedContainers() Containers {
var containers Containers
for _, name := range c.order {
containers = append([]Container{c.containerMap[name]}, containers...)
}
return containers
}
// expandEnv creates a new container map
// with expanded names and sets the RawName of each
// container to the map key.
// It also expand variables in the groups.
func (c *config) expandEnv() {
// Container map
c.containerMap = make(map[string]Container)
for rawName, container := range c.RawContainerMap {
container.RawName = rawName
c.containerMap[container.Name()] = container
}
// Groups
c.groups = make(map[string][]string)
for groupRawName, rawNames := range c.RawGroups {
for _, rawName := range rawNames {
c.groups[groupRawName] = append(c.groups[groupRawName], os.ExpandEnv(rawName))
}
}
}
// DependencyGraph returns the dependency graph, which is
// a map describing the dependencies between the containers.
func (c *config) DependencyGraph() DependencyGraph {
dependencyGraph := make(DependencyGraph)
for _, container := range c.containerMap {
dependencyGraph[container.Name()] = container.Dependencies()
}
return dependencyGraph
}
// determineTarget receives the specified target
// and determines which containers should be targeted.
// The target might be extended depending on the value
// given for cascadeDependencies and cascadeAffected.
// Additionally, the target is sorted alphabetically.
func (c *config) determineTarget(target []string, cascadeDependencies string, cascadeAffected string) {
// start from the explicitly targeted target
includedSet := make(map[string]bool)
cascadingSeeds := []string{}
for _, name := range c.explicitlyTargeted(target) {
includedSet[name] = true
cascadingSeeds = append(cascadingSeeds, name)
}
// Cascade until the graph has been fully traversed
// according to the cascading flags.
for len(cascadingSeeds) > 0 {
nextCascadingSeeds := []string{}
for _, seed := range cascadingSeeds {
if cascadeDependencies != "none" {
if dependencies, ok := c.dependencyGraph[seed]; ok {
// Queue direct dependencies if we haven't already considered them
for _, name := range dependencies.forKind(cascadeDependencies) {
if _, alreadyIncluded := includedSet[name]; !alreadyIncluded {
includedSet[name] = true
nextCascadingSeeds = append(nextCascadingSeeds, name)
}
}
}
}
if cascadeAffected != "none" {
// Queue all containers we haven't considered yet which exist
// and directly depend on the seed.
for name, container := range c.containerMap {
if _, alreadyIncluded := includedSet[name]; !alreadyIncluded {
if container.Dependencies().includesAsKind(seed, cascadeAffected) && container.Exists() {
includedSet[name] = true
nextCascadingSeeds = append(nextCascadingSeeds, name)
}
}
}
}
}
cascadingSeeds = nextCascadingSeeds
}
// Keep the ones that are part of the container map
included := []string{}
for name := range includedSet {
if _, exists := c.containerMap[name]; exists {
included = append(included, name)
}
}
// Sort alphabetically
c.target = Target(included)
sort.Strings(c.target)
}
// explicitlyTargeted receives a target and determines which
// containers of the map are targeted.
func (c *config) explicitlyTargeted(target []string) (result []string) {
result = []string{}
// target not given
if len(target) == 0 {
// If default group exists, return its containers
for group, containers := range c.groups {
if group == "default" {
return containers
}
}
// If no default group exists, return all containers
for name, _ := range c.containerMap {
result = append(result, name)
}
return
}
// target given
for _, reference := range target {
success := false
reference = os.ExpandEnv(reference)
// Select reference from listed groups
for group, containers := range c.groups {
if group == reference {
result = append(result, containers...)
success = true
break
}
}
if success {
continue
}
// The reference might just be one container
for name, _ := range c.containerMap {
if name == reference {
result = append(result, reference)
success = true
break
}
}
if success {
continue
}
// Otherwise, fail verbosely
panic(StatusError{fmt.Errorf("No group or container matching `%s`", reference), 64})
}
return
}
// includes checks whether the given needle is
// included in the target
func (t Target) includes(needle string) bool {
for _, name := range t {
if name == needle {
return true
}
}
return false
}