diff options
-rw-r--r-- | cmd/kpod/rm.go | 45 | ||||
-rw-r--r-- | libkpod/remove.go | 53 | ||||
-rw-r--r-- | test/kpod_rm.bats | 10 |
3 files changed, 18 insertions, 90 deletions
diff --git a/cmd/kpod/rm.go b/cmd/kpod/rm.go index 0d3027abd..c7f5f72b2 100644 --- a/cmd/kpod/rm.go +++ b/cmd/kpod/rm.go @@ -4,9 +4,7 @@ import ( "fmt" "github.com/pkg/errors" - "github.com/projectatomic/libpod/libkpod" "github.com/urfave/cli" - "golang.org/x/net/context" ) var ( @@ -30,40 +28,31 @@ var ( // saveCmd saves the image to either docker-archive or oci func rmCmd(c *cli.Context) error { - args := c.Args() - if len(args) == 0 { - return errors.Errorf("specify one or more containers to remove") - } if err := validateFlags(c, rmFlags); err != nil { return err } - config, err := getConfig(c) - if err != nil { - return errors.Wrapf(err, "could not get config") - } - server, err := libkpod.New(config) + runtime, err := getRuntime(c) if err != nil { - return errors.Wrapf(err, "could not get container server") + return errors.Wrapf(err, "Could not get runtime") } - defer server.Shutdown() - err = server.Update() - if err != nil { - return errors.Wrapf(err, "could not update list of containers") + defer runtime.Shutdown(false) + + args := c.Args() + if len(args) == 0 { + return errors.Errorf("specify one or more containers to remove") } - force := c.Bool("force") - for _, container := range c.Args() { - id, err2 := server.Remove(context.Background(), container, force) - if err2 != nil { - if err == nil { - err = err2 - } else { - err = errors.Wrapf(err, "%v. Stop the container before attempting removal or use -f\n", err2) - } - } else { - fmt.Println(id) + for _, container := range args { + ctr, err := runtime.LookupContainer(container) + if err != nil { + return errors.Wrapf(err, "error looking up container", container) + } + err = runtime.RemoveContainer(ctr, c.Bool("force")) + if err != nil { + return errors.Wrapf(err, "error removing container %q", ctr.ID()) } + fmt.Println(ctr.ID()) } - return err + return nil } diff --git a/libkpod/remove.go b/libkpod/remove.go deleted file mode 100644 index 529348840..000000000 --- a/libkpod/remove.go +++ /dev/null @@ -1,53 +0,0 @@ -package libkpod - -import ( - "os" - "path/filepath" - - "github.com/pkg/errors" - "github.com/projectatomic/libpod/oci" - "golang.org/x/net/context" -) - -// Remove removes a container -func (c *ContainerServer) Remove(ctx context.Context, container string, force bool) (string, error) { - ctr, err := c.LookupContainer(container) - if err != nil { - return "", err - } - ctrID := ctr.ID() - - cStatus := c.runtime.ContainerStatus(ctr) - switch cStatus.Status { - case oci.ContainerStatePaused: - return "", errors.Errorf("cannot remove paused container %s", ctrID) - case oci.ContainerStateCreated, oci.ContainerStateRunning: - if force { - _, err = c.ContainerStop(ctx, container, 10) - if err != nil { - return "", errors.Wrapf(err, "unable to stop container %s", ctrID) - } - } else { - return "", errors.Errorf("cannot remove running container %s", ctrID) - } - } - - if err := c.runtime.DeleteContainer(ctr); err != nil { - return "", errors.Wrapf(err, "failed to delete container %s", ctrID) - } - if err := os.Remove(filepath.Join(c.Config().RuntimeConfig.ContainerExitsDir, ctrID)); err != nil && !os.IsNotExist(err) { - return "", errors.Wrapf(err, "failed to remove container exit file %s", ctrID) - } - c.RemoveContainer(ctr) - - if err := c.storageRuntimeServer.DeleteContainer(ctrID); err != nil { - return "", errors.Wrapf(err, "failed to delete storage for container %s", ctrID) - } - - c.ReleaseContainerName(ctr.Name()) - - if err := c.ctrIDIndex.Delete(ctrID); err != nil { - return "", err - } - return ctrID, nil -} diff --git a/test/kpod_rm.bats b/test/kpod_rm.bats index 6794f3eb4..73d469ca2 100644 --- a/test/kpod_rm.bats +++ b/test/kpod_rm.bats @@ -59,21 +59,13 @@ function setup() { } @test "remove a created container" { - skip "Test needs to be converted to kpod run" - start_crio - run crioctl pod run --config "$TESTDATA"/sandbox_config.json - echo "$output" - [ "$status" -eq 0 ] - pod_id="$output" - run crioctl ctr create --config "$TESTDATA"/container_config.json --pod "$pod_id" + run ${KPOD_BINARY} ${KPOD_OPTIONS} create $BB ls echo "$output" [ "$status" -eq 0 ] ctr_id="$output" run bash -c ${KPOD_BINARY} $KPOD_OPTIONS rm -f "$ctr_id" echo "$output" [ "$status" -eq 0 ] - cleanup_pods - stop_crio } @test "remove a running container" { |