forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
143 lines (120 loc) · 5.41 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
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
package packit
import (
"fmt"
"os"
"github.com/Masterminds/semver/v3"
"github.com/pelletier/go-toml"
)
// Layer provides a representation of a layer managed by a buildpack as
// described by the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#layers.
type Layer struct {
// Path is the absolute location of the layer on disk.
Path string
// Name is the descriptive name of the layer.
Name string
// Build indicates whether the layer is available to subsequent buildpacks
// during their build phase according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#build-layers.
Build bool
// Launch indicates whether the layer is exported into the application image
// and made available during the launch phase according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#launch-layers.
Launch bool
// Cache indicates whether the layer is persisted and made available to
// subsequent builds of the same application according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#launch-layers
// and
// https://github.com/buildpacks/spec/blob/main/buildpack.md#build-layers.
Cache bool
// SharedEnv is the set of environment variables attached to the layer and
// made available during both the build and launch phases according to the
// specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
SharedEnv Environment
// BuildEnv is the set of environment variables attached to the layer and
// made available during the build phase according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
BuildEnv Environment
// LaunchEnv is the set of environment variables attached to the layer and
// made available during the launch phase according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
LaunchEnv Environment
// ProcessLaunchEnv is a map of environment variables attached to the layer and
// made available to specified proccesses in the launch phase accoring to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks
ProcessLaunchEnv map[string]Environment
// Metadata is an unspecified field allowing buildpacks to communicate extra
// details about the layer. Examples of this type of metadata might include
// details about what versions of software are included in the layer such
// that subsequent builds can inspect that metadata and choose to reuse the
// layer if suitable. The Metadata field ultimately fills the metadata field
// of the Layer Content Metadata TOML file according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#layer-content-metadata-toml.
Metadata map[string]interface{}
// SBOM is a type that implements SBOMFormatter and declares the formats that
// bill-of-materials should be output for the layer SBoM.
SBOM SBOMFormatter
// ExecD references Exec.D scripts or executables by fully-qualified file path.
// These will be executed in `alphabetically ascending order by file name` as per the buildpack launch mechanism.
// See https://github.com/buildpacks/spec/blob/main/buildpack.md#launch.
// The listed executables will be given a numerical prefix so that they will be executed in the slice order.
// E.g. []string{"/helper", "/bin/command", "/${cnbPath}/other-process"} will result in:
// - <layers>/${Name}/exec.d/0-helper
// - <layers>/${Name}/exec.d/1-command
// - <layers>/${Name}/exec.d/2-other-process
// Do not forget to add these files to the `buildpack.toml` so they are available.
// If enough executables are given, this function will pad the prefix with zeros so that they are in the correct
// alphabetically ascending order.
// E.g. []string{"/cmd0", ... , "/cmd10", ..., "/cmd100"} will result in:
// - <layers>/${Name}/exec.d/000-cmd0
// - <layers>/${Name}/exec.d/010-cmd10
// - <layers>/${Name}/exec.d/100-cmd100
// ExecD is only recognized when the buildpack API is v0.5+
// https://buildpacks.io/docs/reference/spec/migration/buildpack-api-0.4-0.5/#execd
ExecD []string
}
// Reset clears the state of a layer such that the layer can be replaced with
// new content and metadata. It clears all environment variables, and removes
// the content of the layer directory on disk.
func (l Layer) Reset() (Layer, error) {
l.Build = false
l.Launch = false
l.Cache = false
l.SharedEnv = Environment{}
l.BuildEnv = Environment{}
l.LaunchEnv = Environment{}
l.ProcessLaunchEnv = make(map[string]Environment)
l.Metadata = nil
err := os.RemoveAll(l.Path)
if err != nil {
return Layer{}, fmt.Errorf("error could not remove file: %s", err)
}
err = os.MkdirAll(l.Path, os.ModePerm)
if err != nil {
return Layer{}, fmt.Errorf("error could not create directory: %s", err)
}
return l, nil
}
type formattedLayer struct {
layer Layer
api *semver.Version
}
func (l formattedLayer) MarshalTOML() ([]byte, error) {
layer := map[string]interface{}{
"metadata": l.layer.Metadata,
}
apiV06, _ := semver.NewVersion("0.6")
if l.api.LessThan(apiV06) {
layer["build"] = l.layer.Build
layer["launch"] = l.layer.Launch
layer["cache"] = l.layer.Cache
} else {
layer["types"] = map[string]bool{
"build": l.layer.Build,
"launch": l.layer.Launch,
"cache": l.layer.Cache,
}
}
return toml.Marshal(layer)
}