Class: Consul::Template::Generator::CTRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/consul/template/generator/ct.rb,
lib/consul/template/generator/run.rb,
lib/consul/template/generator/init.rb,
lib/consul/template/generator/key_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consul_session_id = nil, do_create_session = true) ⇒ CTRunner



7
8
9
10
11
12
13
14
# 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

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



5
6
7
# File 'lib/consul/template/generator/init.rb', line 5

def session
  @session
end

Instance Method Details

#acquire_lock(target_key) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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_lockObject



24
25
26
27
28
# 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_sessionObject



16
17
18
19
20
21
# 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
end

#destroy_sessionObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

#render_template(template) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# 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

#run(template, template_key, comp_hash = nil) ⇒ Object



7
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
# File 'lib/consul/template/generator/run.rb', line 7

def run(template, template_key, comp_hash = nil)
  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}"
      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) ⇒ Object



30
31
32
33
34
35
36
37
# 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