summaryrefslogtreecommitdiff
path: root/libkpod/stop.go
blob: 5ab974634ce92c99b268930edb5b24913bfb349c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package libkpod

import (
	"github.com/projectatomic/libpod/oci"
	"github.com/pkg/errors"
	"golang.org/x/net/context"
)

// ContainerStop stops a running container with a grace period (i.e., timeout).
func (c *ContainerServer) ContainerStop(ctx context.Context, container string, timeout int64) (string, error) {
	ctr, err := c.LookupContainer(container)
	if err != nil {
		return "", errors.Wrapf(err, "failed to find container %s", container)
	}
	ctrID := ctr.ID()

	cStatus := c.runtime.ContainerStatus(ctr)
	switch cStatus.Status {

	case oci.ContainerStatePaused:
		return "", errors.Errorf("cannot stop paused container %s", ctrID)
	default:
		if cStatus.Status != oci.ContainerStateStopped {
			if err := c.runtime.StopContainer(ctx, ctr, timeout); err != nil {
				return "", errors.Wrapf(err, "failed to stop container %s", ctrID)
			}
			if err := c.storageRuntimeServer.StopContainer(ctrID); err != nil {
				return "", errors.Wrapf(err, "failed to unmount container %s", ctrID)
			}
		}
	}

	c.ContainerStateToDisk(ctr)

	return ctrID, nil
}