This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
contributor.go
303 lines (251 loc) · 8.6 KB
/
contributor.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
package packages
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"
"github.com/buildpack/libbuildpack/application"
"github.com/cloudfoundry/libcfbuildpack/build"
"github.com/cloudfoundry/libcfbuildpack/helper"
"github.com/cloudfoundry/libcfbuildpack/layers"
"github.com/paketo-buildpacks/php-composer/composer"
"github.com/paketo-buildpacks/php-web/config"
)
type Metadata struct {
Name string
Hash string
}
func (m Metadata) Identity() (name string, version string) {
return m.Name, m.Hash
}
type Contributor struct {
app application.Application
composerLayer layers.Layer
composerPackagesLayer layers.Layer
cacheLayer layers.Layer
composerMetadata Metadata
composer composer.Composer
composerBuildpackYAML composer.BuildpackYAML
}
func generateRandomHash() [32]byte {
randBuf := make([]byte, 512)
rand.Read(randBuf)
return sha256.Sum256(randBuf)
}
// NewContributor creates a new "packages" contributor for installing Composer packages
func NewContributor(context build.Build, composerPharPath string) (Contributor, bool, error) {
buildpackYAML, err := composer.LoadComposerBuildpackYAML(context.Application.Root)
if err != nil {
return Contributor{}, false, err
}
err = composer.WarnComposerBuildpackYAML(context.Logger, context.Buildpack.Info.Version, context.Application.Root)
if err != nil {
return Contributor{}, false, err
}
path, err := composer.FindComposer(context.Application.Root, buildpackYAML.Composer.JsonPath)
if err != nil {
return Contributor{}, false, err
}
composerDir := filepath.Dir(path)
lockPath := filepath.Join(composerDir, composer.ComposerLock)
var hash [32]byte
if exists, err := helper.FileExists(lockPath); err != nil {
return Contributor{}, false, err
} else if exists {
buf, err := ioutil.ReadFile(lockPath)
if err != nil {
return Contributor{}, false, err
}
hash = sha256.Sum256(buf)
} else {
hash = generateRandomHash()
}
contributor := Contributor{
app: context.Application,
composerLayer: context.Layers.Layer(composer.Dependency),
composerPackagesLayer: context.Layers.Layer(composer.PackagesDependency),
cacheLayer: context.Layers.Layer(composer.CacheDependency),
composerMetadata: Metadata{"PHP Composer", hex.EncodeToString(hash[:])},
composer: composer.NewComposer(composerDir, composerPharPath, context.Logger),
composerBuildpackYAML: buildpackYAML,
}
if err := contributor.initializeEnv(buildpackYAML.Composer.VendorDirectory); err != nil {
return Contributor{}, false, err
}
return contributor, true, nil
}
func (c Contributor) SetupVendorDir() error {
composerLayerVendorDir := filepath.Join(c.composerPackagesLayer.Root, c.composerBuildpackYAML.Composer.VendorDirectory)
composerAppVendorDir := filepath.Join(c.app.Root, c.composerBuildpackYAML.Composer.VendorDirectory)
exists, err := helper.FileExists(composerAppVendorDir)
if err != nil {
return err
} else if exists {
if err := helper.CopyDirectory(composerAppVendorDir, composerLayerVendorDir); err != nil {
return err
}
if err := os.RemoveAll(composerAppVendorDir); err != nil {
return err
}
}
// symlink vendor_home to "vendor" under the app root so PHP apps can find Composer dependencies
return helper.WriteSymlink(composerLayerVendorDir, composerAppVendorDir)
}
func (c Contributor) Contribute() error {
randomHash := generateRandomHash()
if err := c.cacheLayer.Contribute(Metadata{"PHP Composer Cache", hex.EncodeToString(randomHash[:])}, func(layer layers.Layer) error { return nil }, layers.Cache); err != nil {
return err
}
if err := c.alwaysRunComposerInit(c.composerPackagesLayer); err != nil {
return err
}
if err := c.SetupVendorDir(); err != nil {
return err
}
return c.composerPackagesLayer.Contribute(c.composerMetadata, c.contributeComposerPackages, layers.Launch)
}
func (c Contributor) configureGithubOauthToken() error {
githubOauthToken := os.Getenv("COMPOSER_GITHUB_OAUTH_TOKEN")
if githubOauthToken != "" {
github, err := NewDefaultGithub(githubOauthToken)
if err != nil {
return err
}
if ok, err := github.validateToken(); err != nil {
return err
} else if ok {
if err := c.composer.Config("github-oauth.github.com", githubOauthToken, true); err != nil {
return err
}
}
if ok, err := github.checkRateLimit(); err != nil {
return err
} else if !ok {
c.composer.Logger.BodyWarning("The GitHub api rate limit has been exceeded. " +
"Composer will continue by downloading from source, which might result in slower downloads. " +
"You can increase your rate limit with a GitHub OAuth token. " +
"Please obtain a GitHub OAuth token by registering your application at " +
"https://github.com/settings/applications/new. " +
"Then set COMPOSER_GITHUB_OAUTH_TOKEN in your environment to the value of this token.")
}
}
return nil
}
func (c Contributor) installGlobalPackages() error {
if len(c.composerBuildpackYAML.Composer.InstallGlobal) > 0 {
binPath := strings.Join([]string{os.Getenv("PATH"), filepath.Join(c.composerPackagesLayer.Root, "global/vendor/bin")}, string(os.PathListSeparator))
err := os.Setenv("PATH", binPath)
if err != nil {
return err
}
if err := c.setGlobalVendorDir(); err != nil {
return err
}
if err := c.composer.Global(c.composerBuildpackYAML.Composer.InstallGlobal...); err != nil {
return err
}
}
return nil
}
func (c Contributor) alwaysRunComposerInit(layer layers.Layer) error {
phpExtensions, err := c.composer.CheckPlatformReqs()
if err != nil {
return err
}
if err := c.enablePHPExtensions(phpExtensions); err != nil {
return err
}
if err := c.configureGithubOauthToken(); err != nil {
return err
}
if err := c.installGlobalPackages(); err != nil {
return err
}
err = c.warnAboutPublicComposerFiles(layer)
if err != nil {
return err
}
return c.setAppVendorDir()
}
func (c Contributor) contributeComposerPackages(layer layers.Layer) error {
if err := os.MkdirAll(layer.Root, os.ModePerm); err != nil {
return err
}
return c.composer.Install(c.composerBuildpackYAML.Composer.InstallOptions...)
}
func (c Contributor) enablePHPExtensions(extensions []string) error {
buf := bytes.Buffer{}
for _, extension := range extensions {
buf.WriteString(fmt.Sprintf("extension = %s.so\n", extension))
}
return helper.WriteFile(filepath.Join(c.app.Root, ".php.ini.d", "composer-extensions.ini"), 0655, buf.String())
}
func (c Contributor) warnAboutPublicComposerFiles(layer layers.Layer) error {
bpYAML, err := config.LoadBuildpackYAML(c.app.Root)
if err != nil {
return err
}
composerLockPath := filepath.Join(c.app.Root, bpYAML.Config.WebDirectory, "composer.lock")
composerJSONPath := filepath.Join(c.app.Root, bpYAML.Config.WebDirectory, "composer.json")
lockExists, err := helper.FileExists(composerLockPath)
if err != nil {
return err
}
jsonExists, err := helper.FileExists(composerJSONPath)
if err != nil {
return err
}
if lockExists || jsonExists {
layer.Logger.Info("WARNING: your composer.lock or composer.json files are located in the web directory which could publicly expose them. Please make sure this is really what you want")
}
return nil
}
func (c Contributor) initializeEnv(vendorDirectory string) error {
// override anything possibly set by the user
err := os.Setenv("COMPOSER_HOME", filepath.Join(c.composerLayer.Root, ".composer"))
if err != nil {
return err
}
err = os.Setenv("COMPOSER_CACHE_DIR", filepath.Join(c.cacheLayer.Root, "cache"))
if err != nil {
return err
}
// set `--no-interaction` flag to every command, since users cannot interact
err = os.Setenv("COMPOSER_NO_INTERACTION", "1")
if err != nil {
return err
}
err = os.Setenv("PHPRC", filepath.Join(c.composerLayer.Root, "composer-php.ini"))
if err != nil {
return err
}
err = os.Setenv("PHP_INI_SCAN_DIR", filepath.Join(c.app.Root, ".php.ini.d"))
if err != nil {
return err
}
binPath := strings.Join([]string{os.Getenv("PATH"), filepath.Join(c.app.Root, c.composerBuildpackYAML.Composer.VendorDirectory, "bin")}, string(os.PathListSeparator))
err = os.Setenv("PATH", binPath)
if err != nil {
return err
}
return nil
}
func (c Contributor) setGlobalVendorDir() error {
err := os.Setenv("COMPOSER_VENDOR_DIR", filepath.Join(c.composerPackagesLayer.Root, "global", "vendor"))
if err != nil {
return err
}
return nil
}
func (c Contributor) setAppVendorDir() error {
err := os.Setenv("COMPOSER_VENDOR_DIR", filepath.Join(c.composerPackagesLayer.Root, c.composerBuildpackYAML.Composer.VendorDirectory))
if err != nil {
return err
}
return nil
}