-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildFile.go
313 lines (294 loc) · 10.5 KB
/
buildFile.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
/*
Artisan Core - Automation Manager
Copyright (C) 2022-Present SouthWinds Tech Ltd - www.southwinds.io
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package data
import (
"fmt"
"gopkg.in/yaml.v2"
"os"
"runtime"
"southwinds.dev/artisan/conf"
"southwinds.dev/artisan/core"
"southwinds.dev/artisan/merge"
"path/filepath"
"strings"
)
// BuildFile structure of build.yaml file
type BuildFile struct {
// internal, the path from where the buildfile is loaded
path string
// the URI of the Git repo
GitURI string `yaml:"git_uri,omitempty"`
// the runtime to use to run functions
Runtime string `yaml:"runtime,omitempty"`
// the environment variables that apply to the build
// any variables defined at this level will be available to all build profiles
// in addition, the defined variables are added on top of the existing environment
Env map[string]string `yaml:"env,omitempty"`
// a list of labels to be added to the package seal
// they should be used to document key aspects of the package in a generic way
Labels map[string]string `yaml:"labels,omitempty"`
// any input required by functions
Input *Input `yaml:"input,omitempty"`
// a list of build configurations in the form of labels, commands to run and environment variables
Profiles []*Profile `yaml:"profiles,omitempty"`
// a list of functions containing a list of commands to execute
Functions []*Function `yaml:"functions"`
// include other build files
Includes []interface{} `yaml:"includes"`
SKU string `yaml:"sku"`
X map[string]string `json:"x,omitempty"`
}
func (b *BuildFile) GetEnv() map[string]string {
return b.Env
}
func (b *BuildFile) ExportFxs() bool {
for _, function := range b.Functions {
if function.Export != nil && *function.Export {
return true
}
}
return false
}
// DefaultProfile return the default profile if exists
func (b *BuildFile) DefaultProfile() *Profile {
for _, profile := range b.Profiles {
if profile.Default {
return profile
}
}
return nil
}
// Fx return the function in the build file specified by its name
func (b *BuildFile) Fx(name string) *Function {
for _, fx := range b.Functions {
if fx.Name == name {
return fx
}
}
return nil
}
type Profile struct {
// the name of the profile
Name string `yaml:"name"`
// whether this is the default profile
Default bool `yaml:"default"`
// the name of the application
Application string `yaml:"application"`
// the type of license used by the application
// if not empty, it is added to the package seal
License string `yaml:"license"`
// the type of technology used by the application that can be used to determine the tool chain to use
// e.g. java, nodejs, golang, python, php, etc
Type string `yaml:"type"`
// the pipeline Icon
Icon string `yaml:"icon"`
// a set of labels associated with the profile
Labels map[string]string `yaml:"labels"`
// a set of environment variables required by the run commands
Env map[string]string `yaml:"env"`
// the commands to be executed to build the application
Run []string `yaml:"run"`
// the output of the build process, namely either a file or a folder, that has to be compressed
// as part of the packaging process
Target string `yaml:"target"`
// merged target if existed, internal use only
MergedTarget string
}
// GetEnv gets a slice of string with each element containing key=value
func (p *Profile) GetEnv() map[string]string {
return p.Env
}
// Profile return the build profile specified by its name
func (b *BuildFile) Profile(name string) *Profile {
for _, profile := range b.Profiles {
if profile.Name == name {
return profile
}
}
return nil
}
// Survey all missing variables in the profile
func (p *Profile) Survey(bf *BuildFile) conf.Configuration {
env := bf.Env
// merges the profile environment with the passed in environment
for k, v := range p.Env {
env[k] = v
}
// attempt to merge any environment variable in the profile run commands
// run the merge in interactive mode so that any variables not available in the build file environment are surveyed
_, updatedEnvironment := core.MergeEnvironmentVars(p.Run, merge.NewEnVarFromMap(env), true)
// attempt to merge any environment variable in the functions run commands
for _, run := range p.Run {
// if the run line has a function
if ok, fxName := core.HasFunction(run); ok {
// merge any variables on the function
env = bf.Fx(fxName).Survey(merge.NewEnVarFromMap(env)).Vars()
}
}
return updatedEnvironment
}
func LoadBuildFile(path string) (*BuildFile, error) {
return LoadBuildFileWithEnv(path, nil, false)
}
func LoadBuildFileWithEnv(path string, ev conf.Configuration, isChild bool) (*BuildFile, error) {
if !filepath.IsAbs(path) {
abs, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("cannot get absolute path for %s", path)
}
path = abs
}
bytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot load build file from %s: %s", path, err)
}
core.Debug("loaded: '%s'\ncontent:\n%s\n", path, string(bytes))
buildFile := &BuildFile{
path: path,
}
err = yaml.Unmarshal(bytes, buildFile)
if err != nil {
return nil, fmt.Errorf("syntax error in build file %s: %s", path, err)
}
if !isChild {
if ok, validErr := buildFile.Validate(); !ok {
return buildFile, validErr
}
}
if buildFile.Env == nil {
buildFile.Env = map[string]string{}
}
if ev == nil {
ev = merge.NewEnvVarOS()
}
ev.Merge(merge.NewEnVarFromMap(buildFile.Env))
ev.Merge(buildFile.GetXEnv())
ev.Replace()
buildFile.Env = ev.Vars()
buildFile.Env[core.ArtOS] = runtime.GOOS
buildFile.Env[core.ArtArch] = runtime.GOARCH
buildFile.Env[core.ArtShell] = os.Getenv("SHELL")
for _, include := range buildFile.Includes {
switch i := include.(type) {
case string:
file, _ := filepath.Abs(filepath.Join(filepath.Dir(buildFile.path), i))
child, err := LoadBuildFileWithEnv(file, ev, true)
if err != nil {
return nil, fmt.Errorf("build file include not found in path: %s, %s", file, err)
}
buildFile.Env = conf.MergeMaps(buildFile.Env, child.Env)
buildFile.Profiles = append(buildFile.Profiles, child.Profiles...)
buildFile.Functions = append(buildFile.Functions, child.Functions...)
if child.Input != nil {
if child.Input.Var != nil {
if buildFile.Input == nil {
buildFile.Input = &Input{
Var: Vars{},
}
}
buildFile.Input.Var = append(buildFile.Input.Var, child.Input.Var...)
}
if child.Input.Secret != nil {
if buildFile.Input == nil {
buildFile.Input = &Input{
Secret: Secrets{},
}
}
buildFile.Input.Secret = append(buildFile.Input.Secret, child.Input.Secret...)
}
}
buildFile.Labels = conf.MergeMaps(buildFile.Labels, child.Labels)
case []interface{}:
incl := true
for _, condition := range i[1:] {
neq := strings.Split(condition.(string), "!=")
if len(neq) == 2 {
value := buildFile.Env[neq[0]]
incl = incl && !strings.EqualFold(value, neq[1])
} else {
eq := strings.Split(condition.(string), "=")
if len(eq) == 2 {
value := buildFile.Env[eq[0]]
incl = incl && strings.EqualFold(value, eq[1])
}
}
}
if incl {
file, _ := filepath.Abs(filepath.Join(filepath.Dir(buildFile.path), i[0].(string)))
child, err := LoadBuildFileWithEnv(file, ev, true)
if err != nil {
return nil, err
}
buildFile.Env = conf.MergeMaps(buildFile.Env, child.Env)
buildFile.Profiles = append(buildFile.Profiles, child.Profiles...)
buildFile.Functions = append(buildFile.Functions, child.Functions...)
buildFile.Labels = conf.MergeMaps(buildFile.Labels, child.Labels)
}
}
}
return buildFile, nil
}
func (b *BuildFile) GetXEnv() *merge.Envar {
env := merge.NewEnVarEmpty()
for key, value := range b.X {
env.Set(fmt.Sprintf("ART_X_%s", strings.ToUpper(key)), value)
}
return env
}
func (b *BuildFile) Validate() (bool, error) {
// checks any binding has a corresponding input
for _, fx := range b.Functions {
if fx.Input != nil {
if fx.Input.Var != nil {
for _, v := range fx.Input.Var {
// if no inputs were defined whatsoever or inputs were defined, but they do not match the bindings
if b.Input == nil || (b.Input != nil && !b.Input.HasVar(v)) {
return false, fmt.Errorf("function '%s' in build file '%s' has a Var binding '%s' but not corresponding Var definition has been defined in the build file Input section", fx.Name, b.path, v)
}
}
}
if fx.Input.Secret != nil {
for _, s := range fx.Input.Secret {
if !b.Input.HasSecret(s) && !strings.Contains(s, "ART_REG_USER") && !strings.Contains(s, "ART_REG_PWD") {
return false, fmt.Errorf("function '%s' in build file '%s' has a Secret binding '%s' but not corresponding Secret definition has been defined in the build file Input section", fx.Name, b.path, s)
}
}
}
}
if fx.Network != nil {
if fx.Export == nil || !*fx.Export {
return false, fmt.Errorf("network definition found in non exported function '%s'", fx.Name)
}
for _, group := range fx.Network.Groups {
if len(strings.Split(group, ":")) != 4 {
return false, fmt.Errorf("group declaration in network definition for function %s is incorrect: '%s', it must have 4 sections separated by ':' such as 'NAME:TAGS:MIN:MAX'", fx.Name, group)
}
}
for _, rule := range fx.Network.Rules {
if len(strings.Split(rule, ":")) != 3 {
return false, fmt.Errorf("rule declaration in network definition for function %s is incorrect: '%s', it must have 3 sections separated by ':' such as 'NAME_FROM:NAME_TO:PROTOCOL/PORT'", fx.Name, rule)
}
}
}
}
// check that any profiles do not have targets using "."
for _, profile := range b.Profiles {
if profile.Target == "." {
return false, fmt.Errorf("invalid target for profile '%s': it cannot point to the same location of the build file: "+
"the build file you use to build the package must not be the same as the one embedded in the package", profile.Name)
}
}
return true, nil
}