logs.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. }
  45. if len(options.Services) == 0 {
  46. options.Services = project.ServiceNames()
  47. }
  48. containers = containers.filter(isService(options.Services...))
  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. err := s.logContainers(ctx, consumer, c, api.LogOptions{
  85. Follow: options.Follow,
  86. Since: t.Format(time.RFC3339Nano),
  87. Until: options.Until,
  88. Tail: options.Tail,
  89. Timestamps: options.Timestamps,
  90. })
  91. if _, ok := err.(errdefs.ErrNotImplemented); ok {
  92. // ignore
  93. return nil
  94. }
  95. return err
  96. }, func(c types.Container, t time.Time) error {
  97. printer.HandleEvent(api.ContainerEvent{
  98. Type: api.ContainerEventAttach,
  99. Container: "", // actual name will be set by start event
  100. ID: c.ID,
  101. Service: c.Labels[api.ServiceLabel],
  102. })
  103. return nil
  104. })
  105. printer.Stop()
  106. return err
  107. })
  108. }
  109. return eg.Wait()
  110. }
  111. func (s *composeService) logContainers(ctx context.Context, consumer api.LogConsumer, c types.Container, options api.LogOptions) error {
  112. cnt, err := s.apiClient().ContainerInspect(ctx, c.ID)
  113. if err != nil {
  114. return err
  115. }
  116. r, err := s.apiClient().ContainerLogs(ctx, cnt.ID, types.ContainerLogsOptions{
  117. ShowStdout: true,
  118. ShowStderr: true,
  119. Follow: options.Follow,
  120. Since: options.Since,
  121. Until: options.Until,
  122. Tail: options.Tail,
  123. Timestamps: options.Timestamps,
  124. })
  125. if err != nil {
  126. return err
  127. }
  128. defer r.Close() //nolint:errcheck
  129. name := getContainerNameWithoutProject(c)
  130. w := utils.GetWriter(func(line string) {
  131. consumer.Log(name, line)
  132. })
  133. if cnt.Config.Tty {
  134. _, err = io.Copy(w, r)
  135. } else {
  136. _, err = stdcopy.StdCopy(w, w, r)
  137. }
  138. return err
  139. }