-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows the buildx CLI to act a proxy to the configured instance. It allows external code to use buildx itself as a driver for connecting to buildkitd instances. Instance and node selection should follow the same semantics as as `buildx build`, including taking into account the `BUILDX_BUILDER` env var and the `--builder` global flag. Signed-off-by: Brian Goff <[email protected]>
- Loading branch information
Showing
10 changed files
with
248 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package build | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"net" | ||
|
||
"github.com/docker/buildx/builder" | ||
"github.com/docker/buildx/driver" | ||
"github.com/docker/buildx/util/progress" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func Dial(ctx context.Context, nodes []builder.Node, pw progress.Writer) (net.Conn, error) { | ||
nodes, err := filterAvailableNodes(nodes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(nodes) == 0 { | ||
return nil, errors.New("no nodes available") | ||
} | ||
|
||
rand.Shuffle(len(nodes), func(i, j int) { | ||
nodes[i], nodes[j] = nodes[j], nodes[i] | ||
}) | ||
|
||
// TODO: filter nodes by platform | ||
for i := 0; i < len(nodes); i++ { | ||
n := nodes[i] | ||
|
||
pw := progress.WithPrefix(pw, n.Name, false) | ||
c, err := driver.Boot(ctx, ctx, n.Driver, pw) | ||
if err != nil { | ||
continue | ||
} | ||
c.Close() | ||
|
||
conn, err := n.Driver.Dial(ctx) | ||
if err != nil { | ||
continue | ||
} | ||
return conn, nil | ||
} | ||
return nil, errors.New("no nodes available") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package commands | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/docker/buildx/build" | ||
"github.com/docker/buildx/builder" | ||
"github.com/docker/buildx/util/progress" | ||
"github.com/docker/cli/cli/command" | ||
"github.com/moby/buildkit/util/appcontext" | ||
"github.com/moby/buildkit/util/progress/progressui" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type stdioOptions struct { | ||
builder string | ||
} | ||
|
||
func runDialStdio(dockerCli command.Cli, opts stdioOptions) error { | ||
ctx := appcontext.Context() | ||
|
||
contextPathHash, _ := os.Getwd() | ||
b, err := builder.New(dockerCli, | ||
builder.WithName(opts.builder), | ||
builder.WithContextPathHash(contextPathHash), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = updateLastActivity(dockerCli, b.NodeGroup); err != nil { | ||
return errors.Wrapf(err, "failed to update builder last activity time") | ||
} | ||
nodes, err := b.LoadNodes(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
eg, ctx := errgroup.WithContext(ctx) | ||
|
||
printer, err := progress.NewPrinter(ctx, os.Stderr, progressui.AutoMode) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return progress.Wrap("[internal] Dialing builder: "+b.Name, printer.Write, func(sub progress.SubLogger) error { | ||
conn, err := build.Dial(ctx, nodes, printer) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
go func() { | ||
<-ctx.Done() | ||
conn.Close() | ||
}() | ||
|
||
eg.Go(func() error { | ||
io.Copy(conn, os.Stdin) | ||
conn.Close() | ||
return nil | ||
}) | ||
eg.Go(func() error { | ||
io.Copy(os.Stdout, conn) | ||
conn.Close() | ||
return nil | ||
}) | ||
eg.Go(printer.Wait) | ||
|
||
return eg.Wait() | ||
}) | ||
} | ||
|
||
func dialStdioCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command { | ||
opts := stdioOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "dial-stdio", | ||
Short: "Dial stdio", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts.builder = rootOpts.builder | ||
return runDialStdio(dockerCli, opts) | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package tests | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"net" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/moby/buildkit/client" | ||
"github.com/moby/buildkit/util/testutil/integration" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var dialstdioTests = []func(t *testing.T, sb integration.Sandbox){ | ||
testDialStdio, | ||
} | ||
|
||
func testDialStdio(t *testing.T, sb integration.Sandbox) { | ||
errBuf := bytes.NewBuffer(nil) | ||
defer func() { | ||
if t.Failed() { | ||
t.Log(errBuf.String()) | ||
} | ||
}() | ||
c, err := client.New(sb.Context(), "", client.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { | ||
c1, c2 := net.Pipe() | ||
cmd := buildxCmd(sb, withArgs("dial-stdio"), func(cmd *exec.Cmd) { | ||
cmd.Stdin = c1 | ||
cmd.Stdout = c1 | ||
cmd.Stderr = errBuf | ||
}) | ||
|
||
if err := cmd.Start(); err != nil { | ||
c1.Close() | ||
c2.Close() | ||
return nil, errors.Wrap(err, errBuf.String()) | ||
} | ||
|
||
return wrapCmdConn(c2, cmd), nil | ||
})) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer c.Close() | ||
|
||
_, err = c.Info(sb.Context()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
type connWithCloser struct { | ||
net.Conn | ||
closer func() | ||
} | ||
|
||
func (c *connWithCloser) Close() error { | ||
err := c.Conn.Close() | ||
c.closer() | ||
return err | ||
} | ||
|
||
func wrapCmdConn(conn net.Conn, cmd *exec.Cmd) net.Conn { | ||
return &connWithCloser{ | ||
Conn: conn, | ||
closer: func() { | ||
cmd.Process.Kill() | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters