2017-10-09 20:35:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-10-27 00:26:04 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libpod"
|
2017-10-09 20:35:59 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-10-27 00:26:04 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-10-09 20:35:59 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var runDescription = "Runs a command in a new container from the given image"
|
|
|
|
|
|
|
|
var runCommand = cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "run a command in a new container",
|
|
|
|
Description: runDescription,
|
|
|
|
Flags: createFlags,
|
|
|
|
Action: runCmd,
|
|
|
|
ArgsUsage: "IMAGE [COMMAND [ARG...]]",
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCmd(c *cli.Context) error {
|
|
|
|
if err := validateFlags(c, createFlags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-30 18:31:31 +00:00
|
|
|
runtime, err := getRuntime(c)
|
|
|
|
|
2017-10-09 20:35:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error creating libpod runtime")
|
|
|
|
}
|
|
|
|
|
2017-10-27 00:26:04 +00:00
|
|
|
createConfig, err := parseCreateOpts(c, runtime)
|
2017-10-09 20:35:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-27 00:26:04 +00:00
|
|
|
createImage := runtime.NewImage(createConfig.image)
|
|
|
|
|
|
|
|
if !createImage.HasImageLocal() {
|
|
|
|
// The image wasnt found by the user input'd name or its fqname
|
|
|
|
// Pull the image
|
|
|
|
fmt.Printf("Trying to pull %s...", createImage.PullName)
|
|
|
|
createImage.Pull()
|
|
|
|
}
|
|
|
|
|
2017-10-09 20:35:59 +00:00
|
|
|
runtimeSpec, err := createConfigToOCISpec(createConfig)
|
2017-10-27 00:26:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-31 20:43:12 +00:00
|
|
|
defer runtime.Shutdown(false)
|
2017-10-30 18:31:31 +00:00
|
|
|
logrus.Debug("spec is ", runtimeSpec)
|
2017-10-27 00:26:04 +00:00
|
|
|
|
|
|
|
imageName, err := createImage.GetFQName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debug("imageName is ", imageName)
|
|
|
|
|
|
|
|
imageID, err := createImage.GetImageID()
|
2017-10-09 20:35:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-27 00:26:04 +00:00
|
|
|
logrus.Debug("imageID is ", imageID)
|
|
|
|
|
|
|
|
options, err := createConfig.GetContainerCreateOptions(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "unable to parse new container options")
|
|
|
|
}
|
2017-10-09 20:35:59 +00:00
|
|
|
|
2017-10-27 00:26:04 +00:00
|
|
|
// Gather up the options for NewContainer which consist of With... funcs
|
|
|
|
options = append(options, libpod.WithRootFSFromImage(imageID, imageName, false))
|
|
|
|
ctr, err := runtime.NewContainer(runtimeSpec, options...)
|
2017-10-09 20:35:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-27 00:26:04 +00:00
|
|
|
logrus.Debug("new container created ", ctr.ID())
|
|
|
|
if err := ctr.Create(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-30 18:31:31 +00:00
|
|
|
logrus.Debug("container storage created for %q", ctr.ID())
|
2017-10-09 20:35:59 +00:00
|
|
|
|
2017-10-30 18:31:31 +00:00
|
|
|
if c.String("cidfile") != "" {
|
|
|
|
libpod.WriteFile(ctr.ID(), c.String("cidfile"))
|
2017-10-27 00:26:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Start the container
|
|
|
|
if err := ctr.Start(); err != nil {
|
2017-10-30 18:31:31 +00:00
|
|
|
return errors.Wrapf(err, "unable to start container %q", ctr.ID())
|
2017-10-27 00:26:04 +00:00
|
|
|
}
|
|
|
|
logrus.Debug("started container ", ctr.ID())
|
|
|
|
if createConfig.tty {
|
|
|
|
// Attach to the running container
|
|
|
|
logrus.Debug("trying to attach to the container %s", ctr.ID())
|
2017-10-30 18:31:31 +00:00
|
|
|
if err := ctr.Attach(false, c.String("detach-keys")); err != nil {
|
2017-10-27 00:26:04 +00:00
|
|
|
return errors.Wrapf(err, "unable to attach to container %s", ctr.ID())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s\n", ctr.ID())
|
|
|
|
}
|
2017-10-09 20:35:59 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|