diff --git a/cloudbuild_tag.yaml b/cloudbuild_tag.yaml index ddebb435..b9754bab 100644 --- a/cloudbuild_tag.yaml +++ b/cloudbuild_tag.yaml @@ -11,7 +11,8 @@ steps: # Set TAG_NAME as version. - name: 'ubuntu' args: ['sed', '-i', 's/HEAD/$TAG_NAME/g', 'version.go'] -# Create binaries. +# Create binaries for each pair of OS/Arch. +# Create an archive with the latest binaries; that enables a single download url. - name: 'gcr.io/cloud-builders/go:wheezy' entrypoint: 'bash' args: @@ -19,9 +20,10 @@ steps: - | for GOOS in darwin linux windows; do for GOARCH in 386 amd64; do + # Build binary with the new tag and with 'latest' GOOS=$$GOOS GOARCH=$$GOARCH /builder/bin/go.bash build -o container-builder-local_$${GOOS}_$${GOARCH}-$TAG_NAME github.com/GoogleCloudPlatform/container-builder-local done done -# Copy binaries to GCS. + tar -czvf container-builder-local_latest.tar.gz container-builder-local_* - name: 'gcr.io/cloud-builders/gsutil' args: ['-m', 'cp', 'container-builder-local_*', 'gs://container-builder-local/'] diff --git a/integration_tests/cloudbuild_dockerfile.yaml b/integration_tests/cloudbuild_dockerfile.yaml index b108fe22..22b90250 100644 --- a/integration_tests/cloudbuild_dockerfile.yaml +++ b/integration_tests/cloudbuild_dockerfile.yaml @@ -1,5 +1,5 @@ steps: - name: 'gcr.io/cloud-builders/docker' - args: ['build', '-t', 'gcr.io/argo-local-builder/test', '.'] + args: ['build', '-t', 'gcr.io/$PROJECT_ID/test', '.'] -images: ['gcr.io/argo-local-builder/test'] +images: ['gcr.io/$PROJECT_ID/test'] diff --git a/integration_tests/cloudbuild_gcr.yaml b/integration_tests/cloudbuild_gcr.yaml index 71af0acc..cbb2eaa5 100644 --- a/integration_tests/cloudbuild_gcr.yaml +++ b/integration_tests/cloudbuild_gcr.yaml @@ -2,14 +2,14 @@ steps: - name: 'gcr.io/cloud-builders/docker' - args: ['pull', 'gcr.io/argo-local-builder/test'] + args: ['pull', 'gcr.io/$PROJECT_ID/test'] - name: 'gcr.io/cloud-builders/docker' args: - 'build' - '--cache-from' - - 'gcr.io/argo-local-builder/test' + - 'gcr.io/$PROJECT_ID/test' - '-t' - - 'gcr.io/argo-local-builder/test' + - 'gcr.io/$PROJECT_ID/test' - '.' -images: ['gcr.io/argo-local-builder/test'] +images: ['gcr.io/$PROJECT_ID/test'] diff --git a/localbuilder_main.go b/localbuilder_main.go index ee9ddbd8..17ca5cfe 100644 --- a/localbuilder_main.go +++ b/localbuilder_main.go @@ -49,7 +49,7 @@ const ( ) var ( - configFile = flag.String("config", "cloudconfig.yaml", "cloud build config file path") + configFile = flag.String("config", "cloudbuild.yaml", "cloud build config file path") substitutions = flag.String("substitutions", "", `substitutions key=value pairs separated by comma; for example _FOO=bar,_BAZ=argo`) dryRun = flag.Bool("dryrun", true, "If true, nothing will be run") push = flag.Bool("push", false, "If true, the images will be pushed") @@ -98,6 +98,11 @@ func run(source string) error { 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) @@ -191,7 +196,7 @@ func run(source string) error { // Feed the project info to the metadata server. metadataUpdater.SetProjectInfo(projectInfo) - go supplyTokenToMetadata(metadataUpdater, r) + go supplyTokenToMetadata(metadataUpdater, r, stopchan) } // Set initial Docker credentials. @@ -206,8 +211,8 @@ func run(source string) error { // Write initial docker credentials for GCR. This writes the initial // ~/.docker/config.json which is made available to build steps. - go func() { - for ; ; time.Sleep(tokenRefreshDur) { + go func(stopchan <-chan struct{}) { + for { tok, err := gcloud.AccessToken(r) if err != nil { log.Printf("Error getting access token to update docker credentials: %v", err) @@ -216,13 +221,21 @@ func run(source string) error { if err := b.UpdateDockerAccessToken(tok); err != nil { log.Printf("Error updating docker credentials: %v", err) } + select { + case <-time.After(tokenRefreshDur): + continue + case <-stopchan: + return + } } - }() + }(stopchan) } b.Start() <-b.Done + close(stopchan) + if b.Summary().Status == build.StatusError { return fmt.Errorf("Build finished with ERROR status") } @@ -234,8 +247,8 @@ func run(source string) error { } // supplyTokenToMetadata gets gcloud token and supply it to the metadata server. -func supplyTokenToMetadata(metadataUpdater metadata.RealUpdater, r runner.Runner) { - for ; ; time.Sleep(tokenRefreshDur) { +func supplyTokenToMetadata(metadataUpdater metadata.RealUpdater, r runner.Runner, stopchan <-chan struct{}) { + for { accessToken, err := gcloud.AccessToken(r) if err != nil { log.Printf("Error getting gcloud token: %v", err) @@ -251,6 +264,12 @@ func supplyTokenToMetadata(metadataUpdater metadata.RealUpdater, r runner.Runner log.Printf("Error updating token in metadata server: %v", err) continue } + select { + case <-time.After(tokenRefreshDur): + continue + case <-stopchan: + return + } } } diff --git a/metadata/metadata.go b/metadata/metadata.go index d507a741..7dbbe638 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -204,7 +204,7 @@ func StartCloudServer(r runner.Runner, metadataImage string) error { // builds. func CreateCloudbuildNetwork(r runner.Runner, subnet string) error { cmd := []string{"docker", "network", "create", "cloudbuild", "--subnet=" + subnet} - return r.Run(cmd, nil, os.Stdout, os.Stderr, "") + return r.Run(cmd, nil, nil, os.Stderr, "") } func startServer(r runner.Runner, metadataImage string, iptables bool, ip, subnet string) error { @@ -220,14 +220,12 @@ func startServer(r runner.Runner, metadataImage string, iptables bool, ip, subne } else { cmd = []string{"docker", "run", "-d", "--name=metadata", metadataImage} } - log.Println(cmd) if err := r.Run(cmd, nil, nil, os.Stderr, ""); err != nil { return err } // Redirect requests to metadata.google.internal and the fixed metadata IP to the metadata container. cmd = []string{"docker", "network", "connect", "--alias=metadata", "--alias=metadata.google.internal", "--ip=" + ip, "cloudbuild", "metadata"} - log.Println(cmd) if err := r.Run(cmd, nil, nil, os.Stderr, ""); err != nil { return fmt.Errorf("Error connecting metadata to network: %v", err) } @@ -243,7 +241,6 @@ func startServer(r runner.Runner, metadataImage string, iptables bool, ip, subne "-j", "DNAT", // This rule does destination NATting, "--to-destination", metadataHostedIP, // to our spoofed metadata container. } - log.Println(cmd) if err := r.Run(cmd, nil, os.Stdout, os.Stderr, ""); err != nil { return fmt.Errorf("Error updating iptables: %v", err) }