1
0

attach.go 3.9 KB

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