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
- #load_envs(string, env = ENV) ⇒ Object
- #load_envs_from_consul(consul, prefix) ⇒ Object
- #wait_for_consul(consul) ⇒ Object
- #wait_for_service(consul, service) ⇒ Object
Instance Method Details
#build_consul_addr(addr: , host: , port: ) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/aggkit/consul.rb', line 17 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
58 59 60 61 62 63 64 65 |
# File 'lib/aggkit/consul.rb', line 58 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
67 68 69 70 71 72 |
# File 'lib/aggkit/consul.rb', line 67 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 |
#load_envs(string, env = ENV) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/aggkit/consul.rb', line 9 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) ⇒ Object
42 43 44 45 46 |
# File 'lib/aggkit/consul.rb', line 42 def load_envs_from_consul(consul, prefix) consul_http = consul_addr(consul) envs = Aggkit::Exec.capture!("aggconsul --consul=#{consul_http} --env #{prefix} --pristine -d", "Can't load envs from #{prefix}") load_envs(envs) end |
#wait_for_consul(consul) ⇒ Object
48 49 50 51 |
# File 'lib/aggkit/consul.rb', line 48 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
53 54 55 56 |
# File 'lib/aggkit/consul.rb', line 53 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 |