/
image.go
337 lines (271 loc) · 6.98 KB
/
image.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package docker
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
docker "github.com/docker/docker/api/types"
"github.com/lastbackend/lastbackend/pkg/distribution/types"
"github.com/lastbackend/lastbackend/pkg/log"
"io"
"net/http"
)
const (
logLevel = 3
)
func (r *Runtime) Auth(ctx context.Context, secret *types.SecretAuthData) (string, error) {
config := types.AuthConfig{
Username: secret.Username,
Password: secret.Password,
}
js, err := json.Marshal(config)
if err != nil {
return types.EmptyString, err
}
return base64.URLEncoding.EncodeToString(js), nil
}
func (r *Runtime) Pull(ctx context.Context, spec *types.ImageManifest, out io.Writer) (*types.Image, error) {
log.V(logLevel).Debugf("Docker: Name pull: %s", spec.Name)
options := docker.ImagePullOptions{
PrivilegeFunc: func() (string, error) {
return "", errors.New("access denied")
},
RegistryAuth: spec.Auth,
}
res, err := r.client.ImagePull(ctx, spec.Name, options)
if err != nil {
return nil, err
}
defer res.Close()
const bufferSize = 1024
var buffer = make([]byte, bufferSize)
for {
select {
case <-ctx.Done():
image, err := r.Inspect(ctx, spec.Name)
if err != nil {
return nil, err
}
return image, nil
default:
readBytes, err := res.Read(buffer)
if err != nil && err != io.EOF {
return nil, err
}
if readBytes == 0 {
image, err := r.Inspect(ctx, spec.Name)
if err != nil {
return nil, err
}
return image, nil
}
_, err = func(p []byte) (n int, err error) {
if out != nil {
n, err = out.Write(p)
if err != nil {
return n, err
}
if f, ok := out.(http.Flusher); ok {
f.Flush()
}
}
return n, nil
}(buffer[0:readBytes])
if err != nil {
return nil, err
}
for i := 0; i < readBytes; i++ {
buffer[i] = 0
}
}
}
}
func (r *Runtime) Push(ctx context.Context, spec *types.ImageManifest, out io.Writer) (*types.Image, error) {
log.V(logLevel).Debugf("Docker: Name push: %s", spec.Name)
options := docker.ImagePushOptions{
RegistryAuth: spec.Auth,
}
res, err := r.client.ImagePush(ctx, spec.Name, options)
if err != nil {
return nil, err
}
defer res.Close()
const bufferSize = 5e+6 // 5e+6 = 5MB
var (
readBytesLast = 0
bufferLast = make([]byte, bufferSize)
)
result := new(struct {
Progress map[string]interface{} `json:"progressDetail"`
ErrorDetail *struct {
Message string `json:"message"`
Error string `json:"error"`
} `json:"errorDetail,omitempty"`
Aux struct {
Tag string `json:"Tag"`
Digest string `json:"Digest"`
Size int `json:"Limit"`
} `json:"aux"`
})
err = func(stream io.ReadCloser, data interface{}) error {
for {
buffer := make([]byte, bufferSize)
readBytes, err := res.Read(buffer)
if err != nil && err != io.EOF {
return err
}
if readBytes == 0 {
if err := json.Unmarshal(bufferLast[:readBytesLast], &data); err != nil {
result = nil
break
}
if result.ErrorDetail != nil {
return fmt.Errorf("%s", result.ErrorDetail.Message)
}
break
}
bufferLast = make([]byte, bufferSize)
readBytesLast = readBytes
copy(bufferLast, buffer)
if out != nil {
out.Write(buffer[:readBytes])
}
}
return nil
}(res, result)
if err != nil {
return nil, err
}
imageID := spec.Name
if result != nil {
imageID = fmt.Sprintf("%s@%s", spec.Name, result.Aux.Digest)
}
image, err := r.Inspect(ctx, imageID)
if err != nil {
return nil, err
}
return image, err
}
func (r *Runtime) Build(ctx context.Context, stream io.Reader, spec *types.SpecBuildImage, out io.Writer) (*types.Image, error) {
options := docker.ImageBuildOptions{
Tags: spec.Tags,
Memory: spec.Memory,
Dockerfile: spec.Dockerfile,
ExtraHosts: spec.ExtraHosts,
Context: spec.Context,
NoCache: spec.NoCache,
SuppressOutput: spec.SuppressOutput,
}
if spec.AuthConfigs != nil {
options.AuthConfigs = make(map[string]docker.AuthConfig, 0)
for k, v := range spec.AuthConfigs {
options.AuthConfigs[k] = docker.AuthConfig(v)
}
}
res, err := r.client.ImageBuild(ctx, stream, options)
if err != nil {
return nil, err
}
defer res.Body.Close()
const bufferSize = 1024
var buffer = make([]byte, bufferSize)
for {
select {
case <-ctx.Done():
return nil, nil
default:
readBytes, err := res.Body.Read(buffer)
if err != nil && err != io.EOF {
return nil, err
}
if readBytes == 0 {
// TODO: get image info
return new(types.Image), err
}
_, err = func(p []byte) (n int, err error) {
if out != nil {
n, err = out.Write(p)
if err != nil {
return n, err
}
if f, ok := out.(http.Flusher); ok {
f.Flush()
}
}
return n, nil
}(buffer[0:readBytes])
if err != nil {
return nil, err
}
for i := 0; i < readBytes; i++ {
buffer[i] = 0
}
}
}
}
func (r *Runtime) Remove(ctx context.Context, ID string) error {
log.V(logLevel).Debugf("Docker: Name remove: %s", ID)
var options docker.ImageRemoveOptions
options = docker.ImageRemoveOptions{
Force: false,
PruneChildren: true,
}
_, err := r.client.ImageRemove(ctx, ID, options)
return err
}
func (r *Runtime) List(ctx context.Context) ([]*types.Image, error) {
var images = make([]*types.Image, 0)
il, err := r.client.ImageList(ctx, docker.ImageListOptions{All: true})
if err != nil {
return images, err
}
for _, i := range il {
if len(i.RepoTags) == 0 {
continue
}
img, err := r.Inspect(ctx, i.ID)
if err != nil {
return images, err
}
images = append(images, img)
}
return images, nil
}
func (r *Runtime) Inspect(ctx context.Context, id string) (*types.Image, error) {
info, _, err := r.client.ImageInspectWithRaw(ctx, id)
image := new(types.Image)
image.Meta.ID = info.ID
if len(info.RepoDigests) > 0 {
image.Meta.Digest = info.RepoDigests[0]
}
image.Meta.Tags = info.RepoTags
image.Status.Size = info.Size
image.Status.VirtualSize = info.VirtualSize
if info.ContainerConfig!= nil {
image.Status.Container.Envs = info.ContainerConfig.Env
}
if info.Config!= nil {
image.Status.Container.Exec.Command = info.Config.Cmd
image.Status.Container.Exec.Entrypoint = info.Config.Entrypoint
image.Status.Container.Exec.Workdir = info.Config.WorkingDir
}
return image, err
}