Module: Consult

Defined in:
lib/consult.rb,
lib/consult/cli.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: CLI, Template

Constant Summary collapse

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/consult.rb', line 23

def config
  @config
end

.force_renderObject (readonly)

Returns the value of attribute force_render.



23
24
25
# File 'lib/consult.rb', line 23

def force_render
  @force_render
end

.templatesObject (readonly)

Returns the value of attribute templates.



23
24
25
# File 'lib/consult.rb', line 23

def templates
  @templates
end

Class Method Details

.active_templatesObject

Return only the templates that are relevant for the current environment



96
97
98
# File 'lib/consult.rb', line 96

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

.configure_consulObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/consult.rb', line 59

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_vaultObject



77
78
79
80
81
82
83
84
85
# File 'lib/consult.rb', line 77

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



107
108
109
110
111
112
# File 'lib/consult.rb', line 107

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



91
92
93
# File 'lib/consult.rb', line 91

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

.load(config_dir: nil, force_render: false, verbose: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/consult.rb', line 25

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

  if verbose
    puts "Consult: Loading config from #{yaml}"
  end

  @all_config = if yaml.exist?
    if Gem::Version.new(YAML::VERSION) < Gem::Version.new('4.0')
      YAML.safe_load(ERB.new(yaml.read).result, [], [], true, symbolize_names: true).to_h
    else
      YAML.safe_load(ERB.new(yaml.read).result, aliases: true, symbolize_names: true).to_h
    end
  else
    STDERR.puts "Consult: No config file found at #{root} -> #{yaml}"
  end

  @all_config ||= {}

  @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, env_vars: @config[:vars]})) } || []

  @force_render = force_render

  if @templates.empty? && @force_render
    STDERR.puts "Consult: No template was found for env #{env.inspect} with forced rendering (re-run with `--no-force` if this is acceptable)"
    exit 1
  end

  configure_consul
  configure_vault
end

.render!Object

Render templates.



101
102
103
# File 'lib/consult.rb', line 101

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

.root(directory: nil) ⇒ Object



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

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