/
manifest.go
334 lines (273 loc) · 8.01 KB
/
manifest.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
package build
import (
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"github.com/cloud66/habitus/configuration"
"github.com/cloud66/habitus/secrets"
"gopkg.in/yaml.v2"
)
var (
validTypes = []string{"file"}
)
// Artifact holds a parsed source for a build artifact
type Artifact struct {
Step Step
Source string
Dest string // this is only the folder. Filename comes from the source
}
// Cleanup holds everything that's needed for a cleanup
type Cleanup struct {
Commands []string
}
// holds a single secret
type Secret struct {
Name string
Type string
Value string
}
// Step Holds a single step in the build process
// Public structs. They are used to store the build for the builders
type Step struct {
Name string
Label string
Dockerfile string
Artifacts []Artifact
Manifest Manifest
Cleanup *Cleanup
DependsOn []*Step
Command string
Secrets []Secret
}
// Manifest Holds the whole build process
type Manifest struct {
Steps []Step
IsPrivileged bool
SecretProviders map[string]secrets.SecretProvider
buildLevels [][]Step
}
type cleanup struct {
Commands []string `yaml:"commands"`
}
type secret struct {
Type string `yaml:"type"`
Value string `yaml:"value"`
}
// Private structs. They are used to load from yaml
type step struct {
Name string `yaml:"name"`
Dockerfile string `yaml:"dockerfile"`
Artifacts []string `yaml:"artifacts"`
Cleanup *cleanup `yaml:"cleanup"`
DependsOn []string `yaml:"depends_on"`
Command string `yaml:"command"`
Secrets map[string]secret `yaml:"secrets"`
}
// This is loaded from the build.yml file
type build struct {
Version string `yaml:"version"`
Workdir string `yaml:"work_dir"`
Steps map[string]step `yaml:"steps"`
}
// Habitus build namespace
type namespace struct {
BuildConfig build `yaml:"build"`
Config *configuration.Config
}
// LoadBuildFromFile loads Build from a yaml file
func LoadBuildFromFile(config *configuration.Config) (*Manifest, error) {
config.Logger.Noticef("Using '%s' as build file", config.Buildfile)
n := namespace{Config: config}
data, err := ioutil.ReadFile(config.Buildfile)
if err != nil {
return nil, err
}
data = parseForEnvVars(config, data)
err = yaml.Unmarshal([]byte(data), &n)
if err != nil {
return nil, err
}
// check the version. for now we are going to support only one version
// in future, version will select the parser
if (n.BuildConfig.Version != "2016-02-13") && (n.BuildConfig.Version != "2016-03-14") {
return nil, errors.New("Invalid build schema version")
}
return n.convertToBuild(n.BuildConfig.Version)
}
func (n *namespace) convertToBuild(version string) (*Manifest, error) {
r := Manifest{
SecretProviders: make(map[string]secrets.SecretProvider),
}
r.SecretProviders["file"] = &secrets.FileProvider{}
r.IsPrivileged = false
r.Steps = []Step{}
for name, s := range n.BuildConfig.Steps {
convertedStep := Step{}
convertedStep.Manifest = r
convertedStep.Dockerfile = s.Dockerfile
convertedStep.Name = s.Name
convertedStep.Label = name
convertedStep.Artifacts = []Artifact{}
convertedStep.Command = s.Command
if s.Cleanup != nil && !n.Config.NoSquash {
convertedStep.Cleanup = &Cleanup{Commands: s.Cleanup.Commands}
r.IsPrivileged = true
} else {
convertedStep.Cleanup = &Cleanup{}
}
// TODO: should done through proper schema validation
if version == "2016-03-14" {
for name, s := range s.Secrets {
convertedSecret := Secret{}
convertedSecret.Name = name
convertedSecret.Type = s.Type
convertedSecret.Value = s.Value
if !stringInSlice(s.Type, validTypes) {
return nil, fmt.Errorf("Invalid type %s'", s.Type)
}
if !stringInSlice(s.Type, strings.Split(n.Config.SecretProviders, ",")) {
return nil, fmt.Errorf("Unsupported type '%s'", s.Type)
}
r.SecretProviders[s.Type].RegisterSecret(name, s.Value)
convertedStep.Secrets = append(convertedStep.Secrets, convertedSecret)
}
}
for _, a := range s.Artifacts {
convertedArt := Artifact{}
convertedArt.Step = convertedStep
parts := strings.Split(a, ":")
convertedArt.Source = parts[0]
if len(parts) == 1 {
// only one use the base
convertedArt.Dest = "."
} else {
convertedArt.Dest = parts[1]
}
convertedStep.Artifacts = append(convertedStep.Artifacts, convertedArt)
}
// is it unique?
for _, s := range r.Steps {
if s.Name == convertedStep.Name {
return nil, fmt.Errorf("Step name '%s' is not unique", convertedStep.Name)
}
}
r.Steps = append(r.Steps, convertedStep)
}
// now that we have the Manifest built from the file, we can resolve dependencies
for idx, step := range r.Steps {
bStep := n.BuildConfig.Steps[step.Label]
for _, d := range bStep.DependsOn {
convertedStep, err := r.FindStepByLabel(d)
if err != nil {
return nil, err
}
if convertedStep == nil {
return nil, fmt.Errorf("can't find step %s", d)
}
r.Steps[idx].DependsOn = append(r.Steps[idx].DependsOn, convertedStep)
}
}
// build the dependency tree
bl, err := r.serviceOrder(r.Steps)
if err != nil {
return nil, err
}
r.buildLevels = bl
return &r, nil
}
func (m *Manifest) getStepsByLevel(level int) ([]Step, error) {
if level >= len(m.buildLevels) {
return nil, errors.New("level not available")
}
return m.buildLevels[level], nil
}
// takes in a list of steps and returns an array of steps ordered by their dependency order
// result[0] will be an array of all steps with no dependency
// result[1] will be an array of steps depending on one or more of result[0] steps and so on
func (m *Manifest) serviceOrder(mainList []Step) ([][]Step, error) {
list := append([]Step(nil), mainList...)
if len(list) == 0 {
return [][]Step{}, nil
}
var result [][]Step
// find all steps with no dependencies
for {
var level []Step
for _, step := range list {
if len(step.DependsOn) == 0 {
level = append(level, step)
}
}
// if none is found while there where items in the list, then we have a circular dependency somewhere
if len(list) != 0 && len(level) == 0 {
return nil, errors.New("Found circular dependency in services")
}
result = append(result, level)
// now take out all of those found from the list of other items (they are now 'resolved')
for idx, step := range list { // for every step
stepDeps := append([]*Step(nil), step.DependsOn...) // clone the dependency list so we can remove items from it
for kdx, dep := range stepDeps { // iterate through its dependeneis
for _, resolved := range level { // and find any resolved step in them and take it out
if resolved.Name == dep.Name {
list[idx].DependsOn = append(list[idx].DependsOn[:kdx], list[idx].DependsOn[kdx+1:]...)
}
}
}
}
// take out everything we have in this level from the list
for _, s := range level {
listCopy := append([]Step(nil), list...)
for idx, l := range listCopy {
if s.Name == l.Name {
list = append(list[:idx], list[idx+1:]...)
}
}
}
// we are done
if len(list) == 0 {
break
}
}
return result, nil
}
// FindStepByName finds a step by name. Returns nil if not found
func (m *Manifest) FindStepByName(name string) (*Step, error) {
for _, step := range m.Steps {
if step.Name == name {
return &step, nil
}
}
return nil, nil
}
func (m *Manifest) FindStepByLabel(label string) (*Step, error) {
for _, step := range m.Steps {
if step.Label == label {
return &step, nil
}
}
return nil, nil
}
func parseForEnvVars(config *configuration.Config, value []byte) []byte {
r, _ := regexp.Compile("(?U)_env\\((.*)\\)")
matched := r.ReplaceAllFunc(value, func(s []byte) []byte {
m := string(s)
parts := r.FindStringSubmatch(m)
if len(config.EnvVars) == 0 {
return []byte(os.Getenv(parts[1]))
} else {
return []byte(config.EnvVars.Find(parts[1]))
}
})
return matched
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}