forked from paketo-buildpacks/bundle-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle_install_process.go
254 lines (215 loc) · 7.44 KB
/
bundle_install_process.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
package bundleinstall
import (
"bytes"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
pfs "github.com/paketo-buildpacks/packit/v2/fs"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface Executable --output fakes/executable.go
//go:generate faux --interface VersionResolver --output fakes/version_resolver.go
//go:generate faux --interface Calculator --output fakes/calculator.go
// Executable defines the interface for executing an external process.
type Executable interface {
Execute(pexec.Execution) error
}
// VersionResolver defines the interface for looking up and comparing the
// versions of Ruby installed in the environment.
type VersionResolver interface {
Lookup() (version string, err error)
CompareMajorMinor(left string, right string) (bool, error)
}
// Calculator defines the interface for calculating a checksum of the given set
// of file paths.
type Calculator interface {
Sum(paths ...string) (string, error)
}
// BundleInstallProcess performs the "bundle install" build process.
type BundleInstallProcess struct {
executable Executable
logger scribe.Emitter
versionResolver VersionResolver
calculator Calculator
}
// NewBundleInstallProcess initializes an instance of BundleInstallProcess.
func NewBundleInstallProcess(executable Executable, logger scribe.Emitter, versionResolver VersionResolver, calculator Calculator) BundleInstallProcess {
return BundleInstallProcess{
executable: executable,
logger: logger,
versionResolver: versionResolver,
calculator: calculator,
}
}
// ShouldRun will return true if it is determined that the BundleInstallProcess
// be executed during the build phase.
//
// The criteria for determining that the install process should be executed is
// if the major or minor version of Ruby has changed, or if the contents of the
// Gemfile or Gemfile.lock have changed.
//
// In addition to reporting if the install process should execute, this method
// will return the current version of Ruby and the checksum of the Gemfile and
// Gemfile.lock contents.
func (ip BundleInstallProcess) ShouldRun(metadata map[string]interface{}, workingDir string) (bool, string, string, error) {
rubyVersion, err := ip.versionResolver.Lookup()
if err != nil {
return false, "", "", err
}
cachedRubyVersion, ok := metadata["ruby_version"].(string)
rubyVersionMatch := true
if ok {
rubyVersionMatch, err = ip.versionResolver.CompareMajorMinor(cachedRubyVersion, rubyVersion)
if err != nil {
return false, "", "", err
}
}
var sum string
_, err = os.Stat(filepath.Join(workingDir, "Gemfile.lock"))
if err != nil {
if !os.IsNotExist(err) {
return false, "", "", err
}
} else {
sum, err = ip.calculator.Sum(filepath.Join(workingDir, "Gemfile"), filepath.Join(workingDir, "Gemfile.lock"))
if err != nil {
return false, "", "", err
}
}
cachedSHA, ok := metadata["cache_sha"].(string)
cacheMatch := ok && cachedSHA == sum
shouldRun := !cacheMatch || !rubyVersionMatch
return shouldRun, sum, rubyVersion, nil
}
// Execute will configure and install a set of gems into a layer location using
// the Bundler CLI.
//
// First, to configure the Bundler environment, Execute will copy the local
// Bundler configuration, if any, into the target layer path. The configuration
// file created in the layer will become the defacto configuration file by
// setting `BUNDLE_USER_CONFIG` in the local environment while executing the
// subsequent Bundle CLI commands. The configuration will then be modifed with
// any settings specific to the invocation of Execute. These configurations
// will override any settings previously applied in the local Bundle
// configuration.
//
// Once fully configured, Execute will run "bundle install" as a child process.
// During the execution of the "bundle install" process, Execute will have
// configured the command to use any locally vendored cache, enabling offline
// execution.
func (ip BundleInstallProcess) Execute(workingDir, layerPath string, config map[string]string, keepBuildFiles bool) error {
ip.logger.Debug.Subprocess("Setting up bundle install config paths:")
localConfigPath := filepath.Join(workingDir, ".bundle", "config")
backupConfigPath := filepath.Join(workingDir, ".bundle", "config.bak")
globalConfigPath := filepath.Join(layerPath, "config")
ip.logger.Debug.Subprocess(" Local config path: %s", localConfigPath)
ip.logger.Debug.Subprocess(" Backup config path: %s", backupConfigPath)
ip.logger.Debug.Subprocess(" Global config path: %s", globalConfigPath)
err := os.RemoveAll(globalConfigPath)
if err != nil {
return err
}
err = os.MkdirAll(layerPath, os.ModePerm)
if err != nil {
return err
}
if _, err := os.Stat(localConfigPath); err == nil {
if _, err := os.Stat(backupConfigPath); err == nil {
err = pfs.Copy(backupConfigPath, localConfigPath)
if err != nil {
return err
}
}
err = pfs.Copy(localConfigPath, globalConfigPath)
if err != nil {
return err
}
err = pfs.Copy(localConfigPath, backupConfigPath)
if err != nil {
return err
}
}
ip.logger.Debug.Subprocess("Adding global config path to $BUNDLE_USER_CONFIG")
ip.logger.Debug.Break()
env := append(os.Environ(), fmt.Sprintf("BUNDLE_USER_CONFIG=%s", globalConfigPath))
var keys []string
for key := range config {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
args := []string{"config", "--global", key, config[key]}
ip.logger.Subprocess("Running 'bundle %s'", strings.Join(args, " "))
err := ip.executable.Execute(pexec.Execution{
Args: args,
Stdout: ip.logger.ActionWriter,
Stderr: ip.logger.ActionWriter,
Env: env,
})
if err != nil {
return fmt.Errorf("failed to execute bundle config output:\nerror: %s", err)
}
}
buffer := bytes.NewBuffer(nil)
errorBuffer := bytes.NewBuffer(nil)
args := []string{"config", "--global", "cache_path", "--parseable"}
ip.logger.Subprocess("Running 'bundle %s'", strings.Join(args, " "))
err = ip.executable.Execute(pexec.Execution{
Args: args,
Stdout: buffer,
Stderr: errorBuffer,
Env: env,
})
if err != nil {
return fmt.Errorf("failed to execute bundle config output:\n%s\nerror: %s", errorBuffer.String(), err)
}
cachePath := filepath.Join("vendor", "cache")
if buffer.String() != "" {
// output is in the form: cache_path=path/to/custom/cache
cachePathRaw := (strings.SplitN(buffer.String(), "=", 2))[1]
cachePath = strings.Trim(cachePathRaw, "\n")
}
ip.logger.Action(buffer.String())
args = []string{"install"}
_, err = os.Stat(filepath.Join(workingDir, cachePath))
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
args = append(args, "--local")
}
ip.logger.Subprocess("Running 'bundle %s'", strings.Join(args, " "))
err = ip.executable.Execute(pexec.Execution{
Args: args,
Stdout: ip.logger.ActionWriter,
Stderr: ip.logger.ActionWriter,
Env: env,
})
if err != nil {
return fmt.Errorf("failed to execute bundle install output:\n%s\nerror: %s", buffer.String(), err)
}
if !keepBuildFiles {
err = filepath.Walk(layerPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
switch filepath.Base(path) {
case "Makefile", "mkmf.log", "gem_make.out":
err = os.Remove(path)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to cleanup gem extension build files: %w", err)
}
}
return nil
}