Skip to content

Commit 1a8861a

Browse files
committed
CI: warden-cpi - fixup ruby template scripts
- addresses ruby style / deprecation issues
1 parent 1373784 commit 1a8861a

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed
Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
require 'yaml'
2-
require 'json'
3-
require 'fileutils'
4-
require 'tmpdir'
1+
require "yaml"
2+
require "json"
3+
require "fileutils"
4+
require "tmpdir"
55

66
garden_archive_path = ARGV[0]
77

8-
%w{
8+
%w[
99
/var/vcap/sys/run/garden
1010
/var/vcap/sys/log/garden
1111
/var/vcap/data/garden/depo
1212
/var/vcap/data/garden/bin
1313
/var/vcap/data/tmp
14-
}.each {|path| FileUtils.mkdir_p path}
14+
].each { |path| FileUtils.mkdir_p path }
1515

16-
installed_garden_job_path = File.join('/', 'var', 'vcap', 'jobs', 'garden')
16+
installed_garden_job_path = File.join("/", "var", "vcap", "jobs", "garden")
1717

1818
Dir.mktmpdir do |workspace|
1919
`tar xzf #{garden_archive_path} -C #{workspace}`
20-
garden_job_path = File.join(workspace, 'garden')
20+
garden_job_path = File.join(workspace, "garden")
2121
FileUtils.mkdir_p garden_job_path
22-
`tar xzf #{File.join(workspace, 'jobs', 'garden.tgz')} -C #{garden_job_path}`
23-
garden_job_spec_path = File.join(garden_job_path, 'job.MF')
22+
`tar xzf #{File.join(workspace, "jobs", "garden.tgz")} -C #{garden_job_path}`
23+
garden_job_spec_path = File.join(garden_job_path, "job.MF")
2424
job_spec = YAML.load_file(garden_job_spec_path)
25-
job_spec['packages'].each do |package_name|
26-
package_path = File.join('/', 'var', 'vcap', 'packages', package_name)
25+
job_spec["packages"].each do |package_name|
26+
package_path = File.join("/", "var", "vcap", "packages", package_name)
2727
FileUtils.mkdir_p(package_path)
28-
`tar xzf #{File.join(workspace, 'compiled_packages', "#{package_name}.tgz")} -C #{package_path}`
28+
`tar xzf #{File.join(workspace, "compiled_packages", "#{package_name}.tgz")} -C #{package_path}`
2929
end
30-
context_path = File.join(workspace, 'context.json')
30+
context_path = File.join(workspace, "context.json")
3131
context = {
32-
'default_properties' => job_spec['properties'].map { |key, value| [key, value['default']]}.to_h,
33-
'job_properties' => {
34-
'garden' => {
35-
'allow_host_access': true,
36-
'debug_listen_address': '127.0.0.1:17013',
37-
'default_container_grace_time': '0',
38-
'destroy_containers_on_start': true,
39-
'graph_cleanup_threshold_in_mb': '0',
40-
'listen_address': '127.0.0.1:7777',
41-
'listen_network': 'tcp',
32+
"default_properties" => job_spec["properties"].map { |key, value| [key, value["default"]] }.to_h,
33+
"job_properties" => {
34+
"garden" => {
35+
allow_host_access: true,
36+
debug_listen_address: "127.0.0.1:17013",
37+
default_container_grace_time: "0",
38+
destroy_containers_on_start: true,
39+
graph_cleanup_threshold_in_mb: "0",
40+
listen_address: "127.0.0.1:7777",
41+
listen_network: "tcp"
4242
}
4343
}
4444
}
4545
File.write(context_path, context.to_json)
46-
templates = job_spec['templates']
46+
templates = job_spec["templates"]
4747
templates.each do |src, dst|
48-
src_path = File.join(garden_job_path, 'templates', src)
48+
src_path = File.join(garden_job_path, "templates", src)
4949
dest_path = File.join(installed_garden_job_path, dst)
5050
FileUtils.mkdir_p(File.dirname(dest_path))
51-
`ruby #{File.join(__dir__, 'template-renderer.rb')} #{context_path} #{src_path} #{dest_path}`
51+
`ruby #{File.join(__dir__, "template-renderer.rb")} #{context_path} #{src_path} #{dest_path}`
5252
end
5353
end
5454

55-
`chmod +x #{File.join(installed_garden_job_path, 'bin', '*')}`
55+
`chmod +x #{File.join(installed_garden_job_path, "bin", "*")}`

ci/dockerfiles/warden-cpi/template-renderer.rb

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
class Hash
88
def recursive_merge!(other)
9-
self.merge!(other) do |_, old_value, new_value|
10-
if old_value.class == Hash && new_value.class == Hash
9+
merge!(other) do |_, old_value, new_value|
10+
if old_value.instance_of?(Hash) && new_value.instance_of?(Hash)
1111
old_value.recursive_merge!(new_value)
1212
else
1313
new_value
1414
end
1515
end
16+
1617
self
1718
end
1819
end
@@ -26,20 +27,21 @@ def initialize(spec)
2627
@name = spec["job"]["name"] if spec["job"].is_a?(Hash)
2728
@index = spec["index"]
2829

29-
if !spec['job_properties'].nil?
30-
properties1 = spec['job_properties']
31-
else
32-
properties1 = spec['global_properties'].recursive_merge!(spec['cluster_properties'])
33-
end
30+
properties1 =
31+
if !spec["job_properties"].nil?
32+
spec["job_properties"]
33+
else
34+
spec["global_properties"].recursive_merge!(spec["cluster_properties"])
35+
end
3436

3537
properties = {}
36-
spec['default_properties'].each do |name, value|
38+
spec["default_properties"].each do |name, value|
3739
copy_property(properties, properties1, name, value)
3840
end
3941

40-
@properties = openstruct(properties)
42+
@properties = open_struct(properties)
4143
@raw_properties = properties
42-
@spec = openstruct(spec)
44+
@spec = open_struct(spec)
4345
end
4446

4547
def get_binding
@@ -62,14 +64,16 @@ def if_p(*names)
6264
values = names.map do |name|
6365
value = lookup_property(@raw_properties, name)
6466
return ActiveElseBlock.new(self) if value.nil?
67+
6568
value
6669
end
6770

68-
yield *values
71+
yield(*values)
72+
6973
InactiveElseBlock.new
7074
end
7175

72-
def if_link(name)
76+
def if_link(_name)
7377
false
7478
end
7579

@@ -94,13 +98,13 @@ def copy_property(dst, src, name, default = nil)
9498
dst_ref[keys[-1]] = src_ref.nil? ? default : src_ref
9599
end
96100

97-
def openstruct(object)
101+
def open_struct(object)
98102
case object
99103
when Hash
100-
mapped = object.inject({}) { |h, (k,v)| h[k] = openstruct(v); h }
104+
mapped = object.each_with_object({}) { |(k, v), h| h[k] = open_struct(v) }
101105
OpenStruct.new(mapped)
102106
when Array
103-
object.map { |item| openstruct(item) }
107+
object.map { |item| open_struct(item) }
104108
else
105109
object
106110
end
@@ -142,17 +146,18 @@ def else_if_p(*names, &block)
142146
end
143147

144148
class InactiveElseBlock
145-
def else; end
149+
def else
150+
end
146151

147-
def else_if_p(*names)
152+
def else_if_p(*_names)
148153
InactiveElseBlock.new
149154
end
150155
end
151156
end
152157

153158
# todo do not use JSON in releases
154159
class << JSON
155-
alias dump_array_or_hash dump
160+
alias_method :dump_array_or_hash, :dump
156161

157162
def dump(*args)
158163
arg = args[0]
@@ -170,28 +175,26 @@ def initialize(context)
170175
end
171176

172177
def render(src_path, dst_path)
173-
erb = ERB.new(File.read(src_path), safe_level = nil, trim_mode = "-")
178+
erb = ERB.new(File.read(src_path), trim_mode: "-")
174179
erb.filename = src_path
175180

176-
File.open(dst_path, "w") do |f|
177-
f.write(erb.result(@context.get_binding))
178-
end
181+
File.write(dst_path, erb.result(@context.get_binding))
179182

180-
rescue Exception => e
183+
rescue Exception => e # rubocop:disable Lint/RescueException
181184
name = "#{@context.name}/#{@context.index}"
182185

183-
line_i = e.backtrace.index { |l| l.include?(erb.filename) }
184-
line_num = line_i ? e.backtrace[line_i].split(':')[1] : "unknown"
186+
line_i = e.backtrace&.index { |l| l.include?(erb.filename) }
187+
line_num = line_i ? e.backtrace[line_i].split(":")[1] : "unknown"
185188
location = "(line #{line_num}: #{e.inspect})"
186189

187190
raise("Error filling in template '#{src_path}' for #{name} #{location}")
188191
end
189192
end
190193

191-
if $0 == __FILE__
194+
if $PROGRAM_NAME == __FILE__
192195
context_path, src_path, dst_path = *ARGV
193196

194-
context_hash = JSON.load(File.read(context_path))
197+
context_hash = JSON.parse(File.read(context_path))
195198
context = TemplateEvaluationContext.new(context_hash)
196199

197200
renderer = ERBRenderer.new(context)

0 commit comments

Comments
 (0)