Class: Blender::Discovery::Serf

Inherits:
Object
  • Object
show all
Defined in:
lib/blender/discoveries/serf.rb

Defined Under Namespace

Classes: Property

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Serf

Returns a new instance of Serf.



27
28
29
30
31
32
33
# File 'lib/blender/discoveries/serf.rb', line 27

def initialize(opts = {})
  @config = {}
  @config[:host] = opts[:host] if opts.key?(:host)
  @config[:port] = opts[:port] if opts.key?(:port)
  @config[:authkey] = opts[:authkey] if opts.key?(:authkey)
  @attribute = opts[:attribute] || 'name'
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



25
26
27
# File 'lib/blender/discoveries/serf.rb', line 25

def attribute
  @attribute
end

Instance Method Details

#search(opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/blender/discoveries/serf.rb', line 35

def search(opts = {})
  opts = {} if opts.nil?
  tags = opts[:tags] || {}
  status = opts[:status] || 'alive'
  name = opts[:name]
  hosts = []
  Blender::Log.debug("Using Serf agents '#{attribute}' as discovery return values")
  case attribute
  when 'name'
    property = Property.new('Name', lambda{|data| data})
  when 'ipaddress'
    property = Property.new('Addr', lambda{|data| data.unpack('CCCC').join('.')})
  else
    raise ArgumentError, "Attribute can be either 'name' or 'ipaddress', you have provided '#{attribute}'"
  end
  Blender::Log.debug("Serf memeber list call with arguments: Tags=#{tags}")
  Blender::Log.debug("Serf memeber list call with arguments: status=#{status}")
  Blender::Log.debug("Serf memeber list call with arguments: Name=#{name}")
  Serfx.connect(@config) do |conn|
    conn.members_filtered(tags, status, name).body['Members'].map do |m|
      hosts << property.hook.call(m[property.key])
    end
  end
  hosts
end