forked from GoogleCloudPlatform/cloud-build-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalbuilder_main.go
377 lines (328 loc) · 11.7 KB
/
localbuilder_main.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2017 Google, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main runs the gcb local builder.
package main // import "github.com/GoogleCloudPlatform/container-builder-local"
import (
"bufio"
"bytes"
"context"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
computeMetadata "cloud.google.com/go/compute/metadata"
"golang.org/x/oauth2"
"github.com/google/uuid"
"github.com/GoogleCloudPlatform/container-builder-local/build"
"github.com/GoogleCloudPlatform/container-builder-local/common"
"github.com/GoogleCloudPlatform/container-builder-local/config"
"github.com/GoogleCloudPlatform/container-builder-local/gcloud"
"github.com/GoogleCloudPlatform/container-builder-local/metadata"
"github.com/GoogleCloudPlatform/container-builder-local/runner"
"github.com/GoogleCloudPlatform/container-builder-local/subst"
"github.com/GoogleCloudPlatform/container-builder-local/validate"
"github.com/GoogleCloudPlatform/container-builder-local/volume"
)
const (
volumeNamePrefix = "cloudbuild_vol_"
gcbDockerVersion = "17.06.1-ce"
metadataImageName = "gcr.io/cloud-builders/metadata"
)
var (
configFile = flag.String("config", "cloudbuild.yaml", "File path of the config file")
substitutions = flag.String("substitutions", "", `key=value pairs where the key is already defined in the build request; separate multiple substitutions with a comma, for example: _FOO=bar,_BAZ=baz`)
dryRun = flag.Bool("dryrun", true, "Lints the config file and prints but does not run the commands; Local Builder runs the commands only when dryrun is set to false")
push = flag.Bool("push", false, "Pushes the images to the registry")
noSource = flag.Bool("no-source", false, "Prevents Local Builder from using source for this build")
writeWorkspace = flag.String("write-workspace", "", "Copies the workspace directory to this host directory")
help = flag.Bool("help", false, "Prints the help message")
versionFlag = flag.Bool("version", false, "Prints the local builder version")
)
func exitUsage(msg string) {
log.Fatalf("%s\nUsage: %s --config=cloudbuild.yaml [--substitutions=_FOO=bar] [--dryrun=true/false] [--push=true/false] source", msg, os.Args[0])
}
func main() {
flag.Parse()
args := flag.Args()
if *help {
flag.PrintDefaults()
return
}
if *versionFlag {
log.Printf("Version: %s", version)
return
}
nbSource := 1
if *noSource {
nbSource = 0
}
if len(args) < nbSource {
exitUsage("Specify a source")
} else if len(args) > nbSource {
if nbSource == 1 {
exitUsage("There should be only one positional argument. Pass all the flags before the source.")
} else {
exitUsage("no-source flag can't be used along with source.")
}
}
source := ""
if nbSource == 1 {
source = args[0]
}
if *configFile == "" {
exitUsage("Specify a config file")
}
if err := run(source); err != nil {
log.Fatal(err)
}
}
// run method is used to encapsulate the local builder process, being
// able to return errors to the main function which will then panic. So the
// run function can probably run all the defer functions in any case.
func run(source string) error {
// Create a runner.
r := &runner.RealRunner{
DryRun: *dryRun,
}
// Channel to tell goroutines to stop.
// Do not defer the close() because we want this stop to happen before other
// defer functions.
stopchan := make(chan struct{})
// Clean leftovers from a previous build.
if err := common.Clean(r); err != nil {
return fmt.Errorf("Error cleaning: %v", err)
}
// Check installed docker versions.
if !*dryRun {
dockerServerVersion, dockerClientVersion, err := dockerVersions(r)
if err != nil {
return fmt.Errorf("Error getting local docker versions: %v", err)
}
if dockerServerVersion != gcbDockerVersion {
log.Printf("Warning: The server docker version installed (%s) is different from the one used in GCB (%s)", dockerServerVersion, gcbDockerVersion)
}
if dockerClientVersion != gcbDockerVersion {
log.Printf("Warning: The client docker version installed (%s) is different from the one used in GCB (%s)", dockerClientVersion, gcbDockerVersion)
}
}
// Load config file into a build struct.
buildConfig, err := config.Load(*configFile)
if err != nil {
return fmt.Errorf("Error loading config file: %v", err)
}
// Parse substitutions.
if *substitutions != "" {
substMap, err := common.ParseSubstitutionsFlag(*substitutions)
if err != nil {
return fmt.Errorf("Error parsing substitutions flag: %v", err)
}
buildConfig.Substitutions = substMap
}
// Get the ProjectId to feed both the build and the metadata server.
// This command uses a runner without dryrun to return the real project.
projectInfo, err := gcloud.ProjectInfo(&runner.RealRunner{})
if err != nil {
return fmt.Errorf("Error getting project information from gcloud: %v", err)
}
buildConfig.ProjectId = projectInfo.ProjectID
// Validate the build.
if err := validate.CheckBuild(buildConfig); err != nil {
return fmt.Errorf("Error validating build: %v", err)
}
// Apply substitutions.
if err := subst.SubstituteBuildFields(buildConfig); err != nil {
return fmt.Errorf("Error applying substitutions: %v", err)
}
// Validate the build after substitutions.
if err := validate.CheckBuildAfterSubstitutions(buildConfig); err != nil {
return fmt.Errorf("Error validating build after substitutions: %v", err)
}
// Create a volume, a helper container to copy the source, and defer cleaning.
volumeName := fmt.Sprintf("%s%s", volumeNamePrefix, uuid.New())
if !*dryRun {
vol := volume.New(volumeName, r)
if err := vol.Setup(); err != nil {
return fmt.Errorf("Error creating docker volume: %v", err)
}
if source != "" {
// If the source is a directory, only copy the inner content.
if isDir, err := isDirectory(source); err != nil {
return fmt.Errorf("Error getting directory: %v", err)
} else if isDir {
source = filepath.Clean(source) + "/."
}
if err := vol.Copy(source); err != nil {
return fmt.Errorf("Error copying source to docker volume: %v", err)
}
}
defer vol.Close()
if *writeWorkspace != "" {
defer vol.Export(*writeWorkspace)
}
}
b := build.New(r, *buildConfig, nil /* TokenSource */, stdoutLogger{}, nopEventLogger{}, volumeName, true, *push, *dryRun)
// Do not run the spoofed metadata server on a dryrun.
if !*dryRun {
// Set initial Docker credentials.
tok, err := gcloud.AccessToken(r)
if err != nil {
return fmt.Errorf("Error getting access token to set docker credentials: %v", err)
}
if err := b.SetDockerAccessToken(tok.AccessToken); err != nil {
return fmt.Errorf("Error setting docker credentials: %v", err)
}
b.TokenSource = oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: tok.AccessToken,
})
// On GCE, do not create a spoofed metadata server, use the existing one.
// The cloudbuild network is still needed, with a private subnet.
if computeMetadata.OnGCE() {
if err := metadata.CreateCloudbuildNetwork(r, "172.22.0.0/16"); err != nil {
return fmt.Errorf("Error creating network: %v", err)
}
defer metadata.CleanCloudbuildNetwork(r)
} else {
if err := metadata.StartLocalServer(r, metadataImageName); err != nil {
return fmt.Errorf("Failed to start spoofed metadata server: %v", err)
}
log.Println("Started spoofed metadata server")
metadataUpdater := metadata.RealUpdater{Local: true}
defer metadataUpdater.Stop(r)
// Feed the project info to the metadata server.
metadataUpdater.SetProjectInfo(projectInfo)
go supplyTokenToMetadata(metadataUpdater, r, stopchan)
}
// Write docker credentials for GCR. This writes the initial
// ~/.docker/config.json, which is made available to build steps, and keeps
// a fresh token available. Note that the user could `gcloud auth` to
// switch accounts mid-build, and we wouldn't notice that until token
// refresh; switching accounts mid-build is not supported.
go func(tok *metadata.Token, stopchan <-chan struct{}) {
for {
refresh := time.Duration(0)
if tok != nil {
refresh = common.RefreshDuration(tok.Expiry)
}
select {
case <-time.After(refresh):
var err error
tok, err = gcloud.AccessToken(r)
if err != nil {
log.Printf("Error getting access token to update docker credentials: %v\n", err)
continue
}
if err := b.UpdateDockerAccessToken(tok.AccessToken); err != nil {
log.Printf("Error updating docker credentials: %v", err)
}
b.TokenSource = oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: tok.AccessToken,
})
case <-stopchan:
return
}
}
}(tok, stopchan)
}
b.Start()
<-b.Done
close(stopchan)
if b.GetStatus() == build.StatusError {
return fmt.Errorf("Build finished with ERROR status")
}
if *dryRun {
log.Printf("Warning: this was a dry run; add --dryrun=false if you want to run the build locally.")
}
return nil
}
// supplyTokenToMetadata gets gcloud token and supply it to the metadata server.
func supplyTokenToMetadata(metadataUpdater metadata.RealUpdater, r runner.Runner, stopchan <-chan struct{}) {
for {
tok, err := gcloud.AccessToken(r)
if err != nil {
log.Printf("Error getting gcloud token: %v", err)
continue
}
if err := metadataUpdater.SetToken(tok); err != nil {
log.Printf("Error updating token in metadata server: %v", err)
continue
}
refresh := common.RefreshDuration(tok.Expiry)
select {
case <-time.After(refresh):
continue
case <-stopchan:
return
}
}
}
// dockerVersion gets local server and client docker versions.
func dockerVersions(r runner.Runner) (string, string, error) {
cmd := []string{"docker", "version", "--format", "{{.Server.Version}}"}
var serverb bytes.Buffer
if err := r.Run(cmd, nil, &serverb, os.Stderr, ""); err != nil {
return "", "", err
}
cmd = []string{"docker", "version", "--format", "{{.Client.Version}}"}
var clientb bytes.Buffer
if err := r.Run(cmd, nil, &clientb, os.Stderr, ""); err != nil {
return "", "", err
}
return strings.TrimSpace(serverb.String()), strings.TrimSpace(clientb.String()), nil
}
func isDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
mode := fileInfo.Mode()
return mode.IsDir(), nil
}
type stdoutLogger struct{}
func (stdoutLogger) MakeWriter(prefix string, _ int, stdout bool) io.Writer {
if stdout {
return prefixWriter{os.Stdout, prefix}
}
return prefixWriter{os.Stderr, prefix}
}
func (stdoutLogger) Close() error { return nil }
func (stdoutLogger) WriteMainEntry(msg string) { fmt.Fprintln(os.Stdout, msg) }
type prefixWriter struct {
w interface {
WriteString(string) (int, error)
}
prefix string
}
func (pw prefixWriter) Write(b []byte) (int, error) {
var buf bytes.Buffer
if n, err := buf.Write(b); err != nil {
return n, err
}
scanner := bufio.NewScanner(&buf)
for scanner.Scan() {
line := scanner.Text()
line = fmt.Sprintf("%s: %s\n", pw.prefix, line)
pw.w.WriteString(line)
}
if err := scanner.Err(); err != nil {
return -1, err
}
return len(b), nil
}
type nopEventLogger struct{}
func (nopEventLogger) StartStep(context.Context, int) error { return nil }
func (nopEventLogger) FinishStep(context.Context, int, bool) error { return nil }