2014-04-17 21:43:01 +00:00
|
|
|
package daemon
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
import (
|
2013-03-12 12:36:37 +00:00
|
|
|
"fmt"
|
2013-04-09 16:09:54 +00:00
|
|
|
"sync"
|
2013-01-28 22:30:05 +00:00
|
|
|
"time"
|
2014-05-12 23:40:19 +00:00
|
|
|
|
2014-10-08 17:03:57 +00:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/pkg/units"
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type State struct {
|
2014-08-28 11:39:27 +00:00
|
|
|
sync.Mutex
|
2015-03-12 19:26:17 +00:00
|
|
|
Running bool
|
|
|
|
Paused bool
|
|
|
|
Restarting bool
|
|
|
|
OOMKilled bool
|
|
|
|
removalInProgress bool // Not need for this to be persistent on disk.
|
|
|
|
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 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewState() *State {
|
|
|
|
return &State{
|
|
|
|
waitChan: make(chan struct{}),
|
|
|
|
}
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 23:42:42 +00:00
|
|
|
// String returns a human-readable description of the state
|
|
|
|
func (s *State) String() string {
|
|
|
|
if s.Running {
|
2014-05-21 21:06:18 +00:00
|
|
|
if s.Paused {
|
|
|
|
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
|
|
}
|
2014-08-11 18:35:18 +00:00
|
|
|
if s.Restarting {
|
2014-10-30 23:06:54 +00:00
|
|
|
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2014-08-11 18:35:18 +00:00
|
|
|
}
|
2014-08-11 18:07:37 +00:00
|
|
|
|
2014-05-12 23:40:19 +00:00
|
|
|
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
2013-01-27 23:42:42 +00:00
|
|
|
}
|
2014-08-11 18:07:37 +00:00
|
|
|
|
2015-03-12 19:26:17 +00:00
|
|
|
if s.removalInProgress {
|
|
|
|
return "Removal In Progress"
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Dead {
|
|
|
|
return "Dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:51:58 +00:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "Created"
|
|
|
|
}
|
|
|
|
|
2014-03-19 03:05:54 +00:00
|
|
|
if s.FinishedAt.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
2014-08-11 18:07:37 +00:00
|
|
|
|
2014-10-30 23:06:54 +00:00
|
|
|
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2013-01-27 23:42:42 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 23:25:50 +00: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 19:26:17 +00:00
|
|
|
|
|
|
|
if s.Dead {
|
|
|
|
return "dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:51:58 +00:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "created"
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:25:50 +00:00
|
|
|
return "exited"
|
|
|
|
}
|
|
|
|
|
2014-06-06 11:28:12 +00:00
|
|
|
func wait(waitChan <-chan struct{}, timeout time.Duration) error {
|
|
|
|
if timeout < 0 {
|
|
|
|
<-waitChan
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
|
|
|
return fmt.Errorf("Timed out: %v", timeout)
|
|
|
|
case <-waitChan:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitRunning waits until state is running. If state already running it returns
|
2014-09-05 08:22:32 +00:00
|
|
|
// immediately. If you want wait forever you must supply negative timeout.
|
2014-06-06 11:28:12 +00:00
|
|
|
// Returns pid, that was passed to SetRunning
|
|
|
|
func (s *State) WaitRunning(timeout time.Duration) (int, error) {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
|
|
|
if s.Running {
|
2014-06-06 11:28:12 +00:00
|
|
|
pid := s.Pid
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return pid, nil
|
|
|
|
}
|
|
|
|
waitChan := s.waitChan
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
if err := wait(waitChan, timeout); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return s.GetPid(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitStop waits until state is stopped. If state already stopped it returns
|
2014-09-05 08:22:32 +00:00
|
|
|
// immediately. If you want wait forever you must supply negative timeout.
|
2014-06-06 11:30:04 +00:00
|
|
|
// Returns exit code, that was passed to SetStopped
|
2014-06-06 11:28:12 +00:00
|
|
|
func (s *State) WaitStop(timeout time.Duration) (int, error) {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
if !s.Running {
|
|
|
|
exitCode := s.ExitCode
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return exitCode, nil
|
|
|
|
}
|
|
|
|
waitChan := s.waitChan
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
if err := wait(waitChan, timeout); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return s.GetExitCode(), nil
|
|
|
|
}
|
|
|
|
|
2013-11-21 20:21:03 +00:00
|
|
|
func (s *State) IsRunning() bool {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.Running
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
|
|
|
}
|
2013-11-21 20:21:03 +00:00
|
|
|
|
2014-06-06 11:28:12 +00:00
|
|
|
func (s *State) GetPid() int {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.Pid
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
2013-11-21 20:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) GetExitCode() int {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.ExitCode
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
2013-11-21 20:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetRunning(pid int) {
|
|
|
|
s.Lock()
|
2014-08-28 11:39:27 +00:00
|
|
|
s.setRunning(pid)
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) setRunning(pid int) {
|
2014-09-30 08:30:58 +00:00
|
|
|
s.Error = ""
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Running = true
|
|
|
|
s.Paused = false
|
2014-08-13 21:56:35 +00:00
|
|
|
s.Restarting = false
|
2014-07-29 07:14:10 +00: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-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 17:03:57 +00:00
|
|
|
func (s *State) SetStopped(exitStatus *execdriver.ExitStatus) {
|
2013-11-21 20:21:03 +00:00
|
|
|
s.Lock()
|
2014-10-08 17:03:57 +00:00
|
|
|
s.setStopped(exitStatus)
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-10-08 17:03:57 +00:00
|
|
|
func (s *State) setStopped(exitStatus *execdriver.ExitStatus) {
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Running = false
|
2014-08-13 21:56:35 +00:00
|
|
|
s.Restarting = false
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
2014-10-08 17:03:57 +00:00
|
|
|
s.ExitCode = exitStatus.ExitCode
|
2014-10-30 23:06:54 +00:00
|
|
|
s.OOMKilled = exitStatus.OOMKilled
|
2014-07-29 07:14:10 +00:00
|
|
|
close(s.waitChan) // fire waiters for stop
|
|
|
|
s.waitChan = make(chan struct{})
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
2014-05-21 21:06:18 +00:00
|
|
|
|
2015-04-16 14:56:15 +00:00
|
|
|
// SetRestarting is when docker handles the auto restart of containers when they are
|
2014-08-11 18:07:37 +00:00
|
|
|
// in the middle of a stop and being restarted again
|
2014-10-08 17:03:57 +00:00
|
|
|
func (s *State) SetRestarting(exitStatus *execdriver.ExitStatus) {
|
2014-08-11 18:07:37 +00:00
|
|
|
s.Lock()
|
2014-08-13 21:56:35 +00: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()
|
2014-10-08 17:03:57 +00:00
|
|
|
s.ExitCode = exitStatus.ExitCode
|
2014-10-30 23:06:54 +00:00
|
|
|
s.OOMKilled = exitStatus.OOMKilled
|
2014-08-13 21:56:35 +00:00
|
|
|
close(s.waitChan) // fire waiters for stop
|
|
|
|
s.waitChan = make(chan struct{})
|
2014-08-11 18:07:37 +00:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-09-30 08:30:58 +00:00
|
|
|
// setError sets the container's error state. This is useful when we want to
|
|
|
|
// know the error that occurred when container transits to another state
|
|
|
|
// when inspecting it
|
|
|
|
func (s *State) setError(err error) {
|
|
|
|
s.Error = err.Error()
|
|
|
|
}
|
|
|
|
|
2014-08-11 18:07:37 +00:00
|
|
|
func (s *State) IsRestarting() bool {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-08-11 18:07:37 +00:00
|
|
|
res := s.Restarting
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-08-11 18:07:37 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2014-05-21 21:06:18 +00:00
|
|
|
func (s *State) SetPaused() {
|
|
|
|
s.Lock()
|
|
|
|
s.Paused = true
|
2014-06-06 11:28:12 +00:00
|
|
|
s.Unlock()
|
2014-05-21 21:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetUnpaused() {
|
|
|
|
s.Lock()
|
|
|
|
s.Paused = false
|
2014-06-06 11:28:12 +00:00
|
|
|
s.Unlock()
|
2014-05-21 21:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) IsPaused() bool {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.Paused
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
2014-05-21 21:06:18 +00:00
|
|
|
}
|
2015-03-12 19:26:17 +00:00
|
|
|
|
|
|
|
func (s *State) SetRemovalInProgress() error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
if s.removalInProgress {
|
|
|
|
return fmt.Errorf("Status is already RemovalInProgress")
|
|
|
|
}
|
|
|
|
s.removalInProgress = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) ResetRemovalInProgress() {
|
|
|
|
s.Lock()
|
|
|
|
s.removalInProgress = false
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetDead() {
|
|
|
|
s.Lock()
|
|
|
|
s.Dead = true
|
|
|
|
s.Unlock()
|
|
|
|
}
|