logs.go 4.3 KB

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