module Consult

Constants

CONSUL_DISK_TOKEN
VERSION

Attributes

config[R]
force_render[R]
templates[R]

Public Class Methods

active_templates() click to toggle source

Return only the templates that are relevant for the current environment

# File lib/consult.rb, line 75
def active_templates
  force_render ? templates : templates.select(&:should_render?)
end
configure_consul() click to toggle source
# File lib/consult.rb, line 38
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.to_s.length > 0

  Diplomat.configure do |c|
    @config[:consul].each do |opt, val|
      c.send "#{opt}=".to_sym, val
    end
  end
end
configure_vault() click to toggle source
# File lib/consult.rb, line 56
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_token() click to toggle source

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

# File lib/consult.rb, line 86
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
env() click to toggle source
# File lib/consult.rb, line 70
def env
  @all_config[:env] || ENV['RAILS_ENV'] || (defined?(::Rails) && ::Rails.env)
end
load(config_dir: nil, force_render: false, verbose: nil) click to toggle source
# File lib/consult.rb, line 23
def load(config_dir: nil, force_render: false, verbose: nil)
  root directory: config_dir
  yaml = root.join('config', 'consult.yml')

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

  @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.merge(verbose: verbose)) } || []

  @force_render = force_render

  configure_consul
  configure_vault
end
render!() click to toggle source

Render templates.

# File lib/consult.rb, line 80
def render!
  active_templates.each(&:render)
end
root(directory: nil) click to toggle source
# File lib/consult.rb, line 66
def root(directory: nil)
  @_root ||= directory ? Pathname.new(directory) : (defined?(::Rails) && ::Rails.root)
end