Skip to content

Commit

Permalink
Add support for --no-cloud.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseaton committed Jun 15, 2023
1 parent b63ad2c commit b8235d2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 50 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ Google has been archived. This fork is an attempt to keep the project alive, at
original, and maybe improve on it a bit.

#### New Features
- Add support for loading `.env` file secrets for `secretEnv` replacements.
- Added support for loading `.env` file secrets for `secretEnv` replacements.
This will convert only the matching `secretEnv` into a corresponding `env` with a value.
To use this, just add the `--env=<envfilepath>` argument with `envfilepath` as the path to the `.env` file.
*This is not supported in the cloud, and only works for GCBL. Which means the cloud will simply treat a secretEnv as
intended :)*
- No contributor agreements. Just code!
- Added support for skipping cloud token gathering and metadata with argument `--no-cloud`. This let's you run
cloud-build-local *without* an active `gcloud` authentication token (which requires a GCP account).

## Usage
To run a local build you should make sure you've got credentials to GCP if using any resources (`gcloud auth login`),
Expand Down
113 changes: 64 additions & 49 deletions localbuilder_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var (
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")
noCloud = flag.Bool("no-cloud", false, "Skip usage and checks for an active Google Cloud project/environment.")
bindMountSource = flag.Bool("bind-mount-source", false, "Bind mounts the source directory under /workspace rather "+
" than copying its contents into /workspace. It is an error to use this flag with --noSource")
writeWorkspace = flag.String("write-workspace", "", "Copies the workspace directory to this host directory")
Expand Down Expand Up @@ -131,7 +132,7 @@ func run(ctx context.Context, source string) error {
}

// Check installed docker versions.
if !*dryRun {
if !*dryRun && !*noCloud {
dockerServerVersion, dockerClientVersion, err := dockerVersions(ctx, r)
if err != nil {
return fmt.Errorf("Error getting local docker versions: %v", err)
Expand Down Expand Up @@ -243,28 +244,9 @@ func run(ctx context.Context, source string) error {
cancelableCtx, cancel := context.WithCancel(ctx)
defer cancel()
if !*dryRun {
// Set initial Docker credentials.
tok, err := gcloud.AccessToken(ctx, r)
if err != nil {
return fmt.Errorf("Error getting access token to set docker credentials: %v", err)
}
if err := b.SetDockerAccessToken(ctx, 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.
var mdTokenSetter metadataTokenSetter
if computeMetadata.OnGCE() {
if err := metadata.CreateCloudbuildNetwork(ctx, r, "172.22.0.0/16"); err != nil {
return fmt.Errorf("Error creating network: %v", err)
}
defer metadata.CleanCloudbuildNetwork(ctx, r)
mdTokenSetter = nopTokenSetter{}
} else {
if *noCloud {
//start spoofed metadata server
if err := metadata.StartLocalServer(ctx, r, metadataImageName); err != nil {
return fmt.Errorf("Failed to start spoofed metadata server: %v", err)
}
Expand All @@ -275,38 +257,71 @@ func run(ctx context.Context, source string) error {
// Feed the project info to the metadata server.
metadataUpdater.SetProjectInfo(ctx, projectInfo)
mdTokenSetter = metadataUpdater
}

// Keep credentials up-to-date.
go func(ctx context.Context, tok *metadata.Token) {
var refresh time.Duration
for {
select {
case <-time.After(refresh):
case <-ctx.Done():
return
} else {
// Set initial Docker credentials.
tok, err := gcloud.AccessToken(ctx, r)
if err != nil {
return fmt.Errorf("Error getting access token to set docker credentials: %v", err)
}
if err := b.SetDockerAccessToken(ctx, 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(ctx, r, "172.22.0.0/16"); err != nil {
return fmt.Errorf("Error creating network: %v", err)
}
tok, err := gcloud.AccessToken(ctx, r)
if err != nil {
log.Printf("Error getting gcloud token: %v", err)
continue
defer metadata.CleanCloudbuildNetwork(ctx, r)
mdTokenSetter = nopTokenSetter{}
} else {
if err := metadata.StartLocalServer(ctx, 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(ctx, r)

// Supply token to the metadata server.
if err := mdTokenSetter.SetToken(ctx, tok); err != nil {
log.Printf("Error updating token in metadata server: %v", err)
}
// Feed the project info to the metadata server.
metadataUpdater.SetProjectInfo(ctx, projectInfo)
mdTokenSetter = metadataUpdater
}

// Keep credentials up-to-date.
go func(ctx context.Context, tok *metadata.Token) {
var refresh time.Duration
for {
select {
case <-time.After(refresh):
case <-ctx.Done():
return
}
tok, err := gcloud.AccessToken(ctx, r)
if err != nil {
log.Printf("Error getting gcloud token: %v", err)
continue
}

// Keep a fresh token in ~/.docker/config.json, which in turn is
// available to build steps. Note that use of `gcloud auth` to switch
// accounts mid-build is not supported.
if err := b.UpdateDockerAccessToken(ctx, tok.AccessToken); err != nil {
log.Printf("Error updating docker credentials: %v", err)
// Supply token to the metadata server.
if err := mdTokenSetter.SetToken(ctx, tok); err != nil {
log.Printf("Error updating token in metadata server: %v", err)
}

// Keep a fresh token in ~/.docker/config.json, which in turn is
// available to build steps. Note that use of `gcloud auth` to switch
// accounts mid-build is not supported.
if err := b.UpdateDockerAccessToken(ctx, tok.AccessToken); err != nil {
log.Printf("Error updating docker credentials: %v", err)
}
b.TokenSource = oauth2.StaticTokenSource(&oauth2.Token{AccessToken: tok.AccessToken})
refresh = common.RefreshDuration(tok.Expiry)
}
b.TokenSource = oauth2.StaticTokenSource(&oauth2.Token{AccessToken: tok.AccessToken})
refresh = common.RefreshDuration(tok.Expiry)
}
}(cancelableCtx, tok)
}(cancelableCtx, tok)
}
}

b.Start(ctx)
Expand Down

0 comments on commit b8235d2

Please sign in to comment.