-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsend.go
177 lines (148 loc) · 4.51 KB
/
send.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package scratchbuild
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
digest "github.com/opencontainers/go-digest"
)
// Options contains configuration options for the client
type Options struct {
// Dir is the directory that we build the container from
Dir string
// Name is the name of the repository
Name string
// BaseURL is the base URL of the repository. For Docker this is https://index.docker.io
// For GCR it is https://gcr.io
BaseURL string
//
User string
Password string
// Token is the bearer token for the repository. For GCR you can use $(gcloud auth print-access-token).
// For Docker, supply your Docker Hub username and password instead.
Token func() string
// Tag is the tag for the image. Set to "latest" if you're out of ideas
Tags []string
}
// Client lets you send a container up to a repository
type Client struct {
Options
}
// New creates a new Client
func New(o *Options) *Client {
return &Client{
Options: *o,
}
}
func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if c.Token != nil {
req.Header.Set("Authorization", "Bearer "+c.Token())
}
return req, nil
}
func (c *Client) sendBlob(digest digest.Digest, data []byte) error {
uploaded, err := c.isBlobUploaded(digest)
if err != nil {
return fmt.Errorf("could not check if blob is already uploaded: %w", err)
}
if uploaded {
fmt.Printf("blob already uploaded\n")
return nil
}
// The repository tells us where the blob should be uploaded to
loc, err := c.getBlobUploadLocation()
if err != nil {
return fmt.Errorf("could not get location for blob upload: %w", err)
}
if err := c.uploadBlob(loc, digest, data); err != nil {
return fmt.Errorf("blob upload failed: %w", err)
}
return nil
}
func (c *Client) isBlobUploaded(digest digest.Digest) (bool, error) {
u := strings.Join([]string{c.BaseURL, "v2", c.Name, "blobs", digest.String()}, "/")
req, err := c.newRequest(http.MethodHead, u, nil)
if err != nil {
return false, fmt.Errorf("could nto build request: %w", err)
}
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return false, fmt.Errorf("blob upload failed: %w", err)
}
return rsp.StatusCode == http.StatusOK, nil
}
func (c *Client) getBlobUploadLocation() (*url.URL, error) {
u := strings.Join([]string{c.BaseURL, "v2", c.Name, "blobs/uploads/"}, "/")
req, err := c.newRequest(http.MethodPost, u, nil)
if err != nil {
return nil, fmt.Errorf("could not build request: %w", err)
}
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("blob upload failed: %w", err)
}
defer rsp.Body.Close()
body, err := io.ReadAll(rsp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read body on blob upload response: %w", err)
}
if rsp.StatusCode != http.StatusAccepted {
return nil, fmt.Errorf("unexpected status %s. %s", rsp.Status, string(body))
}
return rsp.Location()
}
func (c *Client) uploadBlob(loc *url.URL, digest digest.Digest, data []byte) error {
q := loc.Query()
q.Set("digest", digest.String())
loc.RawQuery = q.Encode()
r := bytes.NewReader(data)
req, err := c.newRequest(http.MethodPut, loc.String(), r)
if err != nil {
return err
}
req.ContentLength = int64(len(data))
req.Header.Set("Content-Type", "application/octet-stream")
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("blob upload failed: %w", err)
}
defer rsp.Body.Close()
body, err := io.ReadAll(rsp.Body)
if err != nil {
return fmt.Errorf("failed to read body on blob upload response: %w", err)
}
if rsp.StatusCode != http.StatusCreated {
return fmt.Errorf("unexpected status %s. %s", rsp.Status, string(body))
}
return nil
}
func (c *Client) sendManifest(digest digest.Digest, data []byte, mediaType, tag string) error {
u := strings.Join([]string{c.BaseURL, "v2", c.Name, "manifests", tag}, "/")
b := bytes.NewReader(data)
req, err := c.newRequest(http.MethodPut, u, b)
if err != nil {
return err
}
req.Header.Set("Content-Type", mediaType)
log.Printf("Sending %s", u)
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("manifest upload failed: %w", err)
}
defer rsp.Body.Close()
body, err := io.ReadAll(rsp.Body)
if err != nil {
return fmt.Errorf("failed to read body on manifest upload response: %w", err)
}
if rsp.StatusCode != http.StatusCreated && rsp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status %s. %s", rsp.Status, string(body))
}
return nil
}