This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.go
91 lines (73 loc) · 2.76 KB
/
build.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
package staticfile
import (
"fmt"
"path/filepath"
"time"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface InstallProcess --output fakes/install_process.go
type InstallProcess interface {
Execute(context packit.BuildContext, templConfig Config) error
}
//go:generate faux --interface EntryResolver --output fakes/entry_resolver.go
type EntryResolver interface {
Resolve(name string, entries []packit.BuildpackPlanEntry, priorities []interface{}) (packit.BuildpackPlanEntry, []packit.BuildpackPlanEntry)
}
//go:generate faux --interface ScriptWriter --output fakes/script_writer.go
type ScriptWriter interface {
WriteInitScript(profileDPath string) error
WriteStartLoggingScript(profileDPath string) error
}
func Build(
installProcess InstallProcess,
bpYMLParser BpYMLParser,
scriptWriter ScriptWriter,
entryResolver EntryResolver,
logger scribe.Emitter,
clock chronos.Clock,
) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
logger.Process("Parsing buildpack.yml for nginx config")
config, err := bpYMLParser.Parse(filepath.Join(context.WorkingDir, "buildpack.yml"))
if err != nil {
return packit.BuildResult{}, fmt.Errorf("failed to parse buildpack.yml: %v", err)
}
layer, err := context.Layers.Get(LayerNameStaticfile)
if err != nil {
return packit.BuildResult{}, fmt.Errorf("failed to get layer: %v", err)
}
entry, _ := entryResolver.Resolve(StaticfileDependency, context.Plan.Entries, nil)
layer, err = layer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
layer.Launch = entry.Metadata["launch"] == true
logger.Process("Writing profile.d scripts")
err = scriptWriter.WriteInitScript(filepath.Join(layer.Path, "profile.d"))
if err != nil {
return packit.BuildResult{}, fmt.Errorf("failed to write init script: %v", err)
}
err = scriptWriter.WriteStartLoggingScript(filepath.Join(layer.Path, "profile.d"))
if err != nil {
return packit.BuildResult{}, fmt.Errorf("failed to write start_logging script: %v", err)
}
logger.Process("Executing build process")
logger.Subprocess("Filling out nginx.conf template")
duration, err := clock.Measure(func() error {
return installProcess.Execute(context, config)
})
if err != nil {
return packit.BuildResult{}, fmt.Errorf("failed to install config: %v", err)
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
layer.SharedEnv.Default("APP_ROOT", context.WorkingDir)
logger.EnvironmentVariables(layer)
return packit.BuildResult{
Layers: []packit.Layer{layer},
}, nil
}
}