inspec/lib/resources/platform.rb
Jared Quick 10dc5621fb Add platform resource and platform supports (#2393)
* Add platform resource and platform supports.

Signed-off-by: Jared Quick <jquick@chef.io>

* Cache platform and inspec checks and implement inspec_version.

Signed-off-by: Jared Quick <jquick@chef.io>

* Deprecate current inspec support in favor of inspec_version.

Signed-off-by: Jared Quick <jquick@chef.io>

* Update resource/profile skip messages.

Signed-off-by: Jared Quick <jquick@chef.io>

* Update load_resource to use platform instead of os.

Signed-off-by: Jared Quick <jquick@chef.io>

* Update platform example.

Signed-off-by: Jared Quick <jquick@chef.io>
2018-01-02 11:04:13 -08:00

92 lines
2.1 KiB
Ruby

# encoding: utf-8
module Inspec::Resources
class PlatformResource < Inspec.resource(1)
name 'platform'
desc 'Use the platform InSpec resource to test the platform on which the system is running.'
example "
describe platform do
its('name') { should eq 'redhat' }
end
describe platform do
it { should be_in_family('unix') }
end
"
def initialize
@platform = inspec.backend.os
end
# add helper methods for easy access of properties
%w{name family release arch}.each do |property|
define_method(property.to_sym) do
@platform.send(property)
end
end
def [](name)
# convert string to symbol
name = name.to_sym if name.is_a? String
@platform[name]
end
def platform?(name)
@platform.name == name ||
@platform.family_hierarchy.include?(name)
end
def in_family?(family)
@platform.family_hierarchy.include?(family)
end
def families
@platform.family_hierarchy
end
def supported?(supports)
return true if supports.nil? || supports.empty?
status = true
supports.each do |s|
s.each do |k, v|
# ignore the inspec check for supports
# TODO: remove in inspec 2.0
if k == :inspec
next
elsif %i(os_family os-family platform_family platform-family).include?(k)
status = in_family?(v)
elsif %i(os platform).include?(k)
status = platform?(v)
elsif %i(os_name os-name platform_name platform-name).include?(k)
status = name == v
elsif k == :release
status = check_release(v)
else
status = false
end
break if status == false
end
return true if status == true
end
status
end
def to_s
'Platform Detection'
end
private
def check_release(value)
# allow wild card matching
if value.include?('*')
cleaned = Regexp.escape(value).gsub('\*', '.*?')
!(release =~ /#{cleaned}/).nil?
else
release == value
end
end
end
end