github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/container/wait.go (about) 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 container 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 "io" 24 25 "github.com/containerd/containerd" 26 "github.com/containerd/nerdctl/v2/pkg/api/types" 27 "github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker" 28 ) 29 30 // Wait blocks until all the containers specified by reqs have stopped, then print their exit codes. 31 func Wait(ctx context.Context, client *containerd.Client, reqs []string, options types.ContainerWaitOptions) error { 32 var containers []containerd.Container 33 walker := &containerwalker.ContainerWalker{ 34 Client: client, 35 OnFound: func(ctx context.Context, found containerwalker.Found) error { 36 if found.MatchCount > 1 { 37 return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req) 38 } 39 containers = append(containers, found.Container) 40 return nil 41 }, 42 } 43 44 // check if all containers from `reqs` exist 45 if err := walker.WalkAll(ctx, reqs, false); err != nil { 46 return err 47 } 48 49 var errs []error 50 w := options.Stdout 51 for _, container := range containers { 52 if waitErr := waitContainer(ctx, w, container); waitErr != nil { 53 errs = append(errs, waitErr) 54 } 55 } 56 return errors.Join(errs...) 57 } 58 59 func waitContainer(ctx context.Context, w io.Writer, container containerd.Container) error { 60 task, err := container.Task(ctx, nil) 61 if err != nil { 62 return err 63 } 64 65 statusC, err := task.Wait(ctx) 66 if err != nil { 67 return err 68 } 69 70 status := <-statusC 71 code, _, err := status.Result() 72 if err != nil { 73 return err 74 } 75 fmt.Fprintln(w, code) 76 return nil 77 }