/
config.go
297 lines (275 loc) · 8.09 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
package crane
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"gopkg.in/v2/yaml"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
)
type Config interface {
DependencyGraph(excluded []string) DependencyGraph
ContainersForReference(reference string) (result []string)
Path() string
UniqueId() string
Prefix() string
ContainerMap() ContainerMap
Container(name string) Container
}
type config struct {
RawContainerMap map[string]*container `json:"containers" yaml:"containers"`
RawGroups map[string][]string `json:"groups" yaml:"groups"`
RawHooksMap map[string]hooks `json:"hooks" yaml:"hooks"`
containerMap ContainerMap
groups map[string][]string
path string
prefix string
uniqueId string
}
// ContainerMap maps the container name
// to its configuration
type ContainerMap map[string]Container
// 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}
} else {
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)
}
// 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, prefix string) Config {
var config *config
configFile := findConfig(location)
if isVerbose() {
printInfof("Using configuration file `%s`\n", configFile)
}
config = readConfig(configFile)
config.initialize()
config.path = path.Dir(configFile)
config.prefix = prefix
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) ContainerMap() ContainerMap {
return c.containerMap
}
func (c *config) Container(name string) Container {
return c.containerMap[name]
}
// 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.RawContainerMap {
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.RawHooksMap {
hooksMap[os.ExpandEnv(hooksRawName)] = hooks
}
// Groups
c.groups = make(map[string][]string)
for groupRawName, rawNames := range c.RawGroups {
groupName := os.ExpandEnv(groupRawName)
for _, rawName := range rawNames {
c.groups[groupName] = append(c.groups[groupName], os.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
}
}
// DependencyGraph returns the dependency graph, which is
// a map describing the dependencies between the containers.
func (c *config) DependencyGraph(excluded []string) DependencyGraph {
dependencyGraph := make(DependencyGraph)
for _, container := range c.containerMap {
if !includes(excluded, container.Name()) {
dependencyGraph[container.Name()] = container.Dependencies(excluded)
}
}
return dependencyGraph
}
// 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 = os.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
}