-
Notifications
You must be signed in to change notification settings - Fork 18
/
dockerbuilder.go
197 lines (155 loc) · 5.72 KB
/
dockerbuilder.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
package task
import (
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/internal/executor/environment"
"github.com/cirruslabs/cirrus-cli/pkg/parser/boolevator"
"github.com/cirruslabs/cirrus-cli/pkg/parser/nameable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/node"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parseable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parserkit"
"github.com/cirruslabs/cirrus-cli/pkg/parser/schema"
"github.com/cirruslabs/cirrus-cli/pkg/parser/task/command"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
jsschema "github.com/lestrrat-go/jsschema"
"google.golang.org/protobuf/types/known/anypb"
"strconv"
"strings"
)
type DockerBuilder struct {
proto api.Task
platform api.Platform
osVersion string
fallbackName string
alias string
dependsOn []string
onlyIfExpression string
parseable.DefaultParser
}
func NewDockerBuilder(
env map[string]string,
parserKit *parserkit.ParserKit,
additionalTaskProperties []*descriptor.FieldDescriptorProto,
) *DockerBuilder {
dbuilder := &DockerBuilder{}
dbuilder.proto.Environment = map[string]string{"CIRRUS_OS": "linux"}
AttachEnvironmentFields(&dbuilder.DefaultParser, &dbuilder.proto)
AttachBaseTaskFields(&dbuilder.DefaultParser, &dbuilder.proto, env, parserKit, additionalTaskProperties)
AttachBaseTaskInstructions(&dbuilder.DefaultParser, &dbuilder.proto, env, parserKit)
dbuilder.OptionalField(nameable.NewSimpleNameable("alias"), schema.String(""), func(node *node.Node) error {
name, err := node.GetExpandedStringValue(environment.Merge(dbuilder.proto.Environment, env))
if err != nil {
return err
}
dbuilder.alias = name
return nil
})
dependsOnSchema := schema.StringOrListOfStrings("List of task names this task depends on.")
dbuilder.OptionalField(nameable.NewSimpleNameable("depends_on"), dependsOnSchema, func(node *node.Node) error {
dependsOn, err := node.GetSliceOfNonEmptyStrings()
if err != nil {
return err
}
dbuilder.dependsOn = dependsOn
return nil
})
dbuilder.CollectibleField("only_if", schema.Condition(""), func(node *node.Node) error {
onlyIfExpression, err := node.GetStringValue()
if err != nil {
return err
}
dbuilder.onlyIfExpression = onlyIfExpression
return nil
})
// no-op
sipSchema := schema.Condition("")
dbuilder.OptionalField(nameable.NewSimpleNameable("use_static_ip"), sipSchema, func(node *node.Node) error {
return nil
})
platformSchema := schema.Enum([]interface{}{"linux", "windows"}, "Container Platform.")
dbuilder.OptionalField(nameable.NewSimpleNameable("platform"), platformSchema, func(node *node.Node) error {
platformName, err := node.GetExpandedStringValue(environment.Merge(dbuilder.proto.Environment, env))
if err != nil {
return err
}
platformValue, ok := api.Platform_value[strings.ToUpper(platformName)]
if !ok {
return node.ParserError("unknown platform name: %q", platformName)
}
dbuilder.platform = api.Platform(platformValue)
dbuilder.proto.Environment["CIRRUS_OS"] = platformName
return nil
})
osVersionSchema := schema.Enum([]interface{}{"2019", "1709", "1803"}, "Windows version of container.")
dbuilder.OptionalField(nameable.NewSimpleNameable("os_version"), osVersionSchema, func(node *node.Node) error {
osVersion, err := node.GetExpandedStringValue(environment.Merge(dbuilder.proto.Environment, env))
if err != nil {
return err
}
dbuilder.osVersion = osVersion
return nil
})
return dbuilder
}
func (dbuilder *DockerBuilder) Parse(node *node.Node, parserKit *parserkit.ParserKit) error {
if err := dbuilder.DefaultParser.Parse(node, parserKit); err != nil {
return err
}
instance := &api.DockerBuilder{
Platform: dbuilder.platform,
OsVersion: dbuilder.osVersion,
}
anyInstance, err := anypb.New(instance)
if err != nil {
return err
}
dbuilder.proto.Instance = anyInstance
// Since the parsing is almost done and no other commands are expected,
// we can safely append cache upload commands, if applicable
dbuilder.proto.Commands = append(dbuilder.proto.Commands, command.GenUploadCacheCmds(dbuilder.proto.Commands)...)
return nil
}
func (dbuilder *DockerBuilder) Name() string {
return dbuilder.proto.Name
}
func (dbuilder *DockerBuilder) SetName(name string) {
dbuilder.proto.Name = name
}
func (dbuilder *DockerBuilder) FallbackName() string {
return dbuilder.fallbackName
}
func (dbuilder *DockerBuilder) SetFallbackName(name string) {
dbuilder.fallbackName = name
}
func (dbuilder *DockerBuilder) Alias() string {
return dbuilder.alias
}
func (dbuilder *DockerBuilder) DependsOnNames() []string {
return dbuilder.dependsOn
}
func (dbuilder *DockerBuilder) ID() int64 { return dbuilder.proto.LocalGroupId }
func (dbuilder *DockerBuilder) SetID(id int64) {
dbuilder.proto.LocalGroupId = id
}
func (dbuilder *DockerBuilder) SetIndexWithinBuild(id int64) {
dbuilder.proto.Metadata.Properties["indexWithinBuild"] = strconv.FormatInt(id, 10)
}
func (dbuilder *DockerBuilder) Proto() interface{} {
return &dbuilder.proto
}
func (dbuilder *DockerBuilder) DependsOnIDs() []int64 { return dbuilder.proto.RequiredGroups }
func (dbuilder *DockerBuilder) SetDependsOnIDs(ids []int64) { dbuilder.proto.RequiredGroups = ids }
func (dbuilder *DockerBuilder) Enabled(env map[string]string, boolevator *boolevator.Boolevator) (bool, error) {
if dbuilder.onlyIfExpression == "" {
return true, nil
}
evaluation, err := boolevator.Eval(dbuilder.onlyIfExpression, environment.Merge(dbuilder.proto.Environment, env))
if err != nil {
return false, err
}
return evaluation, nil
}
func (dbuilder *DockerBuilder) Schema() *jsschema.Schema {
modifiedSchema := dbuilder.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
return modifiedSchema
}