Module: AppRb::Util::Consul

Defined in:
lib/app-rb/util/consul.rb

Constant Summary collapse

CONSUL_DIR =
"apps"

Class Method Summary collapse

Class Method Details

.consul_wait(consul, service) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/app-rb/util/consul.rb', line 30

def self.consul_wait(consul, service)
  loop do
    url = "curl -s #{consul}/v1/health/service/#{service}"
    statuses = JSON.load(AppRb::Util.just_cmd(url)).flat_map { |s| 
      s["Checks"]
    }.reject { |c| 
      c["CheckID"] == "serfHealth"
    }.map { |c| 
      c["Status"]
    }
    puts "statuses: #{statuses.inspect}"
    break if statuses.uniq == ["passing"] || statuses.uniq == []
    sleep 3
  end
end

.kv_get(consul, relative_path, k) ⇒ Object



48
49
50
# File 'lib/app-rb/util/consul.rb', line 48

def self.kv_get(consul, relative_path, k)
  AppRb::Util.just_cmd "curl -s #{consul}/v1/kv/#{CONSUL_DIR}/#{relative_path}/#{k}?raw"
end

.kv_keys(consul, relative_path) ⇒ Object



62
63
64
65
66
# File 'lib/app-rb/util/consul.rb', line 62

def self.kv_keys(consul, relative_path)
  (JSON.load(AppRb::Util.just_cmd "curl -s #{consul}/v1/kv/#{CONSUL_DIR}/#{relative_path}?recurse") || []).map { |v|
    v["Key"].sub("#{CONSUL_DIR}/#{relative_path}/", "")
  }
end

.kv_set(consul, relative_path, k, v) ⇒ Object



52
53
54
55
# File 'lib/app-rb/util/consul.rb', line 52

def self.kv_set(consul, relative_path, k, v)
  AppRb::Util.do_it "curl -s -X PUT #{consul}/v1/kv/#{CONSUL_DIR}/#{relative_path}/#{k} -d#{v}"
  puts ""
end

.kv_unset(consul, relative_path) ⇒ Object



57
58
59
60
# File 'lib/app-rb/util/consul.rb', line 57

def self.kv_unset(consul, relative_path)
  AppRb::Util.do_it "curl -s -X DELETE #{consul}/v1/kv/#{CONSUL_DIR}/#{relative_path}?recurse"
  puts ""
end

.register_service(host, id, name, port, url, tags = []) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/app-rb/util/consul.rb', line 2

def self.register_service(host, id, name, port, url, tags = [])
  AppRb::Util.do_it %(curl -s -X PUT #{host}:8500/v1/agent/service/register -d'{
    "Id": "#{id}",
    "Name": "#{name}",
    "Port": #{port},
    "Tags": #{JSON.dump(tags)},
    "Check": {
      "DeregisterCriticalServiceAfter": "20m",
      "Interval": "7s",
      "HTTP": "http://#{host}:#{port}#{url}"
    }
  }')
end

.remove_services(consul, tags = [], except = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/app-rb/util/consul.rb', line 16

def self.remove_services(consul, tags = [], except = nil)
  JSON.load(AppRb::Util.just_cmd("curl -s #{consul}/v1/catalog/services")).select { |k, v|
    (v & tags) == tags
  }.keys.each do |service|
    JSON.load(AppRb::Util.just_cmd("curl -s #{consul}/v1/catalog/service/#{service}")).each do |s|
      if except && s["ServiceTags"].index(except)
        # keep this service
      else
        AppRb::Util.do_it %(curl -s -X DELETE #{s["Address"]}:8500/v1/agent/service/deregister/#{s["ServiceID"]})
      end
    end
  end
end