From e91ec38a70f4755d06972a0b65edd1f2e5366581 Mon Sep 17 00:00:00 2001 From: baude Date: Wed, 20 Feb 2019 15:02:18 -0600 Subject: enable podman-remote pod rm add the ability to delete a pod from the remote client. Signed-off-by: baude --- API.md | 7 ++++++ cmd/podman/commands.go | 2 -- cmd/podman/main.go | 1 + cmd/podman/pod.go | 6 +++++ cmd/podman/pod_rm.go | 38 +++++++++++++----------------- cmd/podman/varlink/io.podman.varlink | 2 ++ libpod/adapter/pods.go | 38 ++++++++++++++++++++++++++++++ libpod/adapter/pods_remote.go | 44 +++++++++++++++++++++++++++++++++++ libpod/adapter/shortcuts/shortcuts.go | 27 +++++++++++++++++++++ pkg/varlinkapi/pods.go | 15 ++++++++++++ 10 files changed, 156 insertions(+), 24 deletions(-) create mode 100644 libpod/adapter/pods.go create mode 100644 libpod/adapter/pods_remote.go create mode 100644 libpod/adapter/shortcuts/shortcuts.go diff --git a/API.md b/API.md index 0cb06e3bb..c5e138bca 100755 --- a/API.md +++ b/API.md @@ -51,6 +51,8 @@ in the [API.md](https://github.com/containers/libpod/blob/master/API.md) file in [func GetPodStats(name: string) string, ContainerStats](#GetPodStats) +[func GetPodsByContext(all: bool, latest: bool, args: []string) []string](#GetPodsByContext) + [func GetVersion() string, string, string, string, string, int](#GetVersion) [func GetVolumes(args: []string, all: bool) Volume](#GetVolumes) @@ -529,6 +531,11 @@ $ varlink call unix:/run/podman/io.podman/io.podman.GetPodStats '{"name": "7f62b "pod": "7f62b508b6f12b11d8fe02e0db4de6b9e43a7d7699b33a4fc0d574f6e82b4ebd" } ~~~ +### func GetPodsByContext +
+ +method GetPodsByContext(all: [bool](https://godoc.org/builtin#bool), latest: [bool](https://godoc.org/builtin#bool), args: [[]string](#[]string)) [[]string](#[]string)
+ ### func GetVersion
diff --git a/cmd/podman/commands.go b/cmd/podman/commands.go index 387e35767..466ffa22f 100644 --- a/cmd/podman/commands.go +++ b/cmd/podman/commands.go @@ -15,7 +15,6 @@ func getMainCommands() []*cobra.Command { _diffCommand, _execCommand, generateCommand.Command, - podCommand.Command, _containerKubeCommand, _psCommand, _loadCommand, @@ -100,7 +99,6 @@ func getPodSubCommands() []*cobra.Command { _podPauseCommand, _podPsCommand, _podRestartCommand, - _podRmCommand, _podStartCommand, _podStatsCommand, _podStopCommand, diff --git a/cmd/podman/main.go b/cmd/podman/main.go index 5fa6cf233..b60f3e1e1 100644 --- a/cmd/podman/main.go +++ b/cmd/podman/main.go @@ -45,6 +45,7 @@ var mainCommands = []*cobra.Command{ _infoCommand, _inspectCommand, _killCommand, + podCommand.Command, _pullCommand, _pushCommand, _rmiCommand, diff --git a/cmd/podman/pod.go b/cmd/podman/pod.go index e988875ab..cf87730d7 100644 --- a/cmd/podman/pod.go +++ b/cmd/podman/pod.go @@ -18,7 +18,13 @@ var podCommand = cliconfig.PodmanCommand{ }, } +//podSubCommands are implemented both in local and remote clients +var podSubCommands = []*cobra.Command{ + _podRmCommand, +} + func init() { + podCommand.AddCommand(podSubCommands...) podCommand.AddCommand(getPodSubCommands()...) podCommand.SetUsageTemplate(UsageTemplate()) } diff --git a/cmd/podman/pod_rm.go b/cmd/podman/pod_rm.go index 54cee2a50..b615f88c9 100644 --- a/cmd/podman/pod_rm.go +++ b/cmd/podman/pod_rm.go @@ -2,9 +2,9 @@ package main import ( "fmt" + "github.com/containers/libpod/libpod/adapter" "github.com/containers/libpod/cmd/podman/cliconfig" - "github.com/containers/libpod/cmd/podman/libpodruntime" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -42,36 +42,30 @@ func init() { } -// saveCmd saves the image to either docker-archive or oci +// podRmCmd deletes pods func podRmCmd(c *cliconfig.PodRmValues) error { if err := checkMutuallyExclusiveFlags(&c.PodmanCommand); err != nil { return err } - - runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand) + runtime, err := adapter.GetRuntime(&c.PodmanCommand) if err != nil { return errors.Wrapf(err, "could not get runtime") } defer runtime.Shutdown(false) + podRmIds, podRmErrors := runtime.RemovePods(getContext(), c) + for _, p := range podRmIds { + fmt.Println(p) + } + if len(podRmErrors) == 0 { + return nil + } + // Grab the last error + lastError := podRmErrors[len(podRmErrors)-1] + // Remove the last error from the error slice + podRmErrors = podRmErrors[:len(podRmErrors)-1] - ctx := getContext() - force := c.Force - - // getPodsFromContext returns an error when a requested pod - // isn't found. The only fatal error scenerio is when there are no pods - // in which case the following loop will be skipped. - pods, lastError := getPodsFromContext(&c.PodmanCommand, runtime) - - for _, pod := range pods { - err = runtime.RemovePod(ctx, pod, force, force) - if err != nil { - if lastError != nil { - logrus.Errorf("%q", lastError) - } - lastError = errors.Wrapf(err, "failed to delete pod %v", pod.ID()) - } else { - fmt.Println(pod.ID()) - } + for _, err := range podRmErrors { + logrus.Errorf("%q", err) } return lastError } diff --git a/cmd/podman/varlink/io.podman.varlink b/cmd/podman/varlink/io.podman.varlink index cae77e5b3..53c21cb21 100644 --- a/cmd/podman/varlink/io.podman.varlink +++ b/cmd/podman/varlink/io.podman.varlink @@ -1102,6 +1102,8 @@ method VolumesPrune() -> (prunedNames: []string, prunedErrors: []string) method ImageSave(options: ImageSaveOptions) -> (reply: MoreResponse) +method GetPodsByContext(all: bool, latest: bool, args: []string) -> (pods: []string) + # ImageNotFound means the image could not be found by the provided name or ID in local storage. error ImageNotFound (id: string) diff --git a/libpod/adapter/pods.go b/libpod/adapter/pods.go new file mode 100644 index 000000000..59642c42e --- /dev/null +++ b/libpod/adapter/pods.go @@ -0,0 +1,38 @@ +// +build !remoteclient + +package adapter + +import ( + "context" + "github.com/containers/libpod/libpod/adapter/shortcuts" + + "github.com/containers/libpod/cmd/podman/cliconfig" + "github.com/containers/libpod/libpod" +) + +// Pod ... +type Pod struct { + *libpod.Pod +} + +// RemovePods ... +func (r *LocalRuntime) RemovePods(ctx context.Context, cli *cliconfig.PodRmValues) ([]string, []error) { + var ( + errs []error + podids []string + ) + pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime) + if err != nil { + errs = append(errs, err) + return nil, errs + } + + for _, p := range pods { + if err := r.RemovePod(ctx, p, cli.Force, cli.Force); err != nil { + errs = append(errs, err) + } else { + podids = append(podids, p.ID()) + } + } + return podids, errs +} diff --git a/libpod/adapter/pods_remote.go b/libpod/adapter/pods_remote.go new file mode 100644 index 000000000..3fb147f48 --- /dev/null +++ b/libpod/adapter/pods_remote.go @@ -0,0 +1,44 @@ +// +build remoteclient + +package adapter + +import ( + "context" + + "github.com/containers/libpod/cmd/podman/cliconfig" + "github.com/containers/libpod/cmd/podman/varlink" + "github.com/containers/libpod/libpod" +) + +// Pod ... +type Pod struct { + remotepod +} + +type remotepod struct { + config *libpod.PodConfig + state *libpod.PodInspectState + Runtime *LocalRuntime +} + +func (r *LocalRuntime) RemovePods(ctx context.Context, cli *cliconfig.PodRmValues) ([]string, []error) { + var ( + rmErrs []error + rmPods []string + ) + podIDs, err := iopodman.GetPodsByContext().Call(r.Conn, cli.All, cli.Latest, cli.InputArgs) + if err != nil { + rmErrs = append(rmErrs, err) + return nil, rmErrs + } + + for _, p := range podIDs { + reply, err := iopodman.RemovePod().Call(r.Conn, p, cli.Force) + if err != nil { + rmErrs = append(rmErrs, err) + } else { + rmPods = append(rmPods, reply) + } + } + return rmPods, rmErrs +} diff --git a/libpod/adapter/shortcuts/shortcuts.go b/libpod/adapter/shortcuts/shortcuts.go new file mode 100644 index 000000000..0633399ae --- /dev/null +++ b/libpod/adapter/shortcuts/shortcuts.go @@ -0,0 +1,27 @@ +package shortcuts + +import "github.com/containers/libpod/libpod" + +// GetPodsByContext gets pods whether all, latest, or a slice of names/ids +func GetPodsByContext(all, latest bool, pods []string, runtime *libpod.Runtime) ([]*libpod.Pod, error) { + var outpods []*libpod.Pod + if all { + return runtime.GetAllPods() + } + if latest { + p, err := runtime.GetLatestPod() + if err != nil { + return nil, err + } + outpods = append(outpods, p) + return outpods, nil + } + for _, p := range pods { + pod, err := runtime.LookupPod(p) + if err != nil { + return nil, err + } + outpods = append(outpods, pod) + } + return outpods, nil +} diff --git a/pkg/varlinkapi/pods.go b/pkg/varlinkapi/pods.go index 6e758786a..1dd864d03 100644 --- a/pkg/varlinkapi/pods.go +++ b/pkg/varlinkapi/pods.go @@ -2,6 +2,7 @@ package varlinkapi import ( "encoding/json" + "github.com/containers/libpod/libpod/adapter/shortcuts" "github.com/containers/libpod/pkg/rootless" "syscall" @@ -271,3 +272,17 @@ func (i *LibpodAPI) GetPodStats(call iopodman.VarlinkCall, name string) error { } return call.ReplyGetPodStats(pod.ID(), containersStats) } + +// GetPodsByContext returns a slice of pod ids based on all, latest, or a list +func (i *LibpodAPI) GetPodsByContext(call iopodman.VarlinkCall, all, latest bool, input []string) error { + var podids []string + + pods, err := shortcuts.GetPodsByContext(all, latest, input, i.Runtime) + if err != nil { + return call.ReplyErrorOccurred(err.Error()) + } + for _, p := range pods { + podids = append(podids, p.ID()) + } + return call.ReplyGetPodsByContext(podids) +} -- cgit v1.2.3-54-g00ecf