From 74d984e0560b2cb421287395b025687e3aabe118 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Tue, 5 Feb 2019 10:41:55 -0800 Subject: Add podman system prune and info commands We are missing the equivalence of the docker system commands This patch set adds `podman system prune` and `podman system info` Signed-off-by: Daniel J Walsh --- cmd/podman/commands.go | 4 ++ cmd/podman/commands_remoteclient.go | 4 ++ cmd/podman/containers_prune.go | 37 +++++++------ cmd/podman/images_prune.go | 1 + cmd/podman/main.go | 1 + cmd/podman/run.go | 1 + cmd/podman/system.go | 29 ++++++++++ cmd/podman/system_prune.go | 107 ++++++++++++++++++++++++++++++++++++ cmd/podman/volume_prune.go | 48 ++++++++-------- 9 files changed, 193 insertions(+), 39 deletions(-) create mode 100644 cmd/podman/system.go create mode 100644 cmd/podman/system_prune.go (limited to 'cmd/podman') diff --git a/cmd/podman/commands.go b/cmd/podman/commands.go index d8fdd556f..2efcab695 100644 --- a/cmd/podman/commands.go +++ b/cmd/podman/commands.go @@ -53,6 +53,10 @@ func getImageSubCommands() []cli.Command { } } +func getSystemSubCommands() []cli.Command { + return []cli.Command{infoCommand} +} + func getContainerSubCommands() []cli.Command { return []cli.Command{ attachCommand, diff --git a/cmd/podman/commands_remoteclient.go b/cmd/podman/commands_remoteclient.go index 6701e14a1..0adbd7b4c 100644 --- a/cmd/podman/commands_remoteclient.go +++ b/cmd/podman/commands_remoteclient.go @@ -16,6 +16,10 @@ func getContainerSubCommands() []cli.Command { return []cli.Command{} } +func getSystemSubCommands() []cli.Command { + return []cli.Command{} +} + func getMainAppFlags() []cli.Flag { return []cli.Flag{} } diff --git a/cmd/podman/containers_prune.go b/cmd/podman/containers_prune.go index 92604e82f..09141e9a3 100644 --- a/cmd/podman/containers_prune.go +++ b/cmd/podman/containers_prune.go @@ -1,9 +1,11 @@ package main import ( - "github.com/containers/libpod/cmd/podman/libpodruntime" + "context" + "github.com/containers/libpod/cmd/podman/shared" "github.com/containers/libpod/libpod" + "github.com/containers/libpod/libpod/adapter" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -25,20 +27,11 @@ var ( } ) -func pruneContainersCmd(c *cli.Context) error { - var ( - deleteFuncs []shared.ParallelWorkerInput - ) - - ctx := getContext() - runtime, err := libpodruntime.GetRuntime(c) - if err != nil { - return errors.Wrapf(err, "could not get runtime") - } - defer runtime.Shutdown(false) +func pruneContainers(runtime *adapter.LocalRuntime, ctx context.Context, maxWorkers int, force bool) error { + var deleteFuncs []shared.ParallelWorkerInput filter := func(c *libpod.Container) bool { - state, _ := c.State() + state, err := c.State() if state == libpod.ContainerStateStopped || (state == libpod.ContainerStateExited && err == nil && c.PodID() == "") { return true } @@ -54,7 +47,7 @@ func pruneContainersCmd(c *cli.Context) error { for _, container := range delContainers { con := container f := func() error { - return runtime.RemoveContainer(ctx, con, c.Bool("force")) + return runtime.RemoveContainer(ctx, con, force) } deleteFuncs = append(deleteFuncs, shared.ParallelWorkerInput{ @@ -62,13 +55,23 @@ func pruneContainersCmd(c *cli.Context) error { ParallelFunc: f, }) } + // Run the parallel funcs + deleteErrors, errCount := shared.ParallelExecuteWorkerPool(maxWorkers, deleteFuncs) + return printParallelOutput(deleteErrors, errCount) +} + +func pruneContainersCmd(c *cli.Context) error { + runtime, err := adapter.GetRuntime(c) + 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.GlobalInt("max-workers") } logrus.Debugf("Setting maximum workers to %d", maxWorkers) - // Run the parallel funcs - deleteErrors, errCount := shared.ParallelExecuteWorkerPool(maxWorkers, deleteFuncs) - return printParallelOutput(deleteErrors, errCount) + return pruneContainers(runtime, getContext(), maxWorkers, c.Bool("force")) } diff --git a/cmd/podman/images_prune.go b/cmd/podman/images_prune.go index aef387732..baef813e5 100644 --- a/cmd/podman/images_prune.go +++ b/cmd/podman/images_prune.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/containers/libpod/libpod/adapter" "github.com/pkg/errors" "github.com/urfave/cli" diff --git a/cmd/podman/main.go b/cmd/podman/main.go index 1ca8882eb..d90df8222 100644 --- a/cmd/podman/main.go +++ b/cmd/podman/main.go @@ -97,6 +97,7 @@ func main() { inspectCommand, pullCommand, rmiCommand, + systemCommand, tagCommand, versionCommand, } diff --git a/cmd/podman/run.go b/cmd/podman/run.go index 20cb85347..662aed900 100644 --- a/cmd/podman/run.go +++ b/cmd/podman/run.go @@ -131,6 +131,7 @@ func runCmd(c *cli.Context) error { ctrExitCode, err := readExitFile(runtime.GetConfig().TmpDir, ctr.ID()) if err != nil { logrus.Errorf("Cannot get exit code: %v", err) + exitCode = 127 } else { exitCode = ctrExitCode } diff --git a/cmd/podman/system.go b/cmd/podman/system.go new file mode 100644 index 000000000..9596252ad --- /dev/null +++ b/cmd/podman/system.go @@ -0,0 +1,29 @@ +package main + +import ( + "sort" + + "github.com/urfave/cli" +) + +var ( + systemSubCommands = []cli.Command{ + pruneSystemCommand, + } + systemDescription = "Manage podman" + systemCommand = cli.Command{ + Name: "system", + Usage: "Manage podman", + Description: systemDescription, + ArgsUsage: "", + Subcommands: getSystemSubCommandsSorted(), + UseShortOptionHandling: true, + OnUsageError: usageErrorHandler, + } +) + +func getSystemSubCommandsSorted() []cli.Command { + systemSubCommands = append(systemSubCommands, getSystemSubCommands()...) + sort.Sort(commandSortedAlpha{systemSubCommands}) + return systemSubCommands +} diff --git a/cmd/podman/system_prune.go b/cmd/podman/system_prune.go new file mode 100644 index 000000000..145fb4c94 --- /dev/null +++ b/cmd/podman/system_prune.go @@ -0,0 +1,107 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/containers/libpod/cmd/podman/shared" + "github.com/containers/libpod/libpod/adapter" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "github.com/urfave/cli" +) + +var ( + pruneSystemDescription = ` + podman system prune + + Remove unused data +` + pruneSystemFlags = []cli.Flag{ + cli.BoolFlag{ + Name: "all, a", + Usage: "remove all unused data", + }, + cli.BoolFlag{ + Name: "force, f", + Usage: "Do not prompt for confirmation", + }, + cli.BoolFlag{ + Name: "volumes", + Usage: "Prune volumes", + }, + } + pruneSystemCommand = cli.Command{ + Name: "prune", + Usage: "Remove unused data", + Description: pruneSystemDescription, + Action: pruneSystemCmd, + OnUsageError: usageErrorHandler, + Flags: pruneSystemFlags, + } +) + +func pruneSystemCmd(c *cli.Context) error { + + // Prompt for confirmation if --force is not set + if !c.Bool("force") { + reader := bufio.NewReader(os.Stdin) + volumeString := "" + if c.Bool("volumes") { + volumeString = ` + - all volumes not used by at least one container` + } + fmt.Printf(` +WARNING! This will remove: + - all stopped containers%s + - all dangling images + - all build cache +Are you sure you want to continue? [y/N] `, volumeString) + ans, err := reader.ReadString('\n') + if err != nil { + return errors.Wrapf(err, "error reading input") + } + if strings.ToLower(ans)[0] != 'y' { + return nil + } + } + + runtime, err := adapter.GetRuntime(c) + if err != nil { + return errors.Wrapf(err, "could not get runtime") + } + defer runtime.Shutdown(false) + + ctx := getContext() + fmt.Println("Deleted Containers") + lasterr := pruneContainers(runtime, ctx, shared.Parallelize("rm"), false) + if c.Bool("volumes") { + fmt.Println("Deleted Volumes") + err := volumePrune(runtime, getContext()) + if err != nil { + if lasterr != nil { + logrus.Errorf("%q", lasterr) + } + lasterr = err + } + } + + // Call prune; if any cids are returned, print them and then + // return err in case an error also came up + pruneCids, err := runtime.PruneImages(c.Bool("all")) + if len(pruneCids) > 0 { + fmt.Println("Deleted Images") + for _, cid := range pruneCids { + fmt.Println(cid) + } + } + if err != nil { + if lasterr != nil { + logrus.Errorf("%q", lasterr) + } + lasterr = err + } + return lasterr +} diff --git a/cmd/podman/volume_prune.go b/cmd/podman/volume_prune.go index 652c50f42..41d95f9c7 100644 --- a/cmd/podman/volume_prune.go +++ b/cmd/podman/volume_prune.go @@ -2,12 +2,13 @@ package main import ( "bufio" + "context" "fmt" "os" "strings" - "github.com/containers/libpod/cmd/podman/libpodruntime" "github.com/containers/libpod/libpod" + "github.com/containers/libpod/libpod/adapter" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -37,21 +38,40 @@ var volumePruneCommand = cli.Command{ UseShortOptionHandling: true, } -func volumePruneCmd(c *cli.Context) error { +func volumePrune(runtime *adapter.LocalRuntime, ctx context.Context) error { var lastError error + volumes, err := runtime.GetAllVolumes() + if err != nil { + return err + } + + for _, vol := range volumes { + err = runtime.RemoveVolume(ctx, vol, false, true) + if err == nil { + fmt.Println(vol.Name()) + } else if err != libpod.ErrVolumeBeingUsed { + if lastError != nil { + logrus.Errorf("%q", lastError) + } + lastError = errors.Wrapf(err, "failed to remove volume %q", vol.Name()) + } + } + return lastError +} + +func volumePruneCmd(c *cli.Context) error { + if err := validateFlags(c, volumePruneFlags); err != nil { return err } - runtime, err := libpodruntime.GetRuntime(c) + runtime, err := adapter.GetRuntime(c) if err != nil { return errors.Wrapf(err, "error creating libpod runtime") } defer runtime.Shutdown(false) - ctx := getContext() - // Prompt for confirmation if --force is not set if !c.Bool("force") { reader := bufio.NewReader(os.Stdin) @@ -66,21 +86,5 @@ func volumePruneCmd(c *cli.Context) error { } } - volumes, err := runtime.GetAllVolumes() - if err != nil { - return err - } - - for _, vol := range volumes { - err = runtime.RemoveVolume(ctx, vol, false, true) - if err == nil { - fmt.Println(vol.Name()) - } else if err != libpod.ErrVolumeBeingUsed { - if lastError != nil { - logrus.Errorf("%q", lastError) - } - lastError = errors.Wrapf(err, "failed to remove volume %q", vol.Name()) - } - } - return lastError + return volumePrune(runtime, getContext()) } -- cgit v1.2.3-54-g00ecf