2016-03-18 14:50:19 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2016-10-05 16:29:56 -04:00
|
|
|
"time"
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-02-09 21:57:35 -05:00
|
|
|
"github.com/docker/docker/container"
|
2016-03-18 14:50:19 -04:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
2016-10-05 16:29:56 -04:00
|
|
|
"github.com/docker/docker/restartmanager"
|
2016-03-18 14:50:19 -04:00
|
|
|
)
|
|
|
|
|
2017-02-09 21:57:35 -05:00
|
|
|
func (daemon *Daemon) setStateCounter(c *container.Container) {
|
|
|
|
switch c.StateString() {
|
|
|
|
case "paused":
|
|
|
|
stateCtr.set(c.ID, "paused")
|
|
|
|
case "running":
|
|
|
|
stateCtr.set(c.ID, "running")
|
|
|
|
default:
|
|
|
|
stateCtr.set(c.ID, "stopped")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
// StateChanged updates daemon state changes from containerd
|
|
|
|
func (daemon *Daemon) StateChanged(id string, e libcontainerd.StateInfo) error {
|
|
|
|
c := daemon.containers.Get(id)
|
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("no such container: %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.State {
|
|
|
|
case libcontainerd.StateOOM:
|
|
|
|
// StateOOM is Linux specific and should never be hit on Windows
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return errors.New("Received StateOOM from libcontainerd on Windows. This should never happen.")
|
|
|
|
}
|
2016-04-18 05:48:13 -04:00
|
|
|
daemon.updateHealthMonitor(c)
|
2016-03-18 14:50:19 -04:00
|
|
|
daemon.LogContainerEvent(c, "oom")
|
|
|
|
case libcontainerd.StateExit:
|
2016-10-05 16:29:56 -04:00
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
c.Lock()
|
2016-11-14 15:15:09 -05:00
|
|
|
c.StreamConfig.Wait()
|
2016-03-18 14:50:19 -04:00
|
|
|
c.Reset(false)
|
2016-10-05 16:29:56 -04:00
|
|
|
|
2017-03-31 12:33:36 -04:00
|
|
|
restart, wait, err := c.RestartManager().ShouldRestart(e.ExitCode, c.HasBeenManuallyStopped, time.Since(c.StartedAt))
|
2016-10-05 16:29:56 -04:00
|
|
|
if err == nil && restart {
|
|
|
|
c.RestartCount++
|
|
|
|
c.SetRestarting(platformConstructExitStatus(e))
|
|
|
|
} else {
|
|
|
|
c.SetStopped(platformConstructExitStatus(e))
|
2017-02-14 13:35:20 -05:00
|
|
|
defer daemon.autoRemove(c)
|
2016-10-05 16:29:56 -04:00
|
|
|
}
|
|
|
|
|
2017-03-31 12:33:36 -04:00
|
|
|
// cancel healthcheck here, they will be automatically
|
|
|
|
// restarted if/when the container is started again
|
|
|
|
daemon.stopHealthchecks(c)
|
2016-03-18 14:50:19 -04:00
|
|
|
attributes := map[string]string{
|
|
|
|
"exitCode": strconv.Itoa(int(e.ExitCode)),
|
|
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(c, "die", attributes)
|
|
|
|
daemon.Cleanup(c)
|
2016-10-05 16:29:56 -04:00
|
|
|
|
|
|
|
if err == nil && restart {
|
|
|
|
go func() {
|
|
|
|
err := <-wait
|
|
|
|
if err == nil {
|
2016-09-19 12:01:16 -04:00
|
|
|
if err = daemon.containerStart(c, "", "", false); err != nil {
|
2016-10-29 03:03:26 -04:00
|
|
|
logrus.Debugf("failed to restart container: %+v", err)
|
2016-10-05 16:29:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.SetStopped(platformConstructExitStatus(e))
|
2017-02-14 13:35:20 -05:00
|
|
|
defer daemon.autoRemove(c)
|
2016-10-05 16:29:56 -04:00
|
|
|
if err != restartmanager.ErrRestartCanceled {
|
|
|
|
logrus.Errorf("restartmanger wait error: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-02-09 21:57:35 -05:00
|
|
|
daemon.setStateCounter(c)
|
|
|
|
|
2016-10-05 16:29:56 -04:00
|
|
|
defer c.Unlock()
|
2016-04-01 20:02:38 -04:00
|
|
|
if err := c.ToDisk(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return daemon.postRunProcessing(c, e)
|
2016-03-18 14:50:19 -04:00
|
|
|
case libcontainerd.StateExitProcess:
|
|
|
|
if execConfig := c.ExecCommands.Get(e.ProcessID); execConfig != nil {
|
|
|
|
ec := int(e.ExitCode)
|
2016-10-12 19:56:52 -04:00
|
|
|
execConfig.Lock()
|
|
|
|
defer execConfig.Unlock()
|
2016-03-18 14:50:19 -04:00
|
|
|
execConfig.ExitCode = &ec
|
|
|
|
execConfig.Running = false
|
2016-11-14 15:15:09 -05:00
|
|
|
execConfig.StreamConfig.Wait()
|
2016-03-18 14:50:19 -04:00
|
|
|
if err := execConfig.CloseStreams(); err != nil {
|
2017-01-21 06:35:54 -05:00
|
|
|
logrus.Errorf("failed to cleanup exec %s streams: %s", c.ID, err)
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove the exec command from the container's store only and not the
|
|
|
|
// daemon's store so that the exec command can be inspected.
|
|
|
|
c.ExecCommands.Delete(execConfig.ID)
|
|
|
|
} else {
|
|
|
|
logrus.Warnf("Ignoring StateExitProcess for %v but no exec command found", e)
|
|
|
|
}
|
|
|
|
case libcontainerd.StateStart, libcontainerd.StateRestore:
|
2016-04-18 05:48:13 -04:00
|
|
|
// Container is already locked in this case
|
2016-03-18 14:50:19 -04:00
|
|
|
c.SetRunning(int(e.Pid), e.State == libcontainerd.StateStart)
|
|
|
|
c.HasBeenManuallyStopped = false
|
2016-09-16 13:41:47 -04:00
|
|
|
c.HasBeenStartedBefore = true
|
2017-02-09 21:57:35 -05:00
|
|
|
daemon.setStateCounter(c)
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
if err := c.ToDisk(); err != nil {
|
|
|
|
c.Reset(false)
|
|
|
|
return err
|
|
|
|
}
|
2016-04-18 05:48:13 -04:00
|
|
|
daemon.initHealthMonitor(c)
|
2017-02-09 21:57:35 -05:00
|
|
|
|
2016-04-06 23:38:12 -04:00
|
|
|
daemon.LogContainerEvent(c, "start")
|
2016-03-18 14:50:19 -04:00
|
|
|
case libcontainerd.StatePause:
|
2016-04-18 05:48:13 -04:00
|
|
|
// Container is already locked in this case
|
2016-03-18 14:50:19 -04:00
|
|
|
c.Paused = true
|
2017-02-09 21:57:35 -05:00
|
|
|
daemon.setStateCounter(c)
|
2016-08-19 05:12:01 -04:00
|
|
|
if err := c.ToDisk(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-18 05:48:13 -04:00
|
|
|
daemon.updateHealthMonitor(c)
|
2016-03-18 14:50:19 -04:00
|
|
|
daemon.LogContainerEvent(c, "pause")
|
|
|
|
case libcontainerd.StateResume:
|
2016-04-18 05:48:13 -04:00
|
|
|
// Container is already locked in this case
|
2016-03-18 14:50:19 -04:00
|
|
|
c.Paused = false
|
2017-02-09 21:57:35 -05:00
|
|
|
daemon.setStateCounter(c)
|
2016-08-19 05:12:01 -04:00
|
|
|
if err := c.ToDisk(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-18 05:48:13 -04:00
|
|
|
daemon.updateHealthMonitor(c)
|
2016-03-18 14:50:19 -04:00
|
|
|
daemon.LogContainerEvent(c, "unpause")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-14 13:35:20 -05:00
|
|
|
|
|
|
|
func (daemon *Daemon) autoRemove(c *container.Container) {
|
|
|
|
c.Lock()
|
|
|
|
ar := c.HostConfig.AutoRemove
|
|
|
|
c.Unlock()
|
|
|
|
if !ar {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if err = daemon.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c := daemon.containers.Get(c.ID); c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("container", c.ID).Error("error removing container")
|
|
|
|
}
|
|
|
|
}
|