Skip to content

Commit

Permalink
Don't use Docker if resource has a YB_CONTAINER_*_IP environment va…
Browse files Browse the repository at this point in the history
…riable set (#315)

[Fixes ch4961]
  • Loading branch information
zombiezen authored Jun 2, 2021
1 parent 03eed7b commit 3f9510f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
18 changes: 13 additions & 5 deletions cmd/yb/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/user"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/spf13/pflag"
"github.com/yourbase/yb"
"github.com/yourbase/yb/internal/biome"
"github.com/yourbase/yb/internal/build"
"github.com/yourbase/yb/internal/config"
"github.com/yourbase/yb/internal/ybdata"
"zombiezen.com/go/log"
Expand Down Expand Up @@ -249,7 +251,7 @@ func newDockerNetwork(ctx context.Context, client *docker.Client, mode execution
}

func showDockerWarningsIfNeeded(ctx context.Context, mode executionMode, targets []*yb.Target) {
if !willUseDocker(mode, targets) {
if !willUseDocker(mode, targets) || runtime.GOOS != biome.Linux {
return
}
dockerGroup, err := user.LookupGroup("docker")
Expand Down Expand Up @@ -281,8 +283,10 @@ func showDockerWarningsIfNeeded(ctx context.Context, mode executionMode, targets

func willUseDocker(mode executionMode, targets []*yb.Target) bool {
for _, target := range targets {
if len(target.Resources) > 0 {
return true
for name := range target.Resources {
if os.Getenv(build.ContainerIPEnvVar(name)) == "" {
return true
}
}
}
return willUseDockerForCommands(mode, targets)
Expand All @@ -291,10 +295,14 @@ func willUseDocker(mode executionMode, targets []*yb.Target) bool {
func willUseDockerForCommands(mode executionMode, targets []*yb.Target) bool {
networkAvailable, _ := hostHasDockerNetwork()
for _, target := range targets {
if target.UseContainer ||
len(target.Resources) > 0 && !networkAvailable {
if target.UseContainer {
return true
}
for name := range target.Resources {
if os.Getenv(build.ContainerIPEnvVar(name)) == "" && !networkAvailable {
return true
}
}
}
return mode >= useContainer
}
Expand Down
22 changes: 22 additions & 0 deletions cmd/yb/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ package main

import (
"fmt"
"os"
"strings"
"testing"

"github.com/yourbase/yb"
)

func TestWillUseDocker(t *testing.T) {
const containerEnvVar = "YB_CONTAINER_ENV_IP"
oldEnv, oldEnvExists := os.LookupEnv(containerEnvVar)
os.Setenv(containerEnvVar, "1.2.3.4")
t.Cleanup(func() {
if !oldEnvExists {
os.Unsetenv(containerEnvVar)
} else {
os.Setenv(containerEnvVar, oldEnv)
}
})

networkAvailable, _ := hostHasDockerNetwork()
tests := []struct {
mode executionMode
Expand Down Expand Up @@ -104,6 +116,16 @@ func TestWillUseDocker(t *testing.T) {
want: true,
forCommands: !networkAvailable,
},
{
mode: preferHost,
targets: []*yb.Target{
// This is provided as YB_CONTAINER_ENV_IP.
{Name: "default", UseContainer: false, Resources: map[string]*yb.ResourceDefinition{"env": {}}},
{Name: "foo", UseContainer: false},
},
want: false,
forCommands: false,
},
{
mode: useContainer,
targets: []*yb.Target{
Expand Down
8 changes: 7 additions & 1 deletion internal/build/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func startContainers(ctx context.Context, sys Sys, defs map[string]*yb.ResourceD
}
containers := make(map[string]*container)
for name := range defs {
ip := os.Getenv("YB_CONTAINER_" + strings.ToUpper(name) + "_IP")
ip := os.Getenv(ContainerIPEnvVar(name))
if ip != "" {
log.Infof(ctx, "Using %s address from environment: %s", name, ip)
exp.ips[name] = ip
Expand Down Expand Up @@ -179,6 +179,12 @@ func startContainers(ctx context.Context, sys Sys, defs map[string]*yb.ResourceD
return exp, origCloseFunc, nil
}

// ContainerIPEnvVar returns the name of the environment variable that
// optionally provides the IP address of a target's resource.
func ContainerIPEnvVar(resourceName string) string {
return "YB_CONTAINER_" + strings.ToUpper(resourceName) + "_IP"
}

// startContainer starts a single container with the given definition.
func startContainer(ctx context.Context, sys Sys, resourceName string, cd *yb.ResourceDefinition) (_ *container, err error) {
for _, mount := range cd.Mounts {
Expand Down

0 comments on commit 3f9510f

Please sign in to comment.