Showing with 326 additions and 309 deletions.
  1. +3 −2 .rubocop.yml
  2. +8 −0 CHANGELOG.md
  3. +53 −51 files/rb_task_helper.rb
  4. +1 −1 metadata.json
  5. +39 −37 tasks/delete_local_filebucket.rb
  6. +46 −44 tasks/facts_diff.rb
  7. +176 −174 tasks/run.rb
5 changes: 3 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ Style/BlockDelimiters:
be consistent then.
EnforcedStyle: braces_for_chaining
Style/ClassAndModuleChildren:
Description: Compact style reduces the required amount of indentation.
EnforcedStyle: compact
Enabled: false
Style/EmptyElse:
Description: Enforce against empty else clauses, but allow `nil` for clarity.
EnforcedStyle: empty
Expand Down Expand Up @@ -79,8 +78,10 @@ RSpec/MessageSpies:
EnforcedStyle: receive
Style/Documentation:
Exclude:
- files/*
- lib/puppet/parser/functions/**/*
- spec/**/*
- tasks/*
Style/WordArray:
EnforcedStyle: brackets
Performance/AncestorsInclude:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org).

## [v4.12.1](https://github.com/puppetlabs/puppetlabs-puppet_agent/tree/v4.12.1) (2022-07-13)

[Full Changelog](https://github.com/puppetlabs/puppetlabs-puppet_agent/compare/v4.12.0...v4.12.1)

### Fixed

- \(maint\) Unnest module and class names in Ruby tasks [\#613](https://github.com/puppetlabs/puppetlabs-puppet_agent/pull/613) ([beechtom](https://github.com/beechtom))

## [v4.12.0](https://github.com/puppetlabs/puppetlabs-puppet_agent/tree/v4.12.0) (2022-07-13)

[Full Changelog](https://github.com/puppetlabs/puppetlabs-puppet_agent/compare/v4.11.0...v4.12.0)
Expand Down
104 changes: 53 additions & 51 deletions files/rb_task_helper.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
# frozen_string_literal: true

# Puppet Agent task helper
module PuppetAgent::RbTaskHelper
private

def error_result(error_type, error_message)
{
'_error' => {
'msg' => error_message,
'kind' => error_type,
'details' => {},
},
}
end

def puppet_bin_present?
File.exist?(puppet_bin)
end

# Returns the path to the Puppet agent executable
def puppet_bin
@puppet_bin ||= if Puppet.features.microsoft_windows?
puppet_bin_windows
else
'/opt/puppetlabs/bin/puppet'
end
end

# Returns the path to the Puppet agent executable on Windows
def puppet_bin_windows
require 'win32/registry'

install_dir = begin
Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Puppet Labs\Puppet') do |reg|
# Rescue missing key
dir = begin
reg['RememberedInstallDir64']
rescue StandardError
''
end
# Both keys may exist, make sure the dir exists
break dir if File.exist?(dir)

# Rescue missing key
begin
reg['RememberedInstallDir']
rescue StandardError
''
module PuppetAgent
module RbTaskHelper
private

def error_result(error_type, error_message)
{
'_error' => {
'msg' => error_message,
'kind' => error_type,
'details' => {},
},
}
end

def puppet_bin_present?
File.exist?(puppet_bin)
end

# Returns the path to the Puppet agent executable
def puppet_bin
@puppet_bin ||= if Puppet.features.microsoft_windows?
puppet_bin_windows
else
'/opt/puppetlabs/bin/puppet'
end
end

# Returns the path to the Puppet agent executable on Windows
def puppet_bin_windows
require 'win32/registry'

install_dir = begin
Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Puppet Labs\Puppet') do |reg|
# Rescue missing key
dir = begin
reg['RememberedInstallDir64']
rescue StandardError
''
end
# Both keys may exist, make sure the dir exists
break dir if File.exist?(dir)

# Rescue missing key
begin
reg['RememberedInstallDir']
rescue StandardError
''
end
end
rescue Win32::Registry::Error
# Rescue missing registry path
''
end
rescue Win32::Registry::Error
# Rescue missing registry path
''
end

File.join(install_dir, 'bin', 'puppet.bat')
File.join(install_dir, 'bin', 'puppet.bat')
end
end
end
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppetlabs-puppet_agent",
"version": "4.12.0",
"version": "4.12.1",
"author": "puppetlabs",
"summary": "Upgrades All-In-One Puppet Agents",
"license": "Apache-2.0",
Expand Down
76 changes: 39 additions & 37 deletions tasks/delete_local_filebucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,55 @@
require_relative File.join(params['_installdir'], 'puppet_agent', 'files', 'rb_task_helper.rb')

# Task to delete local filebucket
class PuppetAgent::DeleteLocalFilebucket
include PuppetAgent::RbTaskHelper
module PuppetAgent
class DeleteLocalFilebucket
include PuppetAgent::RbTaskHelper

def initialize(force)
@force = force
end

def run
unless puppet_bin_present?
return error_result(
'puppet_agent/no-puppet-bin-error',
"Puppet executable '#{puppet_bin}' does not exist",
)
def initialize(force)
@force = force
end

begin
path = clientbucketdir
if path && !path.empty? && (File.directory?(path) || force)
FileUtils.rm_r(Dir.glob("#{path}/*"), secure: true, force: force)
{ "success": true }
else
def run
unless puppet_bin_present?
return error_result(
'puppet_agent/no-puppet-bin-error',
"Puppet executable '#{puppet_bin}' does not exist",
)
end

begin
path = clientbucketdir
if path && !path.empty? && (File.directory?(path) || force)
FileUtils.rm_r(Dir.glob("#{path}/*"), secure: true, force: force)
{ "success": true }
else
error_result(
'puppet_agent/cannot-remove-error',
"clientbucketdir: '#{path}' does not exist or is not a directory",
)
end
rescue StandardError => e
error_result(
'puppet_agent/cannot-remove-error',
"clientbucketdir: '#{path}' does not exist or is not a directory",
)
'puppet_agent/cannot-remove-error',
"#{e.class}: #{e.message}",
)
end
rescue StandardError => e
error_result(
'puppet_agent/cannot-remove-error',
"#{e.class}: #{e.message}",
)
end
end

private
private

def clientbucketdir
options = {
failonfail: false,
override_locale: false,
}
def clientbucketdir
options = {
failonfail: false,
override_locale: false,
}

command = "#{puppet_bin} config print clientbucketdir"
Puppet::Util::Execution.execute(command, options).strip
end
command = "#{puppet_bin} config print clientbucketdir"
Puppet::Util::Execution.execute(command, options).strip
end

attr_reader :force
attr_reader :force
end
end

if __FILE__ == $PROGRAM_NAME
Expand Down
90 changes: 46 additions & 44 deletions tasks/facts_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,63 @@
require_relative File.join(params['_installdir'], 'puppet_agent', 'files', 'rb_task_helper.rb')

# Task to run `puppet facts diff` command
class PuppetAgent::FactsDiff
include PuppetAgent::RbTaskHelper
module PuppetAgent
class FactsDiff
include PuppetAgent::RbTaskHelper

def initialize(exclude)
@exclude = exclude
end

def run
unless puppet_bin_present?
return error_result(
'puppet_agent/no-puppet-bin-error',
"Puppet executable '#{puppet_bin}' does not exist",
)
def initialize(exclude)
@exclude = exclude
end

unless suitable_puppet_version?
return error_result(
'puppet_agent/no-suitable-puppet-version',
"puppet facts diff command is only available on puppet 6.x(>= 6.20.0), target has: #{Puppet.version}",
)
end
def run
unless puppet_bin_present?
return error_result(
'puppet_agent/no-puppet-bin-error',
"Puppet executable '#{puppet_bin}' does not exist",
)
end

if @exclude && !exclude_parameter_supported?
return error_result(
'puppet_agent/exclude-parameter-not-supported',
"exclude parameter is only available on puppet >= 6.22.0, target has: #{Puppet.version}",
)
end
unless suitable_puppet_version?
return error_result(
'puppet_agent/no-suitable-puppet-version',
"puppet facts diff command is only available on puppet 6.x(>= 6.20.0), target has: #{Puppet.version}",
)
end

options = {
failonfail: true,
override_locale: false
}
if @exclude && !exclude_parameter_supported?
return error_result(
'puppet_agent/exclude-parameter-not-supported',
"exclude parameter is only available on puppet >= 6.22.0, target has: #{Puppet.version}",
)
end

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

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

minified_run_result = run_result.delete("\n").delete(' ')
minified_run_result == '{}' ? 'No differences found' : run_result
end
run_result = Puppet::Util::Execution.execute(command, options)

private
minified_run_result = run_result.delete("\n").delete(' ')
minified_run_result == '{}' ? 'No differences found' : run_result
end

def suitable_puppet_version?
puppet_version = Puppet.version
Puppet::Util::Package.versioncmp(puppet_version, '6.20.0') >= 0 &&
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
end
private

def exclude_parameter_supported?
puppet_version = Puppet.version
Puppet::Util::Package.versioncmp(puppet_version, '6.22.0') >= 0 &&
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
def suitable_puppet_version?
puppet_version = Puppet.version
Puppet::Util::Package.versioncmp(puppet_version, '6.20.0') >= 0 &&
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
end

def exclude_parameter_supported?
puppet_version = Puppet.version
Puppet::Util::Package.versioncmp(puppet_version, '6.22.0') >= 0 &&
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
end
end
end

Expand Down
Loading