2016-03-18 14:50:19 -04:00
|
|
|
package libcontainerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2016-06-02 14:10:55 -04:00
|
|
|
"time"
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
containerd "github.com/docker/containerd/api/grpc/types"
|
2016-10-18 14:18:19 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2016-03-18 14:50:19 -04:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2016-07-25 22:32:51 -04:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2016-08-17 12:38:34 -04:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-03-18 14:50:19 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type client struct {
|
|
|
|
clientCommon
|
|
|
|
|
|
|
|
// Platform specific properties below here.
|
|
|
|
remote *remote
|
|
|
|
q queue
|
|
|
|
exitNotifiers map[string]*exitNotifier
|
2016-06-02 14:10:55 -04:00
|
|
|
liveRestore bool
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:49:36 -04:00
|
|
|
// AddProcess is the handler for adding a process to an already running
|
|
|
|
// container. It's called through docker exec. It returns the system pid of the
|
|
|
|
// exec'd process.
|
2016-10-17 17:39:52 -04:00
|
|
|
func (clnt *client) AddProcess(ctx context.Context, containerID, processFriendlyName string, specp Process, attachStdio StdioCallback) (int, error) {
|
2016-03-18 14:50:19 -04:00
|
|
|
clnt.lock(containerID)
|
|
|
|
defer clnt.unlock(containerID)
|
|
|
|
container, err := clnt.getContainer(containerID)
|
|
|
|
if err != nil {
|
2016-10-17 13:49:36 -04:00
|
|
|
return -1, err
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
spec, err := container.spec()
|
|
|
|
if err != nil {
|
2016-10-17 13:49:36 -04:00
|
|
|
return -1, err
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
sp := spec.Process
|
|
|
|
sp.Args = specp.Args
|
|
|
|
sp.Terminal = specp.Terminal
|
2016-07-13 13:24:41 -04:00
|
|
|
if len(specp.Env) > 0 {
|
2016-03-18 14:50:19 -04:00
|
|
|
sp.Env = specp.Env
|
|
|
|
}
|
|
|
|
if specp.Cwd != nil {
|
|
|
|
sp.Cwd = *specp.Cwd
|
|
|
|
}
|
|
|
|
if specp.User != nil {
|
|
|
|
sp.User = specs.User{
|
|
|
|
UID: specp.User.UID,
|
|
|
|
GID: specp.User.GID,
|
|
|
|
AdditionalGids: specp.User.AdditionalGids,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if specp.Capabilities != nil {
|
|
|
|
sp.Capabilities = specp.Capabilities
|
|
|
|
}
|
|
|
|
|
|
|
|
p := container.newProcess(processFriendlyName)
|
|
|
|
|
|
|
|
r := &containerd.AddProcessRequest{
|
|
|
|
Args: sp.Args,
|
|
|
|
Cwd: sp.Cwd,
|
|
|
|
Terminal: sp.Terminal,
|
|
|
|
Id: containerID,
|
|
|
|
Env: sp.Env,
|
|
|
|
User: &containerd.User{
|
|
|
|
Uid: sp.User.UID,
|
|
|
|
Gid: sp.User.GID,
|
|
|
|
AdditionalGids: sp.User.AdditionalGids,
|
|
|
|
},
|
|
|
|
Pid: processFriendlyName,
|
|
|
|
Stdin: p.fifo(syscall.Stdin),
|
|
|
|
Stdout: p.fifo(syscall.Stdout),
|
|
|
|
Stderr: p.fifo(syscall.Stderr),
|
|
|
|
Capabilities: sp.Capabilities,
|
|
|
|
ApparmorProfile: sp.ApparmorProfile,
|
|
|
|
SelinuxLabel: sp.SelinuxLabel,
|
|
|
|
NoNewPrivileges: sp.NoNewPrivileges,
|
2016-03-23 22:54:32 -04:00
|
|
|
Rlimits: convertRlimits(sp.Rlimits),
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
iopipe, err := p.openFifos(sp.Terminal)
|
|
|
|
if err != nil {
|
2016-10-17 13:49:36 -04:00
|
|
|
return -1, err
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:49:36 -04:00
|
|
|
resp, err := clnt.remote.apiClient.AddProcess(ctx, r)
|
|
|
|
if err != nil {
|
2016-03-18 14:50:19 -04:00
|
|
|
p.closeFifos(iopipe)
|
2016-10-17 13:49:36 -04:00
|
|
|
return -1, err
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 14:18:19 -04:00
|
|
|
var stdinOnce sync.Once
|
|
|
|
stdin := iopipe.Stdin
|
|
|
|
iopipe.Stdin = ioutils.NewWriteCloserWrapper(stdin, func() error {
|
|
|
|
var err error
|
|
|
|
stdinOnce.Do(func() { // on error from attach we don't know if stdin was already closed
|
|
|
|
err = stdin.Close()
|
|
|
|
if err2 := p.sendCloseStdin(); err == nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
container.processes[processFriendlyName] = p
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
if err := attachStdio(*iopipe); err != nil {
|
2016-10-18 14:18:19 -04:00
|
|
|
p.closeFifos(iopipe)
|
2016-10-17 13:49:36 -04:00
|
|
|
return -1, err
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:49:36 -04:00
|
|
|
return int(resp.SystemPid), nil
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
2016-04-18 05:48:13 -04:00
|
|
|
func (clnt *client) SignalProcess(containerID string, pid string, sig int) error {
|
|
|
|
clnt.lock(containerID)
|
|
|
|
defer clnt.unlock(containerID)
|
|
|
|
_, err := clnt.remote.apiClient.Signal(context.Background(), &containerd.SignalRequest{
|
|
|
|
Id: containerID,
|
|
|
|
Pid: pid,
|
|
|
|
Signal: uint32(sig),
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
func (clnt *client) Resize(containerID, processFriendlyName string, width, height int) error {
|
|
|
|
clnt.lock(containerID)
|
|
|
|
defer clnt.unlock(containerID)
|
|
|
|
if _, err := clnt.getContainer(containerID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := clnt.remote.apiClient.UpdateProcess(context.Background(), &containerd.UpdateProcessRequest{
|
|
|
|
Id: containerID,
|
|
|
|
Pid: processFriendlyName,
|
|
|
|
Width: uint32(width),
|
|
|
|
Height: uint32(height),
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) Pause(containerID string) error {
|
|
|
|
return clnt.setState(containerID, StatePause)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) setState(containerID, state string) error {
|
|
|
|
clnt.lock(containerID)
|
|
|
|
container, err := clnt.getContainer(containerID)
|
|
|
|
if err != nil {
|
|
|
|
clnt.unlock(containerID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if container.systemPid == 0 {
|
|
|
|
clnt.unlock(containerID)
|
|
|
|
return fmt.Errorf("No active process for container %s", containerID)
|
|
|
|
}
|
|
|
|
st := "running"
|
|
|
|
if state == StatePause {
|
|
|
|
st = "paused"
|
|
|
|
}
|
|
|
|
chstate := make(chan struct{})
|
|
|
|
_, err = clnt.remote.apiClient.UpdateContainer(context.Background(), &containerd.UpdateContainerRequest{
|
|
|
|
Id: containerID,
|
|
|
|
Pid: InitFriendlyName,
|
|
|
|
Status: st,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
clnt.unlock(containerID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.pauseMonitor.append(state, chstate)
|
|
|
|
clnt.unlock(containerID)
|
|
|
|
<-chstate
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) Resume(containerID string) error {
|
|
|
|
return clnt.setState(containerID, StateResume)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) Stats(containerID string) (*Stats, error) {
|
|
|
|
resp, err := clnt.remote.apiClient.Stats(context.Background(), &containerd.StatsRequest{containerID})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return (*Stats)(resp), nil
|
|
|
|
}
|
|
|
|
|
2016-04-22 12:23:56 -04:00
|
|
|
// Take care of the old 1.11.0 behavior in case the version upgrade
|
2016-06-08 15:58:45 -04:00
|
|
|
// happened without a clean daemon shutdown
|
2016-04-22 12:23:56 -04:00
|
|
|
func (clnt *client) cleanupOldRootfs(containerID string) {
|
|
|
|
// Unmount and delete the bundle folder
|
|
|
|
if mts, err := mount.GetMounts(); err == nil {
|
|
|
|
for _, mts := range mts {
|
|
|
|
if strings.HasSuffix(mts.Mountpoint, containerID+"/rootfs") {
|
|
|
|
if err := syscall.Unmount(mts.Mountpoint, syscall.MNT_DETACH); err == nil {
|
|
|
|
os.RemoveAll(strings.TrimSuffix(mts.Mountpoint, "/rootfs"))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 11:55:39 -04:00
|
|
|
func (clnt *client) setExited(containerID string, exitCode uint32) error {
|
2016-03-18 14:50:19 -04:00
|
|
|
clnt.lock(containerID)
|
|
|
|
defer clnt.unlock(containerID)
|
|
|
|
|
|
|
|
err := clnt.backend.StateChanged(containerID, StateInfo{
|
2016-04-01 20:02:38 -04:00
|
|
|
CommonStateInfo: CommonStateInfo{
|
|
|
|
State: StateExit,
|
|
|
|
ExitCode: exitCode,
|
|
|
|
}})
|
2016-03-18 14:50:19 -04:00
|
|
|
|
2016-04-22 12:23:56 -04:00
|
|
|
clnt.cleanupOldRootfs(containerID)
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) GetPidsForContainer(containerID string) ([]int, error) {
|
|
|
|
cont, err := clnt.getContainerdContainer(containerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pids := make([]int, len(cont.Pids))
|
|
|
|
for i, p := range cont.Pids {
|
|
|
|
pids[i] = int(p)
|
|
|
|
}
|
|
|
|
return pids, nil
|
|
|
|
}
|
|
|
|
|
2016-03-20 18:58:23 -04:00
|
|
|
// Summary returns a summary of the processes running in a container.
|
|
|
|
// This is a no-op on Linux.
|
|
|
|
func (clnt *client) Summary(containerID string) ([]Summary, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
func (clnt *client) getContainerdContainer(containerID string) (*containerd.Container, error) {
|
|
|
|
resp, err := clnt.remote.apiClient.State(context.Background(), &containerd.StateRequest{Id: containerID})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, cont := range resp.Containers {
|
|
|
|
if cont.Id == containerID {
|
|
|
|
return cont, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid state response")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) UpdateResources(containerID string, resources Resources) error {
|
|
|
|
clnt.lock(containerID)
|
|
|
|
defer clnt.unlock(containerID)
|
|
|
|
container, err := clnt.getContainer(containerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if container.systemPid == 0 {
|
|
|
|
return fmt.Errorf("No active process for container %s", containerID)
|
|
|
|
}
|
|
|
|
_, err = clnt.remote.apiClient.UpdateContainer(context.Background(), &containerd.UpdateContainerRequest{
|
|
|
|
Id: containerID,
|
|
|
|
Pid: InitFriendlyName,
|
|
|
|
Resources: (*containerd.UpdateResource)(&resources),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) getExitNotifier(containerID string) *exitNotifier {
|
|
|
|
clnt.mapMutex.RLock()
|
|
|
|
defer clnt.mapMutex.RUnlock()
|
|
|
|
return clnt.exitNotifiers[containerID]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) getOrCreateExitNotifier(containerID string) *exitNotifier {
|
|
|
|
clnt.mapMutex.Lock()
|
|
|
|
w, ok := clnt.exitNotifiers[containerID]
|
|
|
|
defer clnt.mapMutex.Unlock()
|
|
|
|
if !ok {
|
|
|
|
w = &exitNotifier{c: make(chan struct{}), client: clnt}
|
|
|
|
clnt.exitNotifiers[containerID] = w
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
func (clnt *client) restore(cont *containerd.Container, lastEvent *containerd.Event, attachStdio StdioCallback, options ...CreateOption) (err error) {
|
2016-05-02 20:22:11 -04:00
|
|
|
clnt.lock(cont.Id)
|
|
|
|
defer clnt.unlock(cont.Id)
|
|
|
|
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugf("libcontainerd: restore container %s state %s", cont.Id, cont.Status)
|
2016-05-02 20:22:11 -04:00
|
|
|
|
|
|
|
containerID := cont.Id
|
|
|
|
if _, err := clnt.getContainer(containerID); err == nil {
|
|
|
|
return fmt.Errorf("container %s is already active", containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
clnt.deleteContainer(cont.Id)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
container := clnt.newContainer(cont.BundlePath, options...)
|
|
|
|
container.systemPid = systemPid(cont)
|
|
|
|
|
|
|
|
var terminal bool
|
|
|
|
for _, p := range cont.Processes {
|
|
|
|
if p.Pid == InitFriendlyName {
|
|
|
|
terminal = p.Terminal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iopipe, err := container.openFifos(terminal)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-18 14:18:19 -04:00
|
|
|
var stdinOnce sync.Once
|
|
|
|
stdin := iopipe.Stdin
|
|
|
|
iopipe.Stdin = ioutils.NewWriteCloserWrapper(stdin, func() error {
|
|
|
|
var err error
|
|
|
|
stdinOnce.Do(func() { // on error from attach we don't know if stdin was already closed
|
|
|
|
err = stdin.Close()
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
2016-05-02 20:22:11 -04:00
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
if err := attachStdio(*iopipe); err != nil {
|
2016-10-18 14:18:19 -04:00
|
|
|
container.closeFifos(iopipe)
|
2016-05-02 20:22:11 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clnt.appendContainer(container)
|
|
|
|
|
|
|
|
err = clnt.backend.StateChanged(containerID, StateInfo{
|
|
|
|
CommonStateInfo: CommonStateInfo{
|
|
|
|
State: StateRestore,
|
|
|
|
Pid: container.systemPid,
|
|
|
|
}})
|
|
|
|
|
|
|
|
if err != nil {
|
2016-10-18 14:18:19 -04:00
|
|
|
container.closeFifos(iopipe)
|
2016-05-02 20:22:11 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-11 11:55:39 -04:00
|
|
|
if lastEvent != nil {
|
2016-05-02 20:22:11 -04:00
|
|
|
// This should only be a pause or resume event
|
2016-07-11 11:55:39 -04:00
|
|
|
if lastEvent.Type == StatePause || lastEvent.Type == StateResume {
|
2016-05-02 20:22:11 -04:00
|
|
|
return clnt.backend.StateChanged(containerID, StateInfo{
|
|
|
|
CommonStateInfo: CommonStateInfo{
|
2016-07-11 11:55:39 -04:00
|
|
|
State: lastEvent.Type,
|
2016-05-02 20:22:11 -04:00
|
|
|
Pid: container.systemPid,
|
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Warnf("libcontainerd: unexpected backlog event: %#v", lastEvent)
|
2016-05-02 20:22:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-25 22:32:51 -04:00
|
|
|
func (clnt *client) getContainerLastEventSinceTime(id string, tsp *timestamp.Timestamp) (*containerd.Event, error) {
|
2016-07-11 11:55:39 -04:00
|
|
|
er := &containerd.EventsRequest{
|
2016-07-25 22:32:51 -04:00
|
|
|
Timestamp: tsp,
|
2016-07-11 11:55:39 -04:00
|
|
|
StoredOnly: true,
|
2016-07-25 22:32:51 -04:00
|
|
|
Id: id,
|
2016-07-11 11:55:39 -04:00
|
|
|
}
|
|
|
|
events, err := clnt.remote.apiClient.Events(context.Background(), er)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("libcontainerd: failed to get container events stream for %s: %q", er.Id, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ev *containerd.Event
|
|
|
|
for {
|
|
|
|
e, err := events.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "EOF" {
|
|
|
|
break
|
2016-06-02 14:10:55 -04:00
|
|
|
}
|
2016-07-25 22:32:51 -04:00
|
|
|
logrus.Errorf("libcontainerd: failed to get container event for %s: %q", id, err)
|
2016-07-11 11:55:39 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("libcontainerd: received past event %#v", e)
|
|
|
|
|
|
|
|
switch e.Type {
|
|
|
|
case StateExit, StatePause, StateResume:
|
|
|
|
ev = e
|
2016-06-02 14:10:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 11:55:39 -04:00
|
|
|
return ev, nil
|
|
|
|
}
|
|
|
|
|
2016-07-25 22:32:51 -04:00
|
|
|
func (clnt *client) getContainerLastEvent(id string) (*containerd.Event, error) {
|
|
|
|
ev, err := clnt.getContainerLastEventSinceTime(id, clnt.remote.restoreFromTimestamp)
|
|
|
|
if err == nil && ev == nil {
|
|
|
|
// If ev is nil and the container is running in containerd,
|
|
|
|
// we already consumed all the event of the
|
|
|
|
// container, included the "exit" one.
|
|
|
|
// Thus, we request all events containerd has in memory for
|
|
|
|
// this container in order to get the last one (which should
|
|
|
|
// be an exit event)
|
|
|
|
logrus.Warnf("libcontainerd: client is out of sync, restore was called on a fully synced container (%s).", id)
|
|
|
|
// Request all events since beginning of time
|
|
|
|
t := time.Unix(0, 0)
|
|
|
|
tsp, err := ptypes.TimestampProto(t)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("libcontainerd: getLastEventSinceTime() failed to convert timestamp: %q", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return clnt.getContainerLastEventSinceTime(id, tsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ev, err
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
func (clnt *client) Restore(containerID string, attachStdio StdioCallback, options ...CreateOption) error {
|
2016-07-11 11:55:39 -04:00
|
|
|
// Synchronize with live events
|
|
|
|
clnt.remote.Lock()
|
|
|
|
defer clnt.remote.Unlock()
|
|
|
|
// Check that containerd still knows this container.
|
|
|
|
//
|
|
|
|
// In the unlikely event that Restore for this container process
|
|
|
|
// the its past event before the main loop, the event will be
|
|
|
|
// processed twice. However, this is not an issue as all those
|
|
|
|
// events will do is change the state of the container to be
|
|
|
|
// exactly the same.
|
2016-05-02 20:22:11 -04:00
|
|
|
cont, err := clnt.getContainerdContainer(containerID)
|
2016-07-11 11:55:39 -04:00
|
|
|
// Get its last event
|
|
|
|
ev, eerr := clnt.getContainerLastEvent(containerID)
|
|
|
|
if err != nil || cont.Status == "Stopped" {
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "container not found") {
|
|
|
|
// Legitimate error
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ev == nil {
|
2016-07-27 12:21:13 -04:00
|
|
|
if _, err := clnt.getContainer(containerID); err == nil {
|
|
|
|
// If ev is nil and the container is running in containerd,
|
|
|
|
// we already consumed all the event of the
|
|
|
|
// container, included the "exit" one.
|
|
|
|
// Thus we return to avoid overriding the Exit Code.
|
|
|
|
logrus.Warnf("libcontainerd: restore was called on a fully synced container (%s)", containerID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// the container is not running so we need to fix the state within docker
|
|
|
|
ev = &containerd.Event{
|
|
|
|
Type: StateExit,
|
|
|
|
Status: 1,
|
|
|
|
}
|
2016-07-11 11:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the exit status for this container
|
|
|
|
ec := uint32(0)
|
|
|
|
if eerr == nil && ev.Type == StateExit {
|
|
|
|
ec = ev.Status
|
|
|
|
}
|
|
|
|
clnt.setExited(containerID, ec)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// container is still alive
|
|
|
|
if clnt.liveRestore {
|
2016-10-17 17:39:52 -04:00
|
|
|
if err := clnt.restore(cont, ev, attachStdio, options...); err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Errorf("libcontainerd: error restoring %s: %v", containerID, err)
|
2016-07-11 11:55:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill the container if liveRestore == false
|
|
|
|
w := clnt.getOrCreateExitNotifier(containerID)
|
|
|
|
clnt.lock(cont.Id)
|
|
|
|
container := clnt.newContainer(cont.BundlePath)
|
|
|
|
container.systemPid = systemPid(cont)
|
|
|
|
clnt.appendContainer(container)
|
|
|
|
clnt.unlock(cont.Id)
|
|
|
|
|
|
|
|
container.discardFifos()
|
|
|
|
|
|
|
|
if err := clnt.Signal(containerID, int(syscall.SIGTERM)); err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Errorf("libcontainerd: error sending sigterm to %v: %v", containerID, err)
|
2016-07-11 11:55:39 -04:00
|
|
|
}
|
2016-07-22 18:21:10 -04:00
|
|
|
// Let the main loop handle the exit event
|
|
|
|
clnt.remote.Unlock()
|
2016-07-11 11:55:39 -04:00
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
if err := clnt.Signal(containerID, int(syscall.SIGKILL)); err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Errorf("libcontainerd: error sending sigkill to %v: %v", containerID, err)
|
2016-06-02 14:10:55 -04:00
|
|
|
}
|
|
|
|
select {
|
2016-07-11 11:55:39 -04:00
|
|
|
case <-time.After(2 * time.Second):
|
2016-06-02 14:10:55 -04:00
|
|
|
case <-w.wait():
|
2016-07-22 18:21:10 -04:00
|
|
|
// relock because of the defer
|
|
|
|
clnt.remote.Lock()
|
2016-06-02 14:10:55 -04:00
|
|
|
return nil
|
2016-05-02 20:22:11 -04:00
|
|
|
}
|
2016-07-11 11:55:39 -04:00
|
|
|
case <-w.wait():
|
2016-07-22 18:21:10 -04:00
|
|
|
// relock because of the defer
|
|
|
|
clnt.remote.Lock()
|
2016-07-11 11:55:39 -04:00
|
|
|
return nil
|
2016-05-02 20:22:11 -04:00
|
|
|
}
|
2016-07-26 22:51:03 -04:00
|
|
|
// relock because of the defer
|
|
|
|
clnt.remote.Lock()
|
2016-06-02 14:10:55 -04:00
|
|
|
|
|
|
|
clnt.deleteContainer(containerID)
|
|
|
|
|
2016-07-11 11:55:39 -04:00
|
|
|
return clnt.setExited(containerID, uint32(255))
|
2016-05-02 20:22:11 -04:00
|
|
|
}
|
|
|
|
|
2016-05-12 10:52:00 -04:00
|
|
|
func (clnt *client) CreateCheckpoint(containerID string, checkpointID string, checkpointDir string, exit bool) error {
|
|
|
|
clnt.lock(containerID)
|
|
|
|
defer clnt.unlock(containerID)
|
|
|
|
if _, err := clnt.getContainer(containerID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := clnt.remote.apiClient.CreateCheckpoint(context.Background(), &containerd.CreateCheckpointRequest{
|
|
|
|
Id: containerID,
|
|
|
|
Checkpoint: &containerd.Checkpoint{
|
|
|
|
Name: checkpointID,
|
|
|
|
Exit: exit,
|
|
|
|
Tcp: true,
|
|
|
|
UnixSockets: true,
|
|
|
|
Shell: false,
|
|
|
|
EmptyNS: []string{"network"},
|
|
|
|
},
|
|
|
|
CheckpointDir: checkpointDir,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) DeleteCheckpoint(containerID string, checkpointID string, checkpointDir string) error {
|
|
|
|
clnt.lock(containerID)
|
|
|
|
defer clnt.unlock(containerID)
|
|
|
|
if _, err := clnt.getContainer(containerID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := clnt.remote.apiClient.DeleteCheckpoint(context.Background(), &containerd.DeleteCheckpointRequest{
|
|
|
|
Id: containerID,
|
|
|
|
Name: checkpointID,
|
|
|
|
CheckpointDir: checkpointDir,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) ListCheckpoints(containerID string, checkpointDir string) (*Checkpoints, error) {
|
|
|
|
clnt.lock(containerID)
|
|
|
|
defer clnt.unlock(containerID)
|
|
|
|
if _, err := clnt.getContainer(containerID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := clnt.remote.apiClient.ListCheckpoint(context.Background(), &containerd.ListCheckpointRequest{
|
|
|
|
Id: containerID,
|
|
|
|
CheckpointDir: checkpointDir,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return (*Checkpoints)(resp), nil
|
|
|
|
}
|