summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2019-04-17 08:55:59 -0700
committerGitHub <noreply@github.com>2019-04-17 08:55:59 -0700
commit799d4667c1c33ac025741e2082739999f5b4563a (patch)
tree9fe87a7fc5f470314f754b8452163c4f59fd1a2c /cmd
parentd0c5e216ca508d195b805d0e48b159cfbff868a9 (diff)
parent4319552cf89e72925a80c63f427e5ef0a6376046 (diff)
downloadpodman-799d4667c1c33ac025741e2082739999f5b4563a.tar.gz
podman-799d4667c1c33ac025741e2082739999f5b4563a.tar.bz2
podman-799d4667c1c33ac025741e2082739999f5b4563a.zip
Merge pull request #2936 from haircommander/pod-prune
Add podman pod prune
Diffstat (limited to 'cmd')
-rw-r--r--cmd/podman/cliconfig/config.go5
-rw-r--r--cmd/podman/pod.go1
-rw-r--r--cmd/podman/pods_prune.go83
-rw-r--r--cmd/podman/shared/pod.go58
-rw-r--r--cmd/podman/system_prune.go11
-rw-r--r--cmd/podman/varlink/io.podman.varlink3
6 files changed, 131 insertions, 30 deletions
diff --git a/cmd/podman/cliconfig/config.go b/cmd/podman/cliconfig/config.go
index 982c77c17..16c98a13e 100644
--- a/cmd/podman/cliconfig/config.go
+++ b/cmd/podman/cliconfig/config.go
@@ -159,6 +159,11 @@ type PruneContainersValues struct {
Force bool
}
+type PrunePodsValues struct {
+ PodmanCommand
+ Force bool
+}
+
type ImportValues struct {
PodmanCommand
Change []string
diff --git a/cmd/podman/pod.go b/cmd/podman/pod.go
index 2d9bca21d..ed331965e 100644
--- a/cmd/podman/pod.go
+++ b/cmd/podman/pod.go
@@ -24,6 +24,7 @@ var podSubCommands = []*cobra.Command{
_podInspectCommand,
_podKillCommand,
_podPauseCommand,
+ _prunePodsCommand,
_podPsCommand,
_podRestartCommand,
_podRmCommand,
diff --git a/cmd/podman/pods_prune.go b/cmd/podman/pods_prune.go
new file mode 100644
index 000000000..89401a98a
--- /dev/null
+++ b/cmd/podman/pods_prune.go
@@ -0,0 +1,83 @@
+package main
+
+import (
+ "context"
+
+ "github.com/containers/libpod/cmd/podman/cliconfig"
+ "github.com/containers/libpod/cmd/podman/shared"
+ "github.com/containers/libpod/pkg/adapter"
+ "github.com/pkg/errors"
+ "github.com/sirupsen/logrus"
+ "github.com/spf13/cobra"
+)
+
+var (
+ prunePodsCommand cliconfig.PrunePodsValues
+ prunePodsDescription = `
+ podman pod prune
+
+ Removes all exited pods
+`
+ _prunePodsCommand = &cobra.Command{
+ Use: "prune",
+ Args: noSubArgs,
+ Short: "Remove all stopped pods",
+ Long: prunePodsDescription,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ prunePodsCommand.InputArgs = args
+ prunePodsCommand.GlobalFlags = MainGlobalOpts
+ return prunePodsCmd(&prunePodsCommand)
+ },
+ }
+)
+
+func init() {
+ prunePodsCommand.Command = _prunePodsCommand
+ prunePodsCommand.SetHelpTemplate(HelpTemplate())
+ prunePodsCommand.SetUsageTemplate(UsageTemplate())
+ flags := prunePodsCommand.Flags()
+ flags.BoolVarP(&prunePodsCommand.Force, "force", "f", false, "Force removal of a running pods. The default is false")
+}
+
+func prunePods(runtime *adapter.LocalRuntime, ctx context.Context, maxWorkers int, force bool) error {
+ var deleteFuncs []shared.ParallelWorkerInput
+
+ states := []string{shared.PodStateStopped, shared.PodStateExited}
+ delPods, err := runtime.GetPodsByStatus(states)
+ if err != nil {
+ return err
+ }
+ if len(delPods) < 1 {
+ return nil
+ }
+ for _, pod := range delPods {
+ p := pod
+ f := func() error {
+ return runtime.RemovePod(ctx, p, force, force)
+ }
+
+ deleteFuncs = append(deleteFuncs, shared.ParallelWorkerInput{
+ ContainerID: p.ID(),
+ ParallelFunc: f,
+ })
+ }
+ // Run the parallel funcs
+ deleteErrors, errCount := shared.ParallelExecuteWorkerPool(maxWorkers, deleteFuncs)
+ return printParallelOutput(deleteErrors, errCount)
+}
+
+func prunePodsCmd(c *cliconfig.PrunePodsValues) error {
+ runtime, err := adapter.GetRuntime(&c.PodmanCommand)
+ if err != nil {
+ return errors.Wrapf(err, "could not get runtime")
+ }
+ defer runtime.Shutdown(false)
+
+ maxWorkers := shared.Parallelize("rm")
+ if c.GlobalIsSet("max-workers") {
+ maxWorkers = c.GlobalFlags.MaxWorks
+ }
+ logrus.Debugf("Setting maximum workers to %d", maxWorkers)
+
+ return prunePods(runtime, getContext(), maxWorkers, c.Bool("force"))
+}
diff --git a/cmd/podman/shared/pod.go b/cmd/podman/shared/pod.go
index 4d936d61c..3f4cb0312 100644
--- a/cmd/podman/shared/pod.go
+++ b/cmd/podman/shared/pod.go
@@ -10,12 +10,12 @@ import (
)
const (
- stopped = "Stopped"
- running = "Running"
- paused = "Paused"
- exited = "Exited"
- errored = "Error"
- created = "Created"
+ PodStateStopped = "Stopped"
+ PodStateRunning = "Running"
+ PodStatePaused = "Paused"
+ PodStateExited = "Exited"
+ PodStateErrored = "Error"
+ PodStateCreated = "Created"
)
// GetPodStatus determines the status of the pod based on the
@@ -24,7 +24,7 @@ const (
func GetPodStatus(pod *libpod.Pod) (string, error) {
ctrStatuses, err := pod.Status()
if err != nil {
- return errored, err
+ return PodStateErrored, err
}
return CreatePodStatusResults(ctrStatuses)
}
@@ -32,44 +32,44 @@ func GetPodStatus(pod *libpod.Pod) (string, error) {
func CreatePodStatusResults(ctrStatuses map[string]libpod.ContainerStatus) (string, error) {
ctrNum := len(ctrStatuses)
if ctrNum == 0 {
- return created, nil
+ return PodStateCreated, nil
}
statuses := map[string]int{
- stopped: 0,
- running: 0,
- paused: 0,
- created: 0,
- errored: 0,
+ PodStateStopped: 0,
+ PodStateRunning: 0,
+ PodStatePaused: 0,
+ PodStateCreated: 0,
+ PodStateErrored: 0,
}
for _, ctrStatus := range ctrStatuses {
switch ctrStatus {
case libpod.ContainerStateExited:
fallthrough
case libpod.ContainerStateStopped:
- statuses[stopped]++
+ statuses[PodStateStopped]++
case libpod.ContainerStateRunning:
- statuses[running]++
+ statuses[PodStateRunning]++
case libpod.ContainerStatePaused:
- statuses[paused]++
+ statuses[PodStatePaused]++
case libpod.ContainerStateCreated, libpod.ContainerStateConfigured:
- statuses[created]++
+ statuses[PodStateCreated]++
default:
- statuses[errored]++
+ statuses[PodStateErrored]++
}
}
- 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
+ if statuses[PodStateRunning] > 0 {
+ return PodStateRunning, nil
+ } else if statuses[PodStatePaused] == ctrNum {
+ return PodStatePaused, nil
+ } else if statuses[PodStateStopped] == ctrNum {
+ return PodStateExited, nil
+ } else if statuses[PodStateStopped] > 0 {
+ return PodStateStopped, nil
+ } else if statuses[PodStateErrored] > 0 {
+ return PodStateErrored, nil
}
- return created, nil
+ return PodStateCreated, nil
}
// GetNamespaceOptions transforms a slice of kernel namespaces
diff --git a/cmd/podman/system_prune.go b/cmd/podman/system_prune.go
index 436d54823..14cb96941 100644
--- a/cmd/podman/system_prune.go
+++ b/cmd/podman/system_prune.go
@@ -59,6 +59,7 @@ func pruneSystemCmd(c *cliconfig.SystemPruneValues) error {
fmt.Printf(`
WARNING! This will remove:
- all stopped containers%s
+ - all stopped pods
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] `, volumeString)
@@ -77,9 +78,17 @@ Are you sure you want to continue? [y/N] `, volumeString)
}
defer runtime.Shutdown(false)
+ rmWorkers := shared.Parallelize("rm")
ctx := getContext()
fmt.Println("Deleted Containers")
- lasterr := pruneContainers(runtime, ctx, shared.Parallelize("rm"), false, false)
+ lasterr := pruneContainers(runtime, ctx, rmWorkers, false, false)
+ fmt.Println("Deleted Pods")
+ if err := prunePods(runtime, ctx, rmWorkers, true); err != nil {
+ if lasterr != nil {
+ logrus.Errorf("%q", lasterr)
+ }
+ lasterr = err
+ }
if c.Bool("volumes") {
fmt.Println("Deleted Volumes")
err := volumePrune(runtime, getContext())
diff --git a/cmd/podman/varlink/io.podman.varlink b/cmd/podman/varlink/io.podman.varlink
index b5295273a..497f130bc 100644
--- a/cmd/podman/varlink/io.podman.varlink
+++ b/cmd/podman/varlink/io.podman.varlink
@@ -1053,6 +1053,9 @@ method TopPod(pod: string, latest: bool, descriptors: []string) -> (stats: []str
# ~~~
method GetPodStats(name: string) -> (pod: string, containers: []ContainerStats)
+# GetPodsByStatus searches for pods whose status is included in statuses
+method GetPodsByStatus(statuses: []string) -> (pods: []string)
+
# ImageExists talks a full or partial image ID or name and returns an int as to whether
# the image exists in local storage. An int result of 0 means the image does exist in
# local storage; whereas 1 indicates the image does not exists in local storage.