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
This commit is contained in:
baude
2018-01-08 14:40:29 -06:00
committed by Atomic Bot
parent da7556de4a
commit 245a338f5a
5 changed files with 34 additions and 5 deletions

View File

@ -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)

View File

@ -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"
}

View File

@ -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

View File

@ -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) {

View File

@ -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 ]
}