mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-18 00:47:48 +08:00
monitor: extend monitor interface from controller
Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
parent
9f884edbbf
commit
a43837d26c
@ -58,7 +58,7 @@ func (cm *AttachCmd) Exec(ctx context.Context, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isProcessID(ctx context.Context, c types.Monitor, ref string) (bool, error) {
|
func isProcessID(ctx context.Context, c types.Monitor, ref string) (bool, error) {
|
||||||
infos, err := c.ListProcesses(ctx)
|
infos, err := c.ListProcesses(ctx, c.AttachedSessionID())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ func (cm *DisconnectCmd) Exec(ctx context.Context, args []string) error {
|
|||||||
}
|
}
|
||||||
isProcess, err := isProcessID(ctx, cm.m, target)
|
isProcess, err := isProcessID(ctx, cm.m, target)
|
||||||
if err == nil && isProcess {
|
if err == nil && isProcess {
|
||||||
if err := cm.m.DisconnectProcess(ctx, target); err != nil {
|
if err := cm.m.DisconnectProcess(ctx, cm.m.AttachedSessionID(), target); err != nil {
|
||||||
return errors.Errorf("disconnecting from process failed %v", target)
|
return errors.Errorf("disconnecting from process failed %v", target)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -23,7 +23,8 @@ func (cm *PsCmd) Info() types.CommandInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cm *PsCmd) Exec(ctx context.Context, args []string) error {
|
func (cm *PsCmd) Exec(ctx context.Context, args []string) error {
|
||||||
plist, err := cm.m.ListProcesses(ctx)
|
ref := cm.m.AttachedSessionID()
|
||||||
|
plist, err := cm.m.ListProcesses(ctx, ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -32,9 +32,9 @@ func (cm *ReloadCmd) Info() types.CommandInfo {
|
|||||||
|
|
||||||
func (cm *ReloadCmd) Exec(ctx context.Context, args []string) error {
|
func (cm *ReloadCmd) Exec(ctx context.Context, args []string) error {
|
||||||
var bo *controllerapi.BuildOptions
|
var bo *controllerapi.BuildOptions
|
||||||
if cm.m.AttachedSessionID() != "" {
|
if ref := cm.m.AttachedSessionID(); ref != "" {
|
||||||
// Rebuilding an existing session; Restore the build option used for building this session.
|
// Rebuilding an existing session; Restore the build option used for building this session.
|
||||||
res, err := cm.m.Inspect(ctx)
|
res, err := cm.m.Inspect(ctx, ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to inspect the current build session: %v\n", err)
|
fmt.Printf("failed to inspect the current build session: %v\n", err)
|
||||||
} else {
|
} else {
|
||||||
@ -46,8 +46,8 @@ func (cm *ReloadCmd) Exec(ctx context.Context, args []string) error {
|
|||||||
if bo == nil {
|
if bo == nil {
|
||||||
return errors.Errorf("no build option is provided")
|
return errors.Errorf("no build option is provided")
|
||||||
}
|
}
|
||||||
if cm.m.AttachedSessionID() != "" {
|
if ref := cm.m.AttachedSessionID(); ref != "" {
|
||||||
if err := cm.m.Disconnect(ctx); err != nil {
|
if err := cm.m.Disconnect(ctx, ref); err != nil {
|
||||||
fmt.Println("disconnect error", err)
|
fmt.Println("disconnect error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ import (
|
|||||||
"github.com/docker/buildx/util/ioset"
|
"github.com/docker/buildx/util/ioset"
|
||||||
"github.com/docker/buildx/util/progress"
|
"github.com/docker/buildx/util/progress"
|
||||||
"github.com/google/shlex"
|
"github.com/google/shlex"
|
||||||
"github.com/moby/buildkit/client"
|
|
||||||
"github.com/moby/buildkit/identity"
|
"github.com/moby/buildkit/identity"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -63,8 +62,8 @@ func RunMonitor(ctx context.Context, curRef string, options *controllerapi.Build
|
|||||||
invokeForwarder := ioset.NewForwarder()
|
invokeForwarder := ioset.NewForwarder()
|
||||||
invokeForwarder.SetIn(&containerIn)
|
invokeForwarder.SetIn(&containerIn)
|
||||||
m := &monitor{
|
m := &monitor{
|
||||||
controllerRef: newControllerRef(c, curRef),
|
BuildxController: c,
|
||||||
invokeIO: invokeForwarder,
|
invokeIO: invokeForwarder,
|
||||||
muxIO: ioset.NewMuxIO(ioset.In{
|
muxIO: ioset.NewMuxIO(ioset.In{
|
||||||
Stdin: io.NopCloser(stdin),
|
Stdin: io.NopCloser(stdin),
|
||||||
Stdout: nopCloser{stdout},
|
Stdout: nopCloser{stdout},
|
||||||
@ -77,6 +76,7 @@ func RunMonitor(ctx context.Context, curRef string, options *controllerapi.Build
|
|||||||
return "Switched IO\n"
|
return "Switched IO\n"
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
m.ref.Store(curRef)
|
||||||
|
|
||||||
// Start container automatically
|
// Start container automatically
|
||||||
fmt.Fprintf(stdout, "Launching interactive container. Press Ctrl-a-c to switch to monitor console\n")
|
fmt.Fprintf(stdout, "Launching interactive container. Press Ctrl-a-c to switch to monitor console\n")
|
||||||
@ -203,7 +203,8 @@ type readWriter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type monitor struct {
|
type monitor struct {
|
||||||
*controllerRef
|
control.BuildxController
|
||||||
|
ref atomic.Value
|
||||||
|
|
||||||
muxIO *ioset.MuxIO
|
muxIO *ioset.MuxIO
|
||||||
invokeIO *ioset.Forwarder
|
invokeIO *ioset.Forwarder
|
||||||
@ -212,15 +213,15 @@ type monitor struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *monitor) DisconnectSession(ctx context.Context, targetID string) error {
|
func (m *monitor) DisconnectSession(ctx context.Context, targetID string) error {
|
||||||
return m.controllerRef.raw().Disconnect(ctx, targetID)
|
return m.Disconnect(ctx, targetID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *monitor) AttachSession(ref string) {
|
func (m *monitor) AttachSession(ref string) {
|
||||||
m.controllerRef.ref.Store(ref)
|
m.ref.Store(ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *monitor) AttachedSessionID() string {
|
func (m *monitor) AttachedSessionID() string {
|
||||||
return m.controllerRef.ref.Load().(string)
|
return m.ref.Load().(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *monitor) Rollback(ctx context.Context, cfg controllerapi.InvokeConfig) string {
|
func (m *monitor) Rollback(ctx context.Context, cfg controllerapi.InvokeConfig) string {
|
||||||
@ -298,87 +299,12 @@ func (m *monitor) invoke(ctx context.Context, pid string, cfg controllerapi.Invo
|
|||||||
defer invokeCancelAndDetachFn()
|
defer invokeCancelAndDetachFn()
|
||||||
m.invokeCancel = invokeCancelAndDetachFn
|
m.invokeCancel = invokeCancelAndDetachFn
|
||||||
|
|
||||||
err := m.controllerRef.invoke(invokeCtx, pid, cfg, containerIn.Stdin, containerIn.Stdout, containerIn.Stderr)
|
err := m.Invoke(invokeCtx, m.AttachedSessionID(), pid, cfg, containerIn.Stdin, containerIn.Stdout, containerIn.Stderr)
|
||||||
close(waitInvokeDoneCh)
|
close(waitInvokeDoneCh)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func newControllerRef(c control.BuildxController, ref string) *controllerRef {
|
|
||||||
cr := &controllerRef{c: c}
|
|
||||||
cr.ref.Store(ref)
|
|
||||||
return cr
|
|
||||||
}
|
|
||||||
|
|
||||||
type controllerRef struct {
|
|
||||||
c control.BuildxController
|
|
||||||
ref atomic.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) raw() control.BuildxController {
|
|
||||||
return c.c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) getRef() (string, error) {
|
|
||||||
ref := c.ref.Load()
|
|
||||||
if ref == "" {
|
|
||||||
return "", errors.Errorf("client is not attached to a session")
|
|
||||||
}
|
|
||||||
return ref.(string), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) Build(ctx context.Context, options controllerapi.BuildOptions, in io.ReadCloser, progress progress.Writer) (ref string, resp *client.SolveResponse, err error) {
|
|
||||||
return c.c.Build(ctx, options, in, progress)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) invoke(ctx context.Context, pid string, options controllerapi.InvokeConfig, ioIn io.ReadCloser, ioOut io.WriteCloser, ioErr io.WriteCloser) error {
|
|
||||||
ref, err := c.getRef()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return c.c.Invoke(ctx, ref, pid, options, ioIn, ioOut, ioErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) Kill(ctx context.Context) error {
|
|
||||||
return c.c.Kill(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) List(ctx context.Context) (refs []string, _ error) {
|
|
||||||
return c.c.List(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) ListProcesses(ctx context.Context) (infos []*controllerapi.ProcessInfo, retErr error) {
|
|
||||||
ref, err := c.getRef()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return c.c.ListProcesses(ctx, ref)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) DisconnectProcess(ctx context.Context, pid string) error {
|
|
||||||
ref, err := c.getRef()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return c.c.DisconnectProcess(ctx, ref, pid)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) Inspect(ctx context.Context) (*controllerapi.InspectResponse, error) {
|
|
||||||
ref, err := c.getRef()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return c.c.Inspect(ctx, ref)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controllerRef) Disconnect(ctx context.Context) error {
|
|
||||||
ref, err := c.getRef()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return c.c.Disconnect(ctx, ref)
|
|
||||||
}
|
|
||||||
|
|
||||||
type nopCloser struct {
|
type nopCloser struct {
|
||||||
io.Writer
|
io.Writer
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,15 @@ package types
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
|
|
||||||
|
"github.com/docker/buildx/controller/control"
|
||||||
controllerapi "github.com/docker/buildx/controller/pb"
|
controllerapi "github.com/docker/buildx/controller/pb"
|
||||||
"github.com/docker/buildx/util/progress"
|
|
||||||
"github.com/moby/buildkit/client"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Monitor provides APIs for attaching and controlling the buildx server.
|
// Monitor provides APIs for attaching and controlling the buildx server.
|
||||||
type Monitor interface {
|
type Monitor interface {
|
||||||
|
control.BuildxController
|
||||||
|
|
||||||
// Rollback re-runs the interactive container with initial rootfs contents.
|
// Rollback re-runs the interactive container with initial rootfs contents.
|
||||||
Rollback(ctx context.Context, cfg controllerapi.InvokeConfig) string
|
Rollback(ctx context.Context, cfg controllerapi.InvokeConfig) string
|
||||||
|
|
||||||
@ -34,27 +34,6 @@ type Monitor interface {
|
|||||||
|
|
||||||
// AttachedSessionID returns the ID of the attached session.
|
// AttachedSessionID returns the ID of the attached session.
|
||||||
AttachedSessionID() string
|
AttachedSessionID() string
|
||||||
|
|
||||||
// Build executes the specified build and returns an ID of the session for debugging that build.
|
|
||||||
Build(ctx context.Context, options controllerapi.BuildOptions, in io.ReadCloser, progress progress.Writer) (ref string, resp *client.SolveResponse, err error)
|
|
||||||
|
|
||||||
// Kill kills the buildx server.
|
|
||||||
Kill(ctx context.Context) error
|
|
||||||
|
|
||||||
// List lists sessions.
|
|
||||||
List(ctx context.Context) (refs []string, _ error)
|
|
||||||
|
|
||||||
// ListPrcesses lists processes in the attached session.
|
|
||||||
ListProcesses(ctx context.Context) (infos []*controllerapi.ProcessInfo, retErr error)
|
|
||||||
|
|
||||||
// DisconnectProcess finishes the specified process.
|
|
||||||
DisconnectProcess(ctx context.Context, pid string) error
|
|
||||||
|
|
||||||
// Inspect returns information about the attached build.
|
|
||||||
Inspect(ctx context.Context) (*controllerapi.InspectResponse, error)
|
|
||||||
|
|
||||||
// Disconnect finishes the attached session.
|
|
||||||
Disconnect(ctx context.Context) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommandInfo is information about a command.
|
// CommandInfo is information about a command.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user