Skip to content

Commit ee55a7f

Browse files
committed
Addressing Rubocop offenses
1 parent f0040a2 commit ee55a7f

File tree

8 files changed

+86
-73
lines changed

8 files changed

+86
-73
lines changed

.rubocop_todo.yml

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2023-04-28 12:30:24 UTC using RuboCop version 1.48.1.
3+
# on 2024-01-17 13:31:29 UTC using RuboCop version 1.50.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 12
9+
# Offense count: 13
1010
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
1111
Metrics/AbcSize:
12-
Max: 94
12+
Max: 92
1313

14-
# Offense count: 1
14+
# Offense count: 2
1515
# Configuration parameters: CountComments, CountAsOne.
1616
Metrics/ClassLength:
17-
Max: 200
17+
Max: 149
1818

19-
# Offense count: 9
19+
# Offense count: 8
2020
# Configuration parameters: AllowedMethods, AllowedPatterns.
2121
Metrics/CyclomaticComplexity:
22-
Max: 25
22+
Max: 23
2323

24-
# Offense count: 19
24+
# Offense count: 20
2525
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
2626
Metrics/MethodLength:
27-
Max: 69
27+
Max: 70
2828

2929
# Offense count: 3
3030
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
@@ -34,7 +34,7 @@ Metrics/ParameterLists:
3434
# Offense count: 7
3535
# Configuration parameters: AllowedMethods, AllowedPatterns.
3636
Metrics/PerceivedComplexity:
37-
Max: 30
37+
Max: 25
3838

3939
# Offense count: 2
4040
# Configuration parameters: IgnoredMetadata.
@@ -48,30 +48,50 @@ RSpec/DescribeClass:
4848
- 'spec/tasks/abs_spec.rb'
4949
- 'spec/unit/task_helper_spec.rb'
5050

51-
# Offense count: 4
51+
# Offense count: 12
5252
# Configuration parameters: CountAsOne.
5353
RSpec/ExampleLength:
54-
Max: 30
54+
Max: 27
5555

56-
# Offense count: 6
56+
# Offense count: 13
5757
RSpec/MultipleExpectations:
5858
Max: 13
5959

60-
# Offense count: 2
60+
# Offense count: 6
6161
# Configuration parameters: AllowSubject.
6262
RSpec/MultipleMemoizedHelpers:
6363
Max: 6
6464

65-
# Offense count: 4
65+
# Offense count: 6
6666
RSpec/StubbedMock:
6767
Exclude:
6868
- 'spec/tasks/abs_spec.rb'
6969

70-
# Offense count: 8
70+
# Offense count: 1
71+
# This cop supports unsafe autocorrection (--autocorrect-all).
72+
Style/CollectionCompact:
73+
Exclude:
74+
- 'tasks/vagrant.rb'
75+
76+
# Offense count: 1
77+
# This cop supports unsafe autocorrection (--autocorrect-all).
78+
# Configuration parameters: AllowedMethods.
79+
# AllowedMethods: nonzero?
80+
Style/IfWithBooleanLiteralBranches:
81+
Exclude:
82+
- 'tasks/vagrant.rb'
83+
84+
# Offense count: 7
7185
Style/MixinUsage:
7286
Exclude:
7387
- 'spec/spec_helper.rb'
7488
- 'tasks/docker.rb'
7589
- 'tasks/docker_exp.rb'
76-
- 'tasks/provision_service.rb'
7790
- 'tasks/vagrant.rb'
91+
92+
# Offense count: 1
93+
# This cop supports unsafe autocorrection (--autocorrect-all).
94+
# Configuration parameters: Methods.
95+
Style/RedundantArgument:
96+
Exclude:
97+
- 'tasks/update_node_pp.rb'

tasks/abs.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ def abs_host
2626
def provision(platform, inventory_location, vars)
2727
uri = URI.parse("https://#{abs_host}/api/v2/request")
2828
jenkins_build_url = if ENV['CI'] == 'true' && ENV['TRAVIS'] == 'true'
29-
ENV['TRAVIS_JOB_WEB_URL']
29+
ENV.fetch('TRAVIS_JOB_WEB_URL', nil)
3030
elsif ENV['CI'] == 'True' && ENV['APPVEYOR'] == 'True'
31-
"https://ci.appveyor.com/project/#{ENV['APPVEYOR_REPO_NAME']}/build/job/#{ENV['APPVEYOR_JOB_ID']}"
31+
"https://ci.appveyor.com/project/#{ENV.fetch('APPVEYOR_REPO_NAME', nil)}/build/job/#{ENV.fetch('APPVEYOR_JOB_ID', nil)}"
3232
elsif ENV['GITHUB_ACTIONS'] == 'true'
33-
"https://github.com/#{ENV['GITHUB_REPOSITORY']}/actions/runs/#{ENV['GITHUB_RUN_ID']}"
33+
"https://github.com/#{ENV.fetch('GITHUB_REPOSITORY', nil)}/actions/runs/#{ENV.fetch('GITHUB_RUN_ID', nil)}"
3434
else
3535
'https://litmus_manual'
3636
end
@@ -91,17 +91,18 @@ def provision(platform, inventory_location, vars)
9191
data.each do |host|
9292
if platform_uses_ssh(host['type'])
9393
node = { 'uri' => host['hostname'],
94-
'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => ENV['ABS_USER'], 'host-key-check' => false, 'connect-timeout' => 120 } },
94+
'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => ENV.fetch('ABS_USER', nil), 'host-key-check' => false, 'connect-timeout' => 120 } },
9595
'facts' => { 'provisioner' => 'abs', 'platform' => host['type'], 'job_id' => job_id } }
9696
if !ENV['ABS_SSH_PRIVATE_KEY'].nil? && !ENV['ABS_SSH_PRIVATE_KEY'].empty?
97-
node['config']['ssh']['private-key'] = ENV['ABS_SSH_PRIVATE_KEY']
97+
node['config']['ssh']['private-key'] = ENV.fetch('ABS_SSH_PRIVATE_KEY', nil)
9898
else
99-
node['config']['ssh']['password'] = ENV['ABS_PASSWORD']
99+
node['config']['ssh']['password'] = ENV.fetch('ABS_PASSWORD', nil)
100100
end
101101
group_name = 'ssh_nodes'
102102
else
103103
node = { 'uri' => host['hostname'],
104-
'config' => { 'transport' => 'winrm', 'winrm' => { 'user' => ENV['ABS_WIN_USER'], 'password' => ENV['ABS_PASSWORD'], 'ssl' => false, 'connect-timeout' => 120 } },
104+
'config' => { 'transport' => 'winrm',
105+
'winrm' => { 'user' => ENV.fetch('ABS_WIN_USER', nil), 'password' => ENV.fetch('ABS_PASSWORD', nil), 'ssl' => false, 'connect-timeout' => 120 } },
105106
'facts' => { 'provisioner' => 'abs', 'platform' => host['type'], 'job_id' => job_id } }
106107
group_name = 'winrm_nodes'
107108
end
@@ -112,7 +113,7 @@ def provision(platform, inventory_location, vars)
112113
add_node_to_group(inventory_hash, node, group_name)
113114
end
114115

115-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
116+
File.write(inventory_full_path, inventory_hash.to_yaml)
116117
{ status: 'ok', nodes: data.length }
117118
end
118119

@@ -146,7 +147,7 @@ def tear_down(node_name, inventory_location)
146147
targets_to_remove.each do |target|
147148
remove_node(inventory_hash, target)
148149
end
149-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
150+
File.write(inventory_full_path, inventory_hash.to_yaml)
150151
{ status: 'ok', removed: targets_to_remove }
151152
end
152153

tasks/docker.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ def install_ssh_components(distro, version, container)
2323
# sometimes the redhat 6 variant containers like to eat their rpmdb, leading to
2424
# issues with "rpmdb: unable to join the environment" errors
2525
# This "fix" is from https://www.srv24x7.com/criticalyum-main-error-rpmdb-open-failed/
26-
run_local_command("docker exec #{container} bash -exc \"rm -f /var/lib/rpm/__db*; "\
27-
'db_verify /var/lib/rpm/Packages; '\
28-
'rpm --rebuilddb; '\
29-
'yum clean all; '\
30-
'yum install -y sudo openssh-server openssh-clients"')
26+
run_local_command("docker exec #{container} bash -exc \"rm -f /var/lib/rpm/__db*; " \
27+
'db_verify /var/lib/rpm/Packages; ' \
28+
'rpm --rebuilddb; ' \
29+
'yum clean all; ' \
30+
'yum install -y sudo openssh-server openssh-clients"')
3131
else
3232
# If systemd is running for init, ensure systemd has finished starting up before proceeding:
33-
check_init_cmd = 'if [[ "$(readlink /proc/1/exe)" == "/usr/lib/systemd/systemd" ]]; then '\
34-
'count=0 ; while ! [[ "$(systemctl is-system-running)" =~ ^running|degraded$ && $count > 20 ]]; '\
35-
'do sleep 0.1 ; count=$((count+1)) ; done ; fi'
33+
check_init_cmd = 'if [[ "$(readlink /proc/1/exe)" == "/usr/lib/systemd/systemd" ]]; then ' \
34+
'count=0 ; while ! [[ "$(systemctl is-system-running)" =~ ^running|degraded$ && $count > 20 ]]; ' \
35+
'do sleep 0.1 ; count=$((count+1)) ; done ; fi'
3636
run_local_command("docker exec #{container} bash -c '#{check_init_cmd}'")
3737
run_local_command("docker exec #{container} yum install -y sudo openssh-server openssh-clients")
3838
end
@@ -74,10 +74,10 @@ def fix_ssh(distro, version, container)
7474
# https://bugzilla.redhat.com/show_bug.cgi?id=1728777
7575
run_local_command("docker exec #{container} sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd") if distro =~ %r{redhat|centos} && version =~ %r{^7}
7676

77-
if !%r{^(7|8|9|2)}.match?(version)
78-
run_local_command("docker exec #{container} service sshd restart")
79-
else
77+
if %r{^(7|8|9|2)}.match?(version)
8078
run_local_command("docker exec #{container} /usr/sbin/sshd")
79+
else
80+
run_local_command("docker exec #{container} service sshd restart")
8181
end
8282
when %r{sles}
8383
run_local_command("docker exec #{container} /usr/sbin/sshd")
@@ -161,7 +161,7 @@ def provision(image, inventory_location, vars)
161161
distro = os_release_facts['ID']
162162
version = os_release_facts['VERSION_ID']
163163

164-
hostname = (ENV['DOCKER_HOST'].nil? || ENV['DOCKER_HOST'].empty?) ? 'localhost' : URI.parse(ENV['DOCKER_HOST']).host || ENV['DOCKER_HOST']
164+
hostname = (ENV['DOCKER_HOST'].nil? || ENV['DOCKER_HOST'].empty?) ? 'localhost' : URI.parse(ENV.fetch('DOCKER_HOST', nil)).host || ENV.fetch('DOCKER_HOST', nil)
165165
begin
166166
# Use the current docker context to determine the docker hostname
167167
docker_context = JSON.parse(run_local_command('docker context inspect'))[0]
@@ -207,7 +207,7 @@ def provision(image, inventory_location, vars)
207207
install_ssh_components(distro, version, full_container_name)
208208
fix_ssh(distro, version, full_container_name)
209209
add_node_to_group(inventory_hash, node, group_name)
210-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
210+
File.write(inventory_full_path, inventory_hash.to_yaml)
211211
{ status: 'ok', node_name: "#{hostname}:#{front_facing_port}", node: node }
212212
end
213213

@@ -222,7 +222,7 @@ def tear_down(node_name, inventory_location)
222222
run_local_command(remove_docker)
223223
remove_node(inventory_hash, node_name)
224224
puts "Removed #{node_name}"
225-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
225+
File.write(inventory_full_path, inventory_hash.to_yaml)
226226
{ status: 'ok' }
227227
end
228228

tasks/docker_exp.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def provision(docker_platform, inventory_location, vars)
3838

3939
group_name = 'docker_nodes'
4040
add_node_to_group(inventory_hash, node, group_name)
41-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
41+
File.write(inventory_full_path, inventory_hash.to_yaml)
4242
{ status: 'ok', node_name: container_id, node: node }
4343
end
4444

@@ -53,7 +53,7 @@ def tear_down(node_name, inventory_location)
5353
run_local_command(remove_docker)
5454
remove_node(inventory_hash, node_name)
5555
puts "Removed #{node_name}"
56-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
56+
File.write(inventory_full_path, inventory_hash.to_yaml)
5757
{ status: 'ok' }
5858
end
5959

tasks/provision_service.rb

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def invoke_cloud_request(params, uri, job_url, verb, retry_attempts)
4646
request.body = if job_url
4747
{ url: job_url, VMs: machines }.to_json
4848
else
49-
{ github_token: ENV['GITHUB_TOKEN'], VMs: machines }.to_json
49+
{ github_token: ENV.fetch('GITHUB_TOKEN', nil), VMs: machines }.to_json
5050
end
5151
when 'delete'
5252
request = Net::HTTP::Delete.new(uri, headers)
@@ -55,11 +55,7 @@ def invoke_cloud_request(params, uri, job_url, verb, retry_attempts)
5555
raise StandardError "Unknown verb: '#{verb}'"
5656
end
5757

58-
if job_url
59-
File.open('request.json', 'wb') do |f|
60-
f.write(request.body)
61-
end
62-
end
58+
File.binwrite('request.json', request.body) if job_url
6359

6460
req_options = {
6561
use_ssl: uri.scheme == 'https',
@@ -89,14 +85,14 @@ def provision(platform, inventory_location, vars, retry_attempts)
8985
# Call the provision service with the information necessary and write the inventory file locally
9086

9187
if ENV['GITHUB_RUN_ID']
92-
job_url = ENV['GITHUB_URL'] || "https://api.github.com/repos/#{ENV['GITHUB_REPOSITORY']}/actions/runs/#{ENV['GITHUB_RUN_ID']}"
88+
job_url = ENV['GITHUB_URL'] || "https://api.github.com/repos/#{ENV.fetch('GITHUB_REPOSITORY', nil)}/actions/runs/#{ENV['GITHUB_RUN_ID']}"
9389
else
9490
puts 'Using GITHUB_TOKEN as no GITHHUB_RUN_ID found'
9591
end
9692
uri = URI.parse(ENV['SERVICE_URL'] || default_uri)
97-
cloud = ENV['CLOUD']
98-
region = ENV['REGION']
99-
zone = ENV['ZONE']
93+
cloud = ENV.fetch('CLOUD', nil)
94+
region = ENV.fetch('REGION', nil)
95+
zone = ENV.fetch('ZONE', nil)
10096
if job_url.nil? && vars
10197
data = JSON.parse(vars.tr(';', ','))
10298
job_url = data['job_url']
@@ -134,12 +130,10 @@ def provision(platform, inventory_location, vars, retry_attempts)
134130
g['targets'] = g['targets'] + bg['targets'] if g['name'] == bg['name']
135131
end
136132
end
137-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
133+
File.write(inventory_full_path, inventory_hash.to_yaml)
138134
else
139135
FileUtils.mkdir_p(File.join(Dir.pwd, '/spec/fixtures'))
140-
File.open(inventory_full_path, 'wb') do |f|
141-
f.write(YAML.dump(response_hash))
142-
end
136+
File.binwrite(inventory_full_path, YAML.dump(response_hash))
143137
end
144138

145139
{

tasks/update_node_pp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def update_file(manifest, target_node)
2121
else
2222
final_manifest = "node '#{target_node}' \n{\n #{manifest} \n}"
2323
end
24-
File.open(site_path, 'w+') { |f| f.write(final_manifest) }
24+
File.write(site_path, final_manifest)
2525
"#{site_path} updated"
2626
end
2727

tasks/update_site_pp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def update_file(manifest)
1111
raise Puppet::Error, "stderr: ' %{stderr}')" % { stderr: stderr } if status != 0
1212

1313
site_path = File.join(path, 'site.pp')
14-
File.open(site_path, 'w+') { |f| f.write(manifest) }
14+
File.write(site_path, manifest)
1515
'site.pp updated'
1616
end
1717

tasks/vagrant.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def generate_vagrantfile(file_path, platform, enable_synced_folder, provider, cp
5454
''
5555
end
5656
vf = <<~VF
57-
Vagrant.configure(\"2\") do |config|
57+
Vagrant.configure("2") do |config|
5858
config.vm.box = '#{platform}'
5959
config.vm.boot_timeout = 600
6060
config.ssh.insert_key = false
@@ -64,9 +64,7 @@ def generate_vagrantfile(file_path, platform, enable_synced_folder, provider, cp
6464
#{provider_config_block}
6565
end
6666
VF
67-
File.open(file_path, 'w') do |f|
68-
f.write(vf)
69-
end
67+
File.write(file_path, vf)
7068
end
7169

7270
def get_vagrant_dir(platform, vagrant_dirs, int = 0)
@@ -187,7 +185,7 @@ def provision(platform, inventory_location, enable_synced_folder, provider, cpus
187185
group_name = 'winrm_nodes'
188186
end
189187
add_node_to_group(inventory_hash, node, group_name)
190-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
188+
File.write(inventory_full_path, inventory_hash.to_yaml)
191189
{ status: 'ok', node_name: node_name, node: node }
192190
end
193191

@@ -203,7 +201,7 @@ def tear_down(node_name, inventory_location)
203201
FileUtils.rm_r(vagrant_env)
204202
end
205203
warn "Removed #{node_name}"
206-
File.open(inventory_full_path, 'w') { |f| f.write inventory_hash.to_yaml }
204+
File.write(inventory_full_path, inventory_hash.to_yaml)
207205
{ status: 'ok' }
208206
end
209207

@@ -213,18 +211,18 @@ def tear_down(node_name, inventory_location)
213211
action = params['action']
214212
node_name = params['node_name']
215213
inventory_location = sanitise_inventory_location(params['inventory'])
216-
enable_synced_folder = params['enable_synced_folder'].nil? ? ENV['VAGRANT_ENABLE_SYNCED_FOLDER'] : params['enable_synced_folder']
214+
enable_synced_folder = params['enable_synced_folder'].nil? ? ENV.fetch('VAGRANT_ENABLE_SYNCED_FOLDER', nil) : params['enable_synced_folder']
217215
if enable_synced_folder.is_a?(String)
218216
enable_synced_folder = enable_synced_folder.casecmp('true').zero? ? true : false
219217
end
220-
provider = params['provider'].nil? ? ENV['VAGRANT_PROVIDER'] : params['provider']
221-
cpus = params['cpus'].nil? ? ENV['VAGRANT_CPUS'] : params['cpus']
222-
memory = params['memory'].nil? ? ENV['VAGRANT_MEMORY'] : params['memory']
223-
hyperv_vswitch = params['hyperv_vswitch'].nil? ? ENV['VAGRANT_HYPERV_VSWITCH'] : params['hyperv_vswitch']
224-
hyperv_smb_username = params['hyperv_smb_username'].nil? ? ENV['VAGRANT_HYPERV_SMB_USERNAME'] : params['hyperv_smb_username']
225-
hyperv_smb_password = params['hyperv_smb_password'].nil? ? ENV['VAGRANT_HYPERV_SMB_PASSWORD'] : params['hyperv_smb_password']
226-
box_url = params['box_url'].nil? ? ENV['VAGRANT_BOX_URL'] : params['box_url']
227-
password = params['password'].nil? ? ENV['VAGRANT_PASSWORD'] : params['password']
218+
provider = params['provider'].nil? ? ENV.fetch('VAGRANT_PROVIDER', nil) : params['provider']
219+
cpus = params['cpus'].nil? ? ENV.fetch('VAGRANT_CPUS', nil) : params['cpus']
220+
memory = params['memory'].nil? ? ENV.fetch('VAGRANT_MEMORY', nil) : params['memory']
221+
hyperv_vswitch = params['hyperv_vswitch'].nil? ? ENV.fetch('VAGRANT_HYPERV_VSWITCH', nil) : params['hyperv_vswitch']
222+
hyperv_smb_username = params['hyperv_smb_username'].nil? ? ENV.fetch('VAGRANT_HYPERV_SMB_USERNAME', nil) : params['hyperv_smb_username']
223+
hyperv_smb_password = params['hyperv_smb_password'].nil? ? ENV.fetch('VAGRANT_HYPERV_SMB_PASSWORD', nil) : params['hyperv_smb_password']
224+
box_url = params['box_url'].nil? ? ENV.fetch('VAGRANT_BOX_URL', nil) : params['box_url']
225+
password = params['password'].nil? ? ENV.fetch('VAGRANT_PASSWORD', nil) : params['password']
228226
raise 'specify a node_name when tearing down' if action == 'tear_down' && node_name.nil?
229227
raise 'specify a platform when provisioning' if action == 'provision' && platform.nil?
230228

0 commit comments

Comments
 (0)