Module: Consult

Defined in:
lib/consult.rb,
lib/consult/version.rb,
lib/consult/template.rb,
lib/consult/utilities.rb,
lib/consult/rails/engine.rb,
lib/consult/template_functions.rb

Defined Under Namespace

Modules: Rails, TemplateFunctions, Utilities Classes: Template

Constant Summary collapse

CONSUL_DISK_TOKEN =
Pathname.new("#{Dir.home}/.consul-token").freeze
VERSION =
'0.8.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/consult.rb', line 21

def config
  @config
end

.templatesObject (readonly)

Returns the value of attribute templates.



21
22
23
# File 'lib/consult.rb', line 21

def templates
  @templates
end

Class Method Details

.active_templatesObject

Return only the templates that are relevant for the current environment



73
74
75
# File 'lib/consult.rb', line 73

def active_templates
  templates.select(&:should_render?)
end

.configure_consulObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/consult.rb', line 36

def configure_consul
  @config[:consul] ||= {}

  # We map conventional `address` and `token` params to Diplomat's unconventional `url` and `acl_token` settings
  # Additionally: prefer env vars over explicit config
  configured_address = @config[:consul].delete(:address)
  @config[:consul][:url] = ENV['CONSUL_HTTP_ADDR'] || configured_address || @config[:consul][:url]
  # If a consul token exists, treat it as special
  # See https://github.com/WeAreFarmGeek/diplomat/pull/160
  (@config[:consul][:options] ||= {}).merge!(headers: {'X-Consul-Token' => consul_token}) if consul_token.present?

  Diplomat.configure do |c|
    @config[:consul].each do |opt, val|
      c.send "#{opt}=".to_sym, val
    end
  end
end

.configure_vaultObject



54
55
56
57
58
59
60
61
62
# File 'lib/consult.rb', line 54

def configure_vault
  return unless @config.key? :vault

  Vault.configure do |c|
    @config[:vault].each do |opt, val|
      c.send "#{opt}=".to_sym, val
    end
  end
end

.consul_tokenObject

Map more conventional ‘token` parameter to Diplomat’s ‘acl_token` configuration. Additionally, we support ~/.consul-token, similar to Vault’s support for ~/.vault-token



84
85
86
87
88
89
# File 'lib/consult.rb', line 84

def consul_token
  ENV['CONSUL_HTTP_TOKEN'] ||
    @config[:consul].delete(:token) ||
    @config[:consul].delete(:acl_token) ||
    (CONSUL_DISK_TOKEN.exist? ? CONSUL_DISK_TOKEN.read.chomp : nil)
end

.envObject



68
69
70
# File 'lib/consult.rb', line 68

def env
  @all_config[:env] || ENV['RAILS_ENV'] || (defined?(::Rails) && ::Rails.env)
end

.load(config_dir: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/consult.rb', line 22

def load(config_dir: nil)
  root directory: config_dir
  yaml = root.join('config', 'consult.yml')

  @all_config = yaml.exist? ? YAML.safe_load(ERB.new(yaml.read).result, [], [], true).to_h : {}
  @all_config.deep_symbolize_keys!

  @config = @all_config[:shared].to_h.deep_merge @all_config[env&.to_sym].to_h
  @templates = @config[:templates]&.map { |name, config| Template.new(name, config) } || []

  configure_consul
  configure_vault
end

.render!Object

Render templates.



78
79
80
# File 'lib/consult.rb', line 78

def render!
  active_templates.each(&:render)
end

.root(directory: nil) ⇒ Object



64
65
66
# File 'lib/consult.rb', line 64

def root(directory: nil)
  @_root ||= directory ? Pathname.new(directory) : (defined?(::Rails) && ::Rails.root)
end