-
Notifications
You must be signed in to change notification settings - Fork 680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance package
resource error handling
#2388
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ class Package < Inspec.resource(1) | |
end | ||
" | ||
|
||
def initialize(package_name = nil, opts = {}) # rubocop:disable Metrics/AbcSize | ||
def initialize(package_name, opts = {}) # rubocop:disable Metrics/AbcSize | ||
@package_name = package_name | ||
@name = @package_name | ||
@cache = nil | ||
|
@@ -45,36 +45,31 @@ def initialize(package_name = nil, opts = {}) # rubocop:disable Metrics/AbcSize | |
elsif ['hpux'].include?(os[:family]) | ||
@pkgman = HpuxPkg.new(inspec) | ||
else | ||
return skip_resource 'The `package` resource is not supported on your OS yet.' | ||
raise Inspec::Exceptions::ResourceSkipped, 'The `package` resource is not supported on your OS yet.' | ||
end | ||
|
||
evaluate_missing_requirements | ||
end | ||
|
||
# returns true if the package is installed | ||
def installed?(_provider = nil, _version = nil) | ||
return false if info.nil? | ||
info[:installed] == true | ||
end | ||
|
||
# returns true it the package is held (if the OS supports it) | ||
def held?(_provider = nil, _version = nil) | ||
return false if info.nil? | ||
return false unless info.key?(:held) | ||
info[:held] == true | ||
end | ||
|
||
# returns the package description | ||
def info | ||
return @cache if [email protected]? | ||
return nil if @pkgman.nil? | ||
@pkgman.info(@package_name) | ||
end | ||
|
||
# return the package version | ||
def version | ||
info = @pkgman.info(@package_name) | ||
return nil if info.nil? | ||
info[:version] | ||
end | ||
|
||
|
@@ -87,7 +82,7 @@ def to_s | |
def evaluate_missing_requirements | ||
missing_requirements_string = @pkgman.missing_requirements.uniq.join(', ') | ||
return if missing_requirements_string.empty? | ||
skip_resource "The following requirements are not met for this resource: #{missing_requirements_string}" | ||
raise Inspec::Exceptions::ResourceSkipped, "The following requirements are not met for this resource: #{missing_requirements_string}" | ||
end | ||
end | ||
|
||
|
@@ -99,7 +94,7 @@ def initialize(inspec) | |
|
||
def missing_requirements | ||
# Each provider can provide an Array of missing requirements that will be | ||
# combined into a `skip_resource` message | ||
# combined into a `ResourceSkipped` exception message. | ||
[] | ||
end | ||
end | ||
|
@@ -108,7 +103,7 @@ def missing_requirements | |
class Deb < PkgManagement | ||
def info(package_name) | ||
cmd = inspec.command("dpkg -s #{package_name}") | ||
return nil if cmd.exit_status.to_i != 0 | ||
return {} if cmd.exit_status.to_i != 0 | ||
|
||
params = SimpleConfig.new( | ||
cmd.stdout.chomp, | ||
|
@@ -152,7 +147,7 @@ def info(package_name) | |
cmd = inspec.command(rpm_cmd) | ||
# CentOS does not return an error code if the package is not installed, | ||
# therefore we need to check for emptyness | ||
return nil if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty? | ||
return {} if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty? | ||
params = SimpleConfig.new( | ||
cmd.stdout.chomp, | ||
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/, | ||
|
@@ -195,7 +190,7 @@ class Brew < PkgManagement | |
def info(package_name) | ||
brew_path = inspec.command('brew').exist? ? 'brew' : '/usr/local/bin/brew' | ||
cmd = inspec.command("#{brew_path} info --json=v1 #{package_name}") | ||
return nil if cmd.exit_status.to_i != 0 | ||
return {} if cmd.exit_status.to_i != 0 | ||
# parse data | ||
pkg = JSON.parse(cmd.stdout)[0] | ||
{ | ||
|
@@ -204,16 +199,18 @@ def info(package_name) | |
version: pkg['installed'][0]['version'], | ||
type: 'brew', | ||
} | ||
rescue JSON::ParserError => _e | ||
return nil | ||
rescue JSON::ParserError => e | ||
raise Inspec::Exceptions::ResourceFailed, | ||
'Failed to parse JSON from `brew` command. ' \ | ||
"Error: #{e}" | ||
end | ||
end | ||
|
||
# Arch Linux | ||
class Pacman < PkgManagement | ||
def info(package_name) | ||
cmd = inspec.command("pacman -Qi #{package_name}") | ||
return nil if cmd.exit_status.to_i != 0 | ||
return {} if cmd.exit_status.to_i != 0 | ||
|
||
params = SimpleConfig.new( | ||
cmd.stdout.chomp, | ||
|
@@ -233,7 +230,7 @@ def info(package_name) | |
class HpuxPkg < PkgManagement | ||
def info(package_name) | ||
cmd = inspec.command("swlist -l product | grep #{package_name}") | ||
return nil if cmd.exit_status.to_i != 0 | ||
return {} if cmd.exit_status.to_i != 0 | ||
pkg = cmd.stdout.strip.split(' ') | ||
{ | ||
name: pkg[0], | ||
|
@@ -268,8 +265,10 @@ def info(package_name) | |
|
||
begin | ||
package = JSON.parse(cmd.stdout) | ||
rescue JSON::ParserError => _e | ||
return nil | ||
rescue JSON::ParserError => e | ||
raise Inspec::Exceptions::ResourceFailed, | ||
'Failed to parse JSON from PowerShell. ' \ | ||
"Error: #{e}" | ||
end | ||
|
||
# What if we match multiple packages? just pick the first one for now. | ||
|
@@ -288,7 +287,7 @@ def info(package_name) | |
class BffPkg < PkgManagement | ||
def info(package_name) | ||
cmd = inspec.command("lslpp -cL #{package_name}") | ||
return nil if cmd.exit_status.to_i != 0 | ||
return {} if cmd.exit_status.to_i != 0 | ||
|
||
bff_pkg = cmd.stdout.split("\n").last.split(':') | ||
{ | ||
|
@@ -313,7 +312,7 @@ def info(package_name) | |
# solaris 10 | ||
def solaris10_info(package_name) | ||
cmd = inspec.command("pkginfo -l #{package_name}") | ||
return nil if cmd.exit_status.to_i != 0 | ||
return {} if cmd.exit_status.to_i != 0 | ||
|
||
params = SimpleConfig.new( | ||
cmd.stdout.chomp, | ||
|
@@ -334,7 +333,7 @@ def solaris10_info(package_name) | |
# solaris 11 | ||
def solaris11_info(package_name) | ||
cmd = inspec.command("pkg info #{package_name}") | ||
return nil if cmd.exit_status.to_i != 0 | ||
return {} if cmd.exit_status.to_i != 0 | ||
|
||
params = SimpleConfig.new( | ||
cmd.stdout.chomp, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this - in the same vein as my prior comment, since we're adopting exceptions as the new normal, moving toward that is a good thing.