class Chef::Compliance::ProfileCollection

Constants

HIDDEN_IVARS

Attributes

events[R]

Event dispatcher for this run.

@return [Chef::EventDispatch::Dispatcher]

Public Class Methods

new(events) click to toggle source
# File lib/chef/compliance/profile_collection.rb, line 30
def initialize(events)
  @events = events
end

Public Instance Methods

from_file(path, cookbook_name) click to toggle source

Add a profile to the profile collection. The cookbook_name needs to be determined by the caller and is used in the ‘include_profile` API to match on. The path should be the complete path on the host of the inspec.yml file, including the filename.

@param path [String] @param cookbook_name [String]

# File lib/chef/compliance/profile_collection.rb, line 41
def from_file(path, cookbook_name)
  new_profile = Profile.from_file(events, path, cookbook_name)
  self << new_profile
  events&.compliance_profile_loaded(new_profile)
end
include_profile(arg) click to toggle source

DSL method to enable profile files. This matches on the name of the profile being included it does not match on the filename of the input file. If the specific profile is omitted then it uses the default profile. The string supports regular expression matching.

@example Specific profile in a cookbook

include_profile “acme_cookbook::ssh-001”

@example The profile named “default” in a cookbook

include_profile “acme_cookbook”

@example Every profile in a cookbook

include_profile “acme_cookbook::.*”

@example Matching profiles by regexp in a cookbook

include_profile “acme_cookbook::ssh.*”

@example Matching profiles by regexp in any cookbook in the cookbook collection

include_profile “.::ssh.

# File lib/chef/compliance/profile_collection.rb, line 82
def include_profile(arg)
  (cookbook_name, profile_name) = arg.split("::")

  profile_name = "default" if profile_name.nil?

  profiles = select { |profile| /^#{cookbook_name}$/.match?(profile.cookbook_name) && /^#{profile_name}$/.match?(profile.pathname) }

  if profiles.empty?
    raise "No inspec profiles matching '#{profile_name}' found in cookbooks matching '#{cookbook_name}'"
  end

  profiles.each(&:enable!)
end
inspec_data() click to toggle source

@return [Array<Profile>] inspec profiles which are enabled in a form suitable to pass to inspec

# File lib/chef/compliance/profile_collection.rb, line 54
def inspec_data
  select(&:enabled?).each_with_object([]) { |profile, arry| arry << profile.inspec_data }
end
inspect() click to toggle source

Omit the event object from error output

# File lib/chef/compliance/profile_collection.rb, line 100
def inspect
  ivar_string = (instance_variables.map(&:to_sym) - HIDDEN_IVARS).map do |ivar|
    "#{ivar}=#{instance_variable_get(ivar).inspect}"
  end.join(", ")
  "#<#{self.class}:#{object_id} #{ivar_string}>"
end
using_profiles?() click to toggle source

@return [Boolean] if any of the profiles are enabled

# File lib/chef/compliance/profile_collection.rb, line 48
def using_profiles?
  any?(&:enabled?)
end