-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathextension.go
More file actions
570 lines (484 loc) · 16.2 KB
/
extension.go
File metadata and controls
570 lines (484 loc) · 16.2 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package extensions
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/cloudfoundry/libbuildpack"
"github.com/cloudfoundry/php-buildpack/src/php/util"
)
// Extension defines the interface that all buildpack extensions must implement.
// This is the Go equivalent of Python's ExtensionHelper class.
type Extension interface {
// Name returns the unique name of the extension
Name() string
// ShouldCompile determines if the extension should install its payload
ShouldCompile(ctx *Context) bool
// Configure configures the extension (called early in build)
Configure(ctx *Context) error
// Compile installs/compiles the extension payload
Compile(ctx *Context, installer *Installer) error
// PreprocessCommands returns list of commands to run once before app starts
PreprocessCommands(ctx *Context) ([]string, error)
// ServiceCommands returns map of long-running service commands (name -> command)
ServiceCommands(ctx *Context) (map[string]string, error)
// ServiceEnvironment returns map of environment variables for services
ServiceEnvironment(ctx *Context) (map[string]string, error)
}
// Context contains the buildpack context (environment, paths, VCAP data, etc.)
// This is the Go equivalent of Python's ctx dict.
type Context struct {
// Core directories
BuildDir string
CacheDir string
DepsDir string
DepsIdx string
BPDir string // Buildpack directory
// Environment
Env map[string]string
// Cloud Foundry VCAP data
VcapServices map[string][]Service
VcapApplication Application
// Additional context data (configuration options, etc.)
Data map[string]interface{}
}
// Service represents a Cloud Foundry bound service
type Service struct {
Name string `json:"name"`
Label string `json:"label"`
Tags []string `json:"tags"`
Plan string `json:"plan"`
Credentials map[string]interface{} `json:"credentials"`
}
// Application represents the Cloud Foundry application metadata
type Application struct {
ApplicationID string `json:"application_id"`
ApplicationName string `json:"application_name"`
ApplicationURIs []string `json:"application_uris"`
Name string `json:"name"`
SpaceName string `json:"space_name"`
SpaceID string `json:"space_id"`
OrganizationID string `json:"organization_id"`
OrganizationName string `json:"organization_name"`
}
// NewContext creates a new Context from the environment
func NewContext() (*Context, error) {
ctx := &Context{
BuildDir: os.Getenv("BUILD_DIR"),
CacheDir: os.Getenv("CACHE_DIR"),
DepsDir: os.Getenv("DEPS_DIR"),
DepsIdx: os.Getenv("DEPS_IDX"),
BPDir: os.Getenv("BP_DIR"),
Env: make(map[string]string),
Data: make(map[string]interface{}),
}
// Parse VCAP_SERVICES
if vcapServicesJSON := os.Getenv("VCAP_SERVICES"); vcapServicesJSON != "" {
if err := json.Unmarshal([]byte(vcapServicesJSON), &ctx.VcapServices); err != nil {
return nil, fmt.Errorf("failed to parse VCAP_SERVICES: %w", err)
}
} else {
ctx.VcapServices = make(map[string][]Service)
}
// Parse VCAP_APPLICATION
if vcapAppJSON := os.Getenv("VCAP_APPLICATION"); vcapAppJSON != "" {
if err := json.Unmarshal([]byte(vcapAppJSON), &ctx.VcapApplication); err != nil {
return nil, fmt.Errorf("failed to parse VCAP_APPLICATION: %w", err)
}
}
// Copy environment variables
for _, env := range os.Environ() {
ctx.Env[env] = os.Getenv(env)
}
return ctx, nil
}
// Get retrieves a value from the context data
func (c *Context) Get(key string) (interface{}, bool) {
val, ok := c.Data[key]
return val, ok
}
// Set stores a value in the context data
func (c *Context) Set(key string, value interface{}) {
c.Data[key] = value
}
// GetString retrieves a string value from the context data
func (c *Context) GetString(key string) string {
if val, ok := c.Data[key]; ok {
if str, ok := val.(string); ok {
return str
}
}
return ""
}
// GetStringSlice retrieves a string slice from the context data
func (c *Context) GetStringSlice(key string) []string {
if val, ok := c.Data[key]; ok {
if slice, ok := val.([]string); ok {
return slice
}
}
return nil
}
// FindServiceByName searches for a service by name
func (c *Context) FindServiceByName(name string) *Service {
for _, services := range c.VcapServices {
for i := range services {
if services[i].Name == name {
return &services[i]
}
}
}
return nil
}
// FindServicesByLabel searches for services by label
func (c *Context) FindServicesByLabel(label string) []Service {
if services, ok := c.VcapServices[label]; ok {
return services
}
return nil
}
// HasService checks if a service with the given name exists
func (c *Context) HasService(name string) bool {
return c.FindServiceByName(name) != nil
}
// Installer provides methods for downloading and installing dependencies.
// This is the Go equivalent of Python's install object.
type Installer struct {
ctx *Context
libbuildpackInst LibbuildpackInstaller
}
// LibbuildpackInstaller interface for libbuildpack dependency installation
type LibbuildpackInstaller interface {
InstallDependency(dep libbuildpack.Dependency, outputDir string) error
InstallOnlyVersion(depName, installDir string) error
}
// NewInstaller creates a new Installer
func NewInstaller(ctx *Context) *Installer {
return &Installer{ctx: ctx, libbuildpackInst: nil}
}
// NewInstallerWithLibbuildpack creates an Installer with a libbuildpack installer
func NewInstallerWithLibbuildpack(ctx *Context, libbuildpackInst LibbuildpackInstaller) *Installer {
return &Installer{ctx: ctx, libbuildpackInst: libbuildpackInst}
}
// InstallDependency installs a dependency using the libbuildpack installer
func (i *Installer) InstallDependency(dep libbuildpack.Dependency, outputDir string) error {
if i.libbuildpackInst == nil {
return fmt.Errorf("libbuildpack installer not available")
}
return i.libbuildpackInst.InstallDependency(dep, outputDir)
}
// InstallOnlyVersion installs the only available version of a dependency
func (i *Installer) InstallOnlyVersion(depName, installDir string) error {
if i.libbuildpackInst == nil {
return fmt.Errorf("libbuildpack installer not available")
}
return i.libbuildpackInst.InstallOnlyVersion(depName, installDir)
}
// Package downloads and installs a package based on a key in the context
// This mimics Python's install.package('PACKAGENAME') method
func (i *Installer) Package(packageKey string) error {
// Context keys are typically uppercase (e.g., PHP_VERSION, COMPOSER_VERSION)
// Convert packageKey to uppercase for context lookups
upperKey := strings.ToUpper(packageKey)
// Get the version and URI from context
versionKey := fmt.Sprintf("%s_VERSION", upperKey)
version, ok := i.ctx.Get(versionKey)
if !ok {
return fmt.Errorf("package version not found for key: %s", versionKey)
}
versionStr, ok := version.(string)
if !ok {
return fmt.Errorf("package version is not a string: %s", versionKey)
}
// Use libbuildpack installer if available
if i.libbuildpackInst != nil {
// Construct dependency object - use lowercase for dependency name
dep := libbuildpack.Dependency{
Name: packageKey,
Version: versionStr,
}
// Determine output directory
buildDir := i.ctx.GetString("BUILD_DIR")
outputDir := filepath.Join(buildDir, packageKey)
// Install the dependency
return i.libbuildpackInst.InstallDependency(dep, outputDir)
}
// Fallback: just log what would be done (shouldn't happen in production)
urlKey := fmt.Sprintf("%s_DOWNLOAD_URL", upperKey)
url, ok := i.ctx.Get(urlKey)
if !ok {
return fmt.Errorf("package URL not found for key: %s", urlKey)
}
urlStr, ok := url.(string)
if !ok {
return fmt.Errorf("package URL is not a string: %s", urlKey)
}
fmt.Printf("Would download package %s from %s\n", packageKey, urlStr)
return nil
}
// Registry manages all registered extensions
type Registry struct {
extensions []Extension
}
// NewRegistry creates a new extension registry
func NewRegistry() *Registry {
return &Registry{
extensions: make([]Extension, 0),
}
}
// Register adds an extension to the registry
func (r *Registry) Register(ext Extension) {
r.extensions = append(r.extensions, ext)
}
// Extensions returns all registered extensions
func (r *Registry) Extensions() []Extension {
return r.extensions
}
// ProcessExtensions runs the specified method on all extensions
func (r *Registry) ProcessExtensions(ctx *Context, method string) error {
for _, ext := range r.extensions {
if !ext.ShouldCompile(ctx) {
continue
}
switch method {
case "configure":
if err := ext.Configure(ctx); err != nil {
return fmt.Errorf("extension %s configure failed: %w", ext.Name(), err)
}
default:
return fmt.Errorf("unknown extension method: %s", method)
}
}
return nil
}
// GetPreprocessCommands collects preprocess commands from all extensions
func (r *Registry) GetPreprocessCommands(ctx *Context) ([]string, error) {
var allCommands []string
for _, ext := range r.extensions {
if !ext.ShouldCompile(ctx) {
continue
}
commands, err := ext.PreprocessCommands(ctx)
if err != nil {
return nil, fmt.Errorf("extension %s preprocess commands failed: %w", ext.Name(), err)
}
allCommands = append(allCommands, commands...)
}
return allCommands, nil
}
// GetServiceCommands collects service commands from all extensions
func (r *Registry) GetServiceCommands(ctx *Context) (map[string]string, error) {
allCommands := make(map[string]string)
for _, ext := range r.extensions {
if !ext.ShouldCompile(ctx) {
continue
}
commands, err := ext.ServiceCommands(ctx)
if err != nil {
return nil, fmt.Errorf("extension %s service commands failed: %w", ext.Name(), err)
}
for name, cmd := range commands {
allCommands[name] = cmd
}
}
return allCommands, nil
}
// GetServiceEnvironment collects service environment variables from all extensions
func (r *Registry) GetServiceEnvironment(ctx *Context) (map[string]string, error) {
allEnv := make(map[string]string)
for _, ext := range r.extensions {
if !ext.ShouldCompile(ctx) {
continue
}
env, err := ext.ServiceEnvironment(ctx)
if err != nil {
return nil, fmt.Errorf("extension %s service environment failed: %w", ext.Name(), err)
}
for key, val := range env {
allEnv[key] = val
}
}
return allEnv, nil
}
// CompileExtensions runs the compile method on all extensions
func (r *Registry) CompileExtensions(ctx *Context, installer *Installer) error {
for _, ext := range r.extensions {
if !ext.ShouldCompile(ctx) {
continue
}
if err := ext.Compile(ctx, installer); err != nil {
return fmt.Errorf("extension %s compile failed: %w", ext.Name(), err)
}
}
return nil
}
// ConfigFileEditor provides methods for editing configuration files
// This is the Go equivalent of Python's utils.ConfigFileEditor
type ConfigFileEditor struct {
path string
lines []string
}
// NewConfigFileEditor creates a new config file editor
func NewConfigFileEditor(path string) (*ConfigFileEditor, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file %s: %w", path, err)
}
lines := make([]string, 0)
currentLine := ""
for _, b := range content {
if b == '\n' {
lines = append(lines, currentLine+"\n")
currentLine = ""
} else {
currentLine += string(b)
}
}
if currentLine != "" {
lines = append(lines, currentLine)
}
return &ConfigFileEditor{
path: path,
lines: lines,
}, nil
}
// UpdateLines replaces lines matching a regex pattern with a new line
func (e *ConfigFileEditor) UpdateLines(pattern, replacement string) error {
re, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("invalid regex pattern %q: %w", pattern, err)
}
for i, line := range e.lines {
// Remove trailing newline for matching
lineContent := strings.TrimSuffix(line, "\n")
if re.MatchString(lineContent) {
e.lines[i] = replacement + "\n"
}
}
return nil
}
// AppendLines appends lines to the file
func (e *ConfigFileEditor) AppendLines(newLines []string) {
e.lines = append(e.lines, newLines...)
}
// Save writes the modified content back to the file
func (e *ConfigFileEditor) Save(path string) error {
content := ""
for _, line := range e.lines {
content += line
}
return os.WriteFile(path, []byte(content), 0644)
}
// PHPConfigHelper provides PHP-specific configuration helpers
type PHPConfigHelper struct {
ctx *Context
phpIniPath string
phpFpmPath string
phpIni *ConfigFileEditor
phpFpm *ConfigFileEditor
}
// NewPHPConfigHelper creates a new PHP config helper
func NewPHPConfigHelper(ctx *Context) *PHPConfigHelper {
return &PHPConfigHelper{
ctx: ctx,
phpIniPath: filepath.Join(ctx.BuildDir, "php", "etc", "php.ini"),
phpFpmPath: filepath.Join(ctx.BuildDir, "php", "etc", "php-fpm.conf"),
}
}
// LoadConfig loads the PHP configuration files
func (h *PHPConfigHelper) LoadConfig() error {
var err error
if h.phpIni == nil {
h.phpIni, err = NewConfigFileEditor(h.phpIniPath)
if err != nil {
return fmt.Errorf("failed to load php.ini: %w", err)
}
}
if h.phpFpm == nil {
h.phpFpm, err = NewConfigFileEditor(h.phpFpmPath)
if err != nil {
return fmt.Errorf("failed to load php-fpm.conf: %w", err)
}
}
return nil
}
// PHPIni returns the php.ini config editor
func (h *PHPConfigHelper) PHPIni() *ConfigFileEditor {
return h.phpIni
}
// PHPFpm returns the php-fpm.conf config editor
func (h *PHPConfigHelper) PHPFpm() *ConfigFileEditor {
return h.phpFpm
}
// PHPIniPath returns the path to php.ini
func (h *PHPConfigHelper) PHPIniPath() string {
return h.phpIniPath
}
// LoadUserExtensions loads user-requested PHP extensions from .bp-config/php/php.ini.d/*.ini files
// and updates the context with the parsed extension lists
func LoadUserExtensions(ctx *Context, buildDir string) error {
userPhpIniDir := filepath.Join(buildDir, ".bp-config", "php", "php.ini.d")
if _, err := os.Stat(userPhpIniDir); os.IsNotExist(err) {
return nil
}
fmt.Println(" Loading user-requested extensions from .bp-config/php/php.ini.d")
currentExtensions := ctx.GetStringSlice("PHP_EXTENSIONS")
currentZendExtensions := ctx.GetStringSlice("ZEND_EXTENSIONS")
extensionsToAdd := make(map[string]bool)
zendExtensionsToAdd := make(map[string]bool)
err := filepath.Walk(userPhpIniDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(strings.ToLower(path), ".ini") {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
fmt.Printf(" WARNING: Failed to read %s: %v\n", path, err)
return nil
}
for _, line := range strings.Split(string(content), "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, ";") || strings.HasPrefix(line, "#") || line == "" {
continue
}
if strings.HasPrefix(line, "extension=") || strings.HasPrefix(line, "extension =") {
extLine := strings.TrimSpace(strings.TrimPrefix(line, "extension="))
extLine = strings.TrimSpace(strings.TrimPrefix(extLine, "extension ="))
extName := strings.TrimSuffix(extLine, ".so")
extName = strings.Trim(extName, "\"' ")
if extName != "" {
extensionsToAdd[extName] = true
}
} else if strings.HasPrefix(line, "zend_extension=") || strings.HasPrefix(line, "zend_extension =") {
extLine := strings.TrimSpace(strings.TrimPrefix(line, "zend_extension="))
extLine = strings.TrimSpace(strings.TrimPrefix(extLine, "zend_extension ="))
extName := strings.TrimSuffix(extLine, ".so")
extName = strings.Trim(extName, "\"' ")
if extName != "" {
zendExtensionsToAdd[extName] = true
}
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to scan user ini files: %w", err)
}
for ext := range extensionsToAdd {
currentExtensions = append(currentExtensions, ext)
}
for ext := range zendExtensionsToAdd {
currentZendExtensions = append(currentZendExtensions, ext)
}
ctx.Set("PHP_EXTENSIONS", util.UniqueStrings(currentExtensions))
ctx.Set("ZEND_EXTENSIONS", util.UniqueStrings(currentZendExtensions))
if len(extensionsToAdd) > 0 || len(zendExtensionsToAdd) > 0 {
fmt.Printf(" Found %d extension(s) and %d zend extension(s) in user config\n",
len(extensionsToAdd), len(zendExtensionsToAdd))
}
return nil
}