-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable to execute shell on a build step for debugging
Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
- Loading branch information
Showing
20 changed files
with
1,396 additions
and
332 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,72 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/containerd/console" | ||
"github.com/moby/buildkit/client" | ||
bccommon "github.com/moby/buildkit/cmd/buildctl/common" | ||
gwclient "github.com/moby/buildkit/frontend/gateway/client" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
var debugShellCommand = cli.Command{ | ||
Name: "shell", | ||
Usage: "exec shell in a vertex", | ||
Action: debugShellAction, | ||
} | ||
|
||
var debugCloseCommand = cli.Command{ | ||
Name: "close", | ||
Usage: "close a build job remaining for debugging", | ||
Action: debugCloseAction, | ||
} | ||
|
||
func debugCloseAction(clicontext *cli.Context) error { | ||
ref := clicontext.Args().Get(0) | ||
if ref == "" { | ||
return fmt.Errorf("ref must be specified") | ||
} | ||
c, err := bccommon.ResolveClient(clicontext) | ||
if err != nil { | ||
return err | ||
} | ||
ctx := clicontext.App.Metadata["context"].(context.Context) | ||
return c.DebugClose(ctx, ref) | ||
} | ||
|
||
func debugShellAction(clicontext *cli.Context) error { | ||
ref := clicontext.Args().Get(0) | ||
vtx := clicontext.Args().Get(1) | ||
if ref == "" || vtx == "" { | ||
return fmt.Errorf("ref and vertex must be specified") | ||
} | ||
c, err := bccommon.ResolveClient(clicontext) | ||
if err != nil { | ||
return err | ||
} | ||
ctx := clicontext.App.Metadata["context"].(context.Context) | ||
return execProcess(ctx, c, ref, vtx, gwclient.StartRequest{ | ||
Args: []string{"/bin/sh"}, | ||
Stdin: os.Stdin, | ||
Stdout: os.Stderr, | ||
Stderr: os.Stderr, | ||
Tty: true, | ||
}) | ||
} | ||
|
||
func execProcess(ctx context.Context, c *client.Client, ref, vtx string, req gwclient.StartRequest) error { | ||
con := console.Current() | ||
defer con.Reset() | ||
if err := con.SetRaw(); err != nil { | ||
return err | ||
} | ||
p, err := c.DebugExecProcess(ctx, ref, vtx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed to start process: %w", err) | ||
} | ||
resizeConsole(ctx, p, con) | ||
return p.Wait() | ||
} |
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,13 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/containerd/console" | ||
gwclient "github.com/moby/buildkit/frontend/gateway/client" | ||
) | ||
|
||
func resizeConsole(ctx context.Context, p gwclient.ContainerProcess, con console.Console) {} |
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,32 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/containerd/console" | ||
gwclient "github.com/moby/buildkit/frontend/gateway/client" | ||
) | ||
|
||
func resizeConsole(ctx context.Context, p gwclient.ContainerProcess, con console.Console) { | ||
ch := make(chan os.Signal, 1) | ||
signal.Notify(ch, syscall.SIGWINCH) | ||
go func() { | ||
for { | ||
select { | ||
case <-ch: | ||
size, err := con.Size() | ||
if err != nil { | ||
continue | ||
} | ||
p.Resize(ctx, gwclient.WinSize{Cols: uint32(size.Width), Rows: uint32(size.Height)}) | ||
} | ||
} | ||
}() | ||
ch <- syscall.SIGWINCH | ||
} |
Oops, something went wrong.