logs.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "errors"
  17. "io"
  18. "time"
  19. "github.com/docker/docker/api/types"
  20. containerType "github.com/docker/docker/api/types/container"
  21. "github.com/docker/docker/errdefs"
  22. "github.com/docker/docker/pkg/stdcopy"
  23. "github.com/sirupsen/logrus"
  24. "golang.org/x/sync/errgroup"
  25. "github.com/docker/compose/v2/pkg/api"
  26. "github.com/docker/compose/v2/pkg/utils"
  27. )
  28. func (s *composeService) Logs(
  29. ctx context.Context,
  30. projectName string,
  31. consumer api.LogConsumer,
  32. options api.LogOptions,
  33. ) error {
  34. var containers Containers
  35. var err error
  36. if options.Index > 0 {
  37. container, err := s.getSpecifiedContainer(ctx, projectName, oneOffExclude, true, options.Services[0], options.Index)
  38. if err != nil {
  39. return err
  40. }
  41. containers = append(containers, container)
  42. } else {
  43. containers, err = s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
  44. if err != nil {
  45. return err
  46. }
  47. }
  48. if options.Project != nil && len(options.Services) == 0 {
  49. // we run with an explicit compose.yaml, so only consider services defined in this file
  50. options.Services = options.Project.ServiceNames()
  51. containers = containers.filter(isService(options.Services...))
  52. }
  53. eg, ctx := errgroup.WithContext(ctx)
  54. for _, c := range containers {
  55. c := c
  56. eg.Go(func() error {
  57. err := s.logContainers(ctx, consumer, c, options)
  58. var notImplErr errdefs.ErrNotImplemented
  59. if errors.As(err, &notImplErr) {
  60. logrus.Warnf("Can't retrieve logs for %q: %s", getCanonicalContainerName(c), err.Error())
  61. return nil
  62. }
  63. return err
  64. })
  65. }
  66. if options.Follow {
  67. containers = containers.filter(isRunning())
  68. printer := newLogPrinter(consumer)
  69. eg.Go(func() error {
  70. _, err := printer.Run(false, "", nil)
  71. return err
  72. })
  73. for _, c := range containers {
  74. printer.HandleEvent(api.ContainerEvent{
  75. Type: api.ContainerEventAttach,
  76. Container: getContainerNameWithoutProject(c),
  77. ID: c.ID,
  78. Service: c.Labels[api.ServiceLabel],
  79. })
  80. }
  81. eg.Go(func() error {
  82. err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container, t time.Time) error {
  83. printer.HandleEvent(api.ContainerEvent{
  84. Type: api.ContainerEventAttach,
  85. Container: getContainerNameWithoutProject(c),
  86. ID: c.ID,
  87. Service: c.Labels[api.ServiceLabel],
  88. })
  89. eg.Go(func() error {
  90. err := s.logContainers(ctx, consumer, c, api.LogOptions{
  91. Follow: options.Follow,
  92. Since: t.Format(time.RFC3339Nano),
  93. Until: options.Until,
  94. Tail: options.Tail,
  95. Timestamps: options.Timestamps,
  96. })
  97. var notImplErr errdefs.ErrNotImplemented
  98. if errors.As(err, &notImplErr) {
  99. // ignore
  100. return nil
  101. }
  102. return err
  103. })
  104. return nil
  105. }, func(c types.Container, t time.Time) error {
  106. printer.HandleEvent(api.ContainerEvent{
  107. Type: api.ContainerEventAttach,
  108. Container: "", // actual name will be set by start event
  109. ID: c.ID,
  110. Service: c.Labels[api.ServiceLabel],
  111. })
  112. return nil
  113. })
  114. printer.Stop()
  115. return err
  116. })
  117. }
  118. return eg.Wait()
  119. }
  120. func (s *composeService) logContainers(ctx context.Context, consumer api.LogConsumer, c types.Container, options api.LogOptions) error {
  121. cnt, err := s.apiClient().ContainerInspect(ctx, c.ID)
  122. if err != nil {
  123. return err
  124. }
  125. r, err := s.apiClient().ContainerLogs(ctx, cnt.ID, containerType.LogsOptions{
  126. ShowStdout: true,
  127. ShowStderr: true,
  128. Follow: options.Follow,
  129. Since: options.Since,
  130. Until: options.Until,
  131. Tail: options.Tail,
  132. Timestamps: options.Timestamps,
  133. })
  134. if err != nil {
  135. return err
  136. }
  137. defer r.Close() //nolint:errcheck
  138. name := getContainerNameWithoutProject(c)
  139. w := utils.GetWriter(func(line string) {
  140. consumer.Log(name, line)
  141. })
  142. if cnt.Config.Tty {
  143. _, err = io.Copy(w, r)
  144. } else {
  145. _, err = stdcopy.StdCopy(w, w, r)
  146. }
  147. return err
  148. }