attach.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/docker/compose-cli/utils"
  22. "github.com/compose-spec/compose-go/types"
  23. moby "github.com/docker/docker/api/types"
  24. "github.com/docker/docker/pkg/stdcopy"
  25. )
  26. func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.ContainerEventListener, selectedServices []string) (Containers, error) {
  27. containers, err := s.getContainers(ctx, project, oneOffExclude, 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, getCanonicalContainerName(c))
  35. }
  36. fmt.Printf("Attaching to %s\n", strings.Join(names, ", "))
  37. for _, container := range containers {
  38. consumer(compose.ContainerEvent{
  39. Type: compose.ContainerEventAttach,
  40. Source: container.ID,
  41. Name: getContainerNameWithoutProject(container),
  42. Service: container.Labels[serviceLabel],
  43. })
  44. err := s.attachContainer(ctx, container, consumer, project)
  45. if err != nil {
  46. return nil, err
  47. }
  48. }
  49. return containers, nil
  50. }
  51. func (s *composeService) attachContainer(ctx context.Context, container moby.Container, consumer compose.ContainerEventListener, project *types.Project) error {
  52. serviceName := container.Labels[serviceLabel]
  53. w := utils.GetWriter(getContainerNameWithoutProject(container), serviceName, container.ID, consumer)
  54. service, err := project.GetService(serviceName)
  55. if err != nil {
  56. return err
  57. }
  58. return s.attachContainerStreams(ctx, container, service.Tty, nil, w)
  59. }
  60. func (s *composeService) attachContainerStreams(ctx context.Context, container moby.Container, tty bool, r io.Reader, w io.Writer) error {
  61. stdin, stdout, err := s.getContainerStreams(ctx, container)
  62. if err != nil {
  63. return err
  64. }
  65. go func() {
  66. <-ctx.Done()
  67. stdout.Close() //nolint:errcheck
  68. if stdin != nil {
  69. stdin.Close() //nolint:errcheck
  70. }
  71. }()
  72. if r != nil && stdin != nil {
  73. go func() {
  74. io.Copy(stdin, r) //nolint:errcheck
  75. }()
  76. }
  77. if w != nil {
  78. go func() {
  79. if tty {
  80. io.Copy(w, stdout) // nolint:errcheck
  81. } else {
  82. stdcopy.StdCopy(w, w, stdout) // nolint:errcheck
  83. }
  84. }()
  85. }
  86. return nil
  87. }
  88. func (s *composeService) getContainerStreams(ctx context.Context, container moby.Container) (io.WriteCloser, io.ReadCloser, error) {
  89. var stdout io.ReadCloser
  90. var stdin io.WriteCloser
  91. if container.State == convert.ContainerRunning {
  92. logs, err := s.apiClient.ContainerLogs(ctx, container.ID, moby.ContainerLogsOptions{
  93. ShowStdout: true,
  94. ShowStderr: true,
  95. Follow: true,
  96. })
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. stdout = logs
  101. } else {
  102. cnx, err := s.apiClient.ContainerAttach(ctx, container.ID, moby.ContainerAttachOptions{
  103. Stream: true,
  104. Stdin: true,
  105. Stdout: true,
  106. Stderr: true,
  107. Logs: false,
  108. })
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. stdout = convert.ContainerStdout{HijackedResponse: cnx}
  113. stdin = convert.ContainerStdin{HijackedResponse: cnx}
  114. }
  115. return stdin, stdout, nil
  116. }