-
Notifications
You must be signed in to change notification settings - Fork 31
/
installer.go
267 lines (214 loc) · 6.44 KB
/
installer.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
package libbuildpack
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/Masterminds/semver"
)
type Installer struct {
manifest *Manifest
appCacheDir string
filesInAppCache map[string]interface{}
versionLine *map[string]string
retryTimeLimit time.Duration
retryTimeInitialInterval time.Duration
}
func NewInstaller(manifest *Manifest) *Installer {
return &Installer{manifest, "", make(map[string]interface{}), &map[string]string{}, 1 * time.Minute, 1 * time.Second}
}
func (i *Installer) SetAppCacheDir(appCacheDir string) (err error) {
i.appCacheDir, err = filepath.Abs(filepath.Join(appCacheDir, "dependencies"))
return
}
func (i *Installer) InstallDependency(dep Dependency, outputDir string) error {
i.manifest.log.BeginStep("Installing %s %s", dep.Name, dep.Version)
tmpDir, err := ioutil.TempDir("", "downloads")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
tmpFile := filepath.Join(tmpDir, "archive")
entry, err := i.manifest.GetEntry(dep)
if err != nil {
return err
}
err = i.FetchDependency(dep, tmpFile)
if err != nil {
return err
}
err = i.warnNewerPatch(dep)
if err != nil {
return err
}
err = i.warnEndOfLife(dep)
if err != nil {
return err
}
if strings.HasSuffix(entry.URI, ".sh") {
return os.Rename(tmpFile, outputDir)
}
err = os.MkdirAll(outputDir, 0755)
if err != nil {
return err
}
if strings.HasSuffix(entry.URI, ".zip") {
return ExtractZip(tmpFile, outputDir)
}
if strings.HasSuffix(entry.URI, ".tar.xz") {
return ExtractTarXz(tmpFile, outputDir)
}
if strings.HasSuffix(entry.URI, ".tar.gz") || strings.HasSuffix(entry.URI, ".tgz") {
return ExtractTarGz(tmpFile, outputDir)
}
basename := filepath.Base(entry.URI)
return CopyFile(tmpFile, filepath.Join(outputDir, basename))
}
func (i *Installer) warnNewerPatch(dep Dependency) error {
v, err := semver.NewVersion(dep.Version)
if err != nil {
return nil
}
if v.Prerelease() != "" {
i.manifest.log.Warning("You are using the pre-release version %s of %s", dep.Version, dep.Name)
return nil
}
versions := i.manifest.AllDependencyVersions(dep.Name)
minor := fmt.Sprintf("%v", v.Minor())
versionLine := *i.GetVersionLine()
if versionLine[dep.Name] == "minor" {
minor = "x"
}
constraint := fmt.Sprintf("%d.%s.x", v.Major(), minor)
latest, err := FindMatchingVersion(constraint, versions)
if err != nil {
return err
}
if latest != dep.Version {
i.manifest.log.Warning(outdatedDependencyWarning(dep, latest))
}
return nil
}
func (i *Installer) warnEndOfLife(dep Dependency) error {
matchVersion := func(versionLine, depVersion string) bool {
return versionLine == depVersion
}
v, err := semver.NewVersion(dep.Version)
if err == nil {
matchVersion = func(versionLine, depVersion string) bool {
constraint, err := semver.NewConstraint(versionLine)
if err != nil {
return false
}
return constraint.Check(v)
}
}
for _, deprecation := range i.manifest.Deprecations {
if deprecation.Name != dep.Name {
continue
}
if !matchVersion(deprecation.VersionLine, dep.Version) {
continue
}
eolTime, err := time.Parse(dateFormat, deprecation.Date)
if err != nil {
return err
}
if eolTime.Sub(i.manifest.currentTime) < thirtyDays {
i.manifest.log.Warning(endOfLifeWarning(dep.Name, deprecation.VersionLine, deprecation.Date, deprecation.Link))
}
}
return nil
}
func (i *Installer) FetchDependency(dep Dependency, outputFile string) error {
entry, err := i.manifest.GetEntry(dep)
if err != nil {
return err
}
if entry.File != "" { // this file is cached by the buildpack
return fetchCachedBuildpackDependency(entry, outputFile, i.manifest.manifestRootDir, i.manifest.log)
}
if i.appCacheDir != "" { // this buildpack caches dependencies in the app cache
return i.fetchAppCachedBuildpackDependency(entry, outputFile)
}
return downloadDependency(entry, outputFile, i.manifest.log, i.retryTimeLimit, i.retryTimeInitialInterval)
}
func (i *Installer) CleanupAppCache() error {
pathsToDelete := []string{}
if err := filepath.Walk(i.appCacheDir, func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if err != nil {
return fmt.Errorf("Failed while cleaning up app cache; couldn't look at %s because: %v", path, err)
}
if path == i.appCacheDir {
return nil
}
if _, ok := i.filesInAppCache[path]; !ok {
pathsToDelete = append(pathsToDelete, path)
}
return nil
}); err != nil {
return err
}
for _, path := range pathsToDelete {
i.manifest.log.Debug("Deleting cached file: %s", path)
if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("Failed while cleaning up app cache; couldn't delete %s because: %v", path, err)
}
}
return nil
}
func (i *Installer) InstallOnlyVersion(depName string, installDir string) error {
depVersions := i.manifest.AllDependencyVersions(depName)
if len(depVersions) > 1 {
return fmt.Errorf("more than one version of %s found", depName)
} else if len(depVersions) == 0 {
return fmt.Errorf("no versions of %s found", depName)
}
dep := Dependency{Name: depName, Version: depVersions[0]}
return i.InstallDependency(dep, installDir)
}
func (i *Installer) fetchAppCachedBuildpackDependency(entry *ManifestEntry, outputFile string) error {
shaURI := sha256.Sum256([]byte(entry.URI))
cacheFile := filepath.Join(i.appCacheDir, hex.EncodeToString(shaURI[:]), filepath.Base(entry.URI))
i.filesInAppCache[cacheFile] = true
i.filesInAppCache[filepath.Dir(cacheFile)] = true
foundCacheFile, err := FileExists(cacheFile)
if err != nil {
return err
}
if foundCacheFile {
i.manifest.log.Info("Copy [%s]", cacheFile)
if err := CopyFile(cacheFile, outputFile); err != nil {
return err
}
return deleteBadFile(entry, outputFile)
}
if err := downloadDependency(entry, outputFile, i.manifest.log, i.retryTimeLimit, i.retryTimeInitialInterval); err != nil {
return err
}
if err := CopyFile(outputFile, cacheFile); err != nil {
return err
}
return nil
}
func (i *Installer) SetVersionLine(depName string, line string) {
(*i.versionLine)[depName] = line
}
func (i *Installer) GetVersionLine() *map[string]string {
return i.versionLine
}
func (i *Installer) SetRetryTimeLimit(duration time.Duration) {
i.retryTimeLimit = duration
return
}
func (i *Installer) SetRetryTimeInitialInterval(duration time.Duration) {
i.retryTimeInitialInterval = duration
return
}