forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.go
207 lines (182 loc) · 5 KB
/
layers.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
package lifecycle
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/buildpack/layertypes"
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/platform"
)
type bpLayersDir struct {
path string
layers []bpLayer
name string
buildpack buildpack.GroupBuildpack
store *buildpack.StoreTOML
}
func readBuildpackLayersDir(layersDir string, bp buildpack.GroupBuildpack, logger Logger) (bpLayersDir, error) {
path := filepath.Join(layersDir, launch.EscapeID(bp.ID))
logger.Debugf("Reading buildpack directory: %s", path)
bpDir := bpLayersDir{
name: bp.ID,
path: path,
layers: []bpLayer{},
buildpack: bp,
}
fis, err := ioutil.ReadDir(path)
if err != nil && !os.IsNotExist(err) {
return bpLayersDir{}, err
}
names := map[string]struct{}{}
var tomls []string
for _, fi := range fis {
logger.Debugf("Reading buildpack directory item: %s", fi.Name())
if fi.IsDir() {
bpDir.layers = append(bpDir.layers, *bpDir.newBPLayer(fi.Name(), bp.API, logger))
names[fi.Name()] = struct{}{}
continue
}
if strings.HasSuffix(fi.Name(), ".toml") {
tomls = append(tomls, filepath.Join(path, fi.Name()))
}
}
for _, tf := range tomls {
name := strings.TrimSuffix(filepath.Base(tf), ".toml")
if name == "store" {
var bpStore buildpack.StoreTOML
_, err := toml.DecodeFile(tf, &bpStore)
if err != nil {
return bpLayersDir{}, errors.Wrapf(err, "failed decoding store.toml for buildpack %q", bp.ID)
}
bpDir.store = &bpStore
continue
}
if name == "launch" {
// don't treat launch.toml as a layer
continue
}
if name == "build" && api.MustParse(bp.API).AtLeast("0.5") {
// if the buildpack API supports build.toml don't treat it as a layer
continue
}
if _, ok := names[name]; !ok {
bpDir.layers = append(bpDir.layers, *bpDir.newBPLayer(name, bp.API, logger))
}
}
sort.Slice(bpDir.layers, func(i, j int) bool {
return bpDir.layers[i].identifier < bpDir.layers[j].identifier
})
return bpDir, nil
}
func forLaunch(l bpLayer) bool {
md, err := l.read()
return err == nil && md.Launch
}
func forMalformed(l bpLayer) bool {
_, err := l.read()
return err != nil
}
func forCached(l bpLayer) bool {
md, err := l.read()
return err == nil && md.Cache
}
func (bd *bpLayersDir) findLayers(f func(layer bpLayer) bool) []bpLayer {
var selectedLayers []bpLayer
for _, l := range bd.layers {
if f(l) {
selectedLayers = append(selectedLayers, l)
}
}
return selectedLayers
}
func (bd *bpLayersDir) newBPLayer(name, buildpackAPI string, logger Logger) *bpLayer {
return &bpLayer{
layer: layer{
path: filepath.Join(bd.path, name),
identifier: fmt.Sprintf("%s:%s", bd.buildpack.ID, name),
},
api: buildpackAPI,
logger: logger,
}
}
type bpLayer struct { // TODO: need to refactor so api and logger won't be part of this struct
layer
api string
logger Logger
}
func (bp *bpLayer) read() (platform.BuildpackLayerMetadata, error) {
tomlPath := bp.path + ".toml"
layerMetadataFile, msg, err := buildpack.DecodeLayerMetadataFile(tomlPath, bp.api)
if err != nil {
return platform.BuildpackLayerMetadata{}, err
}
if msg != "" {
if api.MustParse(bp.api).LessThan("0.6") {
bp.logger.Warn(msg)
} else {
return platform.BuildpackLayerMetadata{}, errors.New(msg)
}
}
var sha string
shaBytes, err := ioutil.ReadFile(bp.path + ".sha")
if err != nil && !os.IsNotExist(err) { // if the sha file doesn't exist, an empty sha will be returned
return platform.BuildpackLayerMetadata{}, err
}
if err == nil {
sha = string(shaBytes)
}
return platform.BuildpackLayerMetadata{LayerMetadata: platform.LayerMetadata{SHA: sha}, LayerMetadataFile: layerMetadataFile}, nil
}
func (bp *bpLayer) remove() error {
if err := os.RemoveAll(bp.path); err != nil && !os.IsNotExist(err) {
return err
}
if err := os.Remove(bp.path + ".sha"); err != nil && !os.IsNotExist(err) {
return err
}
if err := os.Remove(bp.path + ".toml"); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
func (bp *bpLayer) writeMetadata(metadata layertypes.LayerMetadataFile) error {
path := filepath.Join(bp.path + ".toml")
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
return err
}
return buildpack.EncodeLayerMetadataFile(metadata, path, bp.api)
}
func (bp *bpLayer) hasLocalContents() bool {
_, err := ioutil.ReadDir(bp.path)
return !os.IsNotExist(err)
}
func (bp *bpLayer) writeSha(sha string) error {
if err := ioutil.WriteFile(bp.path+".sha", []byte(sha), 0666); err != nil {
return err
} // #nosec G306
return nil
}
func (bp *bpLayer) name() string {
return filepath.Base(bp.path)
}
type layer struct {
path string
identifier string
}
func (l *layer) Identifier() string {
return l.identifier
}
func (l *layer) Path() string {
return l.path
}
type layerDir interface {
Identifier() string
Path() string
}