module Aggkit::Consul
Public Instance Methods
build_consul_addr(addr: ENV['CONSUL_HTTP_ADDR'], host: ENV['CONSUL_HOST'], port: ENV['CONSUL_PORT'])
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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