From fea78d5530f996d008912584761433023491f160 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Wed, 7 Oct 2020 14:47:52 -0400 Subject: This PR allows users to remove external containers directly Currenly if a user specifies the name or ID of an external storage container, we report an error to them. buildah from scratch working-container-2 podman rm working-container-2 Error: no container with name or ID working-container-2 found: no such container Since the user specified the correct name and the container is in storage we force them to specify --storage to remove it. This is a bad experience for the user. This change will just remove the container from storage. If the container is known by libpod, it will remove the container from libpod as well. The podman rm --storage option has been deprecated, and removed from docs. Also cleaned documented options that are not available to podman-remote. Signed-off-by: Daniel J Walsh --- pkg/domain/infra/abi/containers.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'pkg/domain/infra/abi/containers.go') diff --git a/pkg/domain/infra/abi/containers.go b/pkg/domain/infra/abi/containers.go index d92911e0c..71b2fa8e2 100644 --- a/pkg/domain/infra/abi/containers.go +++ b/pkg/domain/infra/abi/containers.go @@ -273,16 +273,6 @@ func (ic *ContainerEngine) ContainerRestart(ctx context.Context, namesOrIds []st func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string, options entities.RmOptions) ([]*entities.RmReport, error) { reports := []*entities.RmReport{} - if options.Storage { - for _, ctr := range namesOrIds { - report := entities.RmReport{Id: ctr} - if err := ic.Libpod.RemoveStorageContainer(ctr, options.Force); err != nil { - report.Err = err - } - reports = append(reports, &report) - } - return reports, nil - } names := namesOrIds for _, cidFile := range options.CIDFiles { @@ -294,6 +284,22 @@ func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string, names = append(names, id) } + // Attempt to remove named containers directly from storage, if container is defined in libpod + // this will fail and code will fall through to removing the container from libpod.` + tmpNames := []string{} + for _, ctr := range names { + report := entities.RmReport{Id: ctr} + if err := ic.Libpod.RemoveStorageContainer(ctr, options.Force); err != nil { + // remove container names that we successfully deleted + tmpNames = append(tmpNames, ctr) + } else { + reports = append(reports, &report) + } + } + if len(tmpNames) < len(names) { + names = tmpNames + } + ctrs, err := getContainersByContext(options.All, options.Latest, names, ic.Libpod) if err != nil && !(options.Ignore && errors.Cause(err) == define.ErrNoSuchCtr) { // Failed to get containers. If force is specified, get the containers ID @@ -302,7 +308,7 @@ func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string, return nil, err } - for _, ctr := range namesOrIds { + for _, ctr := range names { logrus.Debugf("Evicting container %q", ctr) report := entities.RmReport{Id: ctr} id, err := ic.Libpod.EvictContainer(ctx, ctr, options.Volumes) -- cgit v1.2.3-54-g00ecf