diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-04-10 14:11:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-10 14:11:30 +0200 |
commit | 2a8db9d17c5dd762366fe7c90304e76337e5d668 (patch) | |
tree | 68c8f58631d70cdfff949d3a171581b236525a8e /cmd | |
parent | 838b5e1e387f6f6bdae06f053684832f77ca8796 (diff) | |
parent | 7d0e0a7129e18ca7295cf4b0fbec22d0df78d3dd (diff) | |
download | podman-2a8db9d17c5dd762366fe7c90304e76337e5d668.tar.gz podman-2a8db9d17c5dd762366fe7c90304e76337e5d668.tar.bz2 podman-2a8db9d17c5dd762366fe7c90304e76337e5d668.zip |
Merge pull request #5782 from baude/v2containercleanup
v2podman container cleanup
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/podmanV2/containers/cleanup.go | 75 | ||||
-rw-r--r-- | cmd/podmanV2/containers/run.go | 37 | ||||
-rw-r--r-- | cmd/podmanV2/containers/wait.go | 5 |
3 files changed, 107 insertions, 10 deletions
diff --git a/cmd/podmanV2/containers/cleanup.go b/cmd/podmanV2/containers/cleanup.go new file mode 100644 index 000000000..3f45db160 --- /dev/null +++ b/cmd/podmanV2/containers/cleanup.go @@ -0,0 +1,75 @@ +package containers + +import ( + "fmt" + + "github.com/containers/libpod/cmd/podmanV2/parse" + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/cmd/podmanV2/utils" + "github.com/containers/libpod/pkg/domain/entities" + "github.com/spf13/cobra" +) + +var ( + cleanupDescription = ` + podman container cleanup + + Cleans up mount points and network stacks on one or more containers from the host. The container name or ID can be used. This command is used internally when running containers, but can also be used if container cleanup has failed when a container exits. +` + cleanupCommand = &cobra.Command{ + Use: "cleanup [flags] CONTAINER [CONTAINER...]", + Short: "Cleanup network and mountpoints of one or more containers", + Long: cleanupDescription, + RunE: cleanup, + Args: func(cmd *cobra.Command, args []string) error { + return parse.CheckAllLatestAndCIDFile(cmd, args, false, false) + }, + Example: `podman container cleanup --latest + podman container cleanup ctrID1 ctrID2 ctrID3 + podman container cleanup --all`, + } +) + +var ( + cleanupOptions entities.ContainerCleanupOptions +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode}, + Parent: containerCmd, + Command: cleanupCommand, + }) + flags := cleanupCommand.Flags() + flags.BoolVarP(&cleanupOptions.All, "all", "a", false, "Cleans up all containers") + flags.BoolVarP(&cleanupOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of") + flags.BoolVar(&cleanupOptions.Remove, "rm", false, "After cleanup, remove the container entirely") + flags.BoolVar(&cleanupOptions.RemoveImage, "rmi", false, "After cleanup, remove the image entirely") + +} + +func cleanup(cmd *cobra.Command, args []string) error { + var ( + errs utils.OutputErrors + ) + responses, err := registry.ContainerEngine().ContainerCleanup(registry.GetContext(), args, cleanupOptions) + if err != nil { + return err + } + for _, r := range responses { + if r.CleanErr == nil && r.RmErr == nil && r.RmiErr == nil { + fmt.Println(r.Id) + continue + } + if r.RmErr != nil { + errs = append(errs, r.RmErr) + } + if r.RmiErr != nil { + errs = append(errs, r.RmiErr) + } + if r.CleanErr != nil { + errs = append(errs, r.CleanErr) + } + } + return errs.PrintErrors() +} diff --git a/cmd/podmanV2/containers/run.go b/cmd/podmanV2/containers/run.go index f839c358d..0bf0f90f8 100644 --- a/cmd/podmanV2/containers/run.go +++ b/cmd/podmanV2/containers/run.go @@ -5,15 +5,14 @@ import ( "os" "strings" - "github.com/sirupsen/logrus" - - "github.com/containers/libpod/pkg/domain/entities" - + "github.com/containers/common/pkg/config" "github.com/containers/libpod/cmd/podmanV2/common" "github.com/containers/libpod/cmd/podmanV2/registry" "github.com/containers/libpod/libpod/define" + "github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/specgen" "github.com/pkg/errors" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -75,6 +74,30 @@ func run(cmd *cobra.Command, args []string) error { return err } + ie, err := registry.NewImageEngine(cmd, args) + if err != nil { + return err + } + br, err := ie.Exists(registry.GetContext(), args[0]) + if err != nil { + return err + } + pullPolicy, err := config.ValidatePullPolicy(cliVals.Pull) + if err != nil { + return err + } + if !br.Value || pullPolicy == config.PullImageAlways { + if pullPolicy == config.PullImageNever { + return errors.New("unable to find a name and tag match for busybox in repotags: no such image") + } + _, pullErr := ie.Pull(registry.GetContext(), args[0], entities.ImagePullOptions{ + Authfile: cliVals.Authfile, + Quiet: cliVals.Quiet, + }) + if pullErr != nil { + return pullErr + } + } // If -i is not set, clear stdin if !cliVals.Interactive { runOpts.InputStream = nil @@ -110,7 +133,9 @@ func run(cmd *cobra.Command, args []string) error { runOpts.Spec = s report, err := registry.ContainerEngine().ContainerRun(registry.GetContext(), runOpts) // report.ExitCode is set by ContainerRun even it it returns an error - registry.SetExitCode(report.ExitCode) + if report != nil { + registry.SetExitCode(report.ExitCode) + } if err != nil { return err } @@ -118,7 +143,7 @@ func run(cmd *cobra.Command, args []string) error { fmt.Println(report.Id) } if runRmi { - _, err := registry.ImageEngine().Delete(registry.GetContext(), []string{report.Id}, entities.ImageDeleteOptions{}) + _, err := registry.ImageEngine().Delete(registry.GetContext(), []string{args[0]}, entities.ImageDeleteOptions{}) if err != nil { logrus.Errorf("%s", errors.Wrapf(err, "failed removing image")) } diff --git a/cmd/podmanV2/containers/wait.go b/cmd/podmanV2/containers/wait.go index bf3c86200..29a0e5546 100644 --- a/cmd/podmanV2/containers/wait.go +++ b/cmd/podmanV2/containers/wait.go @@ -5,7 +5,6 @@ import ( "fmt" "time" - "github.com/containers/libpod/cmd/podmanV2/parse" "github.com/containers/libpod/cmd/podmanV2/registry" "github.com/containers/libpod/cmd/podmanV2/utils" "github.com/containers/libpod/libpod/define" @@ -23,9 +22,7 @@ var ( Long: waitDescription, RunE: wait, PersistentPreRunE: preRunE, - Args: func(cmd *cobra.Command, args []string) error { - return parse.CheckAllLatestAndCIDFile(cmd, args, false, false) - }, + Args: registry.IdOrLatestArgs, Example: `podman wait --latest podman wait --interval 5000 ctrID podman wait ctrID1 ctrID2`, |