Skip to content

Commit

Permalink
Finalize the state from handle logic
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewavery committed Feb 17, 2018
1 parent 8c1084b commit 532ff7b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
19 changes: 14 additions & 5 deletions lib/apiservers/engine/backends/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions lib/apiservers/engine/backends/container_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 3 additions & 8 deletions lib/apiservers/portlayer/restapi/handlers/containers_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,21 @@ 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)
}

return containers.NewGetStateOK().WithPayload(
&models.ContainerGetStateResponse{
Handle: h.String(),
State: state,
State: state.String(),
})
}

Expand Down

0 comments on commit 532ff7b

Please sign in to comment.