diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2017-11-01 14:38:21 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-01 14:38:21 -0400 |
commit | f5019df3f5da9030ce21e5c8ad3d3921a6585e7f (patch) | |
tree | 05412dcc190ca026dbe51a4ef72bb91ff646e7c6 /libkpod/remove.go | |
parent | 2b74391cd5281f6fdf391ff8ad50fd1490f6bf89 (diff) | |
parent | eab0737f1189a7b88f0a37a6b894ca4345b6853f (diff) | |
download | podman-f5019df3f5da9030ce21e5c8ad3d3921a6585e7f.tar.gz podman-f5019df3f5da9030ce21e5c8ad3d3921a6585e7f.tar.bz2 podman-f5019df3f5da9030ce21e5c8ad3d3921a6585e7f.zip |
Merge pull request #1 from mheon/master
Initial checkin
Diffstat (limited to 'libkpod/remove.go')
-rw-r--r-- | libkpod/remove.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/libkpod/remove.go b/libkpod/remove.go new file mode 100644 index 000000000..9f6ba2a49 --- /dev/null +++ b/libkpod/remove.go @@ -0,0 +1,53 @@ +package libkpod + +import ( + "os" + "path/filepath" + + "github.com/projectatomic/libpod/oci" + "github.com/pkg/errors" + "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 +} |