mirror of
https://github.com/containers/podman.git
synced 2025-10-20 04:34:01 +08:00
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:
@ -14,11 +14,12 @@ var (
|
|||||||
|
|
||||||
Block until one or more containers stop and then print their exit codes
|
Block until one or more containers stop and then print their exit codes
|
||||||
`
|
`
|
||||||
|
waitFlags = []cli.Flag{LatestFlag}
|
||||||
waitCommand = cli.Command{
|
waitCommand = cli.Command{
|
||||||
Name: "wait",
|
Name: "wait",
|
||||||
Usage: "Block on one or more containers",
|
Usage: "Block on one or more containers",
|
||||||
Description: waitDescription,
|
Description: waitDescription,
|
||||||
|
Flags: waitFlags,
|
||||||
Action: waitCmd,
|
Action: waitCmd,
|
||||||
ArgsUsage: "CONTAINER-NAME [CONTAINER-NAME ...]",
|
ArgsUsage: "CONTAINER-NAME [CONTAINER-NAME ...]",
|
||||||
}
|
}
|
||||||
@ -26,7 +27,7 @@ var (
|
|||||||
|
|
||||||
func waitCmd(c *cli.Context) error {
|
func waitCmd(c *cli.Context) error {
|
||||||
args := c.Args()
|
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")
|
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
|
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)
|
ctr, err := runtime.LookupContainer(container)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "unable to find container %s", container)
|
return errors.Wrapf(err, "unable to find container %s", container)
|
||||||
|
@ -1469,7 +1469,11 @@ _podman_unpause() {
|
|||||||
|
|
||||||
_podman_wait() {
|
_podman_wait() {
|
||||||
local options_with_args=""
|
local options_with_args=""
|
||||||
local boolean_options="--help -h"
|
local boolean_options="
|
||||||
|
--help
|
||||||
|
-h
|
||||||
|
-l
|
||||||
|
--latest"
|
||||||
_complete_ "$options_with_args" "$boolean_options"
|
_complete_ "$options_with_args" "$boolean_options"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,10 +21,16 @@ After the container stops, the container's return code is printed.
|
|||||||
**--help, -h**
|
**--help, -h**
|
||||||
Print usage statement
|
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
|
## EXAMPLES
|
||||||
|
|
||||||
podman wait mywebserver
|
podman wait mywebserver
|
||||||
|
|
||||||
|
podman wait --latest
|
||||||
|
|
||||||
podman wait 860a4b23
|
podman wait 860a4b23
|
||||||
|
|
||||||
podman wait mywebserver myftpserver
|
podman wait mywebserver myftpserver
|
||||||
|
@ -271,12 +271,15 @@ func (r *Runtime) GetContainersByList(containers []string) ([]*Container, error)
|
|||||||
|
|
||||||
// GetLatestContainer returns a container object of the latest created container.
|
// GetLatestContainer returns a container object of the latest created container.
|
||||||
func (r *Runtime) GetLatestContainer() (*Container, error) {
|
func (r *Runtime) GetLatestContainer() (*Container, error) {
|
||||||
var lastCreatedIndex int
|
lastCreatedIndex := -1
|
||||||
var lastCreatedTime time.Time
|
var lastCreatedTime time.Time
|
||||||
ctrs, err := r.GetAllContainers()
|
ctrs, err := r.GetAllContainers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "unable to find latest container")
|
return nil, errors.Wrapf(err, "unable to find latest container")
|
||||||
}
|
}
|
||||||
|
if len(ctrs) == 0 {
|
||||||
|
return nil, ErrNoSuchCtr
|
||||||
|
}
|
||||||
for containerIndex, ctr := range ctrs {
|
for containerIndex, ctr := range ctrs {
|
||||||
createdTime := ctr.config.CreatedTime
|
createdTime := ctr.config.CreatedTime
|
||||||
if createdTime.After(lastCreatedTime) {
|
if createdTime.After(lastCreatedTime) {
|
||||||
|
@ -34,3 +34,10 @@ function teardown() {
|
|||||||
run bash -c ${PODMAN_BINARY} ${PODMAN_OPTIONS} wait $ctr_id
|
run bash -c ${PODMAN_BINARY} ${PODMAN_OPTIONS} wait $ctr_id
|
||||||
[ "$status" -eq 0 ]
|
[ "$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 ]
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user