2015-09-08 22:11:42 +00:00
|
|
|
# encoding: utf-8
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
2017-03-25 05:28:42 +00:00
|
|
|
# author: Joe Nuspl
|
2015-09-08 22:11:42 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class GemPackage < Inspec.resource(1)
|
|
|
|
name 'gem'
|
|
|
|
desc 'Use the gem InSpec audit resource to test if a global gem package is installed.'
|
|
|
|
example "
|
|
|
|
describe gem('rubocop') do
|
|
|
|
it { should be_installed }
|
2016-09-21 06:03:17 +00:00
|
|
|
its('version') { should eq '0.33.0' }
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
|
|
|
"
|
|
|
|
|
2017-03-25 05:28:42 +00:00
|
|
|
attr_reader :gem_binary
|
|
|
|
|
|
|
|
def initialize(package_name, gem_binary = nil)
|
2016-03-08 18:06:55 +00:00
|
|
|
@package_name = package_name
|
2017-03-25 05:28:42 +00:00
|
|
|
@gem_binary = case gem_binary
|
|
|
|
when nil
|
|
|
|
'gem'
|
|
|
|
when :chef
|
|
|
|
if inspec.os.windows?
|
2017-04-12 15:22:58 +00:00
|
|
|
'c:\opscode\chef\embedded\bin\gem.bat'
|
2017-03-25 05:28:42 +00:00
|
|
|
else
|
|
|
|
'/opt/chef/embedded/bin/gem'
|
|
|
|
end
|
|
|
|
when :chef_server
|
|
|
|
'/opt/opscode/embedded/bin/gem'
|
|
|
|
else
|
|
|
|
gem_binary
|
|
|
|
end
|
2017-04-12 15:22:58 +00:00
|
|
|
skip_resource 'Unable to retrieve gem information' if info.empty?
|
2015-11-27 13:02:38 +00:00
|
|
|
end
|
2015-09-08 22:11:42 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def info
|
|
|
|
return @info if defined?(@info)
|
|
|
|
|
2017-03-25 05:28:42 +00:00
|
|
|
cmd = inspec.command("#{@gem_binary} list --local -a -q \^#{@package_name}\$")
|
2017-04-04 19:35:40 +00:00
|
|
|
return {} unless cmd.exit_status.zero?
|
2016-03-08 18:06:55 +00:00
|
|
|
|
|
|
|
# extract package name and version
|
|
|
|
# parses data like winrm (1.3.4, 1.3.3)
|
|
|
|
params = /^\s*([^\(]*?)\s*\((.*?)\)\s*$/.match(cmd.stdout.chomp)
|
2017-04-04 19:35:40 +00:00
|
|
|
@info = {
|
|
|
|
installed: !params.nil?,
|
|
|
|
type: 'gem',
|
|
|
|
}
|
|
|
|
return @info unless @info[:installed]
|
2017-04-12 15:22:58 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
versions = params[2].split(',')
|
|
|
|
@info[:name] = params[1]
|
|
|
|
@info[:version] = versions[0]
|
|
|
|
@info
|
|
|
|
end
|
2015-09-08 22:11:42 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def installed?
|
|
|
|
info[:installed] == true
|
|
|
|
end
|
2015-09-08 22:11:42 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def version
|
|
|
|
info[:version]
|
|
|
|
end
|
2015-09-08 22:11:42 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
|
|
|
"gem package #{@package_name}"
|
|
|
|
end
|
2015-09-08 22:11:42 +00:00
|
|
|
end
|
|
|
|
end
|