class Consul::Template::Generator::CTRunner
Attributes
session[RW]
Public Class Methods
new(consul_session_id = nil, do_create_session = true)
click to toggle source
# File lib/consul/template/generator/init.rb, line 7 def initialize(consul_session_id = nil, do_create_session = true) @config = Consul::Template::Generator.config if (consul_session_id.nil? && do_create_session) create_session else @session = consul_session_id end end
Public Instance Methods
acquire_lock(target_key) { || ... }
click to toggle source
# File lib/consul/template/generator/key_value.rb, line 8 def acquire_lock(target_key) lock_key = @config.lock_key target_key @config.logger.debug "Attempting to acquire lock on key: #{lock_key}" Consul::Template::Generator.renew_session @session unless Diplomat::Lock.acquire(lock_key, @session) raise KeyNotLockedError, "Unable to acquire lock on key: #{lock_key}" end @config.logger.debug "Lock acquired on key: #{lock_key}" begin yield ensure Diplomat::Lock.release(lock_key, @session) end end
acquire_session_lock() { || ... }
click to toggle source
# File lib/consul/template/generator/key_value.rb, line 24 def acquire_session_lock acquire_lock(@config.session_lock_key) do yield end end
create_session()
click to toggle source
# File lib/consul/template/generator/init.rb, line 16 def create_session unless @session.nil? destroy_session end @session = Consul::Template::Generator.create_session @config.session_name, @config.session_ttl end
destroy_session()
click to toggle source
# File lib/consul/template/generator/init.rb, line 23 def destroy_session return if @session.nil? attempts = 0 begin destroyed = Consul::Template::Generator.destroy_session @session rescue Consul::Template::Generator.config.logger.info "Failed to destroy session: #{@session}, attempt number #{attempts}" ensure @session = destroyed ? nil : @session unless @session.nil? attempts += 1 sleep 0.25 end end until (@session.nil? || attempts > 4) end
post_graphite_event(path)
click to toggle source
# File lib/consul/template/generator/graphite.rb, line 7 def post_graphite_event(path) @config.logger.debug "Posting event to graphite. Server: #{@config.graphite_host}, Path: #{path}" host, port = @config.graphite_host.split(':') begin s = TCPSocket.open(host, port) s.write("#{path} 1 #{Time.new.to_i}\n") s.close rescue Exception => e @config.logger.error "An unknown error occurred while posting data to graphite: #{e.message}" end end
render_template(template)
click to toggle source
# File lib/consul/template/generator/ct.rb, line 8 def render_template(template) body = nil cmd = %{#{@config.consul_template_binary} -dry -once -template #{template}} procs = ::Open4.popen4(*cmd) do |pid, stdin, stdout, stderr| body = stdout.read.strip # consul-template -dry inserts '> \n' at the top of stdout, remove it body.sub!(/^>\s+\n/, '') end status = procs.to_i hash = ::Digest::MD5.hexdigest(body) return status, body, hash end
retrieve_template(template_key)
click to toggle source
# File lib/consul/template/generator/key_value.rb, line 39 def retrieve_template(template_key) @config.logger.info "Downloading key: #{template_key}" begin Diplomat::Kv.get(template_key, nil, :return, :return) rescue Exception => e raise TemplateUploadError, "Encountered an unexpected error while uploading template: #{e.message}" end end
run(template, template_key, comp_hash = nil, diff_changes = false)
click to toggle source
# File lib/consul/template/generator/run.rb, line 8 def run(template, template_key, comp_hash = nil, diff_changes = false) status, body, hash, uploaded_hash = nil, nil, nil, nil acquire_lock template_key do @config.logger.debug "Attempting to render template: #{template}" status, body, hash = render_template template unless status == 0 raise TemplateRenderError, "consul-template exited with on-zero exit status" end if body.nil? || body.empty? raise TemplateRenderError, "rendered template is nil or empty!" end @config.logger.debug "Template rendered..." if comp_hash.nil? || comp_hash != hash @config.logger.info "Change in template discovered, attempting to upload to key #{template_key}" @config.logger.debug "Existing hash: #{comp_hash || 'nil'}, new hash: #{hash}" if diff_changes @config.logger.info "Diffing templates..." curr_template = retrieve_template template_key diff = Diffy::Diff.new(curr_template, body, :include_diff_info => true, :context => 5).to_s(:text) @config.logger.info diff end uploaded = upload_template(template_key, body) if uploaded @config.logger.info "New template uploaded..." uploaded_hash = hash else raise TemplateUploadError, "Template not uploaded!" end else @config.logger.info "No change in template, skipping upload..." end end return uploaded_hash end
upload_template(template_key, raw_template)
click to toggle source
# File lib/consul/template/generator/key_value.rb, line 30 def upload_template(template_key, raw_template) @config.logger.info "Uploading key: #{template_key}" begin Diplomat::Kv.put(template_key, raw_template) rescue Exception => e raise TemplateUploadError, "Encountered an unexpected error while uploading template: #{e.message}" end end