Class: TemplateConfigurator::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/template_configurator/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Processor

Returns a new instance of Processor.



41
42
43
44
45
# File 'lib/template_configurator/processor.rb', line 41

def initialize(options)
  @options = options
  @locks = {}
  @service = Service.new(@options[:service])
end

Instance Attribute Details

#locksObject

Returns the value of attribute locks.



28
29
30
# File 'lib/template_configurator/processor.rb', line 28

def locks
  @locks
end

#optionsObject

Returns the value of attribute options.



28
29
30
# File 'lib/template_configurator/processor.rb', line 28

def options
  @options
end

#serviceObject

Returns the value of attribute service.



28
29
30
# File 'lib/template_configurator/processor.rb', line 28

def service
  @service
end

Instance Method Details

#lock(file) ⇒ Object



30
31
32
33
34
# File 'lib/template_configurator/processor.rb', line 30

def lock file
  @locks[:file] = File.open(file, File::RDWR|File::CREAT, 0644) 
  @locks[:file].flock(File::LOCK_EX)
  @locks[:file]
end

#reloadObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/template_configurator/processor.rb', line 47

def reload
  if @options[:service][:name].nil?
    TemplateConfigurator.log.info("service not specified; skipping reload")
  else
    begin
      @service.conditional_reload
    rescue ServiceException => e
      TemplateConfigurator.log.error(e.message)
      TemplateConfigurator.log.error(e.output) unless e.output.nil?
    end
  end
end

#renderObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/template_configurator/processor.rb', line 60

def render
  @data = {}
  unless @options[:template][:json_file].nil?
    json_fh = lock(@options[:template][:json_file])
    @data = JSON.parse(json_fh.read)
  end
  TemplateConfigurator.log.debug("json:[#{@data.inspect}]")
  template = ERB.new(File.read(@options[:template][:input_file]), 0, '%<>')

  if @options[:template][:output_file].nil?
    output_fh = STDOUT
    old_output = ""
  else
    output_fh = lock(@options[:template][:output_file])
    old_output = output_fh.read
  end
  
  new_output = template.result(binding)

  new_sha1 = Digest::SHA1.hexdigest(new_output)
  old_sha1 = Digest::SHA1.hexdigest(old_output)

  TemplateConfigurator.log.debug("old_sha1:#{old_sha1} new_sha1:#{new_sha1}")

  if new_sha1 == old_sha1
    TemplateConfigurator.log.debug("SHA1 checksum unchanged")
  else
    TemplateConfigurator.log.info "SHA1 checksum changed"
    # Write the new configuration
    if @options[:dry_run]
      TemplateConfigurator.log.debug("Not attemptig service reload due to dry-run")
      output_fh.write new_output
    elsif !@options[:template][:output_file].nil?
      TemplateConfigurator.log.debug("writing new configuation (#{new_output.length} bytes)")
      output_fh.rewind
      output_fh.write(new_output)
      output_fh.flush
      output_fh.truncate(output_fh.pos)
      output_fh.flush
      reload()
    else
      TemplateConfigurator.log.debug("Not attemptig service reload due to missing output file parameter")
      output_fh.write new_output
    end
  end

end

#unlockObject



36
37
38
39
# File 'lib/template_configurator/processor.rb', line 36

def unlock
  @locks[:file].flock(File::LOCK_UN)
  @locks[:file]
end