/
config.go
452 lines (412 loc) · 11.5 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package crane
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"gopkg.in/v2/yaml"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/imdario/mergo"
)
type Config interface {
DependencyMap() map[string]*Dependencies
ContainersForReference(reference string) (result []string)
Path() string
UniqueID() string
Prefix() string
Tag() string
NetworkNames() []string
VolumeNames() []string
MacSyncNames() []string
Network(name string) Network
Volume(name string) Volume
MacSync(volume string) MacSync
ContainerMap() ContainerMap
Container(name string) Container
ContainerInfo(name string) ContainerInfo
}
type config struct {
RawContainers map[string]*container `json:"containers" yaml:"containers"`
RawGroups map[string][]string `json:"groups" yaml:"groups"`
RawHooks map[string]hooks `json:"hooks" yaml:"hooks"`
RawNetworks map[string]*network `json:"networks" yaml:"networks"`
RawVolumes map[string]*volume `json:"volumes" yaml:"volumes"`
RawMacSyncs map[string]*macSync `json:"mac-syncs" yaml:"mac-syncs"`
containerMap ContainerMap
networkMap NetworkMap
volumeMap VolumeMap
macSyncMap MacSyncMap
groups map[string][]string
path string
prefix string
tag string
uniqueID string
}
// ContainerMap maps the container name
// to its configuration
type ContainerMap map[string]Container
type NetworkMap map[string]Network
type VolumeMap map[string]Volume
type MacSyncMap map[string]MacSync
// configFilenames returns a slice of
// files to read the config from.
// If the --config option was given,
// it will only use the given file.
func configFilenames(location string) []string {
if len(location) > 0 {
return []string{location}
}
return []string{"crane.json", "crane.yaml", "crane.yml"}
}
// findConfig returns the filename of the
// config. It searches parent directories
// if it can't find any of the config
// filenames in the current directory.
func findConfig(location string) string {
configFiles := configFilenames(location)
// Absolute path to config given
if len(location) > 0 && path.IsAbs(location) {
if _, err := os.Stat(location); err == nil {
return location
}
} else { // Relative config
configPath, _ := os.Getwd()
for {
for _, f := range configFiles {
// the root path is a `/` but others don't have a trailing `/`
filename := strings.TrimSuffix(configPath, "/") + "/" + f
if _, err := os.Stat(filename); err == nil {
return filename
}
}
// loop only if we haven't yet reached the root
if parentPath := path.Dir(configPath); len(parentPath) != len(configPath) {
configPath = parentPath
} else {
break
}
}
}
panic(StatusError{fmt.Errorf("No configuration found %v", configFiles), 78})
}
// 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)
}
func findOverride(filename, override string) string {
if len(override) > 0 {
if !path.IsAbs(override) {
override = path.Join(path.Dir(filename), override)
}
if _, err := os.Stat(override); err == nil {
return override
} else {
panic(StatusError{fmt.Errorf("Override %v not found", override), 78})
}
}
overrideFile := ""
lastIndex := strings.LastIndex(filename, ".")
if lastIndex > 0 {
overrideFile = filename[:lastIndex] + ".override" + filename[lastIndex:]
} else {
overrideFile = filename + ".override"
}
if _, err := os.Stat(overrideFile); err == nil {
return overrideFile
}
return ""
}
// 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
// location.
// Containers will be ordered so that they can be
// brought up and down with Docker.
func NewConfig(location string, override string, prefix string, tag string) Config {
var config *config
configFile := findConfig(location)
if isVerbose() {
printInfof("Using configuration file `%s`\n", configFile)
}
configBase := readConfig(configFile)
overrideFile := findOverride(configFile, override)
if len(overrideFile) > 0 {
if isVerbose() {
printInfof("Using override file `%s`\n", overrideFile)
}
config = readConfig(overrideFile)
mergo.Merge(config, configBase)
} else {
config = configBase
}
config.path = path.Dir(configFile)
config.initialize()
config.validate()
config.prefix = prefix
config.tag = tag
milliseconds := time.Now().UnixNano() / 1000000
config.uniqueID = strconv.FormatInt(milliseconds, 10)
return config
}
// Return path of config file
func (c *config) Path() string {
return c.path
}
func (c *config) UniqueID() string {
return c.uniqueID
}
func (c *config) Prefix() string {
return c.prefix
}
func (c *config) Tag() string {
return c.tag
}
func (c *config) ContainerMap() ContainerMap {
return c.containerMap
}
func (c *config) Container(name string) Container {
return c.containerMap[name]
}
func (c *config) ContainerInfo(name string) ContainerInfo {
return c.Container(name)
}
func (c *config) NetworkNames() []string {
networks := []string{}
for name, _ := range c.networkMap {
networks = append(networks, name)
}
sort.Strings(networks)
return networks
}
func (c *config) VolumeNames() []string {
volumes := []string{}
for name, _ := range c.volumeMap {
volumes = append(volumes, name)
}
sort.Strings(volumes)
return volumes
}
func (c *config) MacSyncNames() []string {
macSyncs := []string{}
for name, _ := range c.macSyncMap {
macSyncs = append(macSyncs, name)
}
sort.Strings(macSyncs)
return macSyncs
}
func (c *config) Network(name string) Network {
return c.networkMap[name]
}
func (c *config) Volume(name string) Volume {
return c.volumeMap[name]
}
func (c *config) MacSync(name string) MacSync {
parts := strings.Split(name, ":")
if !path.IsAbs(parts[0]) {
parts[0] = c.path + "/" + parts[0]
}
return c.macSyncMap[strings.Join(parts, ":")]
}
// Load configuration into the internal structs from the raw, parsed ones
func (c *config) initialize() {
// Local container map to query by expanded name
containerMap := make(map[string]*container)
for rawName, container := range c.RawContainers {
container.RawName = rawName
containerMap[container.Name()] = container
}
// Local hooks map to query by expanded name
hooksMap := make(map[string]hooks)
for hooksRawName, hooks := range c.RawHooks {
hooksMap[expandEnv(hooksRawName)] = hooks
}
// Groups
c.groups = make(map[string][]string)
for groupRawName, rawNames := range c.RawGroups {
groupName := expandEnv(groupRawName)
for _, rawName := range rawNames {
c.groups[groupName] = append(c.groups[groupName], expandEnv(rawName))
}
if hooks, ok := hooksMap[groupName]; ok {
// attach group-defined hooks to the group containers
for _, name := range c.groups[groupName] {
if overriden := containerMap[name].hooks.CopyFrom(hooks); overriden {
panic(StatusError{fmt.Errorf("Multiple conflicting hooks inherited from groups for container `%s`", name), 64})
}
}
}
}
// Container map
c.containerMap = make(map[string]Container)
for name, container := range containerMap {
if hooks, ok := hooksMap[name]; ok {
// attach container-defined hooks, overriding potential group-inherited hooks
container.hooks.CopyFrom(hooks)
}
c.containerMap[name] = container
}
c.setNetworkMap()
c.setVolumeMap()
c.setMacSyncMap()
}
func (c *config) setNetworkMap() {
c.networkMap = make(map[string]Network)
for rawName, net := range c.RawNetworks {
if net == nil {
net = &network{}
}
net.RawName = rawName
c.networkMap[net.Name()] = net
}
}
func (c *config) setVolumeMap() {
c.volumeMap = make(map[string]Volume)
for rawName, vol := range c.RawVolumes {
if vol == nil {
vol = &volume{}
}
vol.RawName = rawName
c.volumeMap[vol.Name()] = vol
}
}
func (c *config) setMacSyncMap() {
c.macSyncMap = make(map[string]MacSync)
for rawVolume, sync := range c.RawMacSyncs {
if sync == nil {
sync = &macSync{}
}
sync.RawVolume = rawVolume
sync.configPath = c.path
c.macSyncMap[sync.Volume()] = sync
}
}
func (c *config) validate() {
for name, container := range c.RawContainers {
if len(container.RawImage) == 0 {
panic(StatusError{fmt.Errorf("No image specified for `%s`", name), 64})
}
}
}
// DependencyMap returns a map of containers to their dependencies.
func (c *config) DependencyMap() map[string]*Dependencies {
dependencyMap := make(map[string]*Dependencies)
for _, container := range c.containerMap {
if includes(allowed, container.Name()) {
dependencyMap[container.Name()] = container.Dependencies()
}
}
return dependencyMap
}
// ContainersForReference receives a reference and determines which
// containers of the map that resolves to.
func (c *config) ContainersForReference(reference string) (result []string) {
containers := []string{}
if len(reference) == 0 {
// reference not given
var defaultGroup []string
for group, containers := range c.groups {
if group == "default" {
defaultGroup = containers
break
}
}
if defaultGroup != nil {
// If default group exists, return its containers
containers = defaultGroup
} else {
// Otherwise, return all containers
for name := range c.containerMap {
containers = append(containers, name)
}
}
} else {
// reference given
reference = expandEnv(reference)
// Select reference from listed groups
for group, groupContainers := range c.groups {
if group == reference {
containers = append(containers, groupContainers...)
break
}
}
if len(containers) == 0 {
// The reference might just be one container
for name := range c.containerMap {
if name == reference {
containers = append(containers, reference)
break
}
}
}
if len(containers) == 0 {
// reference was not found anywhere
panic(StatusError{fmt.Errorf("No group or container matching `%s`", reference), 64})
}
}
// ensure all container references exist
for _, container := range containers {
containerDeclared := false
for name := range c.containerMap {
if container == name {
containerDeclared = true
break
}
}
if !containerDeclared {
panic(StatusError{fmt.Errorf("Invalid container reference `%s`", container), 64})
}
if !includes(result, container) {
result = append(result, container)
}
}
return
}