summaryrefslogtreecommitdiff
path: root/cmd/podmanV2
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-04-07 13:01:47 +0200
committerGitHub <noreply@github.com>2020-04-07 13:01:47 +0200
commitc0e29b4a31e330927b7a980209b2aae192f9bafe (patch)
treebf854e19f042700c1a4b3cc3c02379198b0bb2d6 /cmd/podmanV2
parent64b6a197339e0436168e254ef9caf674ee9ff932 (diff)
parent1bfb96b54010b048e9bea155a75d150f4bf8853c (diff)
downloadpodman-c0e29b4a31e330927b7a980209b2aae192f9bafe.tar.gz
podman-c0e29b4a31e330927b7a980209b2aae192f9bafe.tar.bz2
podman-c0e29b4a31e330927b7a980209b2aae192f9bafe.zip
Merge pull request #5734 from baude/v2run
v2podman run
Diffstat (limited to 'cmd/podmanV2')
-rw-r--r--cmd/podmanV2/common/create.go1
-rw-r--r--cmd/podmanV2/containers/run.go125
2 files changed, 125 insertions, 1 deletions
diff --git a/cmd/podmanV2/common/create.go b/cmd/podmanV2/common/create.go
index f81d021c8..e2eb8cbda 100644
--- a/cmd/podmanV2/common/create.go
+++ b/cmd/podmanV2/common/create.go
@@ -29,7 +29,6 @@ func getDefaultContainerConfig() *config.Config {
}
func GetCreateFlags(cf *ContainerCLIOpts) *pflag.FlagSet {
- //createFlags := c.Flags()
createFlags := pflag.FlagSet{}
createFlags.StringSliceVar(
&cf.Annotation,
diff --git a/cmd/podmanV2/containers/run.go b/cmd/podmanV2/containers/run.go
new file mode 100644
index 000000000..bd90aee2f
--- /dev/null
+++ b/cmd/podmanV2/containers/run.go
@@ -0,0 +1,125 @@
+package containers
+
+import (
+ "fmt"
+ "os"
+ "strings"
+
+ "github.com/sirupsen/logrus"
+
+ "github.com/containers/libpod/pkg/domain/entities"
+
+ "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/specgen"
+ "github.com/pkg/errors"
+ "github.com/spf13/cobra"
+)
+
+var (
+ runDescription = "Runs a command in a new container from the given image"
+ runCommand = &cobra.Command{
+ Use: "run [flags] IMAGE [COMMAND [ARG...]]",
+ Short: "Run a command in a new container",
+ Long: runDescription,
+ PreRunE: preRunE,
+ RunE: run,
+ Example: `podman run imageID ls -alF /etc
+ podman run --network=host imageID dnf -y install java
+ podman run --volume /var/hostdir:/var/ctrdir -i -t fedora /bin/bash`,
+ }
+)
+
+var (
+ runOpts = entities.ContainerRunOptions{
+ OutputStream: os.Stdout,
+ InputStream: os.Stdin,
+ ErrorStream: os.Stderr,
+ }
+ runRmi bool
+)
+
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Mode: []entities.EngineMode{entities.ABIMode},
+ Command: runCommand,
+ })
+ flags := runCommand.Flags()
+ flags.AddFlagSet(common.GetCreateFlags(&cliVals))
+ flags.AddFlagSet(common.GetNetFlags())
+ flags.SetNormalizeFunc(common.AliasFlags)
+ flags.BoolVar(&runOpts.SigProxy, "sig-proxy", true, "Proxy received signals to the process")
+ flags.BoolVar(&runRmi, "rmi", false, "Remove container image unless used by other containers")
+ if registry.IsRemote() {
+ _ = flags.MarkHidden("authfile")
+ }
+}
+
+func run(cmd *cobra.Command, args []string) error {
+ var (
+ err error
+ )
+ cliVals.Net, err = common.NetFlagsToNetOptions(cmd)
+ if err != nil {
+ return err
+ }
+ if af := cliVals.Authfile; len(af) > 0 {
+ if _, err := os.Stat(af); err != nil {
+ return errors.Wrapf(err, "error checking authfile path %s", af)
+ }
+ }
+ runOpts.Rm = cliVals.Rm
+ if err := createInit(cmd); err != nil {
+ return err
+ }
+
+ // If -i is not set, clear stdin
+ if !cliVals.Interactive {
+ runOpts.InputStream = nil
+ }
+
+ // If attach is set, clear stdin/stdout/stderr and only attach requested
+ if cmd.Flag("attach").Changed {
+ runOpts.OutputStream = nil
+ runOpts.ErrorStream = nil
+ if !cliVals.Interactive {
+ runOpts.InputStream = nil
+ }
+
+ for _, stream := range cliVals.Attach {
+ switch strings.ToLower(stream) {
+ case "stdout":
+ runOpts.OutputStream = os.Stdout
+ case "stderr":
+ runOpts.ErrorStream = os.Stderr
+ case "stdin":
+ runOpts.InputStream = os.Stdin
+ default:
+ return errors.Wrapf(define.ErrInvalidArg, "invalid stream %q for --attach - must be one of stdin, stdout, or stderr", stream)
+ }
+ }
+ }
+ runOpts.Detach = cliVals.Detach
+ runOpts.DetachKeys = cliVals.DetachKeys
+ s := specgen.NewSpecGenerator(args[0])
+ if err := common.FillOutSpecGen(s, &cliVals, args); err != nil {
+ return err
+ }
+ runOpts.Spec = s
+ report, err := registry.ContainerEngine().ContainerRun(registry.GetContext(), runOpts)
+ if err != nil {
+ return err
+ }
+ if cliVals.Detach {
+ fmt.Println(report.Id)
+ }
+ registry.SetExitCode(report.ExitCode)
+ if runRmi {
+ _, err := registry.ImageEngine().Delete(registry.GetContext(), []string{report.Id}, entities.ImageDeleteOptions{})
+ if err != nil {
+ logrus.Errorf("%s", errors.Wrapf(err, "failed removing image"))
+ }
+ }
+ return nil
+}