attach.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "strings"
  19. "github.com/docker/compose-cli/api/compose"
  20. convert "github.com/docker/compose-cli/local/moby"
  21. "github.com/compose-spec/compose-go/types"
  22. moby "github.com/docker/docker/api/types"
  23. "github.com/docker/docker/pkg/stdcopy"
  24. )
  25. func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.ContainerEventListener) (Containers, error) {
  26. containers, err := s.getContainers(ctx, project)
  27. if err != nil {
  28. return nil, err
  29. }
  30. var names []string
  31. for _, c := range containers {
  32. names = append(names, getCanonicalContainerName(c))
  33. }
  34. fmt.Printf("Attaching to %s\n", strings.Join(names, ", "))
  35. for _, container := range containers {
  36. err := s.attachContainer(ctx, container, consumer, project)
  37. if err != nil {
  38. return nil, err
  39. }
  40. }
  41. return containers, nil
  42. }
  43. func (s *composeService) attachContainer(ctx context.Context, container moby.Container, consumer compose.ContainerEventListener, project *types.Project) error {
  44. serviceName := container.Labels[serviceLabel]
  45. w := getWriter(serviceName, getContainerNameWithoutProject(container), consumer)
  46. service, err := project.GetService(serviceName)
  47. if err != nil {
  48. return err
  49. }
  50. return s.attachContainerStreams(ctx, container, service.Tty, nil, w)
  51. }
  52. func (s *composeService) attachContainerStreams(ctx context.Context, container moby.Container, tty bool, r io.Reader, w io.Writer) error {
  53. stdin, stdout, err := s.getContainerStreams(ctx, container)
  54. if err != nil {
  55. return err
  56. }
  57. go func() {
  58. <-ctx.Done()
  59. stdout.Close() //nolint:errcheck
  60. if stdin != nil {
  61. stdin.Close() //nolint:errcheck
  62. }
  63. }()
  64. if r != nil && stdin != nil {
  65. go func() {
  66. io.Copy(stdin, r) //nolint:errcheck
  67. }()
  68. }
  69. if w != nil {
  70. go func() {
  71. if tty {
  72. io.Copy(w, stdout) // nolint:errcheck
  73. } else {
  74. stdcopy.StdCopy(w, w, stdout) // nolint:errcheck
  75. }
  76. }()
  77. }
  78. return nil
  79. }
  80. func (s *composeService) getContainerStreams(ctx context.Context, container moby.Container) (io.WriteCloser, io.ReadCloser, error) {
  81. var stdout io.ReadCloser
  82. var stdin io.WriteCloser
  83. if container.State == convert.ContainerRunning {
  84. logs, err := s.apiClient.ContainerLogs(ctx, container.ID, moby.ContainerLogsOptions{
  85. ShowStdout: true,
  86. ShowStderr: true,
  87. Follow: true,
  88. })
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. stdout = logs
  93. } else {
  94. cnx, err := s.apiClient.ContainerAttach(ctx, container.ID, moby.ContainerAttachOptions{
  95. Stream: true,
  96. Stdin: true,
  97. Stdout: true,
  98. Stderr: true,
  99. Logs: false,
  100. })
  101. if err != nil {
  102. return nil, nil, err
  103. }
  104. stdout = convert.ContainerStdout{HijackedResponse: cnx}
  105. stdin = convert.ContainerStdin{HijackedResponse: cnx}
  106. }
  107. return stdin, stdout, nil
  108. }