-
Notifications
You must be signed in to change notification settings - Fork 198
/
containers.go
54 lines (43 loc) · 1.46 KB
/
containers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package host_cleaning
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/docker"
"github.com/werf/werf/pkg/image"
)
func werfContainersFlushByFilterSet(ctx context.Context, filterSet filters.Args, options CommonOptions) error {
containers, err := werfContainersByFilterSet(ctx, filterSet)
if err != nil {
return err
}
if err := containersRemove(ctx, containers, options); err != nil {
return err
}
return nil
}
func werfContainersByFilterSet(ctx context.Context, filterSet filters.Args) ([]types.Container, error) {
filterSet.Add("name", image.StageContainerNamePrefix)
return containersByFilterSet(ctx, filterSet)
}
func containersByFilterSet(ctx context.Context, filterSet filters.Args) ([]types.Container, error) {
containersOptions := types.ContainerListOptions{}
containersOptions.All = true
containersOptions.Quiet = true
containersOptions.Filters = filterSet
return docker.Containers(ctx, containersOptions)
}
func containersRemove(ctx context.Context, containers []types.Container, options CommonOptions) error {
for _, container := range containers {
if options.DryRun {
logboek.Context(ctx).LogLn(logContainerName(container))
logboek.Context(ctx).LogOptionalLn()
} else {
if err := docker.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{Force: options.RmForce}); err != nil {
return err
}
}
}
return nil
}