Class: Bosh::Consul::Models::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/consul/models/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(director_client) ⇒ Cluster

Returns a new instance of Cluster.



5
6
7
8
9
10
# File 'lib/bosh/consul/models/cluster.rb', line 5

def initialize(director_client)
  @director_client = director_client

  # FIXME: waiting on an API endpoint to discover the domain
  @domain = "consul"
end

Instance Attribute Details

#director_clientObject (readonly)

Returns the value of attribute director_client.



2
3
4
# File 'lib/bosh/consul/models/cluster.rb', line 2

def director_client
  @director_client
end

#domainObject (readonly)

Returns the value of attribute domain.



3
4
5
# File 'lib/bosh/consul/models/cluster.rb', line 3

def domain
  @domain
end

#leaderObject (readonly)

Returns the value of attribute leader.



3
4
5
# File 'lib/bosh/consul/models/cluster.rb', line 3

def leader
  @leader
end

#peersObject (readonly)

Returns the value of attribute peers.



3
4
5
# File 'lib/bosh/consul/models/cluster.rb', line 3

def peers
  @peers
end

Instance Method Details

#discover_from_ips(ips) ⇒ Object

Discover the consul cluster from the IPs of servers or clients to the cluster Setups up @leader & @peers IP addresses



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bosh/consul/models/cluster.rb', line 29

def discover_from_ips(ips)
  ip = ips.first
  if @leader = get(ip, "/v1/status/leader")
    @leader =~ /"(.*):(\d+)"/
    @leader, @cluster_port = $1, $2
    @peers = JSON.parse(get(ip, "/v1/status/peers"))
    @peers.map! do |peer|
      peer =~ /(.*):(\d+)/
      $1
    end
  end
end

#load_from_agent(leader_ip) ⇒ Object



22
23
24
25
# File 'lib/bosh/consul/models/cluster.rb', line 22

def load_from_agent(leader_ip)
  @leader = @peers = nil
  discover_from_ips([leader_ip])
end

#load_from_deployment_name(deployment_name) ⇒ Object

A [re-]initialization method to get IPs for a cluster



13
14
15
16
17
18
19
20
# File 'lib/bosh/consul/models/cluster.rb', line 13

def load_from_deployment_name(deployment_name)
  @leader = @peers = nil

  # fetch_vm_state raises error if name invalid
  vms = director_client.fetch_vm_state(deployment_name)
  deployment_ips = vms.map {|vm| vm["ips"].first }
  discover_from_ips(deployment_ips)
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/bosh/consul/models/cluster.rb', line 42

def valid?
  @leader
end