Class: ComplianceEngine::ModuleLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/compliance_engine/module_loader.rb

Overview

Load compliance engine data from a Puppet module

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, fileclass: File, dirclass: Dir) ⇒ ModuleLoader

Initialize a ModuleLoader from a Puppet module path

Parameters:

  • path (String)

    the path to the Puppet module

  • fileclass (File) (defaults to: File)

    the class to use for file operations (default: ‘File`)

  • dirclass (Dir) (defaults to: Dir)

    the class to use for directory operations (default: ‘Dir`)

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/compliance_engine/module_loader.rb', line 14

def initialize(path, fileclass: File, dirclass: Dir)
  raise ComplianceEngine::Error, "#{path} is not a directory" unless fileclass.directory?(path)

  @name = nil
  @version = nil
  @files = []

  # Read the Puppet module's metadata.json
   = File.join(path.to_s, 'metadata.json')
  if fileclass.exist?()
    begin
       = ComplianceEngine::DataLoader::Json.new(, fileclass: fileclass)
      @name = .data['name']
      @version = .data['version']
    rescue => e
      warn "Could not parse #{metadata_json}: #{e.message}"
    end
  end

  # In this directory, we want to look for all yaml and json files
  # under SIMP/compliance_profiles and simp/compliance_profiles.
  globs = ['SIMP/compliance_profiles', 'simp/compliance_profiles']
          .select { |dir| fileclass.directory?(File.join(path, dir)) }
          .map { |dir|
    ['yaml', 'json'].map { |type| File.join(path, dir, '**', "*.#{type}") }
  }.flatten
  # debug "Globs: #{globs}"
  # Using .each here to make mocking with rspec easier.
  globs.each do |glob|
    dirclass.glob(glob).each do |file|
      key = if Object.const_defined?(:Zip) && file.is_a?(Zip::Entry)
              File.join(file.zipfile.to_s, '.', file.to_s)
            else
              file.to_s
            end
      loader = if File.extname(file.to_s) == '.json'
                 ComplianceEngine::DataLoader::Json.new(file.to_s, fileclass: fileclass, key: key)
               else
                 ComplianceEngine::DataLoader::Yaml.new(file.to_s, fileclass: fileclass, key: key)
               end
      @files << loader
    rescue => e
      warn "Could not load #{file}: #{e.message}"
    end
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



61
62
63
# File 'lib/compliance_engine/module_loader.rb', line 61

def files
  @files
end

#nameObject (readonly)

Returns the value of attribute name.



61
62
63
# File 'lib/compliance_engine/module_loader.rb', line 61

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



61
62
63
# File 'lib/compliance_engine/module_loader.rb', line 61

def version
  @version
end