summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel J Walsh <dwalsh@redhat.com>2018-09-13 12:16:14 -0400
committerDaniel J Walsh <dwalsh@redhat.com>2018-09-13 12:16:14 -0400
commitecec1a5430885baf96d2e3d6153c7454c41a4617 (patch)
tree5d18f24541094d6a5f775ccb3bfee36ec606bf17
parent61eda671eca96b6fa32369572d9b49850895d37b (diff)
downloadpodman-ecec1a5430885baf96d2e3d6153c7454c41a4617.tar.gz
podman-ecec1a5430885baf96d2e3d6153c7454c41a4617.tar.bz2
podman-ecec1a5430885baf96d2e3d6153c7454c41a4617.zip
Remove duplicate code between create.go and run.go
Create two new createInit for checking if the cotnainer is initialized correctly. createContainer which creates the actual container and containerConfig Also added libpodruntime.GetContainerRuntime to put common runtime code into separate function. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
-rw-r--r--cmd/podman/create.go73
-rw-r--r--cmd/podman/libpodruntime/runtime.go16
-rw-r--r--cmd/podman/run.go114
3 files changed, 57 insertions, 146 deletions
diff --git a/cmd/podman/create.go b/cmd/podman/create.go
index bc010d047..c246b7903 100644
--- a/cmd/podman/create.go
+++ b/cmd/podman/create.go
@@ -57,6 +57,26 @@ var createCommand = cli.Command{
}
func createCmd(c *cli.Context) error {
+ if err := createInit(c); err != nil {
+ return err
+ }
+
+ runtime, err := libpodruntime.GetContainerRuntime(c)
+ if err != nil {
+ return errors.Wrapf(err, "error creating libpod runtime")
+ }
+ defer runtime.Shutdown(false)
+
+ ctr, _, err := createContainer(c, runtime)
+ if err != nil {
+ return err
+ }
+
+ fmt.Printf("%s\n", ctr.ID())
+ return nil
+}
+
+func createInit(c *cli.Context) error {
// TODO should allow user to create based off a directory on the host not just image
// Need CLI support for this
@@ -83,63 +103,46 @@ func createCmd(c *cli.Context) error {
return errors.Errorf("image name or ID is required")
}
+ return nil
+}
+
+func createContainer(c *cli.Context, runtime *libpod.Runtime) (*libpod.Container, *cc.CreateConfig, error) {
+ rtc := runtime.GetConfig()
+ ctx := getContext()
rootfs := ""
if c.Bool("rootfs") {
rootfs = c.Args()[0]
}
- mappings, err := util.ParseIDMapping(c.StringSlice("uidmap"), c.StringSlice("gidmap"), c.String("subuidmap"), c.String("subgidmap"))
- if err != nil {
- return err
- }
- storageOpts, err := libpodruntime.GetDefaultStoreOptions()
- if err != nil {
- return err
- }
- storageOpts.UIDMap = mappings.UIDMap
- storageOpts.GIDMap = mappings.GIDMap
-
- if os.Geteuid() != 0 {
- rootless.SetSkipStorageSetup(true)
- }
-
- runtime, err := libpodruntime.GetRuntimeWithStorageOpts(c, &storageOpts)
- if err != nil {
- return errors.Wrapf(err, "error creating libpod runtime")
- }
- defer runtime.Shutdown(false)
-
- rtc := runtime.GetConfig()
- ctx := getContext()
-
imageName := ""
var data *inspect.ImageData = nil
+
if rootfs == "" && !rootless.SkipStorageSetup() {
newImage, err := runtime.ImageRuntime().New(ctx, c.Args()[0], rtc.SignaturePolicyPath, "", os.Stderr, nil, image.SigningOptions{}, false, false)
if err != nil {
- return err
+ return nil, nil, err
}
data, err = newImage.Inspect(ctx)
imageName = newImage.Names()[0]
}
createConfig, err := parseCreateOpts(ctx, c, runtime, imageName, data)
if err != nil {
- return err
+ return nil, nil, err
}
runtimeSpec, err := cc.CreateConfigToOCISpec(createConfig)
if err != nil {
- return err
+ return nil, nil, err
}
options, err := createConfig.GetContainerCreateOptions(runtime)
if err != nil {
- return err
+ return nil, nil, err
}
became, ret, err := joinOrCreateRootlessUserNamespace(createConfig, runtime)
if err != nil {
- return err
+ return nil, nil, err
}
if became {
os.Exit(ret)
@@ -147,27 +150,25 @@ func createCmd(c *cli.Context) error {
ctr, err := runtime.NewContainer(ctx, runtimeSpec, options...)
if err != nil {
- return err
+ return nil, nil, err
}
createConfigJSON, err := json.Marshal(createConfig)
if err != nil {
- return err
+ return nil, nil, err
}
if err := ctr.AddArtifact("create-config", createConfigJSON); err != nil {
- return err
+ return nil, nil, err
}
- logrus.Debug("new container created ", ctr.ID())
-
if c.String("cidfile") != "" {
err := libpod.WriteFile(ctr.ID(), c.String("cidfile"))
if err != nil {
logrus.Error(err)
}
}
- fmt.Printf("%s\n", ctr.ID())
- return nil
+ logrus.Debugf("New container created %q", ctr.ID())
+ return ctr, createConfig, nil
}
// Checks if a user-specified AppArmor profile is loaded, or loads the default profile if
diff --git a/cmd/podman/libpodruntime/runtime.go b/cmd/podman/libpodruntime/runtime.go
index b7281ed8c..be15d138d 100644
--- a/cmd/podman/libpodruntime/runtime.go
+++ b/cmd/podman/libpodruntime/runtime.go
@@ -7,6 +7,7 @@ import (
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/rootless"
+ "github.com/containers/libpod/pkg/util"
"github.com/containers/storage"
"github.com/pkg/errors"
"github.com/urfave/cli"
@@ -21,6 +22,21 @@ func GetRuntime(c *cli.Context) (*libpod.Runtime, error) {
return GetRuntimeWithStorageOpts(c, &storageOpts)
}
+// GetContainerRuntime generates a new libpod runtime configured by command line options for containers
+func GetContainerRuntime(c *cli.Context) (*libpod.Runtime, error) {
+ mappings, err := util.ParseIDMapping(c.StringSlice("uidmap"), c.StringSlice("gidmap"), c.String("subuidmap"), c.String("subgidmap"))
+ if err != nil {
+ return nil, err
+ }
+ storageOpts, err := GetDefaultStoreOptions()
+ if err != nil {
+ return nil, err
+ }
+ storageOpts.UIDMap = mappings.UIDMap
+ storageOpts.GIDMap = mappings.GIDMap
+ return GetRuntimeWithStorageOpts(c, &storageOpts)
+}
+
func GetRootlessStorageOpts() (storage.StoreOptions, error) {
var opts storage.StoreOptions
diff --git a/cmd/podman/run.go b/cmd/podman/run.go
index 3445daef5..42aa81cd5 100644
--- a/cmd/podman/run.go
+++ b/cmd/podman/run.go
@@ -1,7 +1,6 @@
package main
import (
- "encoding/json"
"fmt"
"io/ioutil"
"os"
@@ -11,11 +10,6 @@ import (
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/libpod"
- "github.com/containers/libpod/libpod/image"
- "github.com/containers/libpod/pkg/inspect"
- "github.com/containers/libpod/pkg/rootless"
- cc "github.com/containers/libpod/pkg/spec"
- "github.com/containers/libpod/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
@@ -42,108 +36,21 @@ var runCommand = cli.Command{
}
func runCmd(c *cli.Context) error {
- var imageName string
-
- // Docker-compatibility: the "-h" flag for run/create is reserved for
- // the hostname (see https://github.com/containers/libpod/issues/1367).
- if c.Bool("help") {
- cli.ShowCommandHelpAndExit(c, "run", 0)
- }
-
- if err := validateFlags(c, createFlags); err != nil {
- return err
- }
-
- if c.String("cidfile") != "" {
- if _, err := os.Stat(c.String("cidfile")); err == nil {
- return errors.Errorf("container id file exists. ensure another container is not using it or delete %s", c.String("cidfile"))
- }
- if err := libpod.WriteFile("", c.String("cidfile")); err != nil {
- return errors.Wrapf(err, "unable to write cidfile %s", c.String("cidfile"))
- }
- }
-
- storageOpts, err := libpodruntime.GetDefaultStoreOptions()
- if err != nil {
- return err
- }
- mappings, err := util.ParseIDMapping(c.StringSlice("uidmap"), c.StringSlice("gidmap"), c.String("subuidmap"), c.String("subgidmap"))
- if err != nil {
+ if err := createInit(c); err != nil {
return err
}
- storageOpts.UIDMap = mappings.UIDMap
- storageOpts.GIDMap = mappings.GIDMap
- if os.Geteuid() != 0 {
- rootless.SetSkipStorageSetup(true)
- }
-
- runtime, err := libpodruntime.GetRuntimeWithStorageOpts(c, &storageOpts)
+ runtime, err := libpodruntime.GetContainerRuntime(c)
if err != nil {
return errors.Wrapf(err, "error creating libpod runtime")
}
defer runtime.Shutdown(false)
- if len(c.Args()) < 1 {
- return errors.Errorf("image name or ID is required")
- }
-
- rootfs := ""
- if c.Bool("rootfs") {
- rootfs = c.Args()[0]
- }
-
- ctx := getContext()
- rtc := runtime.GetConfig()
-
- var newImage *image.Image = nil
- var data *inspect.ImageData = nil
- if rootfs == "" && !rootless.SkipStorageSetup() {
- newImage, err = runtime.ImageRuntime().New(ctx, c.Args()[0], rtc.SignaturePolicyPath, "", os.Stderr, nil, image.SigningOptions{}, false, false)
- if err != nil {
- return errors.Wrapf(err, "unable to find image")
- }
-
- data, err = newImage.Inspect(ctx)
- if err != nil {
- return err
- }
- if len(newImage.Names()) < 1 {
- imageName = newImage.ID()
- } else {
- imageName = newImage.Names()[0]
- }
- }
- createConfig, err := parseCreateOpts(ctx, c, runtime, imageName, data)
- if err != nil {
- return err
- }
-
- runtimeSpec, err := cc.CreateConfigToOCISpec(createConfig)
- if err != nil {
- return err
- }
-
- options, err := createConfig.GetContainerCreateOptions(runtime)
- if err != nil {
- return err
- }
-
- became, ret, err := joinOrCreateRootlessUserNamespace(createConfig, runtime)
- if err != nil {
- return err
- }
- if became {
- os.Exit(ret)
- }
-
- ctr, err := runtime.NewContainer(ctx, runtimeSpec, options...)
+ ctr, createConfig, err := createContainer(c, runtime)
if err != nil {
return err
}
- logrus.Debugf("New container created %q", ctr.ID())
-
if logrus.GetLevel() == logrus.DebugLevel {
cgroupPath, err := ctr.CGroupPath()
if err == nil {
@@ -151,20 +58,7 @@ func runCmd(c *cli.Context) error {
}
}
- createConfigJSON, err := json.Marshal(createConfig)
- if err != nil {
- return err
- }
- if err := ctr.AddArtifact("create-config", createConfigJSON); err != nil {
- return err
- }
-
- if c.String("cidfile") != "" {
- if err := libpod.WriteFile(ctr.ID(), c.String("cidfile")); err != nil {
- logrus.Error(err)
- }
- }
-
+ ctx := getContext()
// Handle detached start
if createConfig.Detach {
if err := ctr.Start(ctx); err != nil {