forked from sylabs/singularity
/
conveyorPacker_oci.go
401 lines (337 loc) · 9.68 KB
/
conveyorPacker_oci.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package sources
import (
"archive/tar"
"bufio"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/containers/image/copy"
"github.com/containers/image/docker"
dockerarchive "github.com/containers/image/docker/archive"
dockerdaemon "github.com/containers/image/docker/daemon"
ociarchive "github.com/containers/image/oci/archive"
oci "github.com/containers/image/oci/layout"
"github.com/containers/image/signature"
"github.com/containers/image/types"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
imagetools "github.com/opencontainers/image-tools/image"
sytypes "github.com/sylabs/singularity/internal/pkg/build/types"
ociclient "github.com/sylabs/singularity/internal/pkg/client/oci"
"github.com/sylabs/singularity/internal/pkg/sylog"
"github.com/sylabs/singularity/internal/pkg/util/shell"
)
// OCIConveyorPacker holds stuff that needs to be packed into the bundle
type OCIConveyorPacker struct {
srcRef types.ImageReference
b *sytypes.Bundle
tmpfsRef types.ImageReference
policyCtx *signature.PolicyContext
imgConfig imgspecv1.ImageConfig
sysCtx *types.SystemContext
}
// Get downloads container information from the specified source
func (cp *OCIConveyorPacker) Get(b *sytypes.Bundle) (err error) {
cp.b = b
policy := &signature.Policy{Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}}
cp.policyCtx, err = signature.NewPolicyContext(policy)
if err != nil {
return err
}
cp.sysCtx = &types.SystemContext{
OCIInsecureSkipTLSVerify: cp.b.Opts.NoHTTPS,
DockerInsecureSkipTLSVerify: cp.b.Opts.NoHTTPS,
DockerAuthConfig: cp.b.Opts.DockerAuthConfig,
}
// add registry and namespace to reference if specified
ref := b.Recipe.Header["from"]
if b.Recipe.Header["namespace"] != "" {
ref = b.Recipe.Header["namespace"] + "/" + ref
}
if b.Recipe.Header["registry"] != "" {
ref = b.Recipe.Header["registry"] + "/" + ref
}
sylog.Debugf("Reference: %v", ref)
switch b.Recipe.Header["bootstrap"] {
case "docker":
ref = "//" + ref
cp.srcRef, err = docker.ParseReference(ref)
case "docker-archive":
cp.srcRef, err = dockerarchive.ParseReference(ref)
case "docker-daemon":
cp.srcRef, err = dockerdaemon.ParseReference(ref)
case "oci":
cp.srcRef, err = oci.ParseReference(ref)
case "oci-archive":
if os.Geteuid() == 0 {
// As root, the direct oci-archive handling will work
cp.srcRef, err = ociarchive.ParseReference(ref)
} else {
// As non-root we need to do a dumb tar extraction first
tmpDir, err := ioutil.TempDir("", "temp-oci-")
if err != nil {
return fmt.Errorf("could not create temporary oci directory: %v", err)
}
defer os.RemoveAll(tmpDir)
refParts := strings.SplitN(b.Recipe.Header["from"], ":", 2)
err = cp.extractArchive(refParts[0], tmpDir)
if err != nil {
return fmt.Errorf("error extracting the OCI archive file: %v", err)
}
// We may or may not have had a ':tag' in the source to handle
if len(refParts) == 2 {
cp.srcRef, err = oci.ParseReference(tmpDir + ":" + refParts[1])
} else {
cp.srcRef, err = oci.ParseReference(tmpDir)
}
}
default:
return fmt.Errorf("OCI ConveyorPacker does not support %s", b.Recipe.Header["bootstrap"])
}
if err != nil {
return fmt.Errorf("Invalid image source: %v", err)
}
// Grab the modified source ref from the cache
cp.srcRef, err = ociclient.ConvertReference(cp.srcRef, cp.sysCtx)
if err != nil {
return err
}
// To to do the RootFS extraction we also have to have a location that
// contains *only* this image
cp.tmpfsRef, err = oci.ParseReference(cp.b.Path + ":" + "tmp")
err = cp.fetch()
if err != nil {
return err
}
cp.imgConfig, err = cp.getConfig()
if err != nil {
return err
}
return nil
}
// Pack puts relevant objects in a Bundle!
func (cp *OCIConveyorPacker) Pack() (*sytypes.Bundle, error) {
err := cp.unpackTmpfs()
if err != nil {
return nil, fmt.Errorf("While unpacking tmpfs: %v", err)
}
err = cp.insertBaseEnv()
if err != nil {
return nil, fmt.Errorf("While inserting base environment: %v", err)
}
err = cp.insertRunScript()
if err != nil {
return nil, fmt.Errorf("While inserting runscript: %v", err)
}
err = cp.insertEnv()
if err != nil {
return nil, fmt.Errorf("While inserting docker specific environment: %v", err)
}
return cp.b, nil
}
func (cp *OCIConveyorPacker) fetch() (err error) {
// cp.srcRef contains the cache source reference
err = copy.Image(context.Background(), cp.policyCtx, cp.tmpfsRef, cp.srcRef, ©.Options{
ReportWriter: ioutil.Discard,
SourceCtx: cp.sysCtx,
})
if err != nil {
return err
}
return nil
}
func (cp *OCIConveyorPacker) getConfig() (imgspecv1.ImageConfig, error) {
img, err := cp.srcRef.NewImage(context.Background(), cp.sysCtx)
if err != nil {
return imgspecv1.ImageConfig{}, err
}
defer img.Close()
imgSpec, err := img.OCIConfig(context.Background())
if err != nil {
return imgspecv1.ImageConfig{}, err
}
return imgSpec.Config, nil
}
// Perform a dumb tar(gz) extraction with no chown, id remapping etc.
// This is needed for non-root handling of `oci-archive` as the extraction
// by containers/archive is failing when uid/gid don't match local machine
// and we're not root
func (cp *OCIConveyorPacker) extractArchive(src string, dst string) error {
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
r := bufio.NewReader(f)
header, err := r.Peek(10) //read a few bytes without consuming
if err != nil {
return err
}
gzipped := strings.Contains(http.DetectContentType(header), "x-gzip")
if gzipped {
r, err := gzip.NewReader(f)
if err != nil {
return err
}
defer r.Close()
}
tr := tar.NewReader(r)
for {
header, err := tr.Next()
switch {
// if no more files are found return
case err == io.EOF:
return nil
// return any other error
case err != nil:
return err
// if the header is nil, just skip it (not sure how this happens)
case header == nil:
continue
}
// ZipSlip protection - don't escape from dst
target := filepath.Join(dst, header.Name)
if !strings.HasPrefix(target, filepath.Clean(dst)+string(os.PathSeparator)) {
return fmt.Errorf("%s: illegal extraction path", target)
}
// check the file type
switch header.Typeflag {
// if its a dir and it doesn't exist create it
case tar.TypeDir:
if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
}
// if it's a file create it
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
defer f.Close()
// copy over contents
if _, err := io.Copy(f, tr); err != nil {
return err
}
}
}
}
func (cp *OCIConveyorPacker) unpackTmpfs() (err error) {
refs := []string{"name=tmp"}
err = imagetools.UnpackLayout(cp.b.Path, cp.b.Rootfs(), "amd64", refs)
return err
}
func (cp *OCIConveyorPacker) insertBaseEnv() (err error) {
if err = makeBaseEnv(cp.b.Rootfs()); err != nil {
sylog.Errorf("%v", err)
}
return
}
func (cp *OCIConveyorPacker) insertRunScript() (err error) {
f, err := os.Create(cp.b.Rootfs() + "/.singularity.d/runscript")
if err != nil {
return
}
defer f.Close()
_, err = f.WriteString("#!/bin/sh\n")
if err != nil {
return
}
if len(cp.imgConfig.Entrypoint) > 0 {
_, err = f.WriteString("OCI_ENTRYPOINT='" + shell.ArgsQuoted(cp.imgConfig.Entrypoint) + "'\n")
if err != nil {
return
}
} else {
_, err = f.WriteString("OCI_ENTRYPOINT=''\n")
if err != nil {
return
}
}
if len(cp.imgConfig.Cmd) > 0 {
_, err = f.WriteString("OCI_CMD='" + shell.ArgsQuoted(cp.imgConfig.Cmd) + "'\n")
if err != nil {
return
}
} else {
_, err = f.WriteString("OCI_CMD=''\n")
if err != nil {
return
}
}
_, err = f.WriteString(`# ENTRYPOINT only - run entrypoint plus args
if [ -z "$OCI_CMD" ] && [ -n "$OCI_ENTRYPOINT" ]; then
SINGULARITY_OCI_RUN="${OCI_ENTRYPOINT} $@"
fi
# CMD only - run CMD or override with args
if [ -n "$OCI_CMD" ] && [ -z "$OCI_ENTRYPOINT" ]; then
if [ $# -gt 0 ]; then
SINGULARITY_OCI_RUN="$@"
else
SINGULARITY_OCI_RUN="${OCI_CMD}"
fi
fi
# ENTRYPOINT and CMD - run ENTRYPOINT with CMD as default args
# override with user provided args
if [ $# -gt 0 ]; then
SINGULARITY_OCI_RUN="${OCI_ENTRYPOINT} $@"
else
SINGULARITY_OCI_RUN="${OCI_ENTRYPOINT} ${OCI_CMD}"
fi
eval ${SINGULARITY_OCI_RUN}
`)
if err != nil {
return
}
f.Sync()
err = os.Chmod(cp.b.Rootfs()+"/.singularity.d/runscript", 0755)
if err != nil {
return
}
return nil
}
func (cp *OCIConveyorPacker) insertEnv() (err error) {
f, err := os.Create(cp.b.Rootfs() + "/.singularity.d/env/10-docker2singularity.sh")
if err != nil {
return
}
defer f.Close()
_, err = f.WriteString("#!/bin/sh\n")
if err != nil {
return
}
for _, element := range cp.imgConfig.Env {
envParts := strings.SplitN(element, "=", 2)
if len(envParts) == 1 {
_, err = f.WriteString("export " + shell.Escape(element) + "\n")
if err != nil {
return
}
} else {
_, err = f.WriteString("export " + envParts[0] + "=\"" + shell.Escape(envParts[1]) + "\"\n")
if err != nil {
return
}
}
}
f.Sync()
err = os.Chmod(cp.b.Rootfs()+"/.singularity.d/env/10-docker2singularity.sh", 0755)
if err != nil {
return
}
return nil
}
// CleanUp removes any tmpfs owned by the conveyorPacker on the filesystem
func (cp *OCIConveyorPacker) CleanUp() {
os.RemoveAll(cp.b.Path)
}