diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | cmd/kpod/main.go | 1 | ||||
-rw-r--r-- | cmd/kpod/spec.go | 4 | ||||
-rw-r--r-- | cmd/kpod/start.go | 125 | ||||
-rw-r--r-- | completions/bash/kpod | 15 | ||||
-rw-r--r-- | docs/kpod-start.1.md | 45 | ||||
-rw-r--r-- | docs/kpod.1.md | 3 | ||||
-rw-r--r-- | libpod/container_attach.go | 20 | ||||
-rw-r--r-- | test/kpod_start.bats | 50 |
9 files changed, 254 insertions, 10 deletions
@@ -56,6 +56,7 @@ libpod is currently in active development. | [kpod-rm(1)](/docs/kpod-rm.1.md) | Removes one or more containers |[![...](/docs/play.png)](https://asciinema.org/a/7EMk22WrfGtKWmgHJX9Nze1Qp)| | [kpod-rmi(1)](/docs/kpod-rmi.1.md) | Removes one or more images |[![...](/docs/play.png)](https://asciinema.org/a/133799)| | [kpod-save(1)](/docs/kpod-save.1.md) | Saves an image to an archive |[![...](/docs/play.png)](https://asciinema.org/a/kp8kOaexEhEa20P1KLZ3L5X4g)| +| [kpod-start(1)](/docs/kpod-start.1.md) | Starts one or more containers | [kpod-stats(1)](/docs/kpod-stats.1.md) | Display a live stream of one or more containers' resource usage statistics|| | [kpod-stop(1)](/docs/kpod-stop.1.md) | Stops one or more running containers || | [kpod-tag(1)](/docs/kpod-tag.1.md) | Add an additional name to a local image |[![...](/docs/play.png)](https://asciinema.org/a/133803)| diff --git a/cmd/kpod/main.go b/cmd/kpod/main.go index 97d942a3c..a4bb12475 100644 --- a/cmd/kpod/main.go +++ b/cmd/kpod/main.go @@ -57,6 +57,7 @@ func main() { rmiCommand, runCommand, saveCommand, + startCommand, statsCommand, stopCommand, tagCommand, diff --git a/cmd/kpod/spec.go b/cmd/kpod/spec.go index b19d4d33a..8d9189a0d 100644 --- a/cmd/kpod/spec.go +++ b/cmd/kpod/spec.go @@ -319,8 +319,8 @@ func (c *createConfig) CreateBlockIO() (spec.LinuxBlockIO, error) { // GetAnnotations returns the all the annotations for the container func (c *createConfig) GetAnnotations() map[string]string { a := getDefaultAnnotations() - // TODO - // Which annotations do we want added by default + // TODO - Which annotations do we want added by default + // TODO - This should be added to the DB long term if c.tty { a["io.kubernetes.cri-o.TTY"] = "true" } diff --git a/cmd/kpod/start.go b/cmd/kpod/start.go new file mode 100644 index 000000000..7f9918fa2 --- /dev/null +++ b/cmd/kpod/start.go @@ -0,0 +1,125 @@ +package main + +import ( + "fmt" + "sync" + + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "github.com/urfave/cli" + "os" + "strconv" +) + +var ( + startFlags = []cli.Flag{ + cli.BoolFlag{ + Name: "attach, a", + Usage: "Attach container's STDOUT and STDERR", + }, + cli.StringFlag{ + Name: "detach-keys", + Usage: "Override the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _.", + }, + cli.BoolFlag{ + Name: "interactive, i", + Usage: "Keep STDIN open even if not attached", + }, + } + startDescription = ` + kpod start + + Starts one or more containers. The container name or ID can be used. +` + + startCommand = cli.Command{ + Name: "start", + Usage: "Start one or more containers", + Description: startDescription, + Flags: startFlags, + Action: startCmd, + ArgsUsage: "CONTAINER-NAME [CONTAINER-NAME ...]", + } +) + +func startCmd(c *cli.Context) error { + args := c.Args() + if len(args) < 1 { + return errors.Errorf("you must provide at least one container name or id") + } + + attach := c.Bool("attach") + + if len(args) > 1 && attach { + return errors.Errorf("you cannot start and attach multiple containers at once") + } + + if err := validateFlags(c, startFlags); err != nil { + return err + } + + runtime, err := getRuntime(c) + if err != nil { + return errors.Wrapf(err, "error creating libpod runtime") + } + defer runtime.Shutdown(false) + + var lastError error + for _, container := range args { + // Create a bool channel to track that the console socket attach + // is successful. + attached := make(chan bool) + // Create a waitgroup so we can sync and wait for all goroutines + // to finish before exiting main + var wg sync.WaitGroup + + ctr, err := runtime.LookupContainer(container) + if err != nil { + if lastError != nil { + fmt.Fprintln(os.Stderr, lastError) + } + lastError = errors.Wrapf(err, "unable to find container %s", container) + continue + } + + // We can only be interactive if both the config and the command-line say so + if c.Bool("interactive") && !ctr.Config().Stdin { + return errors.Errorf("the container was not created with the interactive option") + } + noStdIn := c.Bool("interactive") + tty, err := strconv.ParseBool(ctr.Spec().Annotations["io.kubernetes.cri-o.TTY"]) + if err != nil { + return errors.Wrapf(err, "unable to parse annotations in %s", ctr.ID()) + } + // We only get a terminal session if both a tty was specified in the spec and + // -a on the command-line was given. + if attach && tty { + // We increment the wg counter because we need to do the attach + wg.Add(1) + // Attach to the running container + go func() { + logrus.Debug("trying to attach to the container %s", ctr.ID()) + defer wg.Done() + if err := ctr.Attach(noStdIn, c.String("detach-keys"), attached); err != nil { + logrus.Errorf("unable to attach to container %s: %q", ctr.ID(), err) + } + }() + if !<-attached { + return errors.Errorf("unable to attach to container %s", ctr.ID()) + } + } + err = ctr.Start() + if err != nil { + if lastError != nil { + fmt.Fprintln(os.Stderr, lastError) + } + lastError = errors.Wrapf(err, "unable to start %s", container) + continue + } + if !attach { + fmt.Println(ctr.ID()) + } + wg.Wait() + } + return lastError +} diff --git a/completions/bash/kpod b/completions/bash/kpod index 0fb413fe8..93009e6af 100644 --- a/completions/bash/kpod +++ b/completions/bash/kpod @@ -1334,6 +1334,20 @@ _kpod_ps() { _complete_ "$options_with_args" "$boolean_options" } +_kpod_start() { + local options_with_args=" + --detach-keys + " + + local boolean_options=" + -h + --help + -a + --attach + -i + --interactive" + _complete_ "$options_with_args" "$boolean_options" +} _kpod_stop() { local options_with_args=" --timeout -t @@ -1450,6 +1464,7 @@ _kpod_kpod() { rmi run save + start stats stop tag diff --git a/docs/kpod-start.1.md b/docs/kpod-start.1.md new file mode 100644 index 000000000..d1c52a118 --- /dev/null +++ b/docs/kpod-start.1.md @@ -0,0 +1,45 @@ +% kpod(1) kpod-start - Stop one or more containers +% Brent Baude +# kpod-start "1" "November 2017" "kpod" + +## NAME +kpod start - Start one or more containers + +## SYNOPSIS +**kpod start [OPTIONS] CONTAINER [...]** + +## DESCRIPTION +Start one or more containers. You may use container IDs or names as input. The *attach* and *interactive* +options cannot be used to override the *--tty** and *--interactive* options from when the container +was created. + +## OPTIONS + +**--attach, -a** + +Attach container's STDOUT and STDERR. The default is false. This option cannot be used when +starting multiple containers. + +**--detach-keys** + +Override the key sequence for detaching a container. Format is a single character [a-Z] or +ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _. + +**--interactive, -i** + +Attach container's STDIN. The default is false. + + +## EXAMPLE + +kpod start mywebserver + +kpod start 860a4b23 5421ab4 + +kpod start -i -a 860a4b23 + +## SEE ALSO +kpod(1), kpod-create(1) + +## HISTORY +November 2018, Originally compiled by Brent Baude <bbaude@redhat.com> diff --git a/docs/kpod.1.md b/docs/kpod.1.md index f1b74b58f..0e5204bec 100644 --- a/docs/kpod.1.md +++ b/docs/kpod.1.md @@ -118,6 +118,9 @@ Run a command in a new container ### save Save an image to docker-archive or oci +## start +Starts one or more containers + ### stats Display a live stream of one or more containers' resource usage statistics diff --git a/libpod/container_attach.go b/libpod/container_attach.go index 7775e1a5d..bcfb6ec49 100644 --- a/libpod/container_attach.go +++ b/libpod/container_attach.go @@ -7,7 +7,7 @@ import ( "net" "os" "path/filepath" - "strconv" + //"strconv" "github.com/docker/docker/pkg/term" "github.com/pkg/errors" @@ -31,13 +31,17 @@ func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSi outputStream := os.Stdout errorStream := os.Stderr defer inputStream.Close() - tty, err := strconv.ParseBool(c.runningSpec.Annotations["io.kubernetes.cri-o.TTY"]) - if err != nil { - return errors.Wrapf(err, "unable to parse annotations in %s", c.ID) - } - if !tty { - return errors.Errorf("no tty available for %s", c.ID()) - } + + // TODO Renable this when tty/terminal discussion is had. + /* + tty, err := strconv.ParseBool(c.runningSpec.Annotations["io.kubernetes.cri-o.TTY"]) + if err != nil { + return errors.Wrapf(err, "unable to parse annotations in %s", c.ID) + } + if !tty { + return errors.Errorf("no tty available for %s", c.ID()) + } + */ if terminal.IsTerminal(int(inputStream.Fd())) { oldTermState, err := term.SaveState(inputStream.Fd()) diff --git a/test/kpod_start.bats b/test/kpod_start.bats new file mode 100644 index 000000000..74a7d7edf --- /dev/null +++ b/test/kpod_start.bats @@ -0,0 +1,50 @@ +#!/usr/bin/env bats + +load helpers + +function setup() { + copy_images +} + +function teardown() { + cleanup_test +} + +@test "start bogus container" { + run ${KPOD_BINARY} ${KPOD_OPTIONS} start 1234 + echo "$output" + [ "$status" -eq 1 ] +} + +@test "start single container by id" { + run ${KPOD_BINARY} ${KPOD_OPTIONS} create -d ${ALPINE} ls + ctr_id=${output} + run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} start $ctr_id + echo "$output" + [ "$status" -eq 0 ] +} + +@test "start single container by name" { + ${KPOD_BINARY} ${KPOD_OPTIONS} create -d --name foobar99 ${ALPINE} ls + run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} start foobar + echo "$output" + [ "$status" -eq 0 ] +} + +@test "start multiple containers" { + run ${KPOD_BINARY} ${KPOD_OPTIONS} create -d ${ALPINE} ls + ctr1_id=${output} + run ${KPOD_BINARY} ${KPOD_OPTIONS} create -d ${ALPINE} ls + ctr1_id2=${output} + run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} start $ctr1_id $ctr2_id + echo "$output" + [ "$status" -eq 0 ] +} + +@test "start multiple containers -- attach should fail" { + ${KPOD_BINARY} ${KPOD_OPTIONS} create --name foobar1 -d ${ALPINE} ls + ${KPOD_BINARY} ${KPOD_OPTIONS} create --name foobar2 -d ${ALPINE} ls + run ${KPOD_BINARY} ${KPOD_OPTIONS} start -a foobar1 foobar2 + echo "$output" + [ "$status" -eq 1 ] +} |