diff options
-rwxr-xr-x | API.md | 7 | ||||
-rw-r--r-- | cmd/podman/commands.go | 2 | ||||
-rw-r--r-- | cmd/podman/main.go | 1 | ||||
-rw-r--r-- | cmd/podman/pod.go | 6 | ||||
-rw-r--r-- | cmd/podman/pod_rm.go | 38 | ||||
-rw-r--r-- | cmd/podman/varlink/io.podman.varlink | 2 | ||||
-rw-r--r-- | libpod/adapter/pods.go | 38 | ||||
-rw-r--r-- | libpod/adapter/pods_remote.go | 44 | ||||
-rw-r--r-- | libpod/adapter/shortcuts/shortcuts.go | 27 | ||||
-rw-r--r-- | pkg/rootless/rootless_linux.c | 35 | ||||
-rw-r--r-- | pkg/varlinkapi/pods.go | 15 |
11 files changed, 190 insertions, 25 deletions
@@ -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" } ~~~ +### <a name="GetPodsByContext"></a>func GetPodsByContext +<div style="background-color: #E8E8E8; padding: 15px; margin: 10px; border-radius: 10px;"> + +method GetPodsByContext(all: [bool](https://godoc.org/builtin#bool), latest: [bool](https://godoc.org/builtin#bool), args: [[]string](#[]string)) [[]string](#[]string)</div> + ### <a name="GetVersion"></a>func GetVersion <div style="background-color: #E8E8E8; padding: 15px; margin: 10px; border-radius: 10px;"> 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/rootless/rootless_linux.c b/pkg/rootless/rootless_linux.c index 279a03d3f..acc5d6b2a 100644 --- a/pkg/rootless/rootless_linux.c +++ b/pkg/rootless/rootless_linux.c @@ -12,6 +12,7 @@ #include <fcntl.h> #include <sys/wait.h> #include <string.h> +#include <stdbool.h> static const char *_max_user_namespaces = "/proc/sys/user/max_user_namespaces"; static const char *_unprivileged_user_namespaces = "/proc/sys/kernel/unprivileged_userns_clone"; @@ -186,6 +187,18 @@ reexec_in_user_namespace (int ready) pid_t ppid = getpid (); char **argv; char uid[16]; + char *listen_fds = NULL; + char *listen_pid = NULL; + bool do_socket_activation = false; + + listen_pid = getenv("LISTEN_PID"); + listen_fds = getenv("LISTEN_FDS"); + + if (listen_pid != NULL && listen_fds != NULL) { + if (strtol(listen_pid, NULL, 10) == getpid()) { + do_socket_activation = true; + } + } sprintf (uid, "%d", geteuid ()); @@ -197,8 +210,22 @@ reexec_in_user_namespace (int ready) check_proc_sys_userns_file (_max_user_namespaces); check_proc_sys_userns_file (_unprivileged_user_namespaces); } - if (pid) + if (pid) { + if (do_socket_activation) { + long num_fds; + num_fds = strtol(listen_fds, NULL, 10); + if (num_fds != LONG_MIN && num_fds != LONG_MAX) { + long i; + for (i = 0; i < num_fds; i++) { + close(3+i); + } + } + unsetenv("LISTEN_PID"); + unsetenv("LISTEN_FDS"); + unsetenv("LISTEN_FDNAMES"); + } return pid; + } argv = get_cmd_line_args (ppid); if (argv == NULL) @@ -207,6 +234,12 @@ reexec_in_user_namespace (int ready) _exit (EXIT_FAILURE); } + if (do_socket_activation) { + char s[32]; + sprintf(s, "%d", getpid()); + setenv("LISTEN_PID", s, true); + } + setenv ("_LIBPOD_USERNS_CONFIGURED", "init", 1); setenv ("_LIBPOD_ROOTLESS_UID", uid, 1); 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) +} |