2020-12-08 11:53:36 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-26 00:57:12 +02:00
|
|
|
"errors"
|
2020-12-08 11:53:36 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2023-11-08 10:19:24 +01:00
|
|
|
"github.com/compose-spec/compose-go/v2/types"
|
2021-03-24 15:37:08 +01:00
|
|
|
"github.com/docker/cli/cli/streams"
|
2023-12-05 10:59:26 +01:00
|
|
|
containerType "github.com/docker/docker/api/types/container"
|
2020-12-08 11:53:36 +01:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2021-06-29 10:16:30 +02:00
|
|
|
"github.com/moby/term"
|
2021-06-15 09:57:38 +02:00
|
|
|
|
2021-08-31 18:53:24 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
|
|
|
"github.com/docker/compose/v2/pkg/utils"
|
2020-12-08 11:53:36 +01:00
|
|
|
)
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (s *composeService) attach(ctx context.Context, project *types.Project, listener api.ContainerEventListener, selectedServices []string) (Containers, error) {
|
2021-04-07 15:00:42 +02:00
|
|
|
containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, selectedServices...)
|
2020-12-08 11:53:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-08 18:53:47 +01:00
|
|
|
if len(containers) == 0 {
|
|
|
|
return containers, nil
|
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2024-09-08 07:01:35 +10:00
|
|
|
containers.sorted() // This enforces predictable colors assignment
|
2021-02-12 10:02:05 +01:00
|
|
|
|
2020-12-08 11:53:36 +01:00
|
|
|
var names []string
|
|
|
|
for _, c := range containers {
|
2021-03-16 14:56:26 +01:00
|
|
|
names = append(names, getContainerNameWithoutProject(c))
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
2021-02-12 10:02:05 +01:00
|
|
|
|
fix linting issues with golangci-lint 1.60.2
pkg/watch/watcher_darwin.go:96:16: Error return value of `d.stream.Start` is not checked (errcheck)
d.stream.Start()
^
pkg/prompt/prompt.go:97:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(u.stdout, message)
^
pkg/prompt/prompt.go:99:12: Error return value of `fmt.Scanln` is not checked (errcheck)
fmt.Scanln(&answer)
^
cmd/formatter/logs.go:118:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
^
cmd/formatter/logs.go:120:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
^
pkg/progress/json.go:67:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/json.go:87:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/plain.go:47:14: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.StatusText)
^
pkg/progress/tty.go:162:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, b.Column(0).ANSI)
^
pkg/progress/tty.go:165:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, aec.Hide)
^
pkg/compose/attach.go:53:13: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(s.stdout(), "Attaching to %s\n", strings.Join(names, ", "))
^
pkg/compose/compose.go:194:6: emptyStringTest: replace `len(dependencies) > 0` with `dependencies != ""` (gocritic)
if len(dependencies) > 0 {
^
pkg/compose/convergence.go:461:2: builtinShadow: shadowing of predeclared identifier: max (gocritic)
max := 0
^
pkg/compose/run.go:127:5: emptyStringTest: replace `len(opts.User) > 0` with `opts.User != ""` (gocritic)
if len(opts.User) > 0 {
^
pkg/compose/run.go:139:5: emptyStringTest: replace `len(opts.WorkingDir) > 0` with `opts.WorkingDir != ""` (gocritic)
if len(opts.WorkingDir) > 0 {
^
pkg/compose/viz.go:91:8: emptyStringTest: replace `len(portConfig.HostIP) > 0` with `portConfig.HostIP != ""` (gocritic)
if len(portConfig.HostIP) > 0 {
^
cmd/compatibility/convert.go:66:6: emptyStringTest: replace `len(arg) > 0` with `arg != ""` (gocritic)
if len(arg) > 0 && arg[0] != '-' {
^
pkg/e2e/watch_test.go:208:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(res.Combined())
^
pkg/e2e/watch_test.go:290:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(r.Combined())
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-09-11 12:52:55 +02:00
|
|
|
_, _ = fmt.Fprintf(s.stdout(), "Attaching to %s\n", strings.Join(names, ", "))
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2025-02-12 09:34:07 +01:00
|
|
|
for _, ctr := range containers {
|
|
|
|
err := s.attachContainer(ctx, ctr, listener)
|
2021-02-08 11:42:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
2021-03-08 10:22:24 +01:00
|
|
|
return containers, err
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
|
|
|
|
2025-02-12 09:34:07 +01:00
|
|
|
func (s *composeService) attachContainer(ctx context.Context, container containerType.Summary, listener api.ContainerEventListener) error {
|
2021-06-14 16:26:14 +02:00
|
|
|
serviceName := container.Labels[api.ServiceLabel]
|
2021-06-08 16:15:45 +02:00
|
|
|
containerName := getContainerNameWithoutProject(container)
|
2020-12-08 11:53:36 +01:00
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
listener(api.ContainerEvent{
|
|
|
|
Type: api.ContainerEventAttach,
|
2021-06-08 16:15:45 +02:00
|
|
|
Container: containerName,
|
2023-02-03 14:36:39 +01:00
|
|
|
ID: container.ID,
|
2021-06-08 16:15:45 +02:00
|
|
|
Service: serviceName,
|
2021-03-08 10:22:24 +01:00
|
|
|
})
|
|
|
|
|
2022-12-13 08:51:55 +01:00
|
|
|
wOut := utils.GetWriter(func(line string) {
|
2021-06-14 16:26:14 +02:00
|
|
|
listener(api.ContainerEvent{
|
|
|
|
Type: api.ContainerEventLog,
|
2021-06-08 16:15:45 +02:00
|
|
|
Container: containerName,
|
2023-02-03 14:36:39 +01:00
|
|
|
ID: container.ID,
|
2021-06-08 16:15:45 +02:00
|
|
|
Service: serviceName,
|
|
|
|
Line: line,
|
|
|
|
})
|
|
|
|
})
|
2022-12-13 08:51:55 +01:00
|
|
|
wErr := utils.GetWriter(func(line string) {
|
|
|
|
listener(api.ContainerEvent{
|
|
|
|
Type: api.ContainerEventErr,
|
|
|
|
Container: containerName,
|
2023-02-03 14:36:39 +01:00
|
|
|
ID: container.ID,
|
2022-12-13 08:51:55 +01:00
|
|
|
Service: serviceName,
|
|
|
|
Line: line,
|
|
|
|
})
|
|
|
|
})
|
2022-04-04 12:15:47 +02:00
|
|
|
|
2023-01-13 19:07:44 +01:00
|
|
|
inspect, err := s.apiClient().ContainerInspect(ctx, container.ID)
|
2022-04-04 12:15:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-13 08:51:55 +01:00
|
|
|
_, _, err = s.attachContainerStreams(ctx, container.ID, inspect.Config.Tty, nil, wOut, wErr)
|
2021-03-24 15:37:08 +01:00
|
|
|
return err
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:57:18 +02:00
|
|
|
func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, stdin io.ReadCloser, stdout, stderr io.WriteCloser) (func(), chan bool, error) {
|
2021-06-29 10:16:30 +02:00
|
|
|
detached := make(chan bool)
|
2024-09-10 13:30:14 +01:00
|
|
|
restore := func() { /* noop */ }
|
2021-07-02 07:55:08 +02:00
|
|
|
if stdin != nil {
|
2021-07-20 11:39:29 +02:00
|
|
|
in := streams.NewIn(stdin)
|
|
|
|
if in.IsTerminal() {
|
|
|
|
state, err := term.SetRawTerminal(in.FD())
|
|
|
|
if err != nil {
|
|
|
|
return restore, detached, err
|
|
|
|
}
|
|
|
|
restore = func() {
|
|
|
|
term.RestoreTerminal(in.FD(), state) //nolint:errcheck
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 15:37:08 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 07:55:08 +02:00
|
|
|
streamIn, streamOut, err := s.getContainerStreams(ctx, container)
|
2020-12-08 11:53:36 +01:00
|
|
|
if err != nil {
|
2021-06-29 10:16:30 +02:00
|
|
|
return restore, detached, err
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
2021-07-20 11:39:29 +02:00
|
|
|
if stdin != nil {
|
|
|
|
stdin.Close() //nolint:errcheck
|
2020-12-11 10:08:27 +01:00
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
}()
|
|
|
|
|
2021-07-20 11:39:29 +02:00
|
|
|
if streamIn != nil && stdin != nil {
|
2020-12-08 11:53:36 +01:00
|
|
|
go func() {
|
2021-07-02 07:55:08 +02:00
|
|
|
_, err := io.Copy(streamIn, stdin)
|
2023-09-26 00:57:12 +02:00
|
|
|
var escapeErr term.EscapeError
|
|
|
|
if errors.As(err, &escapeErr) {
|
2021-06-29 10:16:30 +02:00
|
|
|
close(detached)
|
|
|
|
}
|
2020-12-08 11:53:36 +01:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-07-02 07:55:08 +02:00
|
|
|
if stdout != nil {
|
2021-02-08 11:04:46 +01:00
|
|
|
go func() {
|
2024-09-10 13:30:14 +01:00
|
|
|
defer stdout.Close() //nolint:errcheck
|
|
|
|
defer stderr.Close() //nolint:errcheck
|
|
|
|
defer streamOut.Close() //nolint:errcheck
|
2021-02-08 11:04:46 +01:00
|
|
|
if tty {
|
2022-07-13 19:20:40 +02:00
|
|
|
io.Copy(stdout, streamOut) //nolint:errcheck
|
2021-02-08 11:04:46 +01:00
|
|
|
} else {
|
2022-07-13 19:20:40 +02:00
|
|
|
stdcopy.StdCopy(stdout, stderr, streamOut) //nolint:errcheck
|
2021-02-08 11:04:46 +01:00
|
|
|
}
|
|
|
|
}()
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
2021-06-29 10:16:30 +02:00
|
|
|
return restore, detached, nil
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
|
|
|
|
2021-03-08 10:22:24 +01:00
|
|
|
func (s *composeService) getContainerStreams(ctx context.Context, container string) (io.WriteCloser, io.ReadCloser, error) {
|
2020-12-08 11:53:36 +01:00
|
|
|
var stdout io.ReadCloser
|
|
|
|
var stdin io.WriteCloser
|
2023-12-05 10:59:26 +01:00
|
|
|
cnx, err := s.apiClient().ContainerAttach(ctx, container, containerType.AttachOptions{
|
2021-03-08 10:22:24 +01:00
|
|
|
Stream: true,
|
|
|
|
Stdin: true,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
|
|
|
Logs: false,
|
|
|
|
})
|
|
|
|
if err == nil {
|
2021-06-15 09:57:38 +02:00
|
|
|
stdout = ContainerStdout{HijackedResponse: cnx}
|
|
|
|
stdin = ContainerStdin{HijackedResponse: cnx}
|
2021-03-08 10:22:24 +01:00
|
|
|
return stdin, stdout, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to logs API
|
2023-12-05 10:59:26 +01:00
|
|
|
logs, err := s.apiClient().ContainerLogs(ctx, container, containerType.LogsOptions{
|
2021-03-08 10:22:24 +01:00
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Follow: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|
2021-03-08 10:22:24 +01:00
|
|
|
return stdin, logs, nil
|
2020-12-08 11:53:36 +01:00
|
|
|
}
|