mirror of https://github.com/docker/cli.git
Support parallel rm
Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
This commit is contained in:
parent
f612b93d33
commit
5c1362ce59
|
@ -45,13 +45,22 @@ func runRm(dockerCli *command.DockerCli, opts *rmOptions) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
var errs []string
|
var errs []string
|
||||||
for _, name := range opts.containers {
|
options := types.ContainerRemoveOptions{
|
||||||
if name == "" {
|
RemoveVolumes: opts.rmVolumes,
|
||||||
|
RemoveLinks: opts.rmLink,
|
||||||
|
Force: opts.force,
|
||||||
|
}
|
||||||
|
|
||||||
|
errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, container string) error {
|
||||||
|
if container == "" {
|
||||||
return fmt.Errorf("Container name cannot be empty")
|
return fmt.Errorf("Container name cannot be empty")
|
||||||
}
|
}
|
||||||
name = strings.Trim(name, "/")
|
container = strings.Trim(container, "/")
|
||||||
|
return dockerCli.Client().ContainerRemove(ctx, container, options)
|
||||||
|
})
|
||||||
|
|
||||||
if err := removeContainer(dockerCli, ctx, name, opts.rmVolumes, opts.rmLink, opts.force); err != nil {
|
for _, name := range opts.containers {
|
||||||
|
if err := <-errChan; err != nil {
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(dockerCli.Out(), "%s\n", name)
|
fmt.Fprintf(dockerCli.Out(), "%s\n", name)
|
||||||
|
@ -62,15 +71,3 @@ func runRm(dockerCli *command.DockerCli, opts *rmOptions) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeContainer(dockerCli *command.DockerCli, ctx context.Context, container string, removeVolumes, removeLinks, force bool) error {
|
|
||||||
options := types.ContainerRemoveOptions{
|
|
||||||
RemoveVolumes: removeVolumes,
|
|
||||||
RemoveLinks: removeLinks,
|
|
||||||
Force: force,
|
|
||||||
}
|
|
||||||
if err := dockerCli.Client().ContainerRemove(ctx, container, options); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -91,8 +91,8 @@ func getExitCode(dockerCli *command.DockerCli, ctx context.Context, containerID
|
||||||
return c.State.Running, c.State.ExitCode, nil
|
return c.State.Running, c.State.ExitCode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parallelOperation(ctx context.Context, cids []string, op func(ctx context.Context, id string) error) chan error {
|
func parallelOperation(ctx context.Context, containers []string, op func(ctx context.Context, container string) error) chan error {
|
||||||
if len(cids) == 0 {
|
if len(containers) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
const defaultParallel int = 50
|
const defaultParallel int = 50
|
||||||
|
@ -101,18 +101,18 @@ func parallelOperation(ctx context.Context, cids []string, op func(ctx context.C
|
||||||
|
|
||||||
// make sure result is printed in correct order
|
// make sure result is printed in correct order
|
||||||
output := map[string]chan error{}
|
output := map[string]chan error{}
|
||||||
for _, c := range cids {
|
for _, c := range containers {
|
||||||
output[c] = make(chan error, 1)
|
output[c] = make(chan error, 1)
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
for _, c := range cids {
|
for _, c := range containers {
|
||||||
err := <-output[c]
|
err := <-output[c]
|
||||||
errChan <- err
|
errChan <- err
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for _, c := range cids {
|
for _, c := range containers {
|
||||||
sem <- struct{}{} // Wait for active queue sem to drain.
|
sem <- struct{}{} // Wait for active queue sem to drain.
|
||||||
go func(container string) {
|
go func(container string) {
|
||||||
output[container] <- op(ctx, container)
|
output[container] <- op(ctx, container)
|
||||||
|
|
Loading…
Reference in New Issue