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, zipfile_path: nil) ⇒ ModuleLoader

Initialize a ModuleLoader from a Puppet module path

Raises:

Parameters:

  • the path to the Puppet module

  • (defaults to: File)

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

  • (defaults to: Dir)

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

  • (defaults to: nil)

    the path to the zip file if loading from a zip archive



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
60
# File 'lib/compliance_engine/module_loader.rb', line 15

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

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

  # 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
      ComplianceEngine.log.warn "Could not parse #{}: #{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
  # Using .each here to make mocking with rspec easier.
  globs.each do |glob|
    dirclass.glob(glob).sort.each do |file|
      key = if @zipfile_path
              File.join(@zipfile_path, '.', 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
      ComplianceEngine.log.warn "Could not load #{file}: #{e.message}"
    end
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



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

def files
  @files
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

#zipfile_pathObject (readonly)

Returns the value of attribute zipfile_path.



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

def zipfile_path
  @zipfile_path
end