-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpull.go
More file actions
59 lines (56 loc) · 1.6 KB
/
pull.go
File metadata and controls
59 lines (56 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"context"
"encoding/base64"
"encoding/json"
"strings"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/pkg/errors"
)
func pull(ctx context.Context, cli *client.Client, c *types.Container) (string, error) {
log.Debugf("pulling image %s", c.Image)
distributionRef, err := reference.ParseNormalizedNamed(c.Image)
switch {
case err != nil:
return "", errors.Wrapf(err, "can't parse image '%s'", c.Image)
case reference.IsNameOnly(distributionRef):
distributionRef = reference.TagNameOnly(distributionRef)
if tagged, ok := distributionRef.(reference.Tagged); ok {
log.Infof("using default tag: %s\n", tagged.Tag())
}
}
auth, err := findDefaultAuth(distributionRef)
if err != nil {
return "", errors.Wrapf(err, "can't fetch auth for %s", distributionRef.String())
}
var regAuth string
if auth != nil {
encodedJSON, err := json.Marshal(auth)
if err != nil {
return "", err
}
regAuth = base64.URLEncoding.EncodeToString(encodedJSON)
}
var imageId string
if r, err := cli.ImagePull(context.Background(), distributionRef.String(), types.ImagePullOptions{
RegistryAuth: regAuth,
}); err != nil {
return "", errors.Wrapf(err, "can't pull image %s", distributionRef.String())
} else {
var jm jsonmessage.JSONMessage
dec := json.NewDecoder(r)
defer r.Close()
for {
if err := dec.Decode(&jm); err != nil {
break
}
if strings.HasPrefix(jm.Status, "Digest: sha256:") {
imageId = jm.Status[8:]
}
}
}
return imageId, nil
}