mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
4921171587
This patch adds the untilRemoved option to the ContainerWait API which allows the client to wait until the container is not only exited but also removed. This patch also adds some more CLI integration tests for waiting for a created container and waiting with the new --until-removed flag. Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Handle detach sequence in CLI Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Update Container Wait Conditions Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Apply container wait changes to API 1.30 The set of changes to the containerWait API missed the cut for the Docker 17.05 release (API version 1.29). This patch bumps the version checks to use 1.30 instead. This patch also makes a minor update to a testfile which was added to the builder/dockerfile package. Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Remove wait changes from CLI Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Address minor nits on wait changes - Changed the name of the tty Proxy wrapper to `escapeProxy` - Removed the unnecessary Error() method on container.State - Fixes a typo in comment (repeated word) Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Use router.WithCancel in the containerWait handler This handler previously added this functionality manually but now uses the existing wrapper which does it for us. Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Add WaitCondition constants to api/types/container Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Address more ContainerWait review comments - Update ContainerWait backend interface to not return pointer values for container.StateStatus type. - Updated container state's Wait() method comments to clarify that a context MUST be used for cancelling the request, setting timeouts, and to avoid goroutine leaks. - Removed unnecessary buffering when making channels in the client's ContainerWait methods. - Renamed result and error channels in client's ContainerWait methods to clarify that only a single result or error value would be sent on the channel. Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Move container.WaitCondition type to separate file ... to avoid conflict with swagger-generated code for API response Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Address more ContainerWait review comments Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)
376 lines
9.3 KiB
Go
376 lines
9.3 KiB
Go
package container
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/go-units"
|
|
)
|
|
|
|
// 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.
|
|
type State struct {
|
|
sync.Mutex
|
|
// FIXME: Why do we have both paused and running if a
|
|
// container cannot be paused and running at the same time?
|
|
Running bool
|
|
Paused bool
|
|
Restarting bool
|
|
OOMKilled bool
|
|
RemovalInProgress bool // Not need for this to be persistent on disk.
|
|
Dead bool
|
|
Pid int
|
|
ExitCodeValue int `json:"ExitCode"`
|
|
ErrorMsg string `json:"Error"` // contains last known error when starting the container
|
|
StartedAt time.Time
|
|
FinishedAt time.Time
|
|
Health *Health
|
|
|
|
waitStop chan struct{}
|
|
waitRemove chan struct{}
|
|
}
|
|
|
|
// StateStatus is used to return container wait results.
|
|
// Implements exec.ExitCode interface.
|
|
// This type is needed as State include a sync.Mutex field which make
|
|
// copying it unsafe.
|
|
type StateStatus struct {
|
|
exitCode int
|
|
err error
|
|
}
|
|
|
|
// ExitCode returns current exitcode for the state.
|
|
func (s StateStatus) ExitCode() int {
|
|
return s.exitCode
|
|
}
|
|
|
|
// Err returns current error for the state. Returns nil if the container had
|
|
// exited on its own.
|
|
func (s StateStatus) Err() error {
|
|
return s.err
|
|
}
|
|
|
|
// NewState creates a default state object with a fresh channel for state changes.
|
|
func NewState() *State {
|
|
return &State{
|
|
waitStop: make(chan struct{}),
|
|
waitRemove: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
// String returns a human-readable description of the state
|
|
func (s *State) String() string {
|
|
if s.Running {
|
|
if s.Paused {
|
|
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
}
|
|
if s.Restarting {
|
|
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
|
}
|
|
|
|
if h := s.Health; h != nil {
|
|
return fmt.Sprintf("Up %s (%s)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)), h.String())
|
|
}
|
|
|
|
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
}
|
|
|
|
if s.RemovalInProgress {
|
|
return "Removal In Progress"
|
|
}
|
|
|
|
if s.Dead {
|
|
return "Dead"
|
|
}
|
|
|
|
if s.StartedAt.IsZero() {
|
|
return "Created"
|
|
}
|
|
|
|
if s.FinishedAt.IsZero() {
|
|
return ""
|
|
}
|
|
|
|
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
|
}
|
|
|
|
// HealthString returns a single string to describe health status.
|
|
func (s *State) HealthString() string {
|
|
if s.Health == nil {
|
|
return types.NoHealthcheck
|
|
}
|
|
|
|
return s.Health.String()
|
|
}
|
|
|
|
// IsValidHealthString checks if the provided string is a valid container health status or not.
|
|
func IsValidHealthString(s string) bool {
|
|
return s == types.Starting ||
|
|
s == types.Healthy ||
|
|
s == types.Unhealthy ||
|
|
s == types.NoHealthcheck
|
|
}
|
|
|
|
// 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"
|
|
}
|
|
|
|
if s.RemovalInProgress {
|
|
return "removing"
|
|
}
|
|
|
|
if s.Dead {
|
|
return "dead"
|
|
}
|
|
|
|
if s.StartedAt.IsZero() {
|
|
return "created"
|
|
}
|
|
|
|
return "exited"
|
|
}
|
|
|
|
// IsValidStateString checks if the provided string is a valid container state or not.
|
|
func IsValidStateString(s string) bool {
|
|
if s != "paused" &&
|
|
s != "restarting" &&
|
|
s != "removing" &&
|
|
s != "running" &&
|
|
s != "dead" &&
|
|
s != "created" &&
|
|
s != "exited" {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// WaitCondition is an enum type for different states to wait for.
|
|
type WaitCondition int
|
|
|
|
// Possible WaitCondition Values.
|
|
//
|
|
// WaitConditionNotRunning (default) is used to wait for any of the non-running
|
|
// states: "created", "exited", "dead", "removing", or "removed".
|
|
//
|
|
// WaitConditionNextExit is used to wait for the next time the state changes
|
|
// to a non-running state. If the state is currently "created" or "exited",
|
|
// this would cause Wait() to block until either the container runs and exits
|
|
// or is removed.
|
|
//
|
|
// WaitConditionRemoved is used to wait for the container to be removed.
|
|
const (
|
|
WaitConditionNotRunning WaitCondition = iota
|
|
WaitConditionNextExit
|
|
WaitConditionRemoved
|
|
)
|
|
|
|
// Wait waits until the continer is in a certain state indicated by the given
|
|
// condition. A context must be used for cancelling the request, controlling
|
|
// timeouts, and avoiding goroutine leaks. Wait must be called without holding
|
|
// the state lock. Returns a channel from which the caller will receive the
|
|
// result. If the container exited on its own, the result's Err() method will
|
|
// be nil and its ExitCode() method will return the conatiners exit code,
|
|
// otherwise, the results Err() method will return an error indicating why the
|
|
// wait operation failed.
|
|
func (s *State) Wait(ctx context.Context, condition WaitCondition) <-chan StateStatus {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
|
|
if condition == WaitConditionNotRunning && !s.Running {
|
|
// Buffer so we can put it in the channel now.
|
|
resultC := make(chan StateStatus, 1)
|
|
|
|
// Send the current status.
|
|
resultC <- StateStatus{
|
|
exitCode: s.ExitCode(),
|
|
err: s.Err(),
|
|
}
|
|
|
|
return resultC
|
|
}
|
|
|
|
// If we are waiting only for removal, the waitStop channel should
|
|
// remain nil and block forever.
|
|
var waitStop chan struct{}
|
|
if condition < WaitConditionRemoved {
|
|
waitStop = s.waitStop
|
|
}
|
|
|
|
// Always wait for removal, just in case the container gets removed
|
|
// while it is still in a "created" state, in which case it is never
|
|
// actually stopped.
|
|
waitRemove := s.waitRemove
|
|
|
|
resultC := make(chan StateStatus)
|
|
|
|
go func() {
|
|
select {
|
|
case <-ctx.Done():
|
|
// Context timeout or cancellation.
|
|
resultC <- StateStatus{
|
|
exitCode: -1,
|
|
err: ctx.Err(),
|
|
}
|
|
return
|
|
case <-waitStop:
|
|
case <-waitRemove:
|
|
}
|
|
|
|
s.Lock()
|
|
result := StateStatus{
|
|
exitCode: s.ExitCode(),
|
|
err: s.Err(),
|
|
}
|
|
s.Unlock()
|
|
|
|
resultC <- result
|
|
}()
|
|
|
|
return resultC
|
|
}
|
|
|
|
// IsRunning returns whether the running flag is set. Used by Container to check whether a container is running.
|
|
func (s *State) IsRunning() bool {
|
|
s.Lock()
|
|
res := s.Running
|
|
s.Unlock()
|
|
return res
|
|
}
|
|
|
|
// GetPID holds the process id of a container.
|
|
func (s *State) GetPID() int {
|
|
s.Lock()
|
|
res := s.Pid
|
|
s.Unlock()
|
|
return res
|
|
}
|
|
|
|
// ExitCode returns current exitcode for the state. Take lock before if state
|
|
// may be shared.
|
|
func (s *State) ExitCode() int {
|
|
return s.ExitCodeValue
|
|
}
|
|
|
|
// SetExitCode sets current exitcode for the state. Take lock before if state
|
|
// may be shared.
|
|
func (s *State) SetExitCode(ec int) {
|
|
s.ExitCodeValue = ec
|
|
}
|
|
|
|
// SetRunning sets the state of the container to "running".
|
|
func (s *State) SetRunning(pid int, initial bool) {
|
|
s.ErrorMsg = ""
|
|
s.Running = true
|
|
s.Restarting = false
|
|
s.ExitCodeValue = 0
|
|
s.Pid = pid
|
|
if initial {
|
|
s.StartedAt = time.Now().UTC()
|
|
}
|
|
}
|
|
|
|
// SetStopped sets the container state to "stopped" without locking.
|
|
func (s *State) SetStopped(exitStatus *ExitStatus) {
|
|
s.Running = false
|
|
s.Paused = false
|
|
s.Restarting = false
|
|
s.Pid = 0
|
|
s.FinishedAt = time.Now().UTC()
|
|
s.setFromExitStatus(exitStatus)
|
|
close(s.waitStop) // Fire waiters for stop
|
|
s.waitStop = make(chan struct{})
|
|
}
|
|
|
|
// SetRestarting sets the container state to "restarting" without locking.
|
|
// It also sets the container PID to 0.
|
|
func (s *State) SetRestarting(exitStatus *ExitStatus) {
|
|
// 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()
|
|
s.setFromExitStatus(exitStatus)
|
|
close(s.waitStop) // Fire waiters for stop
|
|
s.waitStop = make(chan struct{})
|
|
}
|
|
|
|
// 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.ErrorMsg = err.Error()
|
|
}
|
|
|
|
// IsPaused returns whether the container is paused or not.
|
|
func (s *State) IsPaused() bool {
|
|
s.Lock()
|
|
res := s.Paused
|
|
s.Unlock()
|
|
return res
|
|
}
|
|
|
|
// IsRestarting returns whether the container is restarting or not.
|
|
func (s *State) IsRestarting() bool {
|
|
s.Lock()
|
|
res := s.Restarting
|
|
s.Unlock()
|
|
return res
|
|
}
|
|
|
|
// SetRemovalInProgress sets the container state as being removed.
|
|
// It returns true if the container was already in that state.
|
|
func (s *State) SetRemovalInProgress() bool {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
if s.RemovalInProgress {
|
|
return true
|
|
}
|
|
s.RemovalInProgress = true
|
|
return false
|
|
}
|
|
|
|
// ResetRemovalInProgress makes the RemovalInProgress state to false.
|
|
func (s *State) ResetRemovalInProgress() {
|
|
s.Lock()
|
|
s.RemovalInProgress = false
|
|
s.Unlock()
|
|
}
|
|
|
|
// SetDead sets the container state to "dead"
|
|
func (s *State) SetDead() {
|
|
s.Lock()
|
|
s.Dead = true
|
|
s.Unlock()
|
|
}
|
|
|
|
// SetRemoved assumes this container is already in the "dead" state and
|
|
// closes the internal waitRemove channel to unblock callers waiting for a
|
|
// container to be removed.
|
|
func (s *State) SetRemoved() {
|
|
s.Lock()
|
|
close(s.waitRemove) // Unblock those waiting on remove.
|
|
s.Unlock()
|
|
}
|
|
|
|
// Err returns an error if there is one.
|
|
func (s *State) Err() error {
|
|
if s.ErrorMsg != "" {
|
|
return errors.New(s.ErrorMsg)
|
|
}
|
|
return nil
|
|
}
|