|
| 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 | +} |
0 commit comments