Class: TemplateConfigurator::Processor
- Inherits:
-
Object
- Object
- TemplateConfigurator::Processor
- Defined in:
- lib/template_configurator/processor.rb
Instance Attribute Summary collapse
-
#locks ⇒ Object
Returns the value of attribute locks.
-
#options ⇒ Object
Returns the value of attribute options.
-
#service ⇒ Object
Returns the value of attribute service.
Instance Method Summary collapse
-
#initialize(options) ⇒ Processor
constructor
A new instance of Processor.
- #lock(file) ⇒ Object
- #reload ⇒ Object
- #render ⇒ Object
- #unlock ⇒ Object
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() = @locks = {} @service = Service.new([:service]) end |
Instance Attribute Details
#locks ⇒ Object
Returns the value of attribute locks.
28 29 30 |
# File 'lib/template_configurator/processor.rb', line 28 def locks @locks end |
#options ⇒ Object
Returns the value of attribute options.
28 29 30 |
# File 'lib/template_configurator/processor.rb', line 28 def end |
#service ⇒ Object
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 |
#reload ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/template_configurator/processor.rb', line 47 def reload if [:service][:name].nil? TemplateConfigurator.log.info("service not specified; skipping reload") else begin @service.conditional_reload rescue ServiceException => e TemplateConfigurator.log.error(e.) TemplateConfigurator.log.error(e.output) unless e.output.nil? end end end |
#render ⇒ Object
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 [:template][:json_file].nil? json_fh = lock([:template][:json_file]) @data = JSON.parse(json_fh.read) end TemplateConfigurator.log.debug("json:[#{@data.inspect}]") template = ERB.new(File.read([:template][:input_file]), 0, '%<>') if [:template][:output_file].nil? output_fh = STDOUT old_output = "" else output_fh = lock([: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 [:dry_run] TemplateConfigurator.log.debug("Not attemptig service reload due to dry-run") output_fh.write new_output elsif ![: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 |
#unlock ⇒ Object
36 37 38 39 |
# File 'lib/template_configurator/processor.rb', line 36 def unlock @locks[:file].flock(File::LOCK_UN) @locks[:file] end |