mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-13 15:07:10 +08:00
controller: remove controller grpc service
Remove the controller grpc service along with associated code related to sessions or remote controllers. Data types that are still used with complicated dependency chains have been kept in the same package for a future refactor. Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:

committed by
Jonathan A. Sternberg

parent
2eaea647d8
commit
2f1be25b8f
@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"slices"
|
||||
|
||||
cerrdefs "github.com/containerd/errdefs"
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -23,57 +23,33 @@ func NewAttachCmd(m types.Monitor, stdout io.WriteCloser) types.Command {
|
||||
func (cm *AttachCmd) Info() types.CommandInfo {
|
||||
return types.CommandInfo{
|
||||
Name: "attach",
|
||||
HelpMessage: "attach to a buildx server or a process in the container",
|
||||
HelpMessage: "attach to a process in the container",
|
||||
HelpMessageLong: `
|
||||
Usage:
|
||||
attach ID
|
||||
attach PID
|
||||
|
||||
ID is for a session (visible via list command) or a process (visible via ps command).
|
||||
If you attached to a process, use Ctrl-a-c for switching the monitor to that process's STDIO.
|
||||
PID is for a process (visible via ps command).
|
||||
Use Ctrl-a-c for switching the monitor to that process's STDIO.
|
||||
`,
|
||||
}
|
||||
}
|
||||
|
||||
func (cm *AttachCmd) Exec(ctx context.Context, args []string) error {
|
||||
if len(args) < 2 {
|
||||
return errors.Errorf("ID of session or process must be passed")
|
||||
return errors.Errorf("PID of process must be passed")
|
||||
}
|
||||
ref := args[1]
|
||||
var id string
|
||||
pid := args[1]
|
||||
|
||||
isProcess, err := isProcessID(ctx, cm.m, ref)
|
||||
if err == nil && isProcess {
|
||||
cm.m.Attach(ctx, ref)
|
||||
id = ref
|
||||
}
|
||||
if id == "" {
|
||||
refs, err := cm.m.List(ctx)
|
||||
if err != nil {
|
||||
return errors.Errorf("failed to get the list of sessions: %v", err)
|
||||
}
|
||||
if !slices.Contains(refs, ref) {
|
||||
return errors.Errorf("unknown ID: %q", ref)
|
||||
}
|
||||
cm.m.Detach() // Finish existing attach
|
||||
cm.m.AttachSession(ref)
|
||||
}
|
||||
fmt.Fprintf(cm.stdout, "Attached to process %q. Press Ctrl-a-c to switch to the new container\n", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func isProcessID(ctx context.Context, c types.Monitor, ref string) (bool, error) {
|
||||
sid := c.AttachedSessionID()
|
||||
if sid == "" {
|
||||
return false, errors.Errorf("no attaching session")
|
||||
}
|
||||
infos, err := c.ListProcesses(ctx, sid)
|
||||
infos, err := cm.m.ListProcesses(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return err
|
||||
}
|
||||
for _, p := range infos {
|
||||
if p.ProcessID == ref {
|
||||
return true, nil
|
||||
if p.ProcessID == pid {
|
||||
cm.m.Attach(ctx, pid)
|
||||
fmt.Fprintf(cm.stdout, "Attached to process %q. Press Ctrl-a-c to switch to the new container\n", pid)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
return errors.Wrapf(cerrdefs.ErrNotFound, "pid %s", pid)
|
||||
}
|
||||
|
@ -1,54 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type DisconnectCmd struct {
|
||||
m types.Monitor
|
||||
}
|
||||
|
||||
func NewDisconnectCmd(m types.Monitor) types.Command {
|
||||
return &DisconnectCmd{m}
|
||||
}
|
||||
|
||||
func (cm *DisconnectCmd) Info() types.CommandInfo {
|
||||
return types.CommandInfo{
|
||||
Name: "disconnect",
|
||||
HelpMessage: "disconnect a client from a buildx server. Specific session ID can be specified an arg",
|
||||
HelpMessageLong: fmt.Sprintf(`
|
||||
Usage:
|
||||
disconnect [ID]
|
||||
|
||||
ID is for a session (visible via list command). Default is %q.
|
||||
`, cm.m.AttachedSessionID()),
|
||||
}
|
||||
}
|
||||
|
||||
func (cm *DisconnectCmd) Exec(ctx context.Context, args []string) error {
|
||||
target := cm.m.AttachedSessionID()
|
||||
if len(args) >= 2 {
|
||||
target = args[1]
|
||||
} else if target == "" {
|
||||
return errors.Errorf("no attaching session")
|
||||
}
|
||||
isProcess, err := isProcessID(ctx, cm.m, target)
|
||||
if err == nil && isProcess {
|
||||
sid := cm.m.AttachedSessionID()
|
||||
if sid == "" {
|
||||
return errors.Errorf("no attaching session")
|
||||
}
|
||||
if err := cm.m.DisconnectProcess(ctx, sid, target); err != nil {
|
||||
return errors.Errorf("disconnecting from process failed %v", target)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := cm.m.DisconnectSession(ctx, target); err != nil {
|
||||
return errors.Errorf("disconnecting from session failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -35,9 +35,6 @@ COMMAND and ARG... will be executed in the container.
|
||||
}
|
||||
|
||||
func (cm *ExecCmd) Exec(ctx context.Context, args []string) error {
|
||||
if ref := cm.m.AttachedSessionID(); ref == "" {
|
||||
return errors.Errorf("no attaching session")
|
||||
}
|
||||
if len(args) < 2 {
|
||||
return errors.Errorf("command must be passed")
|
||||
}
|
||||
|
@ -1,36 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type KillCmd struct {
|
||||
m types.Monitor
|
||||
}
|
||||
|
||||
func NewKillCmd(m types.Monitor) types.Command {
|
||||
return &KillCmd{m}
|
||||
}
|
||||
|
||||
func (cm *KillCmd) Info() types.CommandInfo {
|
||||
return types.CommandInfo{
|
||||
Name: "kill",
|
||||
HelpMessage: "kill buildx server",
|
||||
HelpMessageLong: `
|
||||
Usage:
|
||||
kill
|
||||
|
||||
Kills the currently connecting buildx server process.
|
||||
`,
|
||||
}
|
||||
}
|
||||
|
||||
func (cm *KillCmd) Exec(ctx context.Context, args []string) error {
|
||||
if err := cm.m.Kill(ctx); err != nil {
|
||||
return errors.Errorf("failed to kill: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
)
|
||||
|
||||
type ListCmd struct {
|
||||
m types.Monitor
|
||||
|
||||
stdout io.WriteCloser
|
||||
}
|
||||
|
||||
func NewListCmd(m types.Monitor, stdout io.WriteCloser) types.Command {
|
||||
return &ListCmd{m, stdout}
|
||||
}
|
||||
|
||||
func (cm *ListCmd) Info() types.CommandInfo {
|
||||
return types.CommandInfo{
|
||||
Name: "list",
|
||||
HelpMessage: "list buildx sessions",
|
||||
HelpMessageLong: `
|
||||
Usage:
|
||||
list
|
||||
`,
|
||||
}
|
||||
}
|
||||
|
||||
func (cm *ListCmd) Exec(ctx context.Context, args []string) error {
|
||||
refs, err := cm.m.List(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Strings(refs)
|
||||
tw := tabwriter.NewWriter(cm.stdout, 1, 8, 1, '\t', 0)
|
||||
fmt.Fprintln(tw, "ID\tCURRENT_SESSION")
|
||||
for _, k := range refs {
|
||||
fmt.Fprintf(tw, "%-20s\t%v\n", k, k == cm.m.AttachedSessionID())
|
||||
}
|
||||
tw.Flush()
|
||||
return nil
|
||||
}
|
@ -7,7 +7,6 @@ import (
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type PsCmd struct {
|
||||
@ -31,11 +30,7 @@ Usage:
|
||||
}
|
||||
|
||||
func (cm *PsCmd) Exec(ctx context.Context, args []string) error {
|
||||
ref := cm.m.AttachedSessionID()
|
||||
if ref == "" {
|
||||
return errors.Errorf("no attaching session")
|
||||
}
|
||||
plist, err := cm.m.ListProcesses(ctx, ref)
|
||||
plist, err := cm.m.ListProcesses(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
cbuild "github.com/docker/buildx/controller/build"
|
||||
controllererrors "github.com/docker/buildx/controller/errdefs"
|
||||
controllerapi "github.com/docker/buildx/controller/pb"
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
@ -19,11 +20,11 @@ type ReloadCmd struct {
|
||||
stdout io.WriteCloser
|
||||
progress *progress.Printer
|
||||
|
||||
options *controllerapi.BuildOptions
|
||||
options *cbuild.Options
|
||||
invokeConfig *controllerapi.InvokeConfig
|
||||
}
|
||||
|
||||
func NewReloadCmd(m types.Monitor, stdout io.WriteCloser, progress *progress.Printer, options *controllerapi.BuildOptions, invokeConfig *controllerapi.InvokeConfig) types.Command {
|
||||
func NewReloadCmd(m types.Monitor, stdout io.WriteCloser, progress *progress.Printer, options *cbuild.Options, invokeConfig *controllerapi.InvokeConfig) types.Command {
|
||||
return &ReloadCmd{m, stdout, progress, options, invokeConfig}
|
||||
}
|
||||
|
||||
@ -39,34 +40,15 @@ Usage:
|
||||
}
|
||||
|
||||
func (cm *ReloadCmd) Exec(ctx context.Context, args []string) error {
|
||||
var bo *controllerapi.BuildOptions
|
||||
if ref := cm.m.AttachedSessionID(); ref != "" {
|
||||
// Rebuilding an existing session; Restore the build option used for building this session.
|
||||
res, err := cm.m.Inspect(ctx, ref)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to inspect the current build session: %v\n", err)
|
||||
} else {
|
||||
bo = res.Options
|
||||
}
|
||||
} else {
|
||||
bo = cm.options
|
||||
}
|
||||
if bo == nil {
|
||||
return errors.Errorf("no build option is provided")
|
||||
}
|
||||
if ref := cm.m.AttachedSessionID(); ref != "" {
|
||||
if err := cm.m.Disconnect(ctx, ref); err != nil {
|
||||
fmt.Println("disconnect error", err)
|
||||
}
|
||||
}
|
||||
bo := cm.m.Inspect(ctx)
|
||||
|
||||
var resultUpdated bool
|
||||
cm.progress.Unpause()
|
||||
ref, _, _, err := cm.m.Build(ctx, bo, nil, cm.progress) // TODO: support stdin, hold build ref
|
||||
_, _, err := cm.m.Build(ctx, bo, nil, cm.progress) // TODO: support stdin, hold build ref
|
||||
cm.progress.Pause()
|
||||
if err != nil {
|
||||
var be *controllererrors.BuildError
|
||||
if errors.As(err, &be) {
|
||||
ref = be.SessionID
|
||||
resultUpdated = true
|
||||
} else {
|
||||
fmt.Printf("failed to reload: %v\n", err)
|
||||
@ -79,7 +61,6 @@ func (cm *ReloadCmd) Exec(ctx context.Context, args []string) error {
|
||||
} else {
|
||||
resultUpdated = true
|
||||
}
|
||||
cm.m.AttachSession(ref)
|
||||
if resultUpdated {
|
||||
// rollback the running container with the new result
|
||||
id := cm.m.Rollback(ctx, cm.invokeConfig)
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
|
||||
controllerapi "github.com/docker/buildx/controller/pb"
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type RollbackCmd struct {
|
||||
@ -38,9 +37,6 @@ COMMAND and ARG... will be executed in the container.
|
||||
}
|
||||
|
||||
func (cm *RollbackCmd) Exec(ctx context.Context, args []string) error {
|
||||
if ref := cm.m.AttachedSessionID(); ref == "" {
|
||||
return errors.Errorf("no attaching session")
|
||||
}
|
||||
cfg := cm.invokeConfig
|
||||
if len(args) >= 2 {
|
||||
cmds := args[1:]
|
||||
|
Reference in New Issue
Block a user