Class: SensuGenerator::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu_generator/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services = []) ⇒ Generator

Returns a new instance of Generator.



7
8
9
10
11
12
# File 'lib/sensu_generator/generator.rb', line 7

def initialize(services = [])
  @services = services
  @config  = Application.config
  @trigger = Application.trigger
  @logger  = Application.logger
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



15
16
17
# File 'lib/sensu_generator/generator.rb', line 15

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



16
17
18
# File 'lib/sensu_generator/generator.rb', line 16

def connection
  @connection
end

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/sensu_generator/generator.rb', line 15

def logger
  @logger
end

#services=(value) ⇒ Object (writeonly)

Sets the attribute services

Parameters:

  • value

    the value to set the attribute services to.



14
15
16
# File 'lib/sensu_generator/generator.rb', line 14

def services=(value)
  @services = value
end

Instance Method Details

#flush_resultsObject



53
54
55
# File 'lib/sensu_generator/generator.rb', line 53

def flush_results
  CheckFile.remove_all_with(config.file_prefix)
end

#generate!Object



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
# File 'lib/sensu_generator/generator.rb', line 18

def generate!
  @processed_files = []
  @services.each do |svc|
    next unless svc.changed?
    svc.checks.each do |check|
      next if check.nil?
      begin
        if check.class == String
          templates_for(check).each do |src|
            filename = config.file_prefix + "#{svc.name}-#{File.basename(src).gsub(/\.(?:.*)/, '.json')}"
            result = merge_with_default_parameters(
                        JSON.parse(
                          process(template: src, namespace: binding),
                          symbolize_names: true
                        )
                      )

            if result
              write(filename: filename, data: result)
              @processed_files << filename
            end
          end
        else
          #TODO
          # Implement json parameters parsing
        end
      rescue => e
        logger.warn e
        next
      end
    end
  end
  @processed_files
end