kpod_start
Starts one or more containers. Signed-off-by: baude <bbaude@redhat.com> Closes: #83 Approved by: rhatdan
This commit is contained in:
parent
ad255533d4
commit
742475885e
|
|
@ -56,6 +56,7 @@ libpod is currently in active development.
|
||||||
| [kpod-rm(1)](/docs/kpod-rm.1.md) | Removes one or more containers |[](https://asciinema.org/a/7EMk22WrfGtKWmgHJX9Nze1Qp)|
|
| [kpod-rm(1)](/docs/kpod-rm.1.md) | Removes one or more containers |[](https://asciinema.org/a/7EMk22WrfGtKWmgHJX9Nze1Qp)|
|
||||||
| [kpod-rmi(1)](/docs/kpod-rmi.1.md) | Removes one or more images |[](https://asciinema.org/a/133799)|
|
| [kpod-rmi(1)](/docs/kpod-rmi.1.md) | Removes one or more images |[](https://asciinema.org/a/133799)|
|
||||||
| [kpod-save(1)](/docs/kpod-save.1.md) | Saves an image to an archive |[](https://asciinema.org/a/kp8kOaexEhEa20P1KLZ3L5X4g)|
|
| [kpod-save(1)](/docs/kpod-save.1.md) | Saves an image to an archive |[](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-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-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 |[](https://asciinema.org/a/133803)|
|
| [kpod-tag(1)](/docs/kpod-tag.1.md) | Add an additional name to a local image |[](https://asciinema.org/a/133803)|
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ func main() {
|
||||||
rmiCommand,
|
rmiCommand,
|
||||||
runCommand,
|
runCommand,
|
||||||
saveCommand,
|
saveCommand,
|
||||||
|
startCommand,
|
||||||
statsCommand,
|
statsCommand,
|
||||||
stopCommand,
|
stopCommand,
|
||||||
tagCommand,
|
tagCommand,
|
||||||
|
|
|
||||||
|
|
@ -319,8 +319,8 @@ func (c *createConfig) CreateBlockIO() (spec.LinuxBlockIO, error) {
|
||||||
// GetAnnotations returns the all the annotations for the container
|
// GetAnnotations returns the all the annotations for the container
|
||||||
func (c *createConfig) GetAnnotations() map[string]string {
|
func (c *createConfig) GetAnnotations() map[string]string {
|
||||||
a := getDefaultAnnotations()
|
a := getDefaultAnnotations()
|
||||||
// TODO
|
// TODO - Which annotations do we want added by default
|
||||||
// Which annotations do we want added by default
|
// TODO - This should be added to the DB long term
|
||||||
if c.tty {
|
if c.tty {
|
||||||
a["io.kubernetes.cri-o.TTY"] = "true"
|
a["io.kubernetes.cri-o.TTY"] = "true"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -1334,6 +1334,20 @@ _kpod_ps() {
|
||||||
_complete_ "$options_with_args" "$boolean_options"
|
_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() {
|
_kpod_stop() {
|
||||||
local options_with_args="
|
local options_with_args="
|
||||||
--timeout -t
|
--timeout -t
|
||||||
|
|
@ -1450,6 +1464,7 @@ _kpod_kpod() {
|
||||||
rmi
|
rmi
|
||||||
run
|
run
|
||||||
save
|
save
|
||||||
|
start
|
||||||
stats
|
stats
|
||||||
stop
|
stop
|
||||||
tag
|
tag
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
@ -118,6 +118,9 @@ Run a command in a new container
|
||||||
### save
|
### save
|
||||||
Save an image to docker-archive or oci
|
Save an image to docker-archive or oci
|
||||||
|
|
||||||
|
## start
|
||||||
|
Starts one or more containers
|
||||||
|
|
||||||
### stats
|
### stats
|
||||||
Display a live stream of one or more containers' resource usage statistics
|
Display a live stream of one or more containers' resource usage statistics
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
//"strconv"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/term"
|
"github.com/docker/docker/pkg/term"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
@ -31,13 +31,17 @@ func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSi
|
||||||
outputStream := os.Stdout
|
outputStream := os.Stdout
|
||||||
errorStream := os.Stderr
|
errorStream := os.Stderr
|
||||||
defer inputStream.Close()
|
defer inputStream.Close()
|
||||||
tty, err := strconv.ParseBool(c.runningSpec.Annotations["io.kubernetes.cri-o.TTY"])
|
|
||||||
if err != nil {
|
// TODO Renable this when tty/terminal discussion is had.
|
||||||
return errors.Wrapf(err, "unable to parse annotations in %s", c.ID)
|
/*
|
||||||
}
|
tty, err := strconv.ParseBool(c.runningSpec.Annotations["io.kubernetes.cri-o.TTY"])
|
||||||
if !tty {
|
if err != nil {
|
||||||
return errors.Errorf("no tty available for %s", c.ID())
|
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())) {
|
if terminal.IsTerminal(int(inputStream.Fd())) {
|
||||||
oldTermState, err := term.SaveState(inputStream.Fd())
|
oldTermState, err := term.SaveState(inputStream.Fd())
|
||||||
|
|
|
||||||
|
|
@ -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 ]
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue