logs.go 4.3 KB

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