Skip to content

Commit

Permalink
monitor: add debug-shell and on-error
Browse files Browse the repository at this point in the history
Signed-off-by: Kohei Tokunaga <[email protected]>
  • Loading branch information
ktock committed Feb 21, 2023
1 parent aa05f4c commit 3328ff8
Show file tree
Hide file tree
Showing 19 changed files with 962 additions and 244 deletions.
373 changes: 290 additions & 83 deletions build/build.go

Large diffs are not rendered by default.

114 changes: 94 additions & 20 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/docker/buildx/controller"
cbuild "github.com/docker/buildx/controller/build"
"github.com/docker/buildx/controller/control"
controllererrors "github.com/docker/buildx/controller/errdefs"
controllerapi "github.com/docker/buildx/controller/pb"
"github.com/docker/buildx/monitor"
"github.com/docker/buildx/store"
Expand Down Expand Up @@ -345,29 +346,39 @@ func launchControllerAndRunBuild(dockerCli command.Cli, options buildOptions) er
}
}()

// Start build
var ref string
var retErr error
f := ioset.NewSingleForwarder()
pr, pw := io.Pipe()
f.SetWriter(pw, func() io.WriteCloser {
pw.Close() // propagate EOF
logrus.Debug("propagating stdin close")
return nil
})
f.SetReader(os.Stdin)

// Start build
ref, err := c.Build(ctx, options.BuildOptions, pr, os.Stdout, os.Stderr, options.progress)
if err != nil {
return errors.Wrapf(err, "failed to build") // TODO: allow invoke even on error
}
if err := pw.Close(); err != nil {
logrus.Debug("failed to close stdin pipe writer")
}
if err := pr.Close(); err != nil {
logrus.Debug("failed to close stdin pipe reader")
if options.invoke != "debug-shell" {
pr, pw := io.Pipe()
f.SetWriter(pw, func() io.WriteCloser {
pw.Close() // propagate EOF
logrus.Debug("propagating stdin close")
return nil
})
ref, err = c.Build(ctx, options.BuildOptions, pr, os.Stdout, os.Stderr, options.progress)
if err != nil {
var be *controllererrors.BuildError
if errors.As(err, &be) {
ref = be.Ref
retErr = err
// We can proceed to monitor
} else {
return errors.Wrapf(err, "failed to build")
}
}
if err := pw.Close(); err != nil {
logrus.Debug("failed to close stdin pipe writer")
}
if err := pr.Close(); err != nil {
logrus.Debug("failed to close stdin pipe reader")
}
}

// post-build operations
if options.invoke != "" {
if needsMonitor(options.invoke, retErr) {
pr2, pw2 := io.Pipe()
f.SetWriter(pw2, func() io.WriteCloser {
pw2.Close() // propagate EOF
Expand All @@ -380,7 +391,7 @@ func launchControllerAndRunBuild(dockerCli command.Cli, options buildOptions) er
}
return errors.Errorf("failed to configure terminal: %v", err)
}
err = monitor.RunMonitor(ctx, ref, options.BuildOptions, invokeConfig, c, options.progress, pr2, os.Stdout, os.Stderr)
err = monitor.RunMonitor(ctx, ref, &options.BuildOptions, invokeConfig, c, options.progress, pr2, os.Stdout, os.Stderr)
con.Reset()
if err := pw2.Close(); err != nil {
logrus.Debug("failed to close monitor stdin pipe reader")
Expand All @@ -400,9 +411,26 @@ func launchControllerAndRunBuild(dockerCli command.Cli, options buildOptions) er
return nil
}

func needsMonitor(invokeFlag string, retErr error) bool {
switch invokeFlag {
case "debug-shell":
return true
case "on-error":
return retErr != nil
default:
return invokeFlag != ""
}
}

func parseInvokeConfig(invoke string) (cfg controllerapi.ContainerConfig, err error) {
cfg.Tty = true
if invoke == "default" {
switch invoke {
case "default", "debug-shell":
return cfg, nil
case "on-error":
// NOTE: we overwrite the command to run because the original one should fail on the failed step.
// TODO: make this configurable.
cfg.Cmd = []string{"/bin/sh"}
return cfg, nil
}

Expand Down Expand Up @@ -521,3 +549,49 @@ func (s *shmSize) Set(v string) error {
func (s *shmSize) Type() string {
return s.org.Type()
}

func addDebugShellCommand(cmd *cobra.Command, dockerCli command.Cli) {
cmd.AddCommand(
debugShellCmd(dockerCli),
)
}

func debugShellCmd(dockerCli command.Cli) *cobra.Command {
var options control.ControlOptions
var progress string

cmd := &cobra.Command{
Use: "debug-shell",
Short: "Start a monitor",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
c, err := controller.NewController(ctx, options, dockerCli)
if err != nil {
return err
}
defer func() {
if err := c.Close(); err != nil {
logrus.Warnf("failed to close server connection %v", err)
}
}()
con := console.Current()
if err := con.SetRaw(); err != nil {
return errors.Errorf("failed to configure terminal: %v", err)
}
err = monitor.RunMonitor(ctx, "", nil, controllerapi.ContainerConfig{
Tty: true,
}, c, progress, os.Stdin, os.Stdout, os.Stderr)
con.Reset()
return err
},
}

flags := cmd.Flags()

flags.StringVar(&options.Root, "root", "", "Specify root directory of server to connect [experimental]")
flags.BoolVar(&options.Detach, "detach", runtime.GOOS == "linux", "Detach buildx server (supported only on linux) [experimental]")
flags.StringVar(&options.ServerConfig, "server-config", "", "Specify buildx server config file (used only when launching new server) [experimental]")
flags.StringVar(&progress, "progress", "auto", `Set type of progress output ("auto", "plain", "tty"). Use plain to show container output`)

return cmd
}
1 change: 1 addition & 0 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
)
if isExperimental() {
remote.AddControllerCommands(cmd, dockerCli)
addDebugShellCommand(cmd, dockerCli)
}
}

Expand Down
50 changes: 50 additions & 0 deletions controller/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/solver/errdefs"
solverpb "github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/grpcerrors"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/morikuni/aec"
Expand Down Expand Up @@ -239,6 +240,9 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, ng *store.NodeGrou
err = err1
}
if err != nil {
if res != nil {
err = wrapResultContext(err, res)
}
return "", nil, err
}

Expand Down Expand Up @@ -449,3 +453,49 @@ func controllerUlimitOpt2DockerUlimit(u *controllerapi.UlimitOpt) *dockeropts.Ul
}
return dockeropts.NewUlimitOpt(&values)
}

type ResultContextError struct {
ResultContext *build.ResultContext
error
}

func (e *ResultContextError) Unwrap() error {
return e.error
}

func wrapResultContext(wErr error, res *build.ResultContext) error {
if wErr == nil {
return nil
}
def, err := DefinitionFromResultContext(context.TODO(), res)
if err != nil {
logrus.Errorf("failed to get definition from result: %v", err)
return wErr
}
res2, err := build.GetResultAt(context.TODO(), res, def, nil)
if err != nil {
logrus.Errorf("failed to get result: %v", err)
return wErr
}
res.Done()
return &ResultContextError{ResultContext: res2, error: wErr}
}

func DefinitionFromResultContext(ctx context.Context, res *build.ResultContext) (*solverpb.Definition, error) {
if res.Res == nil {
return nil, errors.Errorf("result context doesn't contain build result")
}
ref, err := res.Res.SingleRef()
if err != nil {
return nil, err
}
st, err := ref.ToState()
if err != nil {
return nil, err
}
def, err := st.Marshal(ctx)
if err != nil {
return nil, err
}
return def.ToPB(), nil
}
1 change: 1 addition & 0 deletions controller/control/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type BuildxController interface {
Close() error
List(ctx context.Context) (res []string, _ error)
Disconnect(ctx context.Context, ref string) error
Inspect(ctx context.Context, ref string) (*controllerapi.InspectResponse, error)
}

type ControlOptions struct {
Expand Down
34 changes: 34 additions & 0 deletions controller/errdefs/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package errdefs

import (
"github.com/containerd/typeurl"
"github.com/moby/buildkit/util/grpcerrors"
)

func init() {
typeurl.Register((*Build)(nil), "github.com/docker/buildx", "errdefs.Build+json")
}

type BuildError struct {
Build
error
}

func (e *BuildError) Unwrap() error {
return e.error
}

func (e *BuildError) ToProto() grpcerrors.TypedErrorProto {
return &e.Build
}

func WrapBuild(err error, ref string) error {
if err == nil {
return nil
}
return &BuildError{Build: Build{Ref: ref}, error: err}
}

func (b *Build) WrapError(err error) error {
return &BuildError{error: err, Build: *b}
}
77 changes: 77 additions & 0 deletions controller/errdefs/errdefs.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions controller/errdefs/errdefs.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package errdefs;

import "github.com/moby/buildkit/solver/pb/ops.proto";

message Build {
string Ref = 1;
}
3 changes: 3 additions & 0 deletions controller/errdefs/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package errdefs

//go:generate protoc -I=. -I=../../vendor/ --gogo_out=plugins=grpc:. errdefs.proto
Loading

0 comments on commit 3328ff8

Please sign in to comment.