Skip to content

Commit

Permalink
pass addr (#484)
Browse files Browse the repository at this point in the history
* pass addr

* Detect buildkit IP address using docker inspect.

Co-authored-by: Alex Couture-Beil <alex@earthly.dev>
Co-authored-by: Vlad A. Ionescu <vladaionescu@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 4, 2020
1 parent a68361a commit 09343cf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
11 changes: 11 additions & 0 deletions buildkitd/buildkitd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package buildkitd

import (
"bytes"
"context"
"encoding/base64"
"fmt"
Expand Down Expand Up @@ -305,6 +306,16 @@ func WaitUntilStarted(ctx context.Context, address string, opTimeout time.Durati
}
}

// GetContainerIP returns the IP of the buildkit container.
func GetContainerIP(ctx context.Context) (string, error) {
cmd := exec.CommandContext(ctx, "docker", "inspect", "-f", "{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}", ContainerName)
output, err := cmd.CombinedOutput()
if err != nil {
return "", errors.Wrap(err, "get combined output ip")
}
return string(bytes.TrimSpace(output)), nil
}

// WaitUntilStopped waits until the buildkitd daemon has stopped.
func WaitUntilStopped(ctx context.Context, opTimeout time.Duration) error {
ctxTimeout, cancel := context.WithTimeout(ctx, opTimeout)
Expand Down
4 changes: 1 addition & 3 deletions cmd/debugger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ var (
ErrNoShellFound = fmt.Errorf("no shell found")
)

const remoteConsoleAddr = "127.0.0.1:8373"

func getShellPath() (string, bool) {
for _, sh := range []string{
"bash", "ksh", "zsh", "ash", "sh",
Expand Down Expand Up @@ -238,7 +236,7 @@ func main() {
conslogger.Warnf("Failed to set term: %v", err)
}

err = interactiveMode(ctx, remoteConsoleAddr, quotedCmd)
err = interactiveMode(ctx, debuggerSettings.RepeaterAddr, quotedCmd)
if err != nil {
log.Error(err)
}
Expand Down
21 changes: 12 additions & 9 deletions cmd/earth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ func (app *earthApp) actionPrune(c *cli.Context) error {
}

// Prune via API.
bkClient, err := app.newBuildkitdClient(c.Context)
bkClient, _, err := app.newBuildkitdClient(c.Context)
if err != nil {
return errors.Wrap(err, "buildkitd new client")
}
Expand Down Expand Up @@ -1290,7 +1290,6 @@ func (app *earthApp) actionDocker2Earth(c *cli.Context) error {

func (app *earthApp) actionBuild(c *cli.Context) error {
app.commandName = "build"
sockName := fmt.Sprintf("debugger.sock.%d", time.Now().UnixNano())

if app.imageMode && app.artifactMode {
return errors.New("both image and artifact modes cannot be active at the same time")
Expand Down Expand Up @@ -1354,7 +1353,7 @@ func (app *earthApp) actionBuild(c *cli.Context) error {
return errors.Wrapf(err, "parse target name %s", targetName)
}
}
bkClient, err := app.newBuildkitdClient(c.Context)
bkClient, bkIP, err := app.newBuildkitdClient(c.Context)
if err != nil {
return errors.Wrap(err, "buildkitd new client")
}
Expand All @@ -1377,7 +1376,7 @@ func (app *earthApp) actionBuild(c *cli.Context) error {
debuggerSettings := debuggercommon.DebuggerSettings{
DebugLevelLogging: app.debug,
Enabled: app.interactiveDebugging,
SockPath: fmt.Sprintf("/run/earthly/%s", sockName),
RepeaterAddr: fmt.Sprintf("%s:8373", bkIP),
Term: os.Getenv("TERM"),
}

Expand Down Expand Up @@ -1469,25 +1468,29 @@ func (app *earthApp) actionBuild(c *cli.Context) error {
return nil
}

func (app *earthApp) newBuildkitdClient(ctx context.Context, opts ...client.ClientOpt) (*client.Client, error) {
func (app *earthApp) newBuildkitdClient(ctx context.Context, opts ...client.ClientOpt) (*client.Client, string, error) {
if app.buildkitHost == "" {
// Start our own.
app.buildkitdSettings.Debug = app.debug
opTimeout := time.Duration(app.cfg.Global.BuildkitRestartTimeoutS) * time.Second
bkClient, err := buildkitd.NewClient(
ctx, app.console, app.buildkitdImage, app.buildkitdSettings, opTimeout)
if err != nil {
return nil, errors.Wrap(err, "buildkitd new client (own)")
return nil, "", errors.Wrap(err, "buildkitd new client (own)")
}
return bkClient, nil
bkIP, err := buildkitd.GetContainerIP(ctx)
if err != nil {
return nil, "", errors.Wrap(err, "get container ip")
}
return bkClient, bkIP, nil
}

// Use provided.
bkClient, err := client.New(ctx, app.buildkitHost, opts...)
if err != nil {
return nil, errors.Wrap(err, "buildkitd new client (provided)")
return nil, "", errors.Wrap(err, "buildkitd new client (provided)")
}
return bkClient, nil
return bkClient, "", nil
}

func processSecrets(secrets []string, dotEnvMap map[string]string) (map[string][]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion debugger/common/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const DebuggerSettingsSecretsKey = "earthly_debugger_settings"
type DebuggerSettings struct {
DebugLevelLogging bool `json:"debugLevel"`
Enabled bool `json:"enabled"`
SockPath string `json:"sockPath"`
RepeaterAddr string `json:"repeaterAddr"`
Term string `json:"term"`
}

0 comments on commit 09343cf

Please sign in to comment.