Class: PuppetGenerator::Middleware::CreateOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_generator/middleware/create_output.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CreateOutput

Returns a new instance of CreateOutput.



4
5
6
# File 'lib/puppet_generator/middleware/create_output.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(task) ⇒ Object



8
9
10
11
12
13
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
# File 'lib/puppet_generator/middleware/create_output.rb', line 8

def call(task)
  destination = task.meta[:destination].split(/:/)
  channel     = destination[0]
  sink        = destination[1]

  PuppetGenerator.logger.info(self.class.name){ "Puppet definitions will be output to \"#{task.meta[:destination]}\"."  }
  PuppetGenerator.logger.debug(self.class.name){ "Render template for channel \"#{channel}\" and sink \"#{sink}\"." }

  unless task.meta[:template_tagged_with]
    task.meta[:template_tagged_with] = case channel
                                       when /file/
                                         [ :many_per_file ]
                                       when /^(?:dir|directory)/
                                         [ :one_per_file ]
                                       when /stdout/
                                         [ :many_per_file ]
                                       else
                                         [ :many_per_file ]
                                       end
  end

  template = Models::Template.find(name: task.meta[:command], is_suitable_for: channel.to_sym, is_tagged_with: task.meta[:template_tagged_with] )
  raise Exceptions::WrongTemplateChosen unless template
  PuppetGenerator.logger.debug(self.class.name){ "Chosen template: #{template.name} (#{template.path})." }

  definitions = template.render(task.body)

  exporter = Models::Exporter.find(writes_to: task.meta[:destination])
  raise Exceptions::InvalidExporter unless exporter
  PuppetGenerator.logger.debug(self.class.name){ "Chosen exporter: #{exporter.name}." }

  exporter.write(sink, definitions)

  @app.call(task)
end