Skip to content

Commit 2911a0a

Browse files
committed
(maint) Unnest module and class names in Ruby tasks
This unnests module and class names in the Ruby tasks and the Ruby task helper. Using a compact class declaration introduced a bug, as the `PuppetAgent` class was not declared anywhere, which would result in the Ruby tasks failing when run.
1 parent 5f7f243 commit 2911a0a

File tree

5 files changed

+317
-308
lines changed

5 files changed

+317
-308
lines changed

.rubocop.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ Style/BlockDelimiters:
3737
be consistent then.
3838
EnforcedStyle: braces_for_chaining
3939
Style/ClassAndModuleChildren:
40-
Description: Compact style reduces the required amount of indentation.
41-
EnforcedStyle: compact
40+
Enabled: false
4241
Style/EmptyElse:
4342
Description: Enforce against empty else clauses, but allow `nil` for clarity.
4443
EnforcedStyle: empty
@@ -79,8 +78,10 @@ RSpec/MessageSpies:
7978
EnforcedStyle: receive
8079
Style/Documentation:
8180
Exclude:
81+
- files/*
8282
- lib/puppet/parser/functions/**/*
8383
- spec/**/*
84+
- tasks/*
8485
Style/WordArray:
8586
EnforcedStyle: brackets
8687
Performance/AncestorsInclude:

files/rb_task_helper.rb

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
11
# frozen_string_literal: true
22

33
# Puppet Agent task helper
4-
module PuppetAgent::RbTaskHelper
5-
private
6-
7-
def error_result(error_type, error_message)
8-
{
9-
'_error' => {
10-
'msg' => error_message,
11-
'kind' => error_type,
12-
'details' => {},
13-
},
14-
}
15-
end
16-
17-
def puppet_bin_present?
18-
File.exist?(puppet_bin)
19-
end
20-
21-
# Returns the path to the Puppet agent executable
22-
def puppet_bin
23-
@puppet_bin ||= if Puppet.features.microsoft_windows?
24-
puppet_bin_windows
25-
else
26-
'/opt/puppetlabs/bin/puppet'
27-
end
28-
end
29-
30-
# Returns the path to the Puppet agent executable on Windows
31-
def puppet_bin_windows
32-
require 'win32/registry'
33-
34-
install_dir = begin
35-
Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Puppet Labs\Puppet') do |reg|
36-
# Rescue missing key
37-
dir = begin
38-
reg['RememberedInstallDir64']
39-
rescue StandardError
40-
''
41-
end
42-
# Both keys may exist, make sure the dir exists
43-
break dir if File.exist?(dir)
44-
45-
# Rescue missing key
46-
begin
47-
reg['RememberedInstallDir']
48-
rescue StandardError
49-
''
4+
module PuppetAgent
5+
module RbTaskHelper
6+
private
7+
8+
def error_result(error_type, error_message)
9+
{
10+
'_error' => {
11+
'msg' => error_message,
12+
'kind' => error_type,
13+
'details' => {},
14+
},
15+
}
16+
end
17+
18+
def puppet_bin_present?
19+
File.exist?(puppet_bin)
20+
end
21+
22+
# Returns the path to the Puppet agent executable
23+
def puppet_bin
24+
@puppet_bin ||= if Puppet.features.microsoft_windows?
25+
puppet_bin_windows
26+
else
27+
'/opt/puppetlabs/bin/puppet'
28+
end
29+
end
30+
31+
# Returns the path to the Puppet agent executable on Windows
32+
def puppet_bin_windows
33+
require 'win32/registry'
34+
35+
install_dir = begin
36+
Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Puppet Labs\Puppet') do |reg|
37+
# Rescue missing key
38+
dir = begin
39+
reg['RememberedInstallDir64']
40+
rescue StandardError
41+
''
42+
end
43+
# Both keys may exist, make sure the dir exists
44+
break dir if File.exist?(dir)
45+
46+
# Rescue missing key
47+
begin
48+
reg['RememberedInstallDir']
49+
rescue StandardError
50+
''
51+
end
5052
end
53+
rescue Win32::Registry::Error
54+
# Rescue missing registry path
55+
''
5156
end
52-
rescue Win32::Registry::Error
53-
# Rescue missing registry path
54-
''
55-
end
5657

57-
File.join(install_dir, 'bin', 'puppet.bat')
58+
File.join(install_dir, 'bin', 'puppet.bat')
59+
end
5860
end
5961
end

tasks/delete_local_filebucket.rb

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,55 @@
99
require_relative File.join(params['_installdir'], 'puppet_agent', 'files', 'rb_task_helper.rb')
1010

1111
# Task to delete local filebucket
12-
class PuppetAgent::DeleteLocalFilebucket
13-
include PuppetAgent::RbTaskHelper
12+
module PuppetAgent
13+
class DeleteLocalFilebucket
14+
include PuppetAgent::RbTaskHelper
1415

15-
def initialize(force)
16-
@force = force
17-
end
18-
19-
def run
20-
unless puppet_bin_present?
21-
return error_result(
22-
'puppet_agent/no-puppet-bin-error',
23-
"Puppet executable '#{puppet_bin}' does not exist",
24-
)
16+
def initialize(force)
17+
@force = force
2518
end
2619

27-
begin
28-
path = clientbucketdir
29-
if path && !path.empty? && (File.directory?(path) || force)
30-
FileUtils.rm_r(Dir.glob("#{path}/*"), secure: true, force: force)
31-
{ "success": true }
32-
else
20+
def run
21+
unless puppet_bin_present?
22+
return error_result(
23+
'puppet_agent/no-puppet-bin-error',
24+
"Puppet executable '#{puppet_bin}' does not exist",
25+
)
26+
end
27+
28+
begin
29+
path = clientbucketdir
30+
if path && !path.empty? && (File.directory?(path) || force)
31+
FileUtils.rm_r(Dir.glob("#{path}/*"), secure: true, force: force)
32+
{ "success": true }
33+
else
34+
error_result(
35+
'puppet_agent/cannot-remove-error',
36+
"clientbucketdir: '#{path}' does not exist or is not a directory",
37+
)
38+
end
39+
rescue StandardError => e
3340
error_result(
34-
'puppet_agent/cannot-remove-error',
35-
"clientbucketdir: '#{path}' does not exist or is not a directory",
36-
)
41+
'puppet_agent/cannot-remove-error',
42+
"#{e.class}: #{e.message}",
43+
)
3744
end
38-
rescue StandardError => e
39-
error_result(
40-
'puppet_agent/cannot-remove-error',
41-
"#{e.class}: #{e.message}",
42-
)
4345
end
44-
end
4546

46-
private
47+
private
4748

48-
def clientbucketdir
49-
options = {
50-
failonfail: false,
51-
override_locale: false,
52-
}
49+
def clientbucketdir
50+
options = {
51+
failonfail: false,
52+
override_locale: false,
53+
}
5354

54-
command = "#{puppet_bin} config print clientbucketdir"
55-
Puppet::Util::Execution.execute(command, options).strip
56-
end
55+
command = "#{puppet_bin} config print clientbucketdir"
56+
Puppet::Util::Execution.execute(command, options).strip
57+
end
5758

58-
attr_reader :force
59+
attr_reader :force
60+
end
5961
end
6062

6163
if __FILE__ == $PROGRAM_NAME

tasks/facts_diff.rb

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,63 @@
99
require_relative File.join(params['_installdir'], 'puppet_agent', 'files', 'rb_task_helper.rb')
1010

1111
# Task to run `puppet facts diff` command
12-
class PuppetAgent::FactsDiff
13-
include PuppetAgent::RbTaskHelper
12+
module PuppetAgent
13+
class FactsDiff
14+
include PuppetAgent::RbTaskHelper
1415

15-
def initialize(exclude)
16-
@exclude = exclude
17-
end
18-
19-
def run
20-
unless puppet_bin_present?
21-
return error_result(
22-
'puppet_agent/no-puppet-bin-error',
23-
"Puppet executable '#{puppet_bin}' does not exist",
24-
)
16+
def initialize(exclude)
17+
@exclude = exclude
2518
end
2619

27-
unless suitable_puppet_version?
28-
return error_result(
29-
'puppet_agent/no-suitable-puppet-version',
30-
"puppet facts diff command is only available on puppet 6.x(>= 6.20.0), target has: #{Puppet.version}",
31-
)
32-
end
20+
def run
21+
unless puppet_bin_present?
22+
return error_result(
23+
'puppet_agent/no-puppet-bin-error',
24+
"Puppet executable '#{puppet_bin}' does not exist",
25+
)
26+
end
3327

34-
if @exclude && !exclude_parameter_supported?
35-
return error_result(
36-
'puppet_agent/exclude-parameter-not-supported',
37-
"exclude parameter is only available on puppet >= 6.22.0, target has: #{Puppet.version}",
38-
)
39-
end
28+
unless suitable_puppet_version?
29+
return error_result(
30+
'puppet_agent/no-suitable-puppet-version',
31+
"puppet facts diff command is only available on puppet 6.x(>= 6.20.0), target has: #{Puppet.version}",
32+
)
33+
end
4034

41-
options = {
42-
failonfail: true,
43-
override_locale: false
44-
}
35+
if @exclude && !exclude_parameter_supported?
36+
return error_result(
37+
'puppet_agent/exclude-parameter-not-supported',
38+
"exclude parameter is only available on puppet >= 6.22.0, target has: #{Puppet.version}",
39+
)
40+
end
4541

46-
command = [puppet_bin, 'facts', 'diff']
47-
command << '--exclude' << "\"#{Regexp.new(@exclude)}\"" if @exclude && !@exclude.empty?
42+
options = {
43+
failonfail: true,
44+
override_locale: false
45+
}
4846

49-
run_result = Puppet::Util::Execution.execute(command, options)
47+
command = [puppet_bin, 'facts', 'diff']
48+
command << '--exclude' << "\"#{Regexp.new(@exclude)}\"" if @exclude && !@exclude.empty?
5049

51-
minified_run_result = run_result.delete("\n").delete(' ')
52-
minified_run_result == '{}' ? 'No differences found' : run_result
53-
end
50+
run_result = Puppet::Util::Execution.execute(command, options)
5451

55-
private
52+
minified_run_result = run_result.delete("\n").delete(' ')
53+
minified_run_result == '{}' ? 'No differences found' : run_result
54+
end
5655

57-
def suitable_puppet_version?
58-
puppet_version = Puppet.version
59-
Puppet::Util::Package.versioncmp(puppet_version, '6.20.0') >= 0 &&
60-
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
61-
end
56+
private
6257

63-
def exclude_parameter_supported?
64-
puppet_version = Puppet.version
65-
Puppet::Util::Package.versioncmp(puppet_version, '6.22.0') >= 0 &&
66-
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
58+
def suitable_puppet_version?
59+
puppet_version = Puppet.version
60+
Puppet::Util::Package.versioncmp(puppet_version, '6.20.0') >= 0 &&
61+
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
62+
end
63+
64+
def exclude_parameter_supported?
65+
puppet_version = Puppet.version
66+
Puppet::Util::Package.versioncmp(puppet_version, '6.22.0') >= 0 &&
67+
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
68+
end
6769
end
6870
end
6971

0 commit comments

Comments
 (0)