Module: Covalence::Consul
- Defined in:
- lib/covalence/core/state_stores/consul.rb
Constant Summary collapse
- URL =
Consul ennvironment variables #####
CONSUL_HTTP_ADDR - DNS name and port of your Consul endpoint specified in the format dnsname:port.
Defaults to the local agent HTTP listener.CONSUL_HTTP_SSL - Specifies what protocol to use when talking to the given address, either http or https. CONSUL_HTTP_AUTH - HTTP Basic Authentication credentials to be used when communicating with Consul,
in the format of either user or user:pass.CONSUL_HTTP_TOKEN - HTTP authentication token.
ENV['CONSUL_HTTP_ADDR'] || 'localhost:8500'
Class Method Summary collapse
- .get_key(name) ⇒ Object
- .get_output(name, stack) ⇒ Object
-
.get_state_store(params, workspace_enabled = false) ⇒ Object
Return configuration for remote state store.
-
.has_state_store? ⇒ Boolean
end.
-
.lookup(type, params) ⇒ Object
Key lookups.
- .reset_cache ⇒ Object
Class Method Details
.get_key(name) ⇒ Object
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 58 59 |
# File 'lib/covalence/core/state_stores/consul.rb', line 27 def self.get_key(name) @cache['root'][name] ||= begin # Create and execute HTTP request request = "#{URL}/v1/kv/#{name}" # Configure request headers headers = {} headers['X-Consul-Token'] = ENV['CONSUL_HTTP_TOKEN'] if ENV.has_key? 'CONSUL_HTTP_TOKEN' begin response = RestClient.get request, headers rescue RestClient::ExceptionWithResponse => err fail "Unable to retrieve key '#{name}': " + err. end # Parse JSON response begin parsed = JSON.parse(response) encoded = parsed.first['Value'] rescue JSON::ParserError => err fail "No results or unable to parse response: " + err. end # Return decoded value if encoded != nil Base64.decode64(encoded) else # TODO: not sure if this is the right failure to raise fail "Requested key '#{name}' not found" end end end |
.get_output(name, stack) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/covalence/core/state_stores/consul.rb', line 61 def self.get_output(name, stack) @cache[stack][name] || begin # Retrieve stack state value = self.get_key(stack) # Parse JSON parsed = JSON.parse(value) outputs = parsed.fetch("modules")[0].fetch("outputs") # Populate the cache for subsequent calls outputs.keys.each do |key| @cache[stack][key] = outputs.fetch(key) end # Check outputs for requested key and return if outputs.has_key?(name) @cache[stack][name] else fail("Requested output '#{name}' not found") end end end |
.get_state_store(params, workspace_enabled = false) ⇒ Object
Return configuration for remote state store.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/covalence/core/state_stores/consul.rb', line 86 def self.get_state_store(params, workspace_enabled=false) raise "State store parameters must be a Hash" unless params.is_a?(Hash) required_params = [ 'access_token', 'name' ] required_params.each do |param| raise "Missing '#{param}' store parameter" unless params.has_key?(param) end config = <<-CONF terraform { backend "consul" { path = "#{params['name']}" CONF params.delete('name') params.each do |k,v| v = Covalence::Helpers::ShellInterpolation.parse_shell(v) if v.include?("$(") config += " #{k} = \"#{v}\"\n" end config += " }\n}\n" return config end |
.has_state_store? ⇒ Boolean
end
127 128 129 |
# File 'lib/covalence/core/state_stores/consul.rb', line 127 def self.has_state_store? return true end |
.lookup(type, params) ⇒ Object
Key lookups
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/covalence/core/state_stores/consul.rb', line 132 def self.lookup(type, params) raise "Lookup parameters must be a Hash" unless params.is_a?(Hash) case when type == 'key' raise "Missing 'key' lookup parameter" unless params.has_key? 'key' self.get_key(params['key']) when type == 'state' required_params = [ 'key', 'stack' ] required_params.each do |param| raise "Missing '#{param}' lookup parameter" unless params.has_key?(param) end self.get_output(params['key'],params['stack']) else raise "Consul module does not support the '#{type}' lookup type" end end |
.reset_cache ⇒ Object
21 22 23 |
# File 'lib/covalence/core/state_stores/consul.rb', line 21 def self.reset_cache() @cache = Hash.new{|h,k| h[k] = Hash.new} end |