|
| 1 | +package dap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "io/fs" |
| 8 | + "net" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + |
| 14 | + "github.com/docker/buildx/build" |
| 15 | + "github.com/docker/buildx/util/ioset" |
| 16 | + "github.com/docker/cli/cli-plugins/metadata" |
| 17 | + "github.com/google/go-dap" |
| 18 | + "github.com/pkg/errors" |
| 19 | + "golang.org/x/sync/errgroup" |
| 20 | + "golang.org/x/sync/semaphore" |
| 21 | +) |
| 22 | + |
| 23 | +type shell struct { |
| 24 | + SocketPath string |
| 25 | + fwd *ioset.Forwarder |
| 26 | + |
| 27 | + once sync.Once |
| 28 | + err error |
| 29 | + |
| 30 | + l net.Listener |
| 31 | + eg *errgroup.Group |
| 32 | + sem *semaphore.Weighted |
| 33 | + |
| 34 | + connected chan struct{} |
| 35 | +} |
| 36 | + |
| 37 | +func newShell() *shell { |
| 38 | + return &shell{ |
| 39 | + fwd: ioset.NewForwarder(), |
| 40 | + sem: semaphore.NewWeighted(1), |
| 41 | + connected: make(chan struct{}), |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Init initializes the shell for connections on the client side. |
| 46 | +// Attach will block until the terminal has been initialized. |
| 47 | +func (s *shell) Init() error { |
| 48 | + return s.listen() |
| 49 | +} |
| 50 | + |
| 51 | +func (s *shell) listen() error { |
| 52 | + s.once.Do(func() { |
| 53 | + var dir string |
| 54 | + dir, s.err = os.MkdirTemp("", "buildx-dap-exec") |
| 55 | + if s.err != nil { |
| 56 | + return |
| 57 | + } |
| 58 | + defer func() { |
| 59 | + if s.err != nil { |
| 60 | + os.RemoveAll(dir) |
| 61 | + } |
| 62 | + }() |
| 63 | + s.SocketPath = filepath.Join(dir, "s.sock") |
| 64 | + |
| 65 | + s.l, s.err = net.Listen("unix", s.SocketPath) |
| 66 | + if s.err != nil { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + s.eg, _ = errgroup.WithContext(context.Background()) |
| 71 | + s.eg.Go(func() error { |
| 72 | + conn, err := s.l.Accept() |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + writeLine(conn, "Attached to build process.") |
| 77 | + |
| 78 | + // Set the input of the forwarder to the connection. |
| 79 | + s.fwd.SetIn(&ioset.In{ |
| 80 | + Stdin: io.NopCloser(conn), |
| 81 | + Stdout: conn, |
| 82 | + Stderr: nopCloser{conn}, |
| 83 | + }) |
| 84 | + close(s.connected) |
| 85 | + |
| 86 | + // Start a background goroutine to politely refuse any subsequent connections. |
| 87 | + for { |
| 88 | + conn, err := s.l.Accept() |
| 89 | + if err != nil { |
| 90 | + return nil |
| 91 | + } |
| 92 | + writeLine(conn, "Error: Already connected to exec instance.") |
| 93 | + conn.Close() |
| 94 | + } |
| 95 | + }) |
| 96 | + }) |
| 97 | + return s.err |
| 98 | +} |
| 99 | + |
| 100 | +// Attach will attach the given thread to the shell. |
| 101 | +// Only one container can attach to a shell at any given time. |
| 102 | +// Other attaches will block until the context is canceled or it is |
| 103 | +// able to reserve the shell for its own use. |
| 104 | +// |
| 105 | +// This method is intended to be called by paused threads. |
| 106 | +func (s *shell) Attach(ctx context.Context, t *thread) error { |
| 107 | + rCtx := t.rCtx |
| 108 | + if rCtx == nil { |
| 109 | + return nil |
| 110 | + } |
| 111 | + |
| 112 | + var f dap.StackFrame |
| 113 | + if len(t.stackTrace) > 0 { |
| 114 | + f = t.frames[t.stackTrace[0]].StackFrame |
| 115 | + } |
| 116 | + |
| 117 | + cfg := &build.InvokeConfig{Tty: true} |
| 118 | + if len(cfg.Entrypoint) == 0 && len(cfg.Cmd) == 0 { |
| 119 | + cfg.Entrypoint = []string{"/bin/sh"} // launch shell by default |
| 120 | + cfg.Cmd = []string{} |
| 121 | + cfg.NoCmd = false |
| 122 | + } |
| 123 | + return s.attach(ctx, f, rCtx, cfg) |
| 124 | +} |
| 125 | + |
| 126 | +func (s *shell) attach(ctx context.Context, f dap.StackFrame, rCtx *build.ResultHandle, cfg *build.InvokeConfig) (retErr error) { |
| 127 | + select { |
| 128 | + case <-s.connected: |
| 129 | + case <-ctx.Done(): |
| 130 | + return context.Cause(ctx) |
| 131 | + } |
| 132 | + |
| 133 | + in, out := ioset.Pipe() |
| 134 | + defer in.Close() |
| 135 | + defer out.Close() |
| 136 | + |
| 137 | + s.fwd.SetOut(&out) |
| 138 | + defer s.fwd.SetOut(nil) |
| 139 | + |
| 140 | + // Check if the entrypoint is executable. If it isn't, don't bother |
| 141 | + // trying to invoke. |
| 142 | + if !s.canInvoke(ctx, in.Stdout, rCtx, cfg) { |
| 143 | + writeLine(in.Stdout, "Waiting for build container...") |
| 144 | + return nil |
| 145 | + } |
| 146 | + |
| 147 | + if err := s.sem.Acquire(ctx, 1); err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + defer s.sem.Release(1) |
| 151 | + |
| 152 | + ctr, err := build.NewContainer(ctx, rCtx, cfg) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + defer ctr.Cancel() |
| 157 | + |
| 158 | + writeLineF(in.Stdout, "Running %s in build container from line %d.", |
| 159 | + strings.Join(append(cfg.Entrypoint, cfg.Cmd...), " "), |
| 160 | + f.Line, |
| 161 | + ) |
| 162 | + |
| 163 | + writeLine(in.Stdout, "Changes to the container will be reset after the next step is executed.") |
| 164 | + err = ctr.Exec(ctx, cfg, in.Stdin, in.Stdout, in.Stderr) |
| 165 | + |
| 166 | + // Send newline to properly terminate the output. |
| 167 | + writeLine(in.Stdout, "") |
| 168 | + |
| 169 | + return err |
| 170 | +} |
| 171 | + |
| 172 | +func (s *shell) canInvoke(ctx context.Context, w io.Writer, rCtx *build.ResultHandle, cfg *build.InvokeConfig) bool { |
| 173 | + var cmd string |
| 174 | + if len(cfg.Entrypoint) > 0 { |
| 175 | + cmd = cfg.Entrypoint[0] |
| 176 | + } else if len(cfg.Cmd) > 0 { |
| 177 | + cmd = cfg.Cmd[0] |
| 178 | + } |
| 179 | + |
| 180 | + if cmd == "" { |
| 181 | + return false |
| 182 | + } |
| 183 | + |
| 184 | + st, err := rCtx.StatFile(ctx, cmd, cfg) |
| 185 | + if err != nil { |
| 186 | + writeLineF(w, "stat error: %s", err) |
| 187 | + return false |
| 188 | + } |
| 189 | + |
| 190 | + mode := fs.FileMode(st.Mode) |
| 191 | + if !mode.IsRegular() { |
| 192 | + writeLineF(w, "not a regular file: %s", mode) |
| 193 | + return false |
| 194 | + } |
| 195 | + if mode&0111 == 0 { |
| 196 | + writeLineF(w, "mode is not executable: %s", mode) |
| 197 | + return false |
| 198 | + } |
| 199 | + return true |
| 200 | +} |
| 201 | + |
| 202 | +// SendRunInTerminalRequest will send the request to the client to attach to |
| 203 | +// the socket path that was created by Init. This is intended to be run |
| 204 | +// from the adapter and interact directly with the client. |
| 205 | +func (s *shell) SendRunInTerminalRequest(ctx Context) error { |
| 206 | + // TODO: this should work in standalone mode too. |
| 207 | + docker := os.Getenv(metadata.ReexecEnvvar) |
| 208 | + req := &dap.RunInTerminalRequest{ |
| 209 | + Request: dap.Request{ |
| 210 | + Command: "runInTerminal", |
| 211 | + }, |
| 212 | + Arguments: dap.RunInTerminalRequestArguments{ |
| 213 | + Kind: "integrated", |
| 214 | + Args: []string{docker, "buildx", "dap", "attach", s.SocketPath}, |
| 215 | + Env: map[string]any{ |
| 216 | + "BUILDX_EXPERIMENTAL": "1", |
| 217 | + }, |
| 218 | + }, |
| 219 | + } |
| 220 | + |
| 221 | + resp := ctx.Request(req) |
| 222 | + if !resp.GetResponse().Success { |
| 223 | + return errors.New(resp.GetResponse().Message) |
| 224 | + } |
| 225 | + return nil |
| 226 | +} |
| 227 | + |
| 228 | +type nopCloser struct { |
| 229 | + io.Writer |
| 230 | +} |
| 231 | + |
| 232 | +func (nopCloser) Close() error { |
| 233 | + return nil |
| 234 | +} |
| 235 | + |
| 236 | +func writeLine(w io.Writer, msg string) { |
| 237 | + if os.PathSeparator == '\\' { |
| 238 | + fmt.Fprint(w, msg+"\r\n") |
| 239 | + } else { |
| 240 | + fmt.Fprintln(w, msg) |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +func writeLineF(w io.Writer, format string, a ...any) { |
| 245 | + if os.PathSeparator == '\\' { |
| 246 | + fmt.Fprintf(w, format+"\r\n", a...) |
| 247 | + } else { |
| 248 | + fmt.Fprintf(w, format+"\n", a...) |
| 249 | + } |
| 250 | +} |
0 commit comments