Skip to content

Commit 9b8bb8d

Browse files
authored
Merge pull request containerd#162 from fahedouch/wait_command
nerdctl wait command
2 parents 25d5639 + 12e49cd commit 9b8bb8d

File tree

5 files changed

+167
-1
lines changed

5 files changed

+167
-1
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ It does not necessarily mean that the corresponding features are missing in cont
180180
- [:whale: nerdctl rm](#whale-nerdctl-rm)
181181
- [:whale: nerdctl stop](#whale-nerdctl-stop)
182182
- [:whale: nerdctl start](#whale-nerdctl-start)
183+
- [:whale: nerdctl wait](#whale-nerdctl-wait)
183184
- [:whale: nerdctl kill](#whale-nerdctl-kill)
184185
- [:whale: nerdctl pause](#whale-nerdctl-pause)
185186
- [:whale: nerdctl unpause](#whale-nerdctl-unpause)
@@ -494,6 +495,11 @@ Usage: `nerdctl start [OPTIONS] CONTAINER [CONTAINER...]`
494495

495496
Unimplemented `docker start` flags: `--attach`, `--checkpoint`, `--checkpoint-dir`, `--detach-keys`, `--interactive`
496497

498+
### :whale: nerdctl wait
499+
Block until one or more containers stop, then print their exit codes.
500+
501+
Usage: `nerdctl wait CONTAINER [CONTAINER...]`
502+
497503
### :whale: nerdctl kill
498504
Kill one or more running containers.
499505

@@ -802,7 +808,6 @@ Container management:
802808
- `docker cp`
803809
- `docker diff`
804810
- `docker rename`
805-
- `docker wait`
806811

807812
- `docker container prune`
808813

container.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var containerCommand = &cli.Command{
3636
startCommand,
3737
killCommand,
3838
pauseCommand,
39+
waitCommand,
3940
unpauseCommand,
4041
commitCommand,
4142
},

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func newApp() *cli.App {
152152
pauseCommand,
153153
unpauseCommand,
154154
commitCommand,
155+
waitCommand,
155156
// Build
156157
buildCommand,
157158
// Image management

wait.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
"github.com/containerd/containerd"
24+
"github.com/containerd/containerd/cio"
25+
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
26+
"github.com/pkg/errors"
27+
"github.com/urfave/cli/v2"
28+
"golang.org/x/sync/errgroup"
29+
)
30+
31+
var waitCommand = &cli.Command{
32+
Name: "wait",
33+
Usage: "Block until one or more containers stop, then print their exit codes.",
34+
Action: containerWaitAction,
35+
BashComplete: waitBashComplete,
36+
}
37+
38+
func containerWaitAction(clicontext *cli.Context) error {
39+
if clicontext.NArg() == 0 {
40+
return errors.Errorf("requires at least 1 argument")
41+
}
42+
43+
client, ctx, cancel, err := newClient(clicontext)
44+
if err != nil {
45+
return err
46+
}
47+
defer cancel()
48+
49+
var g errgroup.Group
50+
51+
walker := &containerwalker.ContainerWalker{
52+
Client: client,
53+
OnFound: func(ctx context.Context, found containerwalker.Found) error {
54+
waitContainer(ctx, clicontext, client, found.Container.ID(), &g)
55+
return nil
56+
},
57+
}
58+
59+
for _, req := range clicontext.Args().Slice() {
60+
n, _ := walker.Walk(ctx, req)
61+
if n == 0 {
62+
return errors.Errorf("no such container: %s", req)
63+
}
64+
}
65+
66+
if err := g.Wait(); err != nil {
67+
return err
68+
}
69+
return nil
70+
}
71+
72+
func waitContainer(ctx context.Context, clicontext *cli.Context, client *containerd.Client, id string, g *errgroup.Group) error {
73+
container, err := client.LoadContainer(ctx, id)
74+
if err != nil {
75+
return err
76+
}
77+
78+
task, err := container.Task(ctx, cio.Load)
79+
if err != nil {
80+
return err
81+
}
82+
83+
g.Go(func() error {
84+
statusC, err := task.Wait(ctx)
85+
if err != nil {
86+
return err
87+
}
88+
89+
status := <-statusC
90+
code, _, err := status.Result()
91+
if err != nil {
92+
return err
93+
}
94+
fmt.Fprintf(clicontext.App.Writer, "%d\n", int(code))
95+
return nil
96+
})
97+
98+
return nil
99+
}
100+
101+
func waitBashComplete(clicontext *cli.Context) {
102+
coco := parseCompletionContext(clicontext)
103+
if coco.boring || coco.flagTakesValue {
104+
defaultBashComplete(clicontext)
105+
return
106+
}
107+
// show running container names
108+
statusFilterFn := func(st containerd.ProcessStatus) bool {
109+
return st == containerd.Running
110+
}
111+
bashCompleteContainerNames(clicontext, statusFilterFn)
112+
}

wait_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"testing"
21+
22+
"github.com/containerd/nerdctl/pkg/testutil"
23+
)
24+
25+
func TestWait(t *testing.T) {
26+
const (
27+
testContainerName1 = "nerdctl-test-wait-1"
28+
testContainerName2 = "nerdctl-test-wait-2"
29+
testContainerName3 = "nerdctl-test-wait-3"
30+
)
31+
32+
const expected = `0
33+
0
34+
123
35+
`
36+
base := testutil.NewBase(t)
37+
defer base.Cmd("rm", "-f", testContainerName1, testContainerName2, testContainerName3).Run()
38+
39+
base.Cmd("run", "-d", "--name", testContainerName1, testutil.AlpineImage, "sleep", "2").AssertOK()
40+
41+
base.Cmd("run", "-d", "--name", testContainerName2, testutil.AlpineImage, "sleep", "2").AssertOK()
42+
43+
base.Cmd("run", "--name", testContainerName3, testutil.AlpineImage, "sh", "-euxc", "sleep 3; exit 123").AssertExitCode(123)
44+
45+
base.Cmd("wait", testContainerName1, testContainerName2, testContainerName3).AssertOut(expected)
46+
47+
}

0 commit comments

Comments
 (0)