-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.go
207 lines (154 loc) · 4.19 KB
/
job.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
package tug
import (
"log"
"os"
"os/exec"
"strings"
)
// Job models a Docker muliti-image build operation.
type Job struct {
// Debug can enable additional logging.
Debug bool
// Push can push cached buildx images to the remote Docker registry
// as a side effect during builds.
Push bool
// Builder denotes a buildx builder.
Builder string
// LoadPlatform can load the image for a given platform
// onto the local Docker registry as a side effect during builds.
LoadPlatform *string
// Platforms denotes the list of targeted image platforms.
Platforms []Platform
// OsExclusions skips the given operating systems.
OsExclusions []string
// ArchExclusions skips the given architectures.
ArchExclusions []string
// ListImageName can query the buildx cache
// for any multi-platform images matching the given image name,
// of the form name[:tag].
ListImageName *string
// ImageName denotes the buildx image artifact name,
// of the form name[:tag].
ImageName *string
// BatchSize restricts the number of concurrent builds.
// Zero indicates no restriction.
BatchSize int
// ExtraFlags sends additional command line flags to docker buildx build commands.
ExtraFlags []string
// Directory denotes the Docker build directory (defaults behavior assumes the current working directory).
Directory string
// DockerfileSource denotes the Dockerfile filename, relative to Directory. Default: Dockerfile.
DockerfileSource string
}
// NewJob initializes tug and generates a default Job.
func NewJob(debug bool) (*Job, error) {
platforms, err := AvailablePlatforms(debug)
if err != nil {
return nil, err
}
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
return &Job{
Builder: TugBuilderName,
Platforms: DisableNichePlatforms(platforms),
Directory: cwd,
}, nil
}
// runBatch executes a batch of platforms.
func (o Job) runBatch() error {
cmd := exec.Command("docker")
cmd.Env = os.Environ()
// Work around spurious buildx warnings
cmd.Env = append(cmd.Env, "BUILDX_NO_DEFAULT_LOAD=true")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Args = []string{"docker", "buildx"}
if o.ListImageName != nil {
cmd.Args = append(cmd.Args, "imagetools", "inspect", *o.ListImageName)
return cmd.Run()
}
cmd.Args = append(cmd.Args, "build", "--builder", TugBuilderName)
var platformPairs []string
for _, platform := range o.Platforms {
platformPairs = append(platformPairs, platform.Format())
}
cmd.Args = append(cmd.Args, "--platform")
if o.LoadPlatform == nil {
cmd.Args = append(cmd.Args, strings.Join(platformPairs, ","))
} else {
cmd.Args = append(cmd.Args, *o.LoadPlatform)
cmd.Args = append(cmd.Args, "--load")
}
if o.Push {
cmd.Args = append(cmd.Args, "--push")
}
cmd.Args = append(cmd.Args, "-t", *o.ImageName)
if o.DockerfileSource != "" {
cmd.Args = append(cmd.Args, "-f", o.DockerfileSource)
}
cmd.Args = append(cmd.Args, o.ExtraFlags...)
cmd.Args = append(cmd.Args, o.Directory)
if o.Debug {
log.Printf("Command: %v\n", cmd)
}
return cmd.Run()
}
// Run schedules builds.
func (o Job) Run() error {
var platforms []Platform
for _, platform := range o.Platforms {
var excludedOs bool
for _, osExclusion := range o.OsExclusions {
if platform.Os == osExclusion {
excludedOs = true
break
}
}
if excludedOs {
continue
}
var excludedArch bool
for _, archExclusion := range o.ArchExclusions {
if platform.Arch == archExclusion {
excludedArch = true
break
}
}
if excludedArch {
continue
}
platforms = append(platforms, platform)
}
o.Platforms = platforms
batchSize := o.BatchSize
if batchSize == 0 {
return o.runBatch()
}
var platformGroups [][]Platform
for len(o.Platforms) != 0 {
if len(o.Platforms) < batchSize {
batchSize = len(o.Platforms)
}
platformGroups = append(platformGroups, o.Platforms[0:batchSize])
o.Platforms = o.Platforms[batchSize:]
}
//
// Work around corruption glitch in buildx --push.
//
push := o.Push
o.Push = false
for _, platformGroup := range platformGroups {
o.Platforms = platformGroup
if err := o.runBatch(); err != nil {
return err
}
}
if push {
o.Push = true
o.Platforms = platforms
return o.runBatch()
}
return nil
}