forked from apptainer/singularity
/
bundle.go
105 lines (92 loc) · 3.34 KB
/
bundle.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
// 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 types
import (
"io/ioutil"
"os"
"path/filepath"
ocitypes "github.com/containers/image/types"
"github.com/sylabs/singularity/internal/pkg/sylog"
)
// Bundle is the temporary build environment used during the image
// building process. A Bundle is the programmatic representation of
// the directory structure which will constitute this environmenb.
// /tmp/...:
// fs/ - A chroot filesystem
// .singularity.d/ - Container metadata (from 2.x image format)
// config.json (optional) - Contain information for OCI image bundle
// etc... - The Bundle dir can theoretically contain arbitrary directories,
// files, etc... which can be interpreted by the Chef
type Bundle struct {
// FSObjects is a map of the filesystem objects contained in the Bundle. An object
// will be built as one section of a SIF file.
//
// Known FSObjects labels:
// * rootfs -> root file system
// * .singularity.d -> .singularity.d directory (includes image exec scripts)
// * data -> directory containing data files
FSObjects map[string]string `json:"fsObjects"`
JSONObjects map[string][]byte `json:"jsonObjects"`
Recipe Definition `json:"rawDeffile"`
BindPath []string `json:"bindPath"`
Path string `json:"bundlePath"`
Opts Options `json:"opts"`
}
// Options ...
type Options struct {
// TmpDir specifies a non-standard temporary location to perform a build
TmpDir string
// sections are the parts of the definition to run during the build
Sections []string `json:"sections"`
// noTest indicates if build should skip running the test script
NoTest bool `json:"noTest"`
// force automatically deletes an existing container at build destination while performing build
Force bool `json:"force"`
// update detects and builds using an existing sandbox container at build destination
Update bool `json:"update"`
// noHTTPS
NoHTTPS bool `json:"noHTTPS"`
// contains docker credentials if specified
DockerAuthConfig *ocitypes.DockerAuthConfig
}
// NewBundle creates a Bundle environment
func NewBundle(bundleDir, bundlePrefix string) (b *Bundle, err error) {
b = &Bundle{}
if bundlePrefix == "" {
bundlePrefix = "sbuild-"
}
b.Path, err = ioutil.TempDir(bundleDir, bundlePrefix+"-")
if err != nil {
return nil, err
}
sylog.Debugf("Created temporary directory for bundle %v\n", b.Path)
b.FSObjects = map[string]string{
"rootfs": "fs",
}
for _, fso := range b.FSObjects {
if err = os.MkdirAll(filepath.Join(b.Path, fso), 0755); err != nil {
return
}
}
return b, nil
}
// Rootfs give the path to the root filesystem in the Bundle
func (b *Bundle) Rootfs() string {
return filepath.Join(b.Path, b.FSObjects["rootfs"])
}
// RunSection iterates through the sections specified in a bundle
// and returns true if the given string, s, is a section of the
// definition that should be executed during the build process
func (b Bundle) RunSection(s string) bool {
for _, section := range b.Opts.Sections {
if section == "none" {
return false
}
if section == "all" || section == s {
return true
}
}
return false
}