logs.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "bytes"
  16. "context"
  17. "io"
  18. "github.com/docker/compose-cli/api/compose"
  19. "github.com/docker/compose-cli/utils"
  20. "github.com/docker/docker/api/types"
  21. "github.com/docker/docker/api/types/filters"
  22. "github.com/docker/docker/pkg/stdcopy"
  23. "golang.org/x/sync/errgroup"
  24. )
  25. func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  26. list, err := s.apiClient.ContainerList(ctx, types.ContainerListOptions{
  27. Filters: filters.NewArgs(
  28. projectFilter(projectName),
  29. oneOffFilter(false),
  30. ),
  31. All: true,
  32. })
  33. ignore := func(string) bool {
  34. return false
  35. }
  36. if len(options.Services) > 0 {
  37. ignore = func(s string) bool {
  38. return !contains(options.Services, s)
  39. }
  40. }
  41. if err != nil {
  42. return err
  43. }
  44. eg, ctx := errgroup.WithContext(ctx)
  45. for _, c := range list {
  46. c := c
  47. service := c.Labels[serviceLabel]
  48. if ignore(service) {
  49. continue
  50. }
  51. container, err := s.apiClient.ContainerInspect(ctx, c.ID)
  52. if err != nil {
  53. return err
  54. }
  55. eg.Go(func() error {
  56. r, err := s.apiClient.ContainerLogs(ctx, container.ID, types.ContainerLogsOptions{
  57. ShowStdout: true,
  58. ShowStderr: true,
  59. Follow: options.Follow,
  60. Tail: options.Tail,
  61. })
  62. defer r.Close() // nolint errcheck
  63. if err != nil {
  64. return err
  65. }
  66. w := utils.GetWriter(service, getContainerNameWithoutProject(c), consumer)
  67. if container.Config.Tty {
  68. _, err = io.Copy(w, r)
  69. } else {
  70. _, err = stdcopy.StdCopy(w, w, r)
  71. }
  72. return err
  73. })
  74. }
  75. return eg.Wait()
  76. }
  77. type splitBuffer struct {
  78. service string
  79. container string
  80. consumer compose.ContainerEventListener
  81. }
  82. // getWriter creates a io.Writer that will actually split by line and format by LogConsumer
  83. func getWriter(service, container string, events compose.ContainerEventListener) io.Writer {
  84. return splitBuffer{
  85. service: service,
  86. container: container,
  87. consumer: events,
  88. }
  89. }
  90. func (s splitBuffer) Write(b []byte) (n int, err error) {
  91. split := bytes.Split(b, []byte{'\n'})
  92. for _, line := range split {
  93. if len(line) != 0 {
  94. s.consumer(compose.ContainerEvent{
  95. Type: compose.ContainerEventLog,
  96. Service: s.service,
  97. Source: s.container,
  98. Line: string(line),
  99. })
  100. }
  101. }
  102. return len(b), nil
  103. }