Class: Kafo::PuppetReport

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/puppet_report.rb

Overview

An abstraction over the Puppet report format

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report) ⇒ PuppetReport

Returns a new instance of PuppetReport.

Parameters:

  • report (Hash)

    The Puppet report



35
36
37
# File 'lib/kafo/puppet_report.rb', line 35

def initialize(report)
  @report = report
end

Class Method Details

.load_report_file(path) ⇒ PuppetReport

Load a Puppet report from a path

Both YAML and JSON are supported.

Parameters:

  • path (String)

    The path to Puppet report

Returns:

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kafo/puppet_report.rb', line 14

def self.load_report_file(path)
  raise ArgumentError, 'No path given' unless path || path.empty?
  raise ArgumentError, "#{path} is not a readable file" unless File.file?(path) && File.readable?(path)

  data = case File.extname(path)
         when '.yaml'
           require 'yaml'
           content = File.read(path).gsub(%r{!ruby/object.*$}, '')
           YAML.safe_load(content, permitted_classes: [Time, Symbol])
         when '.json'
           require 'json'
           JSON.parse(File.read(path))
         else
           raise ArgumentError, "Unsupported file extension for #{path}"
         end

  PuppetReport.new(data)
end

Instance Method Details

#failed_resourcesArray[PuppetFailedResource]

Returns The failed resources and their status.

Returns:

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kafo/puppet_report.rb', line 50

def failed_resources
  statuses = @report['resource_statuses']

  raise PuppetReportError, "No resource statuses found in report" unless statuses

  statuses.select { |_title, status| status['failed'] }.map do |title, status|
    # TODO: There's also a message with source Puppet
    # Executing with uid=USER: '/tmp/failing-command'
    # This shows up after Executing '/tmp/failing-command'
    related_logs = logs.select { |log| log['source'].include?(title) }
    PuppetFailedResource.new(status, related_logs)
  end
end

#logsArray[Hash]

Returns The Puppet logs.

Returns:

  • (Array[Hash])

    The Puppet logs



45
46
47
# File 'lib/kafo/puppet_report.rb', line 45

def logs
  @report['logs']
end

#report_formatInteger

Returns The report format.

Returns:

  • (Integer)

    The report format



40
41
42
# File 'lib/kafo/puppet_report.rb', line 40

def report_format
  @report['report_format']
end