forked from cloudfoundry/java-buildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibm_jre_initializer.rb
187 lines (154 loc) · 5.78 KB
/
ibm_jre_initializer.rb
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
# frozen_string_literal: true
# Cloud Foundry Java Buildpack
# Copyright 2013-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
#
# http://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.
require 'fileutils'
require 'java_buildpack/component/versioned_dependency_component'
require 'java_buildpack/jre'
require 'java_buildpack/util/tokenized_version'
module JavaBuildpack
module Jre
# Encapsulates the detect, compile, and release functionality for selecting a JRE.
class IbmJreInitializer < JavaBuildpack::Component::VersionedDependencyComponent
# Creates an instance
#
# @param [Hash] context a collection of utilities used the component
def initialize(context)
@application = context[:application]
@component_name = context[:component_name]
@configuration = context[:configuration]
@droplet = context[:droplet]
@droplet.java_home.root = @droplet.sandbox + 'jre/'
end
# (see JavaBuildpack::Component::BaseComponent#detect)
def detect
@version, @uri = JavaBuildpack::Repository::ConfiguredItem.find_item(@component_name,
@configuration)
@droplet.java_home.version = @version
super
end
# (see JavaBuildpack::Component::BaseComponent#compile)
def compile
download(@version, @uri, @component_name) do |file|
with_timing "Installing #{@component_name} to #{@droplet.sandbox.relative_path_from(@droplet.root)}" do
install_bin(@droplet.sandbox, file)
end
end
@droplet.copy_resources
end
# (see JavaBuildpack::Component::BaseComponent#release)
def release
@droplet
.java_opts
.add_system_property('java.io.tmpdir', '$TMPDIR')
.add_preformatted_options('-Xtune:virtualized')
.add_preformatted_options('-Xshareclasses:none')
@droplet.java_opts.concat mem_opts
end
private
# constant HEAP_RATIO is the ratio of memory assigned to the heap
# as against the container total and is set using -Xmx.
HEAP_RATIO = 0.75
KILO = 1024
private_constant :HEAP_RATIO, :KILO
def install_bin(target_directory, file)
FileUtils.mkdir_p target_directory
response_file = Tempfile.new('response.properties')
response_file.puts('INSTALLER_UI=silent')
response_file.puts('LICENSE_ACCEPTED=TRUE')
response_file.puts("USER_INSTALL_DIR=#{target_directory}")
response_file.close
File.chmod(0o755, file.path) unless File.executable?(file.path)
shell "#{file.path} -i silent -f #{response_file.path} 2>&1"
end
def mem_opts
mopts = []
total_memory = memory_limit_finder
if total_memory.nil?
# if no memory option has been set by cloudfoundry, we just assume defaults
else
calculated_heap_ratio = heap_ratio_verification(heap_ratio)
heap_size = heap_size_calculator(total_memory, calculated_heap_ratio)
mopts.push "-Xmx#{heap_size}"
end
mopts
end
def heap_ratio
@configuration['heap_ratio'] || HEAP_RATIO
end
def memory_limit_finder
memory_limit = ENV['MEMORY_LIMIT']
return nil unless memory_limit
memory_limit_size = memory_size_bytes(memory_limit)
raise "Invalid negative $MEMORY_LIMIT #{memory_limit}" if memory_limit_size.negative?
memory_limit_size
end
def memory_size_bytes(size)
if size == '0'
bytes = 0
else
raise "Invalid memory size '#{size}'" if !size || size.length < 2
unit = size[-1]
value = size[0..-2]
raise "Invalid memory size '#{size}'" unless check_is_integer? value
value = size.to_i
# store the bytes
bytes = calculate_bytes(unit, value)
end
bytes
end
def calculate_bytes(unit, value)
if %w[b B].include?(unit)
bytes = value
elsif %w[k K].include?(unit)
bytes = KILO * value
elsif %w[m M].include?(unit)
bytes = KILO * KILO * value
elsif %w[g G].include?(unit)
bytes = KILO * KILO * KILO * value
else
raise "Invalid unit '#{unit}' in memory size"
end
bytes
end
def check_is_integer?(v)
v = Float(v)
v && v.floor == v
end
def heap_size_calculator(membytes, heapratio)
memory_size_minified(membytes * heapratio)
end
def memory_size_minified(membytes)
giga = membytes / 2**(10 * 3)
mega = membytes / 2**(10 * 2)
kilo = (membytes / 2**(10 * 1)).round
if check_is_integer?(giga)
minified_size_calculator(giga, 'G')
elsif check_is_integer?(mega)
minified_size_calculator(mega, 'M')
elsif check_is_integer?(kilo)
minified_size_calculator(kilo, 'K')
end
end
def minified_size_calculator(order, char)
order.to_i.to_s + char
end
def heap_ratio_verification(ratio)
raise 'Invalid heap ratio' unless ratio.is_a? Numeric
raise 'heap ratio cannot be greater than 100%' unless ratio <= 1
ratio
end
end
end
end