Class: SensuGenerator::Consul

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu_generator/consul.rb

Direct Known Subclasses

ConsulService, ConsulState

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsul

Returns a new instance of Consul.



8
9
10
11
12
13
14
15
# File 'lib/sensu_generator/consul.rb', line 8

def initialize
  @config = config
  Diplomat.configure do |consul|
    config.get[:consul].each do |k, v|
      consul.public_send("#{k}=", v)
    end
  end
end

Instance Attribute Details

#config=(value) ⇒ Object

Sets the attribute config

Parameters:

  • value

    the value to set the attribute config to.



6
7
8
# File 'lib/sensu_generator/consul.rb', line 6

def config=(value)
  @config = value
end

#logger=(value) ⇒ Object

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



6
7
8
# File 'lib/sensu_generator/consul.rb', line 6

def logger=(value)
  @logger = value
end

Instance Method Details

#get_service_props(svc) ⇒ Object



26
27
28
29
# File 'lib/sensu_generator/consul.rb', line 26

def get_service_props(svc)
  result = Diplomat::Service.get(svc, :all)
  result.class == Array ? result.map {|el| el.remove_consul_indexes} : result.remove_consul_indexes
end

#kv_svc_props(svc: name, key: nil) ⇒ Object



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

def kv_svc_props(svc: name, key: nil)
  opts = key ? nil : {recurse: true}
  response = Diplomat::Kv.get("#{svc}/#{key}", opts)
  key ? JSON(response) : response # Maybe the feature of JSON check configuration will be implemented
rescue
  if response
    if response.match(/\s+/) || key.to_s == config.get[:kv_tags_path] # tags value is designed to be a list even if it has only one element
      response.gsub(/\s+/, '').split(',')
    else
      response
    end
  else
    []
  end
end

#sensu_serversObject



17
18
19
20
# File 'lib/sensu_generator/consul.rb', line 17

def sensu_servers
  get_service_props(config.get[:sensu][:service]).map {|el| el.ServiceAddress}.uniq.
    map {|addr| SensuServer.new(address: addr)}
end

#servicesObject



22
23
24
# File 'lib/sensu_generator/consul.rb', line 22

def services
  Diplomat::Service.get_all.to_h
end