-
Notifications
You must be signed in to change notification settings - Fork 31
/
manifest.go
344 lines (280 loc) · 8.4 KB
/
manifest.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
package libbuildpack
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
const dateFormat = "2006-01-02"
const thirtyDays = time.Hour * 24 * 30
const (
CFLINUXFS2 = "cflinuxfs2"
WINDOWS2016 = "windows2016"
ATTENTION_MSG = "!! !!"
WARNING_MSG_CFLINUXFS2 = "This application is being deployed on cflinuxfs2 which is being deprecated in April, 2019.\nPlease migrate this application to cflinuxfs3.\nFor more information about changing the stack, see https://docs.cloudfoundry.org/devguide/deploy-apps/stacks.html"
WARNING_MSG_WINDOWS2016 = "This application is being deployed on the 'windows2016' stack which is deprecated.\nPlease restage this application to the 'windows' stack with '-s windows'.\nAny other applications deployed to the 'windows2016' stack should also be restaged to '-s windows'.\nFor more information, see https://docs.cloudfoundry.org/devguide/deploy-apps/windows-stacks.html"
)
type Dependency struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
}
type DeprecationDate struct {
Name string `yaml:"name"`
VersionLine string `yaml:"version_line"`
Date string `yaml:"date"`
Link string `yaml:"link"`
}
type ManifestEntry struct {
Dependency Dependency `yaml:",inline"`
URI string `yaml:"uri"`
File string `yaml:"file"`
SHA256 string `yaml:"sha256"`
CFStacks []string `yaml:"cf_stacks"`
}
type Manifest struct {
LanguageString string `yaml:"language"`
DefaultVersions []Dependency `yaml:"default_versions"`
ManifestEntries []ManifestEntry `yaml:"dependencies"`
Deprecations []DeprecationDate `yaml:"dependency_deprecation_dates"`
Stack string `yaml:"stack"`
manifestRootDir string
currentTime time.Time //move into installer?
log *Logger
}
type BuildpackMetadata struct {
Language string `yaml:"language"`
Version string `yaml:"version"`
}
func NewManifest(bpDir string, logger *Logger, currentTime time.Time) (*Manifest, error) {
var m Manifest
y := &YAML{}
err := y.Load(filepath.Join(bpDir, "manifest.yml"), &m)
if err != nil {
return nil, err
}
m.manifestRootDir, err = filepath.Abs(bpDir)
if err != nil {
return nil, err
}
m.currentTime = currentTime
m.log = logger
return &m, nil
}
func (m *Manifest) replaceDefaultVersion(oDep Dependency) {
replaced := false
for idx, mDep := range m.DefaultVersions {
if mDep.Name == oDep.Name {
replaced = true
m.DefaultVersions[idx] = oDep
}
}
if !replaced {
m.DefaultVersions = append(m.DefaultVersions, oDep)
}
}
func (m *Manifest) replaceManifestEntry(oEntry ManifestEntry) {
oDep := oEntry.Dependency
replaced := false
for idx, mEntry := range m.ManifestEntries {
mDep := mEntry.Dependency
if mDep.Name == oDep.Name && mDep.Version == oDep.Version {
replaced = true
m.ManifestEntries[idx] = mEntry
}
}
if !replaced {
m.ManifestEntries = append(m.ManifestEntries, oEntry)
}
}
func (m *Manifest) ApplyOverride(depsDir string) error {
files, err := filepath.Glob(filepath.Join(depsDir, "*", "override.yml"))
if err != nil {
return err
}
for _, file := range files {
var overrideYml map[string]Manifest
y := &YAML{}
if err := y.Load(file, &overrideYml); err != nil {
return err
}
if o, found := overrideYml[m.Language()]; found {
for _, oDep := range o.DefaultVersions {
m.replaceDefaultVersion(oDep)
}
for _, oEntry := range o.ManifestEntries {
m.replaceManifestEntry(oEntry)
}
}
}
return nil
}
func (m *Manifest) RootDir() string {
return m.manifestRootDir
}
func (m *Manifest) CheckBuildpackVersion(cacheDir string) {
var md BuildpackMetadata
y := &YAML{}
err := y.Load(filepath.Join(cacheDir, "BUILDPACK_METADATA"), &md)
if err != nil {
return
}
if md.Language != m.Language() {
return
}
version, err := m.Version()
if err != nil {
return
}
if md.Version != version {
m.log.Warning("buildpack version changed from %s to %s", md.Version, version)
}
}
func (m *Manifest) StoreBuildpackMetadata(cacheDir string) error {
version, err := m.Version()
if err != nil {
return err
}
md := BuildpackMetadata{Language: m.Language(), Version: version}
if exists, err := FileExists(cacheDir); err != nil {
return err
} else if !exists {
return nil
}
y := &YAML{}
return y.Write(filepath.Join(cacheDir, "BUILDPACK_METADATA"), &md)
}
func (m *Manifest) Language() string {
return m.LanguageString
}
func (m *Manifest) Version() (string, error) {
version, err := ioutil.ReadFile(filepath.Join(m.manifestRootDir, "VERSION"))
if err != nil {
return "", fmt.Errorf("unable to read VERSION file %s", err)
}
return strings.TrimSpace(string(version)), nil
}
func (m *Manifest) CheckStackSupport() error {
requiredStack := os.Getenv("CF_STACK")
if requiredStack == CFLINUXFS2 {
m.log.Warning("\n" + ATTENTION_MSG + "\n" + WARNING_MSG_CFLINUXFS2 + "\n" + ATTENTION_MSG)
}
if requiredStack == WINDOWS2016 {
m.log.Warning("\n" + ATTENTION_MSG + "\n" + WARNING_MSG_WINDOWS2016 + "\n" + ATTENTION_MSG)
}
if m.manifestSupportsStack(requiredStack) {
return nil
}
return fmt.Errorf("required stack %s was not found", requiredStack)
}
func (m *Manifest) manifestSupportsStack(stack string) bool {
if m.Stack != "" {
return m.Stack == stack
}
if len(m.ManifestEntries) == 0 {
return true
}
for _, entry := range m.ManifestEntries {
if m.entrySupportsStack(&entry, stack) {
return true
}
}
return false
}
func (m *Manifest) DefaultVersion(depName string) (Dependency, error) {
var defaultVersion string
var err error
numDefaults := 0
for _, defaultDep := range m.DefaultVersions {
if depName == defaultDep.Name {
defaultVersion = defaultDep.Version
numDefaults++
}
}
if numDefaults == 0 {
err = fmt.Errorf("no default version for %s", depName)
} else if numDefaults > 1 {
err = fmt.Errorf("found %d default versions for %s", numDefaults, depName)
}
if err != nil {
m.log.Error(defaultVersionsError)
return Dependency{}, err
}
depVersions := m.AllDependencyVersions(depName)
highestVersion, err := FindMatchingVersion(defaultVersion, depVersions)
if err != nil {
m.log.Error(defaultVersionsError)
return Dependency{}, err
}
return Dependency{Name: depName, Version: highestVersion}, nil
}
func fetchCachedBuildpackDependency(entry *ManifestEntry, outputFile, manifestRootDir string, manifestLog *Logger) error {
source := entry.File
if !filepath.IsAbs(source) {
source = filepath.Join(manifestRootDir, source)
}
manifestLog.Info("Copy [%s]", source)
if err := CopyFile(source, outputFile); err != nil {
return err
}
return deleteBadFile(entry, outputFile)
}
func deleteBadFile(entry *ManifestEntry, outputFile string) error {
if err := CheckSha256(outputFile, entry.SHA256); err != nil {
os.Remove(outputFile)
return err
}
return nil
}
func downloadDependency(entry *ManifestEntry, outputFile string, logger *Logger, retryTimeLimit time.Duration, retryTimeInitialInterval time.Duration) error {
filteredURI, err := filterURI(entry.URI)
if err != nil {
return err
}
logger.Info("Download [%s]", filteredURI)
err = downloadFile(entry.URI, outputFile, retryTimeLimit, retryTimeInitialInterval, logger)
if err != nil {
return err
}
return deleteBadFile(entry, outputFile)
}
func (m *Manifest) entrySupportsStack(entry *ManifestEntry, stack string) bool {
if m.Stack != "" {
return m.Stack == stack
}
for _, s := range entry.CFStacks {
if s == stack {
return true
}
}
return false
}
func (m *Manifest) AllDependencyVersions(depName string) []string {
var depVersions []string
currentStack := os.Getenv("CF_STACK")
for _, e := range m.ManifestEntries {
if e.Dependency.Name == depName && m.entrySupportsStack(&e, currentStack) {
depVersions = append(depVersions, e.Dependency.Version)
}
}
return depVersions
}
func (m *Manifest) GetEntry(dep Dependency) (*ManifestEntry, error) {
currentStack := os.Getenv("CF_STACK")
for _, e := range m.ManifestEntries {
if e.Dependency == dep && m.entrySupportsStack(&e, currentStack) {
return &e, nil
}
}
m.log.Error(dependencyMissingError(m, dep))
return nil, fmt.Errorf("dependency %s %s not found", dep.Name, dep.Version)
}
func (m *Manifest) IsCached() bool {
dependenciesDir := filepath.Join(m.manifestRootDir, "dependencies")
isCached, err := FileExists(dependenciesDir)
if err != nil {
m.log.Warning("Error determining if buildpack is cached: %s", err)
}
return isCached
}