2017-11-30 15:46:11 +00:00
|
|
|
package lib
|
2017-08-30 20:03:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
|
|
|
"github.com/pkg/errors"
|
2017-10-15 20:05:41 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-08-30 20:03:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Remove removes a container
|
2017-10-15 20:05:41 +00:00
|
|
|
func (c *ContainerServer) Remove(ctx context.Context, container string, force bool) (string, error) {
|
2017-08-30 20:03:26 +00:00
|
|
|
ctr, err := c.LookupContainer(container)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ctrID := ctr.ID()
|
|
|
|
|
2017-09-19 12:26:32 +00:00
|
|
|
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:
|
2017-08-30 20:03:26 +00:00
|
|
|
if force {
|
2017-10-15 20:05:41 +00:00
|
|
|
_, err = c.ContainerStop(ctx, container, 10)
|
2017-08-30 20:03:26 +00:00
|
|
|
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
|
|
|
|
}
|