-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbuild.go
136 lines (109 loc) · 4.11 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
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
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libjvm
import (
"fmt"
"github.com/buildpacks/libcnb"
"github.com/heroku/color"
"github.com/paketo-buildpacks/libpak"
"github.com/paketo-buildpacks/libpak/bard"
)
type Build struct {
Logger bard.Logger
}
func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
b.Logger.Title(context.Buildpack)
result := libcnb.NewBuildResult()
cr, err := libpak.NewConfigurationResolver(context.Buildpack, &b.Logger)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create configuration resolver\n%w", err)
}
pr := libpak.PlanEntryResolver{Plan: context.Plan}
dr, err := libpak.NewDependencyResolver(context)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency resolver\n%w", err)
}
dc, err := libpak.NewDependencyCache(context)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency cache\n%w", err)
}
dc.Logger = b.Logger
cl := NewCertificateLoader()
cl.Logger = b.Logger.BodyWriter()
v, _ := cr.Resolve("BP_JVM_VERSION")
if _, ok, err := pr.Resolve("jdk"); err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to resolve jdk plan entry\n%w", err)
} else if ok {
dep, err := dr.Resolve("jdk", v)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
}
jdk, be, err := NewJDK(dep, dc, cl)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create jdk\n%w", err)
}
jdk.Logger = b.Logger
result.Layers = append(result.Layers, jdk)
result.BOM.Entries = append(result.BOM.Entries, be)
}
if e, ok, err := pr.Resolve("jre"); err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to resolve jre plan entry\n%w", err)
} else if ok {
dt := JREType
depJRE, err := dr.Resolve("jre", v)
if libpak.IsNoValidDependencies(err) {
warn := color.New(color.FgYellow, color.Bold)
b.Logger.Header(warn.Sprint("No valid JRE available, providing matching JDK instead. Using a JDK at runtime has security implications."))
dt = JDKType
depJRE, err = dr.Resolve("jdk", v)
}
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
}
jre, be, err := NewJRE(context.Application.Path, depJRE, dc, dt, cl, e.Metadata)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create jre\n%w", err)
}
jre.Logger = b.Logger
result.Layers = append(result.Layers, jre)
result.BOM.Entries = append(result.BOM.Entries, be)
if IsLaunchContribution(e.Metadata) {
helpers := []string{"active-processor-count", "java-opts", "link-local-dns", "memory-calculator",
"openssl-certificate-loader", "security-providers-configurer"}
if IsBeforeJava9(depJRE.Version) {
helpers = append(helpers, "security-providers-classpath-8")
} else {
helpers = append(helpers, "security-providers-classpath-9")
}
h, be := libpak.NewHelperLayer(context.Buildpack, helpers...)
h.Logger = b.Logger
result.Layers = append(result.Layers, h)
result.BOM.Entries = append(result.BOM.Entries, be)
depJVMKill, err := dr.Resolve("jvmkill", "")
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
}
jk, be := NewJVMKill(depJVMKill, dc)
jk.Logger = b.Logger
result.Layers = append(result.Layers, jk)
result.BOM.Entries = append(result.BOM.Entries, be)
jsp := NewJavaSecurityProperties(context.Buildpack.Info)
jsp.Logger = b.Logger
result.Layers = append(result.Layers, jsp)
}
}
return result, nil
}