diff options
Diffstat (limited to 'cmd/podman/run.go')
-rw-r--r-- | cmd/podman/run.go | 94 |
1 files changed, 51 insertions, 43 deletions
diff --git a/cmd/podman/run.go b/cmd/podman/run.go index 3ef546940..bea9b1743 100644 --- a/cmd/podman/run.go +++ b/cmd/podman/run.go @@ -8,49 +8,64 @@ import ( "strconv" "strings" + "github.com/containers/libpod/cmd/podman/cliconfig" "github.com/containers/libpod/cmd/podman/libpodruntime" "github.com/containers/libpod/libpod" "github.com/containers/libpod/pkg/rootless" + opentracing "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/sirupsen/logrus" - "github.com/urfave/cli" + "github.com/spf13/cobra" ) -var runDescription = "Runs a command in a new container from the given image" - -var runFlags []cli.Flag = append(createFlags, cli.BoolTFlag{ - Name: "sig-proxy", - Usage: "Proxy received signals to the process (default true)", -}) - -var runCommand = cli.Command{ - Name: "run", - Usage: "Run a command in a new container", - Description: runDescription, - Flags: sortFlags(runFlags), - Action: runCmd, - ArgsUsage: "IMAGE [COMMAND [ARG...]]", - HideHelp: true, - SkipArgReorder: true, - UseShortOptionHandling: true, - OnUsageError: usageErrorHandler, +var ( + runCommand cliconfig.RunValues + + runDescription = "Runs a command in a new container from the given image" + _runCommand = &cobra.Command{ + Use: "run", + Short: "Run a command in a new container", + Long: runDescription, + RunE: func(cmd *cobra.Command, args []string) error { + runCommand.InputArgs = args + runCommand.GlobalFlags = MainGlobalOpts + return runCmd(&runCommand) + }, + Example: `podman run imageID ls -alF /etc + podman run --net=host imageID dnf -y install java + podman run --volume /var/hostdir:/var/ctrdir -i -t fedora /bin/bash`, + } +) + +func init() { + runCommand.Command = _runCommand + runCommand.SetUsageTemplate(UsageTemplate()) + flags := runCommand.Flags() + flags.SetInterspersed(false) + flags.Bool("sig-proxy", true, "Proxy received signals to the process (default true)") + getCreateFlags(&runCommand.PodmanCommand) } -func runCmd(c *cli.Context) error { - if err := createInit(c); err != nil { +func runCmd(c *cliconfig.RunValues) error { + if c.Bool("trace") { + span, _ := opentracing.StartSpanFromContext(Ctx, "runCmd") + defer span.Finish() + } + + if err := createInit(&c.PodmanCommand); err != nil { return err } if os.Geteuid() != 0 { rootless.SetSkipStorageSetup(true) } - runtime, err := libpodruntime.GetRuntime(c) + runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand) if err != nil { return errors.Wrapf(err, "error creating libpod runtime") } defer runtime.Shutdown(false) - ctr, createConfig, err := createContainer(c, runtime) + ctr, createConfig, err := createContainer(&c.PodmanCommand, runtime) if err != nil { return err } @@ -65,7 +80,8 @@ func runCmd(c *cli.Context) error { ctx := getContext() // Handle detached start if createConfig.Detach { - if err := ctr.Start(ctx); err != nil { + // if the container was created as part of a pod, also start its dependencies, if any. + if err := ctr.Start(ctx, c.IsSet("pod")); err != nil { // This means the command did not exist exitCode = 127 if strings.Index(err.Error(), "permission denied") > -1 { @@ -110,14 +126,22 @@ func runCmd(c *cli.Context) error { } } } - if err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"), c.BoolT("sig-proxy"), true); err != nil { + // if the container was created as part of a pod, also start its dependencies, if any. + if err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"), c.Bool("sig-proxy"), true, c.IsSet("pod")); err != nil { + // We've manually detached from the container + // Do not perform cleanup, or wait for container exit code + // Just exit immediately + if errors.Cause(err) == libpod.ErrDetach { + exitCode = 0 + return nil + } // This means the command did not exist exitCode = 127 if strings.Index(err.Error(), "permission denied") > -1 { exitCode = 126 } if c.IsSet("rm") { - if deleteError := runtime.RemoveContainer(ctx, ctr, true); deleteError != nil { + if deleteError := runtime.RemoveContainer(ctx, ctr, true, false); deleteError != nil { logrus.Errorf("unable to remove container %s after failing to start and attach to it", ctr.ID()) } } @@ -140,28 +164,12 @@ func runCmd(c *cli.Context) error { exitCode = int(ecode) } - if createConfig.Rm { - return runtime.RemoveContainer(ctx, ctr, true) - } - - if err := ctr.Cleanup(ctx); err != nil { - // If the container has been removed already, no need to error on cleanup - // Also, if it was restarted, don't error either - if errors.Cause(err) == libpod.ErrNoSuchCtr || - errors.Cause(err) == libpod.ErrCtrRemoved || - errors.Cause(err) == libpod.ErrCtrStateInvalid { - return nil - } - - return err - } - return nil } // Read a container's exit file func readExitFile(runtimeTmp, ctrID string) (int, error) { - exitFile := filepath.Join(runtimeTmp, "exits", ctrID) + exitFile := filepath.Join(runtimeTmp, "exits", fmt.Sprintf("%s-old", ctrID)) logrus.Debugf("Attempting to read container %s exit code from file %s", ctrID, exitFile) |