mirror of
https://github.com/docker/compose.git
synced 2025-07-26 23:24:05 +02:00
Detect new containers on logs --follow
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
396d449d36
commit
287f3156ae
@ -193,11 +193,11 @@ func (s *ServiceProxy) Down(ctx context.Context, project string, options DownOpt
|
||||
}
|
||||
|
||||
// Logs implements Service interface
|
||||
func (s *ServiceProxy) Logs(ctx context.Context, project string, consumer LogConsumer, options LogOptions) error {
|
||||
func (s *ServiceProxy) Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error {
|
||||
if s.LogsFn == nil {
|
||||
return ErrNotImplemented
|
||||
}
|
||||
return s.LogsFn(ctx, project, consumer, options)
|
||||
return s.LogsFn(ctx, projectName, consumer, options)
|
||||
}
|
||||
|
||||
// Ps implements Service interface
|
||||
|
@ -20,31 +20,47 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/docker/compose-cli/pkg/api"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/docker/compose-cli/pkg/api"
|
||||
"github.com/docker/compose-cli/pkg/utils"
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
func (s *composeService) Logs(ctx context.Context, projectName string, consumer api.LogConsumer, options api.LogOptions) error {
|
||||
containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eg, ctx := errgroup.WithContext(ctx)
|
||||
if options.Follow {
|
||||
eg.Go(func() error {
|
||||
printer := newLogPrinter(consumer)
|
||||
return s.watchContainers(projectName, options.Services, printer.HandleEvent, containers, func(c types.Container) error {
|
||||
return s.logContainers(ctx, consumer, c, options)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
for _, c := range containers {
|
||||
service := c.Labels[api.ServiceLabel]
|
||||
container, err := s.apiClient.ContainerInspect(ctx, c.ID)
|
||||
c := c
|
||||
eg.Go(func() error {
|
||||
return s.logContainers(ctx, consumer, c, options)
|
||||
})
|
||||
}
|
||||
return eg.Wait()
|
||||
}
|
||||
|
||||
func (s *composeService) logContainers(ctx context.Context, consumer api.LogConsumer, c types.Container, options api.LogOptions) error {
|
||||
cnt, err := s.apiClient.ContainerInspect(ctx, c.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
name := getContainerNameWithoutProject(c)
|
||||
eg.Go(func() error {
|
||||
r, err := s.apiClient.ContainerLogs(ctx, container.ID, types.ContainerLogsOptions{
|
||||
service := c.Labels[api.ServiceLabel]
|
||||
r, err := s.apiClient.ContainerLogs(ctx, cnt.ID, types.ContainerLogsOptions{
|
||||
ShowStdout: true,
|
||||
ShowStderr: true,
|
||||
Follow: options.Follow,
|
||||
@ -58,16 +74,14 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
|
||||
}
|
||||
defer r.Close() // nolint errcheck
|
||||
|
||||
name := getContainerNameWithoutProject(c)
|
||||
w := utils.GetWriter(func(line string) {
|
||||
consumer.Log(name, service, line)
|
||||
})
|
||||
if container.Config.Tty {
|
||||
if cnt.Config.Tty {
|
||||
_, err = io.Copy(w, r)
|
||||
} else {
|
||||
_, err = stdcopy.StdCopy(w, w, r)
|
||||
}
|
||||
return err
|
||||
})
|
||||
}
|
||||
return eg.Wait()
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
|
||||
}
|
||||
|
||||
eg.Go(func() error {
|
||||
return s.watchContainers(project, options.AttachTo, listener, attached, func(container moby.Container) error {
|
||||
return s.watchContainers(project.Name, options.AttachTo, listener, attached, func(container moby.Container) error {
|
||||
return s.attachContainer(ctx, container, listener, project)
|
||||
})
|
||||
})
|
||||
@ -69,14 +69,14 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
|
||||
type containerWatchFn func(container moby.Container) error
|
||||
|
||||
// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
|
||||
func (s *composeService) watchContainers(project *types.Project, services []string, listener api.ContainerEventListener, containers Containers, onStart containerWatchFn) error {
|
||||
func (s *composeService) watchContainers(projectName string, services []string, listener api.ContainerEventListener, containers Containers, onStart containerWatchFn) error {
|
||||
watched := map[string]int{}
|
||||
for _, c := range containers {
|
||||
watched[c.ID] = 0
|
||||
}
|
||||
|
||||
ctx, stop := context.WithCancel(context.Background())
|
||||
err := s.Events(ctx, project.Name, api.EventsOptions{
|
||||
err := s.Events(ctx, projectName, api.EventsOptions{
|
||||
Services: services,
|
||||
Consumer: func(event api.Event) error {
|
||||
inspected, err := s.apiClient.ContainerInspect(ctx, event.Container)
|
||||
|
Loading…
x
Reference in New Issue
Block a user