-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introduce service hooks #12166
introduce service hooks #12166
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,7 +85,8 @@ func (s *composeService) down(ctx context.Context, projectName string, options a | |
|
||
err = InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error { | ||
serviceContainers := containers.filter(isService(service)) | ||
err := s.removeContainers(ctx, serviceContainers, options.Timeout, options.Volumes) | ||
serv := project.Services[service] | ||
err := s.removeContainers(ctx, serviceContainers, &serv, options.Timeout, options.Volumes) | ||
return err | ||
}, WithRootNodesAndDown(options.Services)) | ||
if err != nil { | ||
|
@@ -94,7 +95,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a | |
|
||
orphans := containers.filter(isOrphaned(project)) | ||
if options.RemoveOrphans && len(orphans) > 0 { | ||
err := s.removeContainers(ctx, orphans, options.Timeout, false) | ||
err := s.removeContainers(ctx, orphans, nil, options.Timeout, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -296,9 +297,19 @@ func (s *composeService) removeVolume(ctx context.Context, id string, w progress | |
return err | ||
} | ||
|
||
func (s *composeService) stopContainer(ctx context.Context, w progress.Writer, container moby.Container, timeout *time.Duration) error { | ||
func (s *composeService) stopContainer(ctx context.Context, w progress.Writer, service *types.ServiceConfig, container moby.Container, timeout *time.Duration) error { | ||
eventName := getContainerProgressName(container) | ||
w.Event(progress.StoppingEvent(eventName)) | ||
|
||
if service != nil { | ||
for _, hook := range service.PreStop { | ||
err := s.runHook(ctx, container, *service, hook, nil) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
Comment on lines
+305
to
+310
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are always iterating over all hooks for PreStop, PostStart, etc.
|
||
} | ||
|
||
timeoutInSecond := utils.DurationSecondToInt(timeout) | ||
err := s.apiClient().ContainerStop(ctx, container.ID, containerType.StopOptions{Timeout: timeoutInSecond}) | ||
if err != nil { | ||
|
@@ -309,32 +320,32 @@ func (s *composeService) stopContainer(ctx context.Context, w progress.Writer, c | |
return nil | ||
} | ||
|
||
func (s *composeService) stopContainers(ctx context.Context, w progress.Writer, containers []moby.Container, timeout *time.Duration) error { | ||
func (s *composeService) stopContainers(ctx context.Context, w progress.Writer, serv *types.ServiceConfig, containers []moby.Container, timeout *time.Duration) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rename to |
||
eg, ctx := errgroup.WithContext(ctx) | ||
for _, container := range containers { | ||
container := container | ||
eg.Go(func() error { | ||
return s.stopContainer(ctx, w, container, timeout) | ||
return s.stopContainer(ctx, w, serv, container, timeout) | ||
}) | ||
} | ||
return eg.Wait() | ||
} | ||
|
||
func (s *composeService) removeContainers(ctx context.Context, containers []moby.Container, timeout *time.Duration, volumes bool) error { | ||
func (s *composeService) removeContainers(ctx context.Context, containers []moby.Container, service *types.ServiceConfig, timeout *time.Duration, volumes bool) error { | ||
eg, _ := errgroup.WithContext(ctx) | ||
for _, container := range containers { | ||
container := container | ||
eg.Go(func() error { | ||
return s.stopAndRemoveContainer(ctx, container, timeout, volumes) | ||
return s.stopAndRemoveContainer(ctx, container, service, timeout, volumes) | ||
}) | ||
} | ||
return eg.Wait() | ||
} | ||
|
||
func (s *composeService) stopAndRemoveContainer(ctx context.Context, container moby.Container, timeout *time.Duration, volumes bool) error { | ||
func (s *composeService) stopAndRemoveContainer(ctx context.Context, container moby.Container, service *types.ServiceConfig, timeout *time.Duration, volumes bool) error { | ||
w := progress.ContextWriter(ctx) | ||
eventName := getContainerProgressName(container) | ||
err := s.stopContainer(ctx, w, container, timeout) | ||
err := s.stopContainer(ctx, w, service, container, timeout) | ||
if errdefs.IsNotFound(err) { | ||
w.Event(progress.RemovedEvent(eventName)) | ||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package compose | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"github.com/compose-spec/compose-go/v2/types" | ||
"github.com/docker/compose/v2/pkg/api" | ||
"github.com/docker/compose/v2/pkg/utils" | ||
moby "github.com/docker/docker/api/types" | ||
containerType "github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/pkg/stdcopy" | ||
) | ||
|
||
func (s composeService) runHook(ctx context.Context, container moby.Container, service types.ServiceConfig, hook types.ServiceHook, listener api.ContainerEventListener) error { | ||
wOut := utils.GetWriter(func(line string) { | ||
listener(api.ContainerEvent{ | ||
Type: api.HookEventLog, | ||
Container: getContainerNameWithoutProject(container) + " ->", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would propose to change the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hooks have no name so far (to be debated) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about we choose an emoji for each hook? 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My first implementation was using 🪝 but this won't run well on a Windows terminal |
||
ID: container.ID, | ||
Service: service.Name, | ||
Line: line, | ||
}) | ||
}) | ||
defer wOut.Close() //nolint:errcheck | ||
|
||
detached := listener == nil | ||
exec, err := s.apiClient().ContainerExecCreate(ctx, container.ID, containerType.ExecOptions{ | ||
User: hook.User, | ||
Privileged: hook.Privileged, | ||
Env: ToMobyEnv(hook.Environment), | ||
WorkingDir: hook.WorkingDir, | ||
Cmd: hook.Command, | ||
Detach: detached, | ||
AttachStdout: !detached, | ||
AttachStderr: !detached, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if detached { | ||
return s.runWaitExec(ctx, exec, service, listener) | ||
} | ||
|
||
height, width := s.stdout().GetTtySize() | ||
consoleSize := &[2]uint{height, width} | ||
attach, err := s.apiClient().ContainerExecAttach(ctx, exec.ID, containerType.ExecAttachOptions{ | ||
Tty: service.Tty, | ||
ConsoleSize: consoleSize, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
defer attach.Close() | ||
|
||
if service.Tty { | ||
_, err = io.Copy(wOut, attach.Reader) | ||
} else { | ||
_, err = stdcopy.StdCopy(wOut, wOut, attach.Reader) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
inspected, err := s.apiClient().ContainerExecInspect(ctx, exec.ID) | ||
if err != nil { | ||
return err | ||
} | ||
if inspected.ExitCode != 0 { | ||
return fmt.Errorf("%s hook exited with status %d", service.Name, inspected.ExitCode) | ||
} | ||
return nil | ||
} | ||
|
||
func (s composeService) runWaitExec(ctx context.Context, exec moby.IDResponse, service types.ServiceConfig, listener api.ContainerEventListener) error { | ||
err := s.apiClient().ContainerExecStart(ctx, exec.ID, containerType.ExecStartOptions{ | ||
Detach: listener == nil, | ||
Tty: service.Tty, | ||
}) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
// We miss a ContainerExecWait API | ||
tick := time.NewTicker(100 * time.Millisecond) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-tick.C: | ||
inspect, err := s.apiClient().ContainerExecInspect(ctx, exec.ID) | ||
if err != nil { | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might have missed something, but shouldn't we return the error in this case? I see the same is done for |
||
} | ||
if !inspect.Running { | ||
if inspect.ExitCode != 0 { | ||
return fmt.Errorf("%s hook exited with status %d", service.Name, inspect.ExitCode) | ||
} | ||
return nil | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add a validation that the service exists here. Since this is the place where we get its value becoming easier later on for debugging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as we run inside
InReverseDependencyOrder
service is guaranteed to exist (actually,InReverseDependencyOrder
could pass a*ServiceConfig
, but this could have impact on immutability)service
is passed as a pointer down tocompose/pkg/compose/down.go
Line 304 in 0aacddf
nil