Sha256: 59249b107d4d3c2dd07e228a899b1769842d1beffcaa47d7d46a98162a5757a6

Contents?: true

Size: 742 Bytes

Versions: 13

Compression:

Stored size: 742 Bytes

Contents

require 'open-uri'
require 'json'
require 'dply/error'

module Dplyr
  class Consul

    def hosts(app_name, service: "app")
      uri = "http://127.0.0.1:8500/v1/catalog/service/#{service}?tag=#{app_name}"
      response = JSON.parse(open(uri).read)
      hosts = []
      response.each do |i|
        host = {}
        metadata_tag = i["ServiceTags"].find {|t| t =~ /\Ametadata:/}
        metadata = metadata_tag ? JSON.parse(metadata_tag.partition(":")[2]) : {}
        host[:user] = metadata["user"]
        host[:dir] = metadata["dir"]
        host[:addr] = i["Address"]
        host[:id] = i["Node"]
        hosts << host
      end
      hosts
    rescue
      raise ::Dply::Error, "failed to load hosts from consul"
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dply-0.3.12 lib/dplyr/consul.rb
dply-0.3.11 lib/dplyr/consul.rb
dply-0.3.10 lib/dplyr/consul.rb
dply-0.3.9 lib/dplyr/consul.rb
dply-0.3.8 lib/dplyr/consul.rb
dply-0.3.7 lib/dplyr/consul.rb
dply-0.3.6 lib/dplyr/consul.rb
dply-0.3.5 lib/dplyr/consul.rb
dply-0.3.4 lib/dplyr/consul.rb
dply-0.3.3 lib/dplyr/consul.rb
dply-0.3.2 lib/dplyr/consul.rb
dply-0.3.1 lib/dplyr/consul.rb
dply-0.3.0 lib/dplyr/consul.rb