Module: Aggkit::Consul
Instance Method Summary collapse
- #build_consul_addr(addr: , host: , port: ) ⇒ Object
- #consul_addr(addr_or_host) ⇒ Object
- #envsubst(*paths) ⇒ Object
- #envsubst_file(templ, output = nil) ⇒ Object
- #erb(*paths) ⇒ Object
- #erb_file(templ, output = nil) ⇒ Object
- #load_envs(string, env = ENV) ⇒ Object
- #load_envs_from_consul(consul, prefix, dereference: true) ⇒ Object
- #wait_for_consul(consul) ⇒ Object
- #wait_for_service(consul, service) ⇒ Object
Instance Method Details
#build_consul_addr(addr: , host: , port: ) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/aggkit/consul.rb', line 18 def build_consul_addr(addr: ENV['CONSUL_HTTP_ADDR'], host: ENV['CONSUL_HOST'], port: ENV['CONSUL_PORT']) return URI('http://localhost:8500') if addr.to_s.empty? && host.to_s.empty? && port.to_s.empty? return URI(addr) unless addr.to_s.empty? uri = URI(host) uri = URI("http://#{host}:8500") if uri.scheme.to_s.empty? if port.to_s.empty? uri.port = 8500 if uri.port.to_i == 80 else uri.port = port.to_i end uri end |
#consul_addr(addr_or_host) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/aggkit/consul.rb', line 34 def consul_addr(addr_or_host) if addr_or_host['http'] addr_or_host else "http://#{addr_or_host}:8500" end end |
#envsubst(*paths) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/aggkit/consul.rb', line 59 def envsubst(*paths) paths = paths.flatten.map{|c| c.to_s.strip }.reject(&:empty?) paths.each do |path| Dir.glob("#{path}/**/*.in") do |templ| envsubst_file(templ, templ.sub(/\.in$/, '')) end end end |
#envsubst_file(templ, output = nil) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/aggkit/consul.rb', line 68 def envsubst_file(templ, output = nil) output ||= templ.sub(/\.in$/, '') Aggkit::Exec.die('filename must ends with .in or output must be provided') if output.strip == templ.strip cmd = "cat '#{templ}' | envsubst > '#{output}'" Aggkit::Exec.execute!(cmd, "envsubst failed: #{cmd}") end |
#erb(*paths) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/aggkit/consul.rb', line 75 def erb(*paths) paths = paths.flatten.map{|c| c.to_s.strip }.reject(&:empty?) paths.each do |path| Dir.glob("#{path}/**/*.erb") do |templ| erb_file(templ, templ.sub(/\.erb$/, '')) end end end |
#erb_file(templ, output = nil) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/aggkit/consul.rb', line 84 def erb_file(templ, output=nil) output ||= templ.sub(/\.erb$/, '') Aggkit::Exec.die('filename must ends with .erb or output must be provided') if output.strip == templ.strip cmd = "erb '#{templ}' > '#{output}'" Aggkit::Exec.execute!(cmd, "erb failed: #{cmd}") end |
#load_envs(string, env = ENV) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/aggkit/consul.rb', line 10 def load_envs(string, env = ENV) envs = Dotenv::Parser.new(string).call envs.each_pair do |k, v| env[k] = v end envs end |
#load_envs_from_consul(consul, prefix, dereference: true) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/aggkit/consul.rb', line 42 def load_envs_from_consul(consul, prefix, dereference: true) consul_http = consul_addr(consul) dr = dereference ? '-d' : '' envs = Aggkit::Exec.capture!("aggconsul --consul=#{consul_http} --env #{prefix} --pristine #{dr}", "Can't load envs from #{prefix}") load_envs(envs) end |
#wait_for_consul(consul) ⇒ Object
49 50 51 52 |
# File 'lib/aggkit/consul.rb', line 49 def wait_for_consul(consul) consul_http = consul_addr(consul) Aggkit::Exec.execute!("aggwait -t 30 -i 5 --consul-addr=#{consul_http} --consul", 'Timeout for consul service') end |
#wait_for_service(consul, service) ⇒ Object
54 55 56 57 |
# File 'lib/aggkit/consul.rb', line 54 def wait_for_service(consul, service) consul_http = consul_addr(consul) Aggkit::Exec.execute!("aggwait -t 60 -i 5 --consul-addr=#{consul_http} --consul-service #{service}", "Timeout for #{service} service") end |