diff options
author | baude <bbaude@redhat.com> | 2018-01-08 14:40:29 -0600 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-01-08 22:44:38 +0000 |
commit | 245a338f5a6519f5729749be646abbefd12701d8 (patch) | |
tree | f40a53649ffd2e67beae4036c029a46b0f1d5c66 | |
parent | da7556de4ad1d89bd97e1f6c5a146eee00e8c040 (diff) | |
download | podman-245a338f5a6519f5729749be646abbefd12701d8.tar.gz podman-245a338f5a6519f5729749be646abbefd12701d8.tar.bz2 podman-245a338f5a6519f5729749be646abbefd12701d8.zip |
Add latest to wait
It is desirable to have a --latest switch on the podman wait
command so we can wait on the latest container created to end.
Also, fixes a panic with latest where no containers are available.
Signed-off-by: baude <bbaude@redhat.com>
Closes: #201
Approved by: baude
-rw-r--r-- | cmd/podman/wait.go | 15 | ||||
-rw-r--r-- | completions/bash/podman | 6 | ||||
-rw-r--r-- | docs/podman-wait.1.md | 6 | ||||
-rw-r--r-- | libpod/runtime_ctr.go | 5 | ||||
-rw-r--r-- | test/podman_wait.bats | 7 |
5 files changed, 34 insertions, 5 deletions
diff --git a/cmd/podman/wait.go b/cmd/podman/wait.go index 27cfecac9..2b2a07738 100644 --- a/cmd/podman/wait.go +++ b/cmd/podman/wait.go @@ -14,11 +14,12 @@ var ( Block until one or more containers stop and then print their exit codes ` - + waitFlags = []cli.Flag{LatestFlag} waitCommand = cli.Command{ Name: "wait", Usage: "Block on one or more containers", Description: waitDescription, + Flags: waitFlags, Action: waitCmd, ArgsUsage: "CONTAINER-NAME [CONTAINER-NAME ...]", } @@ -26,7 +27,7 @@ var ( func waitCmd(c *cli.Context) error { args := c.Args() - if len(args) < 1 { + if len(args) < 1 && !c.Bool("latest") { return errors.Errorf("you must provide at least one container name or id") } @@ -41,7 +42,15 @@ func waitCmd(c *cli.Context) error { } var lastError error - for _, container := range c.Args() { + if c.Bool("latest") { + latestCtr, err := runtime.GetLatestContainer() + if err != nil { + return errors.Wrapf(err, "unable to wait on latest container") + } + args = append(args, latestCtr.ID()) + } + + for _, container := range args { ctr, err := runtime.LookupContainer(container) if err != nil { return errors.Wrapf(err, "unable to find container %s", container) diff --git a/completions/bash/podman b/completions/bash/podman index a552f19d1..8447ab436 100644 --- a/completions/bash/podman +++ b/completions/bash/podman @@ -1469,7 +1469,11 @@ _podman_unpause() { _podman_wait() { local options_with_args="" - local boolean_options="--help -h" + local boolean_options=" + --help + -h + -l + --latest" _complete_ "$options_with_args" "$boolean_options" } diff --git a/docs/podman-wait.1.md b/docs/podman-wait.1.md index 5bdefbd5f..31735b7b5 100644 --- a/docs/podman-wait.1.md +++ b/docs/podman-wait.1.md @@ -21,10 +21,16 @@ After the container stops, the container's return code is printed. **--help, -h** Print usage statement +**--latest, -l** + Instead of providing the container name or ID, use the last created container. If you use methods other than Podman +to run containers such as CRI-O, the last started container could be from either of those methods. + ## EXAMPLES podman wait mywebserver + podman wait --latest + podman wait 860a4b23 podman wait mywebserver myftpserver diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index 0f39ead35..66dcb2f95 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -271,12 +271,15 @@ func (r *Runtime) GetContainersByList(containers []string) ([]*Container, error) // GetLatestContainer returns a container object of the latest created container. func (r *Runtime) GetLatestContainer() (*Container, error) { - var lastCreatedIndex int + lastCreatedIndex := -1 var lastCreatedTime time.Time ctrs, err := r.GetAllContainers() if err != nil { return nil, errors.Wrapf(err, "unable to find latest container") } + if len(ctrs) == 0 { + return nil, ErrNoSuchCtr + } for containerIndex, ctr := range ctrs { createdTime := ctr.config.CreatedTime if createdTime.After(lastCreatedTime) { diff --git a/test/podman_wait.bats b/test/podman_wait.bats index 3109ce91e..9d6c22734 100644 --- a/test/podman_wait.bats +++ b/test/podman_wait.bats @@ -34,3 +34,10 @@ function teardown() { run bash -c ${PODMAN_BINARY} ${PODMAN_OPTIONS} wait $ctr_id [ "$status" -eq 0 ] } + +@test "wait on the latest container" { + ${PODMAN_BINARY} ${PODMAN_OPTIONS} run -d ${ALPINE} sleep 5 + run bash -c ${PODMAN_BINARY} ${PODMAN_OPTIONS} wait -l + echo "$output" + [ "$status" -eq 0 ] +} |