attach.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. convert "github.com/docker/compose-cli/local/moby"
  20. "github.com/docker/compose-cli/pkg/api"
  21. "github.com/docker/compose-cli/utils"
  22. "github.com/compose-spec/compose-go/types"
  23. "github.com/docker/cli/cli/streams"
  24. moby "github.com/docker/docker/api/types"
  25. "github.com/docker/docker/pkg/stdcopy"
  26. )
  27. func (s *composeService) attach(ctx context.Context, project *types.Project, listener api.ContainerEventListener, selectedServices []string) (Containers, error) {
  28. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, selectedServices...)
  29. if err != nil {
  30. return nil, err
  31. }
  32. containers.sorted() // This enforce predictable colors assignment
  33. var names []string
  34. for _, c := range containers {
  35. names = append(names, getContainerNameWithoutProject(c))
  36. }
  37. fmt.Printf("Attaching to %s\n", strings.Join(names, ", "))
  38. for _, container := range containers {
  39. err := s.attachContainer(ctx, container, listener, project)
  40. if err != nil {
  41. return nil, err
  42. }
  43. }
  44. return containers, err
  45. }
  46. func (s *composeService) attachContainer(ctx context.Context, container moby.Container, listener api.ContainerEventListener, project *types.Project) error {
  47. serviceName := container.Labels[api.ServiceLabel]
  48. containerName := getContainerNameWithoutProject(container)
  49. service, err := project.GetService(serviceName)
  50. if err != nil {
  51. return err
  52. }
  53. listener(api.ContainerEvent{
  54. Type: api.ContainerEventAttach,
  55. Container: containerName,
  56. Service: serviceName,
  57. })
  58. w := utils.GetWriter(func(line string) {
  59. listener(api.ContainerEvent{
  60. Type: api.ContainerEventLog,
  61. Container: containerName,
  62. Service: serviceName,
  63. Line: line,
  64. })
  65. })
  66. _, err = s.attachContainerStreams(ctx, container.ID, service.Tty, nil, w)
  67. return err
  68. }
  69. func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, r io.ReadCloser, w io.Writer) (func(), error) {
  70. var (
  71. in *streams.In
  72. restore = func() { /* noop */ }
  73. )
  74. if r != nil {
  75. in = streams.NewIn(r)
  76. restore = in.RestoreTerminal
  77. }
  78. stdin, stdout, err := s.getContainerStreams(ctx, container)
  79. if err != nil {
  80. return restore, err
  81. }
  82. go func() {
  83. <-ctx.Done()
  84. if in != nil {
  85. in.Close() //nolint:errcheck
  86. }
  87. stdout.Close() //nolint:errcheck
  88. }()
  89. if in != nil && stdin != nil {
  90. err := in.SetRawTerminal()
  91. if err != nil {
  92. return restore, err
  93. }
  94. go func() {
  95. io.Copy(stdin, in) //nolint:errcheck
  96. }()
  97. }
  98. if w != nil {
  99. go func() {
  100. if tty {
  101. io.Copy(w, stdout) // nolint:errcheck
  102. } else {
  103. stdcopy.StdCopy(w, w, stdout) // nolint:errcheck
  104. }
  105. }()
  106. }
  107. return restore, nil
  108. }
  109. func (s *composeService) getContainerStreams(ctx context.Context, container string) (io.WriteCloser, io.ReadCloser, error) {
  110. var stdout io.ReadCloser
  111. var stdin io.WriteCloser
  112. cnx, err := s.apiClient.ContainerAttach(ctx, container, moby.ContainerAttachOptions{
  113. Stream: true,
  114. Stdin: true,
  115. Stdout: true,
  116. Stderr: true,
  117. Logs: false,
  118. })
  119. if err == nil {
  120. stdout = convert.ContainerStdout{HijackedResponse: cnx}
  121. stdin = convert.ContainerStdin{HijackedResponse: cnx}
  122. return stdin, stdout, nil
  123. }
  124. // Fallback to logs API
  125. logs, err := s.apiClient.ContainerLogs(ctx, container, moby.ContainerLogsOptions{
  126. ShowStdout: true,
  127. ShowStderr: true,
  128. Follow: true,
  129. })
  130. if err != nil {
  131. return nil, nil, err
  132. }
  133. return stdin, logs, nil
  134. }