-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathstager.go
351 lines (281 loc) · 7.11 KB
/
stager.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
package libbuildpack
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const SENTINEL = "sentinel"
type Stager struct {
buildDir string
cacheDir string
depsDir string
depsIdx string
profileDir string
manifest *Manifest
log *Logger
}
func NewStager(args []string, logger *Logger, manifest *Manifest) *Stager {
buildDir := args[0]
cacheDir := args[1]
depsDir := ""
depsIdx := ""
profileDir := ""
sentinelPath := filepath.Join(string(filepath.Separator), "home", "vcap", "app", ".cloudfoundry", SENTINEL)
exists, err := FileExists(sentinelPath)
if err != nil {
logger.Error("Problem resolving V3 sentinel file: %v", err)
} else if exists {
panic("ERROR: You are running a V2 buildpack after a V3 buildpack. This is unsupported.")
}
if len(args) >= 4 {
depsDir = args[2]
depsIdx = args[3]
}
if len(args) >= 5 && args[4] != "" {
profileDir = args[4]
} else {
profileDir = filepath.Join(buildDir, ".profile.d")
}
s := &Stager{buildDir: buildDir,
cacheDir: cacheDir,
depsDir: depsDir,
depsIdx: depsIdx,
profileDir: profileDir,
manifest: manifest,
log: logger,
}
return s
}
func (s *Stager) Logger() *Logger {
return s.log
}
func (s *Stager) DepsDir() string {
return s.depsDir
}
func (s *Stager) DepDir() string {
return filepath.Join(s.depsDir, s.depsIdx)
}
func (s *Stager) ProfileDir() string {
return s.profileDir
}
func (s *Stager) WriteConfigYml(config interface{}) error {
if config == nil {
config = map[interface{}]interface{}{}
}
bpVersion, err := s.manifest.Version()
if err != nil {
return err
}
data := map[string]interface{}{"name": s.manifest.Language(), "config": config, "version": bpVersion}
y := &YAML{}
return y.Write(filepath.Join(s.DepDir(), "config.yml"), data)
}
func (s *Stager) WriteEnvFile(envVar, envVal string) error {
envDir := filepath.Join(s.DepDir(), "env")
if err := os.MkdirAll(envDir, 0755); err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(envDir, envVar), []byte(envVal), 0644)
}
func (s *Stager) LinkDirectoryInDepDir(destDir, depSubDir string) error {
srcDir := filepath.Join(s.DepDir(), depSubDir)
if err := os.MkdirAll(srcDir, 0755); err != nil {
return err
}
files, err := ioutil.ReadDir(destDir)
if err != nil {
return err
}
for _, file := range files {
relPath, err := filepath.Rel(srcDir, filepath.Join(destDir, file.Name()))
if err != nil {
return err
}
destPath := filepath.Join(srcDir, file.Name())
if _, err := os.Lstat(destPath); err == nil {
os.Remove(destPath)
}
if err := os.Symlink(relPath, filepath.Join(srcDir, file.Name())); err != nil {
return err
}
}
return nil
}
func (s *Stager) CheckBuildpackValid() error {
version, err := s.manifest.Version()
if err != nil {
s.log.Error("Could not determine buildpack version: %s", err)
return err
}
s.log.BeginStep("%s Buildpack version %s", strings.Title(s.manifest.Language()), version)
err = s.manifest.CheckStackSupport()
if err != nil {
s.log.Error("Stack not supported by buildpack: %s", err)
return err
}
s.manifest.CheckBuildpackVersion(s.cacheDir)
return nil
}
func (s *Stager) StagingComplete() {
s.manifest.StoreBuildpackMetadata(s.cacheDir)
}
func (s *Stager) ClearCache() error {
files, err := ioutil.ReadDir(s.cacheDir)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
for _, file := range files {
err = os.RemoveAll(filepath.Join(s.cacheDir, file.Name()))
if err != nil {
return err
}
}
return nil
}
func (s *Stager) ClearDepDir() error {
files, err := ioutil.ReadDir(s.DepDir())
if err != nil {
return err
}
for _, file := range files {
if file.Name() != "config.yml" {
if err := os.RemoveAll(filepath.Join(s.DepDir(), file.Name())); err != nil {
return err
}
}
}
return nil
}
func (s *Stager) WriteProfileD(scriptName, scriptContents string) error {
profileDir := filepath.Join(s.DepDir(), "profile.d")
err := os.MkdirAll(profileDir, 0755)
if err != nil {
return err
}
return writeToFile(strings.NewReader(scriptContents), filepath.Join(profileDir, scriptName), 0755)
}
func (s *Stager) BuildDir() string {
return s.buildDir
}
func (s *Stager) CacheDir() string {
return s.cacheDir
}
func (s *Stager) DepsIdx() string {
return s.depsIdx
}
func (s *Stager) SetStagingEnvironment() error {
for envVar, dir := range stagingEnvVarDirs {
oldVal := os.Getenv(envVar)
depsPaths, err := existingDepsDirs(s.depsDir, dir, s.depsDir)
if err != nil {
return err
}
if len(depsPaths) != 0 {
if len(oldVal) > 0 {
depsPaths = append(depsPaths, oldVal)
}
os.Setenv(envVar, strings.Join(depsPaths, envPathSeparator))
}
}
depsPaths, err := existingDepsDirs(s.depsDir, "env", s.depsDir)
if err != nil {
return err
}
for _, dir := range depsPaths {
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
if file.Mode().IsRegular() {
val, err := ioutil.ReadFile(filepath.Join(dir, file.Name()))
if err != nil {
return err
}
if err := os.Setenv(file.Name(), string(val)); err != nil {
return err
}
}
}
}
return nil
}
func (s *Stager) SetLaunchEnvironment() error {
scriptContents := ""
for envVar, dir := range launchEnvVarDirs {
depsPaths, err := existingDepsDirs(s.depsDir, dir, depsDirEnvVar)
if err != nil {
return err
}
if len(depsPaths) != 0 {
scriptContents += fmt.Sprintf(scriptLineTemplate, envVar, strings.Join(depsPaths, envPathSeparator))
scriptContents += "\n"
}
}
if err := os.MkdirAll(s.profileDir, 0755); err != nil {
return err
}
scriptLocation := filepath.Join(s.ProfileDir(), scriptName)
if err := writeToFile(strings.NewReader(scriptContents), scriptLocation, 0755); err != nil {
return err
}
profileDirs, err := existingDepsDirs(s.depsDir, "profile.d", s.depsDir)
if err != nil {
return err
}
for _, dir := range profileDirs {
sections := strings.Split(dir, string(filepath.Separator))
if len(sections) < 2 {
return errors.New("invalid dep dir")
}
depsIdx := sections[len(sections)-2]
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
if file.Mode().IsRegular() {
src := filepath.Join(dir, file.Name())
dest := filepath.Join(s.profileDir, depsIdx+"_"+file.Name())
if err := CopyFile(src, dest); err != nil {
return err
}
}
}
}
return nil
}
func (s *Stager) BuildpackLanguage() string {
return s.manifest.Language()
}
func (s *Stager) BuildpackVersion() (string, error) {
return s.manifest.Version()
}
func existingDepsDirs(depsDir, subDir, prefix string) ([]string, error) {
files, err := ioutil.ReadDir(depsDir)
if err != nil {
return nil, err
}
var existingDirs []string
for _, file := range files {
if !file.IsDir() {
continue
}
filesystemDir := filepath.Join(depsDir, file.Name(), subDir)
dirToJoin := filepath.Join(prefix, file.Name(), subDir)
addToDirs, err := FileExists(filesystemDir)
if err != nil {
return nil, err
}
if addToDirs {
existingDirs = append([]string{dirToJoin}, existingDirs...)
}
}
return existingDirs, nil
}