Make progress event display homogeneous between up, down, start, stop

Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
Guillaume Tardif 2021-01-29 14:23:15 +01:00
parent fd906c6c91
commit 3252c409f8
2 changed files with 20 additions and 12 deletions

View File

@ -98,7 +98,7 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
}
for _, container := range actual {
name := getCanonicalContainerName(container)
name := getContainerProgressName(container)
diverged := container.Labels[configHashLabel] != expected
if diverged || recreate == compose.RecreateForce || service.Extensions[extLifecycle] == forceRecreate {
@ -132,6 +132,10 @@ func getContainerName(projectName string, service types.ServiceConfig, number in
return name
}
func getContainerProgressName(container moby.Container) string {
return "Container " + getCanonicalContainerName(container)
}
func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
eg, _ := errgroup.WithContext(ctx)
for dep, config := range service.DependsOn {
@ -191,18 +195,19 @@ func getScale(config types.ServiceConfig) (int, error) {
func (s *composeService) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, autoRemove bool) error {
w := progress.ContextWriter(ctx)
w.Event(progress.CreatingEvent(name))
eventName := "Container " + name
w.Event(progress.CreatingEvent(eventName))
err := s.createMobyContainer(ctx, project, service, name, number, nil, autoRemove)
if err != nil {
return err
}
w.Event(progress.CreatedEvent(name))
w.Event(progress.CreatedEvent(eventName))
return nil
}
func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, container moby.Container) error {
w := progress.ContextWriter(ctx)
w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Working, "Recreate"))
w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Recreate"))
err := s.apiClient.ContainerStop(ctx, container.ID, nil)
if err != nil {
return err
@ -225,7 +230,7 @@ func (s *composeService) recreateContainer(ctx context.Context, project *types.P
if err != nil {
return err
}
w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Done, "Recreated"))
w.Event(progress.NewEvent(getContainerProgressName(container), progress.Done, "Recreated"))
setDependentLifecycle(project, service.Name, forceRecreate)
return nil
}
@ -245,12 +250,12 @@ func setDependentLifecycle(project *types.Project, service string, strategy stri
func (s *composeService) restartContainer(ctx context.Context, container moby.Container) error {
w := progress.ContextWriter(ctx)
w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Working, "Restart"))
w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Restart"))
err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
if err != nil {
return err
}
w.Event(progress.NewEvent(getCanonicalContainerName(container), progress.Done, "Restarted"))
w.Event(progress.NewEvent(getContainerProgressName(container), progress.Done, "Restarted"))
return nil
}
@ -336,10 +341,11 @@ func (s *composeService) startService(ctx context.Context, project *types.Projec
}
eg.Go(func() error {
w := progress.ContextWriter(ctx)
w.Event(progress.StartingEvent(getCanonicalContainerName(container)))
eventName := getContainerProgressName(container)
w.Event(progress.StartingEvent(eventName))
err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
if err == nil {
w.Event(progress.StartedEvent(getCanonicalContainerName(container)))
w.Event(progress.StartedEvent(eventName))
}
return err
})

View File

@ -92,7 +92,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
func (s *composeService) stopContainers(ctx context.Context, w progress.Writer, containers []moby.Container) error {
for _, container := range containers {
toStop := container
eventName := "Container " + getCanonicalContainerName(toStop)
eventName := getContainerProgressName(toStop)
w.Event(progress.StoppingEvent(eventName))
err := s.apiClient.ContainerStop(ctx, toStop.ID, nil)
if err != nil {
@ -109,9 +109,11 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
for _, container := range containers {
toDelete := container
eg.Go(func() error {
eventName := "Container " + getCanonicalContainerName(toDelete)
err := s.stopContainers(ctx, w, []moby.Container{container})
eventName := getContainerProgressName(toDelete)
w.Event(progress.StoppingEvent(eventName))
err := s.stopContainers(ctx, w, []moby.Container{toDelete})
if err != nil {
w.Event(progress.ErrorMessageEvent(eventName, "Error while Stopping"))
return err
}
w.Event(progress.RemovingEvent(eventName))