summaryrefslogtreecommitdiff
path: root/cmd/podman/shared/pod.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/podman/shared/pod.go')
-rw-r--r--cmd/podman/shared/pod.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/cmd/podman/shared/pod.go b/cmd/podman/shared/pod.go
new file mode 100644
index 000000000..50c642d59
--- /dev/null
+++ b/cmd/podman/shared/pod.go
@@ -0,0 +1,62 @@
+package shared
+
+import (
+ "github.com/projectatomic/libpod/libpod"
+)
+
+const (
+ stopped = "Stopped"
+ running = "Running"
+ paused = "Paused"
+ exited = "Exited"
+ errored = "Error"
+ created = "Created"
+)
+
+// GetPodStatus 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 GetPodStatus(pod *libpod.Pod) (string, error) {
+ ctrStatuses, err := pod.Status()
+ 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 libpod.ContainerStateStopped:
+ statuses[stopped]++
+ case libpod.ContainerStateRunning:
+ statuses[running]++
+ case libpod.ContainerStatePaused:
+ statuses[paused]++
+ case libpod.ContainerStateCreated, libpod.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
+}