-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate.go
106 lines (85 loc) · 3.86 KB
/
generate.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
package ubinodejsextension
import (
"os"
"strings"
"github.com/paketo-community/ubi-nodejs-extension/internal/utils"
"github.com/paketo-community/ubi-nodejs-extension/structs"
"github.com/Masterminds/semver/v3"
"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/draft"
postal "github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
const PACKAGES = "make gcc gcc-c++ libatomic_ops git openssl-devel nodejs npm nodejs-nodemon nss_wrapper which python3"
const CONFIG_TOML_PATH = "/tmp/config.toml"
//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go
type DependencyManager interface {
Resolve(path, id, version, stack string) (postal.Dependency, error)
Deliver(dependency postal.Dependency, cnbPath, layerPath, platformPath string) error
GenerateBillOfMaterials(dependencies ...postal.Dependency) []packit.BOMEntry
}
func Generate(dependencyManager DependencyManager, logger scribe.Emitter, duringBuildPermissions structs.DuringBuildPermissions, imagesJsonPath string) packit.GenerateFunc {
return func(context packit.GenerateContext) (packit.GenerateResult, error) {
logger.Title("%s %s", context.Info.Name, context.Info.Version)
logger.Process("Resolving Node Engine version")
// Find the version with the highest priority
entryResolver := draft.NewPlanner()
highestPriorityNodeVersion, allNodeVersionsInPriorityOrder := libnodejs.ResolveNodeVersion(entryResolver.Resolve, context.Plan)
if highestPriorityNodeVersion.Name == "" {
return packit.GenerateResult{}, packit.Fail.WithMessage("Node.js no longer requested by build plan")
}
logger.Candidates(allNodeVersionsInPriorityOrder)
configTomlFileContent, err := utils.GenerateConfigTomlContentFromImagesJson(imagesJsonPath, context.Stack)
if err != nil {
return packit.GenerateResult{}, err
}
//save config.toml file
err = os.WriteFile(CONFIG_TOML_PATH, configTomlFileContent, 0644)
if err != nil {
return packit.GenerateResult{}, err
}
nodeVersion, _ := highestPriorityNodeVersion.Metadata["version"].(string)
dependency, err := dependencyManager.Resolve(CONFIG_TOML_PATH, highestPriorityNodeVersion.Name, nodeVersion, context.Stack)
if err != nil {
return packit.GenerateResult{}, err
}
selectedNodeVersion, err := semver.NewVersion(dependency.Version)
if err != nil {
return packit.GenerateResult{}, err
}
selectedNodeMajorVersion := selectedNodeVersion.Major()
var selectedNodeRunImage string
bpNodeRunExtension, bpNodeRunExtensionEnvExists := os.LookupEnv("BP_UBI_RUN_IMAGE_OVERRIDE")
if !bpNodeRunExtensionEnvExists || bpNodeRunExtension == "" {
selectedNodeRunImage = dependency.Source
} else {
logger.Process("Using run image specified by BP_UBI_RUN_IMAGE_OVERRIDE %s", bpNodeRunExtension)
selectedNodeRunImage = bpNodeRunExtension
}
logger.Process("Selected Node Engine Major version %d", selectedNodeMajorVersion)
// Generating build.Dockerfile
buildDockerfileContent, err := utils.GenerateBuildDockerfile(structs.BuildDockerfileProps{
NODEJS_VERSION: selectedNodeMajorVersion,
CNB_USER_ID: duringBuildPermissions.CNB_USER_ID,
CNB_GROUP_ID: duringBuildPermissions.CNB_GROUP_ID,
CNB_STACK_ID: context.Stack,
PACKAGES: PACKAGES,
})
if err != nil {
return packit.GenerateResult{}, err
}
// Generating run.Dockerfile
runDockerfileContent, err := utils.GenerateRunDockerfile(structs.RunDockerfileProps{
Source: selectedNodeRunImage,
})
if err != nil {
return packit.GenerateResult{}, err
}
return packit.GenerateResult{
ExtendConfig: packit.ExtendConfig{Build: packit.ExtendImageConfig{Args: []packit.ExtendImageConfigArg{}}},
BuildDockerfile: strings.NewReader(buildDockerfileContent),
RunDockerfile: strings.NewReader(runDockerfileContent),
}, nil
}
}