mirror of
https://github.com/docker/compose.git
synced 2025-07-25 22:54:54 +02:00
Capture container exit code and dump on console
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
1562af9e41
commit
7a7114fb5f
@ -60,7 +60,7 @@ func (cs *aciComposeService) Create(ctx context.Context, project *types.Project,
|
|||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciComposeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
|
func (cs *aciComposeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
|
||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ func (c *composeService) Create(ctx context.Context, project *types.Project, opt
|
|||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *composeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
|
func (c *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
|
||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ type Service interface {
|
|||||||
// Create executes the equivalent to a `compose create`
|
// Create executes the equivalent to a `compose create`
|
||||||
Create(ctx context.Context, project *types.Project, opts CreateOptions) error
|
Create(ctx context.Context, project *types.Project, opts CreateOptions) error
|
||||||
// Start executes the equivalent to a `compose start`
|
// Start executes the equivalent to a `compose start`
|
||||||
Start(ctx context.Context, project *types.Project, consumer LogConsumer) error
|
Start(ctx context.Context, project *types.Project, options StartOptions) error
|
||||||
// Stop executes the equivalent to a `compose stop`
|
// Stop executes the equivalent to a `compose stop`
|
||||||
Stop(ctx context.Context, project *types.Project) error
|
Stop(ctx context.Context, project *types.Project) error
|
||||||
// Up executes the equivalent to a `compose up`
|
// Up executes the equivalent to a `compose up`
|
||||||
@ -63,6 +63,14 @@ type CreateOptions struct {
|
|||||||
Recreate string
|
Recreate string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartOptions group options of the Start API
|
||||||
|
type StartOptions struct {
|
||||||
|
// Attach will attach to container and pipe stdout/stderr to LogConsumer
|
||||||
|
Attach LogConsumer
|
||||||
|
// CascadeStop will run `Stop` on any container exit
|
||||||
|
CascadeStop bool
|
||||||
|
}
|
||||||
|
|
||||||
// UpOptions group options of the Up API
|
// UpOptions group options of the Up API
|
||||||
type UpOptions struct {
|
type UpOptions struct {
|
||||||
// Detach will create services and return immediately
|
// Detach will create services and return immediately
|
||||||
@ -177,4 +185,5 @@ type Stack struct {
|
|||||||
// LogConsumer is a callback to process log messages from services
|
// LogConsumer is a callback to process log messages from services
|
||||||
type LogConsumer interface {
|
type LogConsumer interface {
|
||||||
Log(service, container, message string)
|
Log(service, container, message string)
|
||||||
|
Exit(service, container string, exitCode int)
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ func startDependencies(ctx context.Context, c *client.Client, project *types.Pro
|
|||||||
if err := c.ComposeService().Create(ctx, project, compose.CreateOptions{}); err != nil {
|
if err := c.ComposeService().Create(ctx, project, compose.CreateOptions{}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := c.ComposeService().Start(ctx, project, nil); err != nil {
|
if err := c.ComposeService().Start(ctx, project, compose.StartOptions{}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -18,6 +18,7 @@ package compose
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/docker/compose-cli/api/compose"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -61,10 +62,12 @@ func runStart(ctx context.Context, opts startOptions, services []string) error {
|
|||||||
|
|
||||||
if opts.Detach {
|
if opts.Detach {
|
||||||
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
||||||
return "", c.ComposeService().Start(ctx, project, nil)
|
return "", c.ComposeService().Start(ctx, project, compose.StartOptions{})
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.ComposeService().Start(ctx, project, formatter.NewLogConsumer(ctx, os.Stdout))
|
return c.ComposeService().Start(ctx, project, compose.StartOptions{
|
||||||
|
Attach: formatter.NewLogConsumer(ctx, os.Stdout),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if opts.Detach {
|
if opts.Detach {
|
||||||
err = c.ComposeService().Start(ctx, project, nil)
|
err = c.ComposeService().Start(ctx, project, compose.StartOptions{})
|
||||||
}
|
}
|
||||||
return "", err
|
return "", err
|
||||||
})
|
})
|
||||||
@ -145,7 +145,9 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.ComposeService().Start(ctx, project, formatter.NewLogConsumer(ctx, os.Stdout))
|
err = c.ComposeService().Start(ctx, project, compose.StartOptions{
|
||||||
|
Attach: formatter.NewLogConsumer(ctx, os.Stdout),
|
||||||
|
})
|
||||||
if errors.Is(ctx.Err(), context.Canceled) {
|
if errors.Is(ctx.Err(), context.Canceled) {
|
||||||
fmt.Println("Gracefully stopping...")
|
fmt.Println("Gracefully stopping...")
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
|
@ -42,12 +42,7 @@ func (l *logConsumer) Log(service, container, message string) {
|
|||||||
if l.ctx.Err() != nil {
|
if l.ctx.Err() != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cf, ok := l.colors[service]
|
cf := l.getColorFunc(service)
|
||||||
if !ok {
|
|
||||||
cf = <-loop
|
|
||||||
l.colors[service] = cf
|
|
||||||
l.computeWidth()
|
|
||||||
}
|
|
||||||
prefix := fmt.Sprintf("%-"+strconv.Itoa(l.width)+"s |", container)
|
prefix := fmt.Sprintf("%-"+strconv.Itoa(l.width)+"s |", container)
|
||||||
|
|
||||||
for _, line := range strings.Split(message, "\n") {
|
for _, line := range strings.Split(message, "\n") {
|
||||||
@ -56,6 +51,21 @@ func (l *logConsumer) Log(service, container, message string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *logConsumer) Exit(service, container string, exitCode int) {
|
||||||
|
msg := fmt.Sprintf("%s exited with code %d\n", container, exitCode)
|
||||||
|
l.writer.Write([]byte(l.getColorFunc(service)(msg)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logConsumer) getColorFunc(service string) colorFunc {
|
||||||
|
cf, ok := l.colors[service]
|
||||||
|
if !ok {
|
||||||
|
cf = <-loop
|
||||||
|
l.colors[service] = cf
|
||||||
|
l.computeWidth()
|
||||||
|
}
|
||||||
|
return cf
|
||||||
|
}
|
||||||
|
|
||||||
func (l *logConsumer) computeWidth() {
|
func (l *logConsumer) computeWidth() {
|
||||||
width := 0
|
width := 0
|
||||||
for n := range l.colors {
|
for n := range l.colors {
|
||||||
|
@ -53,8 +53,8 @@ func (e ecsLocalSimulation) Create(ctx context.Context, project *types.Project,
|
|||||||
return e.compose.Create(ctx, enhanced, opts)
|
return e.compose.Create(ctx, enhanced, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
|
func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
|
||||||
return e.compose.Start(ctx, project, consumer)
|
return e.compose.Start(ctx, project, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project) error {
|
func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project) error {
|
||||||
|
@ -54,3 +54,9 @@ func (a *allowListLogConsumer) Log(service, container, message string) {
|
|||||||
a.delegate.Log(service, container, message)
|
a.delegate.Log(service, container, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *allowListLogConsumer) Exit(service, container string, exitCode int) {
|
||||||
|
if a.allowList[service] {
|
||||||
|
a.delegate.Exit(service, container, exitCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -47,7 +47,7 @@ func (b *ecsAPIService) Create(ctx context.Context, project *types.Project, opts
|
|||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
|
func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
|
||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +144,7 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start executes the equivalent to a `compose start`
|
// Start executes the equivalent to a `compose start`
|
||||||
func (s *composeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
|
func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
|
||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,22 +28,14 @@ import (
|
|||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
moby "github.com/docker/docker/api/types"
|
moby "github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
|
||||||
"github.com/docker/docker/pkg/stdcopy"
|
"github.com/docker/docker/pkg/stdcopy"
|
||||||
"golang.org/x/sync/errgroup"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.LogConsumer) (*errgroup.Group, error) {
|
func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.LogConsumer) (Containers, error) {
|
||||||
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
containers, err := s.getContainers(ctx, project)
|
||||||
Filters: filters.NewArgs(
|
|
||||||
projectFilter(project.Name),
|
|
||||||
),
|
|
||||||
All: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
containers = Containers(containers).filter(isService(project.ServiceNames()...))
|
|
||||||
|
|
||||||
var names []string
|
var names []string
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
@ -51,19 +43,15 @@ func (s *composeService) attach(ctx context.Context, project *types.Project, con
|
|||||||
}
|
}
|
||||||
fmt.Printf("Attaching to %s\n", strings.Join(names, ", "))
|
fmt.Printf("Attaching to %s\n", strings.Join(names, ", "))
|
||||||
|
|
||||||
eg, ctx := errgroup.WithContext(ctx)
|
for _, container := range containers {
|
||||||
for _, c := range containers {
|
s.attachContainer(ctx, container, consumer, project)
|
||||||
container := c
|
|
||||||
eg.Go(func() error {
|
|
||||||
return s.attachContainer(ctx, container, consumer, project)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return eg, nil
|
return containers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) attachContainer(ctx context.Context, container moby.Container, consumer compose.LogConsumer, project *types.Project) error {
|
func (s *composeService) attachContainer(ctx context.Context, container moby.Container, consumer compose.LogConsumer, project *types.Project) error {
|
||||||
serviceName := container.Labels[serviceLabel]
|
serviceName := container.Labels[serviceLabel]
|
||||||
w := utils.GetWriter(serviceName, getCanonicalContainerName(container), consumer)
|
w := getWriter(serviceName, getContainerNameWithoutProject(container), consumer)
|
||||||
|
|
||||||
service, err := project.GetService(serviceName)
|
service, err := project.GetService(serviceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -94,13 +82,15 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container m
|
|||||||
}
|
}
|
||||||
|
|
||||||
if w != nil {
|
if w != nil {
|
||||||
if tty {
|
go func() {
|
||||||
_, err = io.Copy(w, stdout)
|
if tty {
|
||||||
} else {
|
io.Copy(w, stdout) // nolint:errcheck
|
||||||
_, err = stdcopy.StdCopy(w, w, stdout)
|
} else {
|
||||||
}
|
stdcopy.StdCopy(w, w, stdout) // nolint:errcheck
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) getContainerStreams(ctx context.Context, container moby.Container) (io.WriteCloser, io.ReadCloser, error) {
|
func (s *composeService) getContainerStreams(ctx context.Context, container moby.Container) (io.WriteCloser, io.ReadCloser, error) {
|
||||||
|
@ -55,6 +55,16 @@ func getCanonicalContainerName(c moby.Container) string {
|
|||||||
return c.Names[0][1:]
|
return c.Names[0][1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getContainerNameWithoutProject(c moby.Container) string {
|
||||||
|
name := getCanonicalContainerName(c)
|
||||||
|
project := c.Labels[projectLabel]
|
||||||
|
prefix := fmt.Sprintf("%s_%s_", project, c.Labels[serviceLabel])
|
||||||
|
if strings.HasPrefix(name, prefix) {
|
||||||
|
return name[len(project)+1:]
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
|
func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
|
||||||
switch options.Format {
|
switch options.Format {
|
||||||
case "json":
|
case "json":
|
||||||
|
@ -16,11 +16,31 @@
|
|||||||
|
|
||||||
package compose
|
package compose
|
||||||
|
|
||||||
import moby "github.com/docker/docker/api/types"
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/compose-spec/compose-go/types"
|
||||||
|
moby "github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
)
|
||||||
|
|
||||||
// Containers is a set of moby Container
|
// Containers is a set of moby Container
|
||||||
type Containers []moby.Container
|
type Containers []moby.Container
|
||||||
|
|
||||||
|
func (s *composeService) getContainers(ctx context.Context, project *types.Project) (Containers, error) {
|
||||||
|
var containers Containers
|
||||||
|
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
||||||
|
Filters: filters.NewArgs(
|
||||||
|
projectFilter(project.Name),
|
||||||
|
),
|
||||||
|
All: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
containers = containers.filter(isService(project.ServiceNames()...))
|
||||||
|
return containers, nil
|
||||||
|
}
|
||||||
|
|
||||||
// containerPredicate define a predicate we want container to satisfy for filtering operations
|
// containerPredicate define a predicate we want container to satisfy for filtering operations
|
||||||
type containerPredicate func(c moby.Container) bool
|
type containerPredicate func(c moby.Container) bool
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ package compose
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/docker/docker/api/types/container"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
|
|
||||||
@ -25,14 +26,20 @@ import (
|
|||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *composeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
|
func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
|
||||||
var group *errgroup.Group
|
var containers Containers
|
||||||
if consumer != nil {
|
if options.Attach != nil {
|
||||||
eg, err := s.attach(ctx, project, consumer)
|
c, err := s.attach(ctx, project, options.Attach)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
group = eg
|
containers = c
|
||||||
|
} else {
|
||||||
|
c, err := s.getContainers(ctx, project)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
containers = c
|
||||||
}
|
}
|
||||||
|
|
||||||
err := InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
|
err := InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
|
||||||
@ -41,8 +48,24 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, cons
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if group != nil {
|
|
||||||
return group.Wait()
|
if options.Attach == nil {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
eg, ctx := errgroup.WithContext(ctx)
|
||||||
|
for _, c := range containers {
|
||||||
|
c := c
|
||||||
|
eg.Go(func() error {
|
||||||
|
statusC, errC := s.apiClient.ContainerWait(ctx, c.ID, container.WaitConditionNotRunning)
|
||||||
|
select {
|
||||||
|
case status := <-statusC:
|
||||||
|
options.Attach.Exit(c.Labels[serviceLabel], getContainerNameWithoutProject(c), int(status.StatusCode))
|
||||||
|
return nil
|
||||||
|
case err := <-errC:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return eg.Wait()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user