2015-11-12 14:55:17 -05:00
|
|
|
package container
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
import (
|
2013-03-12 08:36:37 -04:00
|
|
|
"fmt"
|
2013-04-09 12:09:54 -04:00
|
|
|
"sync"
|
2013-01-28 17:30:05 -05:00
|
|
|
"time"
|
2014-05-12 19:40:19 -04:00
|
|
|
|
2014-10-08 13:03:57 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-09-17 14:54:14 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-12-16 12:26:49 -05:00
|
|
|
"github.com/docker/go-units"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// State holds the current container state, and has methods to get and
|
|
|
|
// set the state. Container has an embed, which allows all of the
|
|
|
|
// functions defined against State to run against Container.
|
2013-01-18 19:13:39 -05:00
|
|
|
type State struct {
|
2014-08-28 07:39:27 -04:00
|
|
|
sync.Mutex
|
2015-07-30 17:01:53 -04:00
|
|
|
// FIXME: Why do we have both paused and running if a
|
|
|
|
// container cannot be paused and running at the same time?
|
2015-03-12 15:26:17 -04:00
|
|
|
Running bool
|
|
|
|
Paused bool
|
|
|
|
Restarting bool
|
|
|
|
OOMKilled bool
|
2015-11-12 14:55:17 -05:00
|
|
|
RemovalInProgress bool // Not need for this to be persistent on disk.
|
2015-03-12 15:26:17 -04:00
|
|
|
Dead bool
|
|
|
|
Pid int
|
|
|
|
ExitCode int
|
|
|
|
Error string // contains last known error when starting the container
|
|
|
|
StartedAt time.Time
|
|
|
|
FinishedAt time.Time
|
|
|
|
waitChan chan struct{}
|
2014-06-06 07:28:12 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// NewState creates a default state object with a fresh channel for state changes.
|
2014-06-06 07:28:12 -04:00
|
|
|
func NewState() *State {
|
|
|
|
return &State{
|
|
|
|
waitChan: make(chan struct{}),
|
|
|
|
}
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2013-01-27 18:42:42 -05:00
|
|
|
// String returns a human-readable description of the state
|
|
|
|
func (s *State) String() string {
|
|
|
|
if s.Running {
|
2014-05-21 17:06:18 -04:00
|
|
|
if s.Paused {
|
|
|
|
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
|
|
}
|
2014-08-11 14:35:18 -04:00
|
|
|
if s.Restarting {
|
2014-10-30 19:06:54 -04:00
|
|
|
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2014-08-11 14:35:18 -04:00
|
|
|
}
|
2014-08-11 14:07:37 -04:00
|
|
|
|
2014-05-12 19:40:19 -04:00
|
|
|
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
2013-01-27 18:42:42 -05:00
|
|
|
}
|
2014-08-11 14:07:37 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if s.RemovalInProgress {
|
2015-03-12 15:26:17 -04:00
|
|
|
return "Removal In Progress"
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Dead {
|
|
|
|
return "Dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:51:58 -04:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "Created"
|
|
|
|
}
|
|
|
|
|
2014-03-18 23:05:54 -04:00
|
|
|
if s.FinishedAt.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
2014-08-11 14:07:37 -04:00
|
|
|
|
2014-10-30 19:06:54 -04:00
|
|
|
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2013-01-27 18:42:42 -05:00
|
|
|
}
|
|
|
|
|
2014-09-26 19:25:50 -04:00
|
|
|
// StateString returns a single string to describe state
|
|
|
|
func (s *State) StateString() string {
|
|
|
|
if s.Running {
|
|
|
|
if s.Paused {
|
|
|
|
return "paused"
|
|
|
|
}
|
|
|
|
if s.Restarting {
|
|
|
|
return "restarting"
|
|
|
|
}
|
|
|
|
return "running"
|
|
|
|
}
|
2015-03-12 15:26:17 -04:00
|
|
|
|
|
|
|
if s.Dead {
|
|
|
|
return "dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:51:58 -04:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "created"
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:25:50 -04:00
|
|
|
return "exited"
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// IsValidStateString checks if the provided string is a valid container state or not.
|
|
|
|
func IsValidStateString(s string) bool {
|
2015-06-30 17:40:27 -04:00
|
|
|
if s != "paused" &&
|
|
|
|
s != "restarting" &&
|
|
|
|
s != "running" &&
|
|
|
|
s != "dead" &&
|
|
|
|
s != "created" &&
|
|
|
|
s != "exited" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-06 07:28:12 -04:00
|
|
|
func wait(waitChan <-chan struct{}, timeout time.Duration) error {
|
|
|
|
if timeout < 0 {
|
|
|
|
<-waitChan
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeTimedOut.WithArgs(timeout)
|
2014-06-06 07:28:12 -04:00
|
|
|
case <-waitChan:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// waitRunning waits until state is running. If state is already
|
|
|
|
// running it returns immediately. If you want wait forever you must
|
|
|
|
// supply negative timeout. Returns pid, that was passed to
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetRunning.
|
2015-07-30 17:01:53 -04:00
|
|
|
func (s *State) waitRunning(timeout time.Duration) (int, error) {
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Lock()
|
|
|
|
if s.Running {
|
2014-06-06 07:28:12 -04:00
|
|
|
pid := s.Pid
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
return pid, nil
|
|
|
|
}
|
|
|
|
waitChan := s.waitChan
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
if err := wait(waitChan, timeout); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2015-11-02 18:25:26 -05:00
|
|
|
return s.GetPID(), nil
|
2014-06-06 07:28:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitStop waits until state is stopped. If state already stopped it returns
|
2014-09-05 04:22:32 -04:00
|
|
|
// immediately. If you want wait forever you must supply negative timeout.
|
2015-11-12 14:55:17 -05:00
|
|
|
// Returns exit code, that was passed to SetStoppedLocking
|
2014-06-06 07:28:12 -04:00
|
|
|
func (s *State) WaitStop(timeout time.Duration) (int, error) {
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Lock()
|
2014-06-06 07:28:12 -04:00
|
|
|
if !s.Running {
|
|
|
|
exitCode := s.ExitCode
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
return exitCode, nil
|
|
|
|
}
|
|
|
|
waitChan := s.waitChan
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
if err := wait(waitChan, timeout); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2015-07-30 17:01:53 -04:00
|
|
|
return s.getExitCode(), nil
|
2014-06-06 07:28:12 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// IsRunning returns whether the running flag is set. Used by Container to check whether a container is running.
|
2013-11-21 15:21:03 -05:00
|
|
|
func (s *State) IsRunning() bool {
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Lock()
|
2014-06-06 07:28:12 -04:00
|
|
|
res := s.Running
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
return res
|
|
|
|
}
|
2013-11-21 15:21:03 -05:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// GetPID holds the process id of a container.
|
2015-11-02 18:25:26 -05:00
|
|
|
func (s *State) GetPID() int {
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Lock()
|
2014-06-06 07:28:12 -04:00
|
|
|
res := s.Pid
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
return res
|
2013-11-21 15:21:03 -05:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
func (s *State) getExitCode() int {
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Lock()
|
2014-06-06 07:28:12 -04:00
|
|
|
res := s.ExitCode
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
return res
|
2013-11-21 15:21:03 -05:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetRunning sets the state of the container to "running".
|
|
|
|
func (s *State) SetRunning(pid int) {
|
2014-09-30 04:30:58 -04:00
|
|
|
s.Error = ""
|
2014-07-29 03:14:10 -04:00
|
|
|
s.Running = true
|
|
|
|
s.Paused = false
|
2014-08-13 17:56:35 -04:00
|
|
|
s.Restarting = false
|
2014-07-29 03:14:10 -04:00
|
|
|
s.ExitCode = 0
|
|
|
|
s.Pid = pid
|
|
|
|
s.StartedAt = time.Now().UTC()
|
|
|
|
close(s.waitChan) // fire waiters for start
|
|
|
|
s.waitChan = make(chan struct{})
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2016-01-14 13:38:09 -05:00
|
|
|
// SetStoppedLocking locks the container state is sets it to "stopped".
|
2015-11-12 14:55:17 -05:00
|
|
|
func (s *State) SetStoppedLocking(exitStatus *execdriver.ExitStatus) {
|
2013-11-21 15:21:03 -05:00
|
|
|
s.Lock()
|
2015-11-12 14:55:17 -05:00
|
|
|
s.SetStopped(exitStatus)
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetStopped sets the container state to "stopped" without locking.
|
|
|
|
func (s *State) SetStopped(exitStatus *execdriver.ExitStatus) {
|
2014-07-29 03:14:10 -04:00
|
|
|
s.Running = false
|
2014-08-13 17:56:35 -04:00
|
|
|
s.Restarting = false
|
2014-07-29 03:14:10 -04:00
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
2015-10-31 22:31:24 -04:00
|
|
|
s.setFromExitStatus(exitStatus)
|
2014-07-29 03:14:10 -04:00
|
|
|
close(s.waitChan) // fire waiters for stop
|
|
|
|
s.waitChan = make(chan struct{})
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
2014-05-21 17:06:18 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetRestartingLocking is when docker handles the auto restart of containers when they are
|
2014-08-11 14:07:37 -04:00
|
|
|
// in the middle of a stop and being restarted again
|
2015-11-12 14:55:17 -05:00
|
|
|
func (s *State) SetRestartingLocking(exitStatus *execdriver.ExitStatus) {
|
2014-08-11 14:07:37 -04:00
|
|
|
s.Lock()
|
2015-11-12 14:55:17 -05:00
|
|
|
s.SetRestarting(exitStatus)
|
2015-08-19 23:08:31 -04:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetRestarting sets the container state to "restarting".
|
|
|
|
// It also sets the container PID to 0.
|
|
|
|
func (s *State) SetRestarting(exitStatus *execdriver.ExitStatus) {
|
2014-08-13 17:56:35 -04:00
|
|
|
// we should consider the container running when it is restarting because of
|
|
|
|
// all the checks in docker around rm/stop/etc
|
|
|
|
s.Running = true
|
|
|
|
s.Restarting = true
|
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
2015-10-31 22:31:24 -04:00
|
|
|
s.setFromExitStatus(exitStatus)
|
2014-08-13 17:56:35 -04:00
|
|
|
close(s.waitChan) // fire waiters for stop
|
|
|
|
s.waitChan = make(chan struct{})
|
2014-08-11 14:07:37 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetError sets the container's error state. This is useful when we want to
|
2014-09-30 04:30:58 -04:00
|
|
|
// know the error that occurred when container transits to another state
|
|
|
|
// when inspecting it
|
2015-11-12 14:55:17 -05:00
|
|
|
func (s *State) SetError(err error) {
|
2014-09-30 04:30:58 -04:00
|
|
|
s.Error = err.Error()
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// IsPaused returns whether the container is paused or not.
|
|
|
|
func (s *State) IsPaused() bool {
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Lock()
|
2014-06-06 07:28:12 -04:00
|
|
|
res := s.Paused
|
2014-08-28 07:39:27 -04:00
|
|
|
s.Unlock()
|
2014-06-06 07:28:12 -04:00
|
|
|
return res
|
2014-05-21 17:06:18 -04:00
|
|
|
}
|
2015-03-12 15:26:17 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetRemovalInProgress sets the container state as being removed.
|
|
|
|
func (s *State) SetRemovalInProgress() error {
|
2015-03-12 15:26:17 -04:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2015-11-12 14:55:17 -05:00
|
|
|
if s.RemovalInProgress {
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeAlreadyRemoving
|
2015-03-12 15:26:17 -04:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
s.RemovalInProgress = true
|
2015-03-12 15:26:17 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// ResetRemovalInProgress make the RemovalInProgress state to false.
|
|
|
|
func (s *State) ResetRemovalInProgress() {
|
2015-03-12 15:26:17 -04:00
|
|
|
s.Lock()
|
2015-11-12 14:55:17 -05:00
|
|
|
s.RemovalInProgress = false
|
2015-03-12 15:26:17 -04:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// SetDead sets the container state to "dead"
|
|
|
|
func (s *State) SetDead() {
|
2015-03-12 15:26:17 -04:00
|
|
|
s.Lock()
|
|
|
|
s.Dead = true
|
|
|
|
s.Unlock()
|
|
|
|
}
|