Class: Chef::Compliance::ProfileCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/chef/compliance/profile_collection.rb

Constant Summary collapse

HIDDEN_IVARS =
[ :@events ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events) ⇒ ProfileCollection

Returns a new instance of ProfileCollection.



30
31
32
# File 'lib/chef/compliance/profile_collection.rb', line 30

def initialize(events)
  @events = events
end

Instance Attribute Details

#eventsChef::EventDispatch::Dispatcher (readonly)

Event dispatcher for this run.



28
29
30
# File 'lib/chef/compliance/profile_collection.rb', line 28

def events
  @events
end

Instance Method Details

#from_file(path, cookbook_name) ⇒ Object

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.

Parameters:



41
42
43
44
45
# 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) ⇒ Object

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.

include_profile “acme_cookbook::ssh-001”

include_profile “acme_cookbook”

include_profile “acme_cookbook::.*”

include_profile “acme_cookbook::ssh.*”

include_profile “.::ssh.

Examples:

Specific profile in a cookbook

The profile named “default” in a cookbook

Every profile in a cookbook

Matching profiles by regexp in a cookbook

Matching profiles by regexp in any cookbook in the cookbook collection



82
83
84
85
86
87
88
89
90
91
92
93
94
# 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_dataArray<Profile>

Returns inspec profiles which are enabled in a form suitable to pass to inspec.

Returns:

  • (Array<Profile>)

    inspec profiles which are enabled in a form suitable to pass to inspec



54
55
56
# File 'lib/chef/compliance/profile_collection.rb', line 54

def inspec_data
  select(&:enabled?).each_with_object([]) { |profile, arry| arry << profile.inspec_data }
end

#inspectObject

Omit the event object from error output



100
101
102
103
104
105
# 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?Boolean

Returns if any of the profiles are enabled.

Returns:

  • (Boolean)

    if any of the profiles are enabled



48
49
50
# File 'lib/chef/compliance/profile_collection.rb', line 48

def using_profiles?
  any?(&:enabled?)
end