From 65d7f22720b1c5ba4b6bea1d96cd43bb5b04831d Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 28 Apr 2020 11:41:53 +0200 Subject: cmd, podman: handle --pod new:POD Signed-off-by: Giuseppe Scrivano --- cmd/podman/containers/create.go | 28 ++++++++++++++++++++++++++++ cmd/podman/containers/run.go | 4 ++++ 2 files changed, 32 insertions(+) (limited to 'cmd') diff --git a/cmd/podman/containers/create.go b/cmd/podman/containers/create.go index 7eed84f29..9e2d47d21 100644 --- a/cmd/podman/containers/create.go +++ b/cmd/podman/containers/create.go @@ -1,8 +1,10 @@ package containers import ( + "context" "fmt" "os" + "strings" "github.com/containers/common/pkg/config" "github.com/containers/libpod/cmd/podman/common" @@ -105,6 +107,10 @@ func create(cmd *cobra.Command, args []string) error { return err } + if _, err := createPodIfNecessary(s); err != nil { + return err + } + report, err := registry.ContainerEngine().ContainerCreate(registry.GetContext(), s) if err != nil { return err @@ -205,3 +211,25 @@ func openCidFile(cidfile string) (*os.File, error) { } return cidFile, nil } + +// createPodIfNecessary automatically creates a pod when requested. if the pod name +// has the form new:ID, the pod ID is created and the name in the spec generator is replaced +// with ID. +func createPodIfNecessary(s *specgen.SpecGenerator) (*entities.PodCreateReport, error) { + if !strings.HasPrefix(s.Pod, "new:") { + return nil, nil + } + podName := strings.Replace(s.Pod, "new:", "", 1) + if len(podName) < 1 { + return nil, errors.Errorf("new pod name must be at least one character") + } + createOptions := entities.PodCreateOptions{ + Name: podName, + Infra: true, + Net: &entities.NetOptions{ + PublishPorts: s.PortMappings, + }, + } + s.Pod = podName + return registry.ContainerEngine().PodCreate(context.Background(), createOptions) +} diff --git a/cmd/podman/containers/run.go b/cmd/podman/containers/run.go index e3fe4cd0b..d0180840b 100644 --- a/cmd/podman/containers/run.go +++ b/cmd/podman/containers/run.go @@ -144,6 +144,10 @@ func run(cmd *cobra.Command, args []string) error { } runOpts.Spec = s + if _, err := createPodIfNecessary(s); err != nil { + return err + } + report, err := registry.ContainerEngine().ContainerRun(registry.GetContext(), runOpts) // report.ExitCode is set by ContainerRun even it it returns an error if report != nil { -- cgit v1.2.3-54-g00ecf