diff --git a/cmd/podman/inspect.go b/cmd/podman/inspect.go index 1fa8f55a28..a36f8c7a13 100644 --- a/cmd/podman/inspect.go +++ b/cmd/podman/inspect.go @@ -4,9 +4,9 @@ import ( "context" "strings" - "github.com/containers/libpod/cmd/podman/batchcontainer" "github.com/containers/libpod/cmd/podman/formats" "github.com/containers/libpod/cmd/podman/libpodruntime" + "github.com/containers/libpod/cmd/podman/shared" "github.com/containers/libpod/libpod" "github.com/containers/libpod/pkg/util" "github.com/pkg/errors" @@ -121,7 +121,7 @@ func iterateInput(ctx context.Context, c *cli.Context, args []string, runtime *l inspectError = errors.Wrapf(err, "error getting libpod container inspect data %q", ctr.ID) break } - data, err = batchcontainer.GetCtrInspectInfo(ctr, libpodInspectData) + data, err = shared.GetCtrInspectInfo(ctr, libpodInspectData) if err != nil { inspectError = errors.Wrapf(err, "error parsing container data %q", ctr.ID()) break @@ -156,7 +156,7 @@ func iterateInput(ctx context.Context, c *cli.Context, args []string, runtime *l inspectError = errors.Wrapf(err, "error getting libpod container inspect data %q", ctr.ID) break } - data, err = batchcontainer.GetCtrInspectInfo(ctr, libpodInspectData) + data, err = shared.GetCtrInspectInfo(ctr, libpodInspectData) if err != nil { inspectError = errors.Wrapf(err, "error parsing container data %q", ctr.ID) break diff --git a/cmd/podman/pod_ps.go b/cmd/podman/pod_ps.go index 52fbea6e52..20beae53aa 100644 --- a/cmd/podman/pod_ps.go +++ b/cmd/podman/pod_ps.go @@ -7,9 +7,9 @@ import ( "strings" "time" - "github.com/containers/libpod/cmd/podman/batchcontainer" "github.com/containers/libpod/cmd/podman/formats" "github.com/containers/libpod/cmd/podman/libpodruntime" + "github.com/containers/libpod/cmd/podman/shared" "github.com/containers/libpod/libpod" "github.com/containers/libpod/pkg/util" "github.com/docker/go-units" @@ -28,7 +28,7 @@ const ( ) var ( - bc_opts batchcontainer.PsOptions + bc_opts shared.PsOptions ) type podPsCtrInfo struct { @@ -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.ContainerStatus() + ctr_statuses, err := p.Status() 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 := p.Status() + status, err := shared.GetPodStatus(p) if err != nil { return false } @@ -473,13 +473,13 @@ func getAndSortPodJSONParams(pods []*libpod.Pod, opts podPsOptions, runtime *lib return nil, err } ctrNum := len(ctrs) - status, err := pod.Status() + status, err := shared.GetPodStatus(pod) if err != nil { return nil, err } for _, ctr := range ctrs { - batchInfo, err := batchcontainer.BatchContainerOp(ctr, bc_opts) + batchInfo, err := shared.BatchContainerOp(ctr, bc_opts) if err != nil { return nil, err } diff --git a/cmd/podman/ps.go b/cmd/podman/ps.go index 94e8a4920b..0ad5112175 100644 --- a/cmd/podman/ps.go +++ b/cmd/podman/ps.go @@ -8,9 +8,9 @@ import ( "strings" "time" - "github.com/containers/libpod/cmd/podman/batchcontainer" "github.com/containers/libpod/cmd/podman/formats" "github.com/containers/libpod/cmd/podman/libpodruntime" + "github.com/containers/libpod/cmd/podman/shared" "github.com/containers/libpod/libpod" "github.com/containers/libpod/pkg/util" "github.com/cri-o/ocicni/pkg/ocicni" @@ -52,25 +52,25 @@ type psTemplateParams struct { // psJSONParams will be populated by data from libpod.Container, // the members of the struct are the sama data types as their sources. type psJSONParams struct { - ID string `json:"id"` - Image string `json:"image"` - ImageID string `json:"image_id"` - Command []string `json:"command"` - ExitCode int32 `json:"exitCode"` - Exited bool `json:"exited"` - CreatedAt time.Time `json:"createdAt"` - StartedAt time.Time `json:"startedAt"` - ExitedAt time.Time `json:"exitedAt"` - Status string `json:"status"` - PID int `json:"PID"` - Ports []ocicni.PortMapping `json:"ports"` - Size *batchcontainer.ContainerSize `json:"size,omitempty"` - Names string `json:"names"` - Labels fields.Set `json:"labels"` - Mounts []string `json:"mounts"` - ContainerRunning bool `json:"ctrRunning"` - Namespaces *batchcontainer.Namespace `json:"namespace,omitempty"` - Pod string `json:"pod,omitempty"` + ID string `json:"id"` + Image string `json:"image"` + ImageID string `json:"image_id"` + Command []string `json:"command"` + ExitCode int32 `json:"exitCode"` + Exited bool `json:"exited"` + CreatedAt time.Time `json:"createdAt"` + StartedAt time.Time `json:"startedAt"` + ExitedAt time.Time `json:"exitedAt"` + Status string `json:"status"` + PID int `json:"PID"` + Ports []ocicni.PortMapping `json:"ports"` + Size *shared.ContainerSize `json:"size,omitempty"` + Names string `json:"names"` + Labels fields.Set `json:"labels"` + Mounts []string `json:"mounts"` + ContainerRunning bool `json:"ctrRunning"` + Namespaces *shared.Namespace `json:"namespace,omitempty"` + Pod string `json:"pod,omitempty"` } // Type declaration and functions for sorting the PS output @@ -218,7 +218,7 @@ func psCmd(c *cli.Context) error { format := genPsFormat(c.String("format"), c.Bool("quiet"), c.Bool("size"), c.Bool("namespace"), c.Bool("pod")) - opts := batchcontainer.PsOptions{ + opts := shared.PsOptions{ All: c.Bool("all"), Filter: c.String("filter"), Format: format, @@ -497,11 +497,11 @@ func sortPsOutput(sortBy string, psOutput psSorted) (psSorted, error) { } // getTemplateOutput returns the modified container information -func getTemplateOutput(psParams []psJSONParams, opts batchcontainer.PsOptions) ([]psTemplateParams, error) { +func getTemplateOutput(psParams []psJSONParams, opts shared.PsOptions) ([]psTemplateParams, error) { var ( psOutput []psTemplateParams pod, status, size string - ns *batchcontainer.Namespace + ns *shared.Namespace ) // If the user is trying to filter based on size, or opted to sort on size // the size bool must be set. @@ -589,13 +589,13 @@ func getTemplateOutput(psParams []psJSONParams, opts batchcontainer.PsOptions) ( } // getAndSortJSONOutput returns the container info in its raw, sorted form -func getAndSortJSONParams(containers []*libpod.Container, opts batchcontainer.PsOptions) ([]psJSONParams, error) { +func getAndSortJSONParams(containers []*libpod.Container, opts shared.PsOptions) ([]psJSONParams, error) { var ( psOutput psSorted - ns *batchcontainer.Namespace + ns *shared.Namespace ) for _, ctr := range containers { - batchInfo, err := batchcontainer.BatchContainerOp(ctr, opts) + batchInfo, err := shared.BatchContainerOp(ctr, opts) if err != nil { if errors.Cause(err) == libpod.ErrNoSuchCtr { logrus.Warn(err) @@ -605,7 +605,7 @@ func getAndSortJSONParams(containers []*libpod.Container, opts batchcontainer.Ps } if opts.Namespace { - ns = batchcontainer.GetNamespaces(batchInfo.Pid) + ns = shared.GetNamespaces(batchInfo.Pid) } params := psJSONParams{ ID: ctr.ID(), @@ -634,7 +634,7 @@ func getAndSortJSONParams(containers []*libpod.Container, opts batchcontainer.Ps return sortPsOutput(opts.Sort, psOutput) } -func generatePsOutput(containers []*libpod.Container, opts batchcontainer.PsOptions) error { +func generatePsOutput(containers []*libpod.Container, opts shared.PsOptions) error { if len(containers) == 0 && opts.Format != formats.JSONString { return nil } diff --git a/cmd/podman/shared/.pod.go.swp b/cmd/podman/shared/.pod.go.swp new file mode 100644 index 0000000000..a8565e752e Binary files /dev/null and b/cmd/podman/shared/.pod.go.swp differ diff --git a/cmd/podman/batchcontainer/container.go b/cmd/podman/shared/container.go similarity index 99% rename from cmd/podman/batchcontainer/container.go rename to cmd/podman/shared/container.go index 364b28d40f..7a54550460 100644 --- a/cmd/podman/batchcontainer/container.go +++ b/cmd/podman/shared/container.go @@ -1,4 +1,4 @@ -package batchcontainer +package shared import ( "encoding/json" diff --git a/cmd/podman/shared/pod.go b/cmd/podman/shared/pod.go new file mode 100644 index 0000000000..50c642d592 --- /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 +} diff --git a/libpod/pod_api.go b/libpod/pod_api.go index 4d404587f7..d1e19063cc 100644 --- a/libpod/pod_api.go +++ b/libpod/pod_api.go @@ -8,15 +8,6 @@ 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, @@ -371,9 +362,9 @@ func (p *Pod) Kill(signal uint) (map[string]error, error) { return nil, nil } -// ContainerStatus gets the status of all containers in the pod +// Status gets the status of all containers in the pod // Returns a map of Container ID to Container Status -func (p *Pod) ContainerStatus() (map[string]ContainerStatus, error) { +func (p *Pod) Status() (map[string]ContainerStatus, error) { p.lock.Lock() defer p.lock.Unlock() @@ -405,54 +396,6 @@ func (p *Pod) ContainerStatus() (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 ( diff --git a/pkg/varlinkapi/containers.go b/pkg/varlinkapi/containers.go index df8b669963..58452716a2 100644 --- a/pkg/varlinkapi/containers.go +++ b/pkg/varlinkapi/containers.go @@ -9,7 +9,7 @@ import ( "syscall" "time" - "github.com/containers/libpod/cmd/podman/batchcontainer" + "github.com/containers/libpod/cmd/podman/shared" "github.com/containers/libpod/cmd/podman/varlink" "github.com/containers/libpod/libpod" "github.com/containers/storage/pkg/archive" @@ -26,12 +26,12 @@ func (i *LibpodAPI) ListContainers(call iopodman.VarlinkCall) error { if err != nil { return call.ReplyErrorOccurred(err.Error()) } - opts := batchcontainer.PsOptions{ + opts := shared.PsOptions{ Namespace: true, Size: true, } for _, ctr := range containers { - batchInfo, err := batchcontainer.BatchContainerOp(ctr, opts) + batchInfo, err := shared.BatchContainerOp(ctr, opts) if err != nil { return call.ReplyErrorOccurred(err.Error()) } @@ -47,11 +47,11 @@ func (i *LibpodAPI) GetContainer(call iopodman.VarlinkCall, name string) error { if err != nil { return call.ReplyContainerNotFound(name) } - opts := batchcontainer.PsOptions{ + opts := shared.PsOptions{ Namespace: true, Size: true, } - batchInfo, err := batchcontainer.BatchContainerOp(ctr, opts) + batchInfo, err := shared.BatchContainerOp(ctr, opts) if err != nil { return call.ReplyErrorOccurred(err.Error()) } @@ -68,7 +68,7 @@ func (i *LibpodAPI) InspectContainer(call iopodman.VarlinkCall, name string) err if err != nil { return call.ReplyErrorOccurred(err.Error()) } - data, err := batchcontainer.GetCtrInspectInfo(ctr, inspectInfo) + data, err := shared.GetCtrInspectInfo(ctr, inspectInfo) if err != nil { return call.ReplyErrorOccurred(err.Error()) } diff --git a/pkg/varlinkapi/pods.go b/pkg/varlinkapi/pods.go index 272c348fc8..98a622d8a5 100644 --- a/pkg/varlinkapi/pods.go +++ b/pkg/varlinkapi/pods.go @@ -4,7 +4,7 @@ import ( "encoding/json" "syscall" - "github.com/projectatomic/libpod/cmd/podman/batchcontainer" + "github.com/projectatomic/libpod/cmd/podman/shared" "github.com/projectatomic/libpod/cmd/podman/varlink" "github.com/projectatomic/libpod/libpod" ) @@ -40,7 +40,7 @@ func (i *LibpodAPI) ListPods(call iopodman.VarlinkCall) error { if err != nil { return call.ReplyErrorOccurred(err.Error()) } - opts := batchcontainer.PsOptions{} + opts := shared.PsOptions{} for _, pod := range pods { listPod, err := makeListPod(pod, opts) if err != nil { @@ -57,7 +57,7 @@ func (i *LibpodAPI) GetPod(call iopodman.VarlinkCall, name string) error { if err != nil { return call.ReplyPodNotFound(name) } - opts := batchcontainer.PsOptions{} + opts := shared.PsOptions{} listPod, err := makeListPod(pod, opts) if err != nil { diff --git a/pkg/varlinkapi/util.go b/pkg/varlinkapi/util.go index 74afabd0f2..e0b6799349 100644 --- a/pkg/varlinkapi/util.go +++ b/pkg/varlinkapi/util.go @@ -5,7 +5,7 @@ import ( "strconv" "time" - "github.com/containers/libpod/cmd/podman/batchcontainer" + "github.com/containers/libpod/cmd/podman/shared" "github.com/containers/libpod/cmd/podman/varlink" "github.com/containers/libpod/libpod" ) @@ -15,12 +15,12 @@ func getContext() context.Context { return context.TODO() } -func makeListContainer(containerID string, batchInfo batchcontainer.BatchContainerStruct) iopodman.ListContainerData { +func makeListContainer(containerID string, batchInfo shared.BatchContainerStruct) iopodman.ListContainerData { var ( mounts []iopodman.ContainerMount ports []iopodman.ContainerPortMappings ) - ns := batchcontainer.GetNamespaces(batchInfo.Pid) + ns := shared.GetNamespaces(batchInfo.Pid) for _, mount := range batchInfo.ConConfig.Spec.Mounts { m := iopodman.ContainerMount{ @@ -78,7 +78,7 @@ func makeListContainer(containerID string, batchInfo batchcontainer.BatchContain return lc } -func makeListPodContainers(containerID string, batchInfo batchcontainer.BatchContainerStruct) iopodman.ListPodContainerInfo { +func makeListPodContainers(containerID string, batchInfo shared.BatchContainerStruct) iopodman.ListPodContainerInfo { lc := iopodman.ListPodContainerInfo{ Id: containerID, Status: batchInfo.ConState.String(), @@ -87,10 +87,10 @@ func makeListPodContainers(containerID string, batchInfo batchcontainer.BatchCon return lc } -func makeListPod(pod *libpod.Pod, batchInfo batchcontainer.PsOptions) (iopodman.ListPodData, error) { +func makeListPod(pod *libpod.Pod, batchInfo shared.PsOptions) (iopodman.ListPodData, error) { var listPodsContainers []iopodman.ListPodContainerInfo var errPodData = iopodman.ListPodData{} - status, err := pod.Status() + status, err := shared.GetPodStatus(pod) if err != nil { return errPodData, err } @@ -99,7 +99,7 @@ func makeListPod(pod *libpod.Pod, batchInfo batchcontainer.PsOptions) (iopodman. return errPodData, err } for _, ctr := range containers { - batchInfo, err := batchcontainer.BatchContainerOp(ctr, batchInfo) + batchInfo, err := shared.BatchContainerOp(ctr, batchInfo) if err != nil { return errPodData, err }