Skip to content

Commit

Permalink
ctr: Implement the pull-image command
Browse files Browse the repository at this point in the history
Get the image service for a given container id, and give it the image
name to pull.

Signed-off-by: Samuel Ortiz <[email protected]>
Signed-off-by: wllenyj <[email protected]>
  • Loading branch information
Samuel Ortiz authored and wllenyj committed Jun 19, 2022
1 parent c7a8823 commit 4b12e77
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cmd/ctr/commands/shim/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var Command = cli.Command{
execCommand,
startCommand,
stateCommand,
pullImageCommand,
},
}

Expand Down Expand Up @@ -230,6 +231,24 @@ var execCommand = cli.Command{
},
}

var pullImageCommand = cli.Command{
Name: "pull-image",
Usage: "pull an image",
Action: func(context *cli.Context) error {
service, err := getImageService(context)
if err != nil {
return err
}
_, err = service.PullImage(gocontext.Background(), &task.PullImageRequest{
Image: context.Args().First(),
})
if err != nil {
return err
}
return nil
},
}

func getTaskService(context *cli.Context) (task.TaskService, error) {
id := context.GlobalString("id")
if id == "" {
Expand Down Expand Up @@ -259,3 +278,28 @@ func getTaskService(context *cli.Context) (task.TaskService, error) {

return nil, fmt.Errorf("fail to connect to container %s's shim", id)
}

func getImageService(context *cli.Context) (task.ImageService, error) {
id := context.GlobalString("id")
if id == "" {
return nil, fmt.Errorf("container id must be specified")
}
ns := context.GlobalString("namespace")

// /containerd-shim/ns/id/shim.sock is the old way to generate shim socket.
s1 := filepath.Join(string(filepath.Separator), "containerd-shim", ns, id, "shim.sock")
// this should not error, ctr always get a default ns
ctx := namespaces.WithNamespace(gocontext.Background(), ns)
s2, _ := shim.SocketAddress(ctx, context.GlobalString("address"), id)
s2 = strings.TrimPrefix(s2, "unix://")

for _, socket := range []string{s2, "\x00" + s1} {
conn, err := net.Dial("unix", socket)
if err == nil {
client := ttrpc.NewClient(conn)
return task.NewImageClient(client), nil
}
}

return nil, fmt.Errorf("fail to connect to container %s's shim", id)
}

0 comments on commit 4b12e77

Please sign in to comment.