diff options
author | haircommander <pehunt@redhat.com> | 2018-08-14 13:03:05 -0400 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-08-16 20:31:50 +0000 |
commit | 67f79eaf73af86432a7948a49f121c7830cba82e (patch) | |
tree | a28f7c6874d07734de265eb862c2042c83890303 | |
parent | 37e3f47ef3ecc6f36fca65f47914139c636f34ac (diff) | |
download | podman-67f79eaf73af86432a7948a49f121c7830cba82e.tar.gz podman-67f79eaf73af86432a7948a49f121c7830cba82e.tar.bz2 podman-67f79eaf73af86432a7948a49f121c7830cba82e.zip |
Moved getPodStatus to pod API to be used in varlink
Signed-off-by: haircommander <pehunt@redhat.com>
Closes: #1275
Approved by: mheon
-rw-r--r-- | cmd/podman/pod_ps.go | 52 | ||||
-rw-r--r-- | libpod/pod_api.go | 61 |
2 files changed, 62 insertions, 51 deletions
diff --git a/cmd/podman/pod_ps.go b/cmd/podman/pod_ps.go index 1c030f266..52fbea6e5 100644 --- a/cmd/podman/pod_ps.go +++ b/cmd/podman/pod_ps.go @@ -296,7 +296,7 @@ func generatePodFilterFuncs(filter, filterValue string, runtime *libpod.Runtime) return nil, errors.Errorf("%s is not a valid status", filterValue) } return func(p *libpod.Pod) bool { - ctr_statuses, err := p.Status() + ctr_statuses, err := p.ContainerStatus() if err != nil { return false } @@ -324,7 +324,7 @@ func generatePodFilterFuncs(filter, filterValue string, runtime *libpod.Runtime) return nil, errors.Errorf("%s is not a valid pod status", filterValue) } return func(p *libpod.Pod) bool { - status, err := getPodStatus(p) + status, err := p.Status() if err != nil { return false } @@ -460,52 +460,6 @@ func getPodTemplateOutput(psParams []podPsJSONParams, opts podPsOptions) ([]podP return psOutput, nil } -func getPodStatus(pod *libpod.Pod) (string, error) { - ctr_statuses, err := pod.Status() - if err != nil { - return ERROR, err - } - ctrNum := len(ctr_statuses) - if ctrNum == 0 { - return CREATED, nil - } - statuses := map[string]int{ - STOPPED: 0, - RUNNING: 0, - PAUSED: 0, - CREATED: 0, - ERROR: 0, - } - for _, ctr_status := range ctr_statuses { - switch ctr_status { - case libpod.ContainerStateStopped: - statuses[STOPPED]++ - case libpod.ContainerStateRunning: - statuses[RUNNING]++ - case libpod.ContainerStatePaused: - statuses[PAUSED]++ - case libpod.ContainerStateCreated, libpod.ContainerStateConfigured: - statuses[CREATED]++ - default: - statuses[ERROR]++ - } - } - - if statuses[RUNNING] > 0 { - return RUNNING, nil - } else if statuses[PAUSED] == ctrNum { - return PAUSED, nil - } else if statuses[STOPPED] == ctrNum { - return EXITED, nil - } else if statuses[STOPPED] > 0 { - return STOPPED, nil - } else if statuses[ERROR] > 0 { - return ERROR, nil - } else { - return CREATED, nil - } -} - // getAndSortPodJSONOutput returns the container info in its raw, sorted form func getAndSortPodJSONParams(pods []*libpod.Pod, opts podPsOptions, runtime *libpod.Runtime) ([]podPsJSONParams, error) { var ( @@ -519,7 +473,7 @@ func getAndSortPodJSONParams(pods []*libpod.Pod, opts podPsOptions, runtime *lib return nil, err } ctrNum := len(ctrs) - status, err := getPodStatus(pod) + status, err := pod.Status() if err != nil { return nil, err } diff --git a/libpod/pod_api.go b/libpod/pod_api.go index d1e19063c..4d404587f 100644 --- a/libpod/pod_api.go +++ b/libpod/pod_api.go @@ -8,6 +8,15 @@ import ( "github.com/ulule/deepcopier" ) +const ( + stopped = "Stopped" + running = "Running" + paused = "Paused" + exited = "Exited" + errored = "Error" + created = "Created" +) + // Start starts all containers within a pod // It combines the effects of Init() and Start() on a container // If a container has already been initialized it will be started, @@ -362,9 +371,9 @@ func (p *Pod) Kill(signal uint) (map[string]error, error) { return nil, nil } -// Status gets the status of all containers in the pod +// ContainerStatus gets the status of all containers in the pod // Returns a map of Container ID to Container Status -func (p *Pod) Status() (map[string]ContainerStatus, error) { +func (p *Pod) ContainerStatus() (map[string]ContainerStatus, error) { p.lock.Lock() defer p.lock.Unlock() @@ -396,6 +405,54 @@ func (p *Pod) Status() (map[string]ContainerStatus, error) { return status, nil } +// Status determines the status of the pod based on the +// statuses of the containers in the pod. +// Returns a string representation of the pod status +func (p *Pod) Status() (string, error) { + ctrStatuses, err := p.ContainerStatus() + if err != nil { + return errored, err + } + ctrNum := len(ctrStatuses) + if ctrNum == 0 { + return created, nil + } + statuses := map[string]int{ + stopped: 0, + running: 0, + paused: 0, + created: 0, + errored: 0, + } + for _, ctrStatus := range ctrStatuses { + switch ctrStatus { + case ContainerStateStopped: + statuses[stopped]++ + case ContainerStateRunning: + statuses[running]++ + case ContainerStatePaused: + statuses[paused]++ + case ContainerStateCreated, ContainerStateConfigured: + statuses[created]++ + default: + statuses[errored]++ + } + } + + if statuses[running] > 0 { + return running, nil + } else if statuses[paused] == ctrNum { + return paused, nil + } else if statuses[stopped] == ctrNum { + return exited, nil + } else if statuses[stopped] > 0 { + return stopped, nil + } else if statuses[errored] > 0 { + return errored, nil + } + return created, nil +} + // Inspect returns a PodInspect struct to describe the pod func (p *Pod) Inspect() (*PodInspect, error) { var ( |