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, load_dotfiles: false) ⇒ 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`)

  • zipfile_path (String, nil) (defaults to: nil)

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

  • load_dotfiles (Boolean) (defaults to: false)

    whether to load files whose relative path contains a component (directory or filename) beginning with ‘.’. Defaults to false so that dotfiles are skipped during normal module scanning, matching the behavior of Ruby’s Dir.glob on real filesystems. Set to true only when the caller explicitly needs dotfile support (e.g. zip-based environment loading).

Raises:



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/compliance_engine/module_loader.rb', line 20

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

  @name = nil
  @version = nil
  @files = []
  @path = path.to_s
  @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 StandardError => 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.
  # The loops are structured this way (rather than building a flat globs
  # array first) so that each glob result can be checked against its
  # base directory for dotfile filtering.
  ['SIMP/compliance_profiles', 'simp/compliance_profiles'].each do |dir|
    base = File.join(path, dir)
    next unless fileclass.directory?(base)

    # Using .each here to make mocking with rspec easier.
    ['yaml', 'json'].each do |type|
      dirclass.glob(File.join(base, '**', "*.#{type}")).sort.each do |file|
        unless load_dotfiles
          # Skip any file whose path (relative to the compliance_profiles
          # base) contains a component beginning with '.', e.g. hidden
          # files (.profile.yaml) or files inside hidden directories
          # (.hidden/profile.yaml).
          relative = file.to_s.delete_prefix("#{base}/")
          next if relative.split('/').any? { |part| part.start_with?('.') }
        end

        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 StandardError => e
        ComplianceEngine.log.warn "Could not load #{file}: #{e.message}"
      end
    end
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



80
81
82
# File 'lib/compliance_engine/module_loader.rb', line 80

def files
  @files
end

#nameObject (readonly)

Returns the value of attribute name.



80
81
82
# File 'lib/compliance_engine/module_loader.rb', line 80

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



80
81
82
# File 'lib/compliance_engine/module_loader.rb', line 80

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



80
81
82
# File 'lib/compliance_engine/module_loader.rb', line 80

def version
  @version
end

#zipfile_pathObject (readonly)

Returns the value of attribute zipfile_path.



80
81
82
# File 'lib/compliance_engine/module_loader.rb', line 80

def zipfile_path
  @zipfile_path
end