forked from heroku/docker-registry-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
108 lines (89 loc) · 2.8 KB
/
layer.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package registry
import (
"io"
"net/http"
"net/url"
"github.com/docker/distribution"
digest "github.com/opencontainers/go-digest"
)
func (registry *Registry) DownloadLayer(repository string, digest digest.Digest) (io.ReadCloser, error) {
url := registry.url("/v2/%s/blobs/%s", repository, digest)
registry.Logf("registry.layer.download url=%s repository=%s digest=%s", url, repository, digest)
resp, err := registry.Client.Get(url)
if err != nil {
return nil, err
}
return resp.Body, nil
}
func (registry *Registry) UploadLayer(repository string, digest digest.Digest, content io.Reader) error {
uploadUrl, err := registry.initiateUpload(repository)
if err != nil {
return err
}
q := uploadUrl.Query()
q.Set("digest", digest.String())
uploadUrl.RawQuery = q.Encode()
registry.Logf("registry.layer.upload url=%s repository=%s digest=%s", uploadUrl, repository, digest)
upload, err := http.NewRequest("PUT", uploadUrl.String(), content)
if err != nil {
return err
}
upload.Header.Set("Content-Type", "application/octet-stream")
_, err = registry.Client.Do(upload)
return err
}
func (registry *Registry) HasLayer(repository string, digest digest.Digest) (bool, error) {
checkUrl := registry.url("/v2/%s/blobs/%s", repository, digest)
registry.Logf("registry.layer.check url=%s repository=%s digest=%s", checkUrl, repository, digest)
resp, err := registry.Client.Head(checkUrl)
if resp != nil {
defer resp.Body.Close()
}
if err == nil {
return resp.StatusCode == http.StatusOK, nil
}
urlErr, ok := err.(*url.Error)
if !ok {
return false, err
}
httpErr, ok := urlErr.Err.(*HttpStatusError)
if !ok {
return false, err
}
if httpErr.Response.StatusCode == http.StatusNotFound {
return false, nil
}
return false, err
}
func (registry *Registry) LayerMetadata(repository string, digest digest.Digest) (distribution.Descriptor, error) {
checkUrl := registry.url("/v2/%s/blobs/%s", repository, digest)
registry.Logf("registry.layer.check url=%s repository=%s digest=%s", checkUrl, repository, digest)
resp, err := registry.Client.Head(checkUrl)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return distribution.Descriptor{}, err
}
return distribution.Descriptor{
Digest: digest,
Size: resp.ContentLength,
}, nil
}
func (registry *Registry) initiateUpload(repository string) (*url.URL, error) {
initiateUrl := registry.url("/v2/%s/blobs/uploads/", repository)
registry.Logf("registry.layer.initiate-upload url=%s repository=%s", initiateUrl, repository)
resp, err := registry.Client.Post(initiateUrl, "application/octet-stream", nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
location := resp.Header.Get("Location")
locationUrl, err := url.Parse(location)
if err != nil {
return nil, err
}
return locationUrl, nil
}