From 532ff7bac2ddfb74d97e8c269cfeb352cd43ca7a Mon Sep 17 00:00:00 2001 From: matthewavery Date: Fri, 16 Feb 2018 22:14:22 -0800 Subject: [PATCH] Finalize the state from handle logic --- lib/apiservers/engine/backends/container.go | 19 ++++++++++++++----- .../engine/backends/container_proxy.go | 1 + .../restapi/handlers/containers_handlers.go | 11 +++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/apiservers/engine/backends/container.go b/lib/apiservers/engine/backends/container.go index d7f22bc9c8..c4e6e5097b 100644 --- a/lib/apiservers/engine/backends/container.go +++ b/lib/apiservers/engine/backends/container.go @@ -114,6 +114,15 @@ const ( maxElapsedTime = 2 * time.Minute ) +// These are the constants used for the portlayer exec states checks returned when obtaining the state of a container handle +const ( + RunningState = "Running" + CreatedState = "Created" + SuspendedState = "Suspended" + StartingState = "Starting" + StoppedState = "Stopped" +) + var ( publicIfaceName = "public" @@ -232,16 +241,16 @@ func (c *Container) ContainerExecCreate(name string, config *types.ExecConfig) ( } switch state { - case "STOPPED": + case StoppedState: return InternalServerError(fmt.Sprintf("Container (%s) is not running", name)) - case "CREATED": + case CreatedState: return InternalServerError(fmt.Sprintf("Container (%s) is not running", name)) - case "SUSPENDED": + case SuspendedState: return InternalServerError(fmt.Sprintf("Container (%s) is not running", name)) - case "STARTING": + case StartingState: // This is a transient state, returning conflict error to trigger a retry in the operation. return ConflictError(fmt.Sprintf("container (%s) is still starting", id)) - case "RUNNING": + case RunningState: // NO-OP - this is the state that allows an exec to occur. default: return InternalServerError(fmt.Sprintf("Container (%s) is in an unknown state: %s", id, state)) diff --git a/lib/apiservers/engine/backends/container_proxy.go b/lib/apiservers/engine/backends/container_proxy.go index 2145662ffc..193d676696 100644 --- a/lib/apiservers/engine/backends/container_proxy.go +++ b/lib/apiservers/engine/backends/container_proxy.go @@ -964,6 +964,7 @@ func (c *ContainerProxy) GetStateFromHandle(op trace.Operation, handle string) ( return handle, "", InternalServerError(err.Error()) } } + return resp.Payload.Handle, resp.Payload.State, nil } diff --git a/lib/apiservers/portlayer/restapi/handlers/containers_handlers.go b/lib/apiservers/portlayer/restapi/handlers/containers_handlers.go index 86449cc42b..e9a85be2fd 100644 --- a/lib/apiservers/portlayer/restapi/handlers/containers_handlers.go +++ b/lib/apiservers/portlayer/restapi/handlers/containers_handlers.go @@ -178,18 +178,13 @@ func (handler *ContainersHandlersImpl) GetStateHandler(params containers.GetStat return containers.NewGetStateNotFound() } - var state string - switch h.State(op) { + state := h.State(op) + switch state { case exec.StateRunning: - state = "RUNNING" case exec.StateStopped: - state = "STOPPED" case exec.StateCreated: - state = "CREATED" case exec.StateStarting: - state = "STARTING" case exec.StateSuspended: - state = "SUSPENDED" default: return containers.NewGetStateDefault(http.StatusServiceUnavailable) } @@ -197,7 +192,7 @@ func (handler *ContainersHandlersImpl) GetStateHandler(params containers.GetStat return containers.NewGetStateOK().WithPayload( &models.ContainerGetStateResponse{ Handle: h.String(), - State: state, + State: state.String(), }) }