Class: Villein::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/villein/client.rb

Overview

Villein::Client allows you to order existing serf agent. You will need RPC address and agent name to command.

Direct Known Subclasses

Agent

Defined Under Namespace

Classes: LengthExceedsLimitError, SerfConnectionError, SerfError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpc_addr, name: nil, serf: 'serf', silence: true) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
# File 'lib/villein/client.rb', line 22

def initialize(rpc_addr, name: nil, serf: 'serf', silence: true)
  @rpc_addr = rpc_addr
  @name = name
  @serf = serf
  @silence = true
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/villein/client.rb', line 32

def name
  @name
end

#rpc_addrObject (readonly)

Returns the value of attribute rpc_addr.



32
33
34
# File 'lib/villein/client.rb', line 32

def rpc_addr
  @rpc_addr
end

#serfObject (readonly)

Returns the value of attribute serf.



32
33
34
# File 'lib/villein/client.rb', line 32

def serf
  @serf
end

#silence=(value) ⇒ Object (writeonly)

Sets the attribute silence

Parameters:

  • value

    the value to set the attribute silence to.



30
31
32
# File 'lib/villein/client.rb', line 30

def silence=(value)
  @silence = value
end

Instance Method Details

#delete_tag(key) ⇒ Object

Remove tag from the agent. Using Villein::Client#tags method is recommended. It provides high-level API via Villein::Tags.



96
97
98
# File 'lib/villein/client.rb', line 96

def delete_tag(key)
  call_serf 'tags', '-delete', key
end

#event(name, payload, coalesce: true) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/villein/client.rb', line 34

def event(name, payload, coalesce: true)
  options = []

  unless coalesce
    options << '-coalesce=false'
  end

  call_serf 'event', *options, name, payload
end

#force_leave(node) ⇒ Object



58
59
60
# File 'lib/villein/client.rb', line 58

def force_leave(node)
  call_serf 'force-leave', node
end

#get_tagsObject

Get tag from the agent. Using Villein::Client#tags method is recommended. It provides high-level API via Villein::Tags.



88
89
90
91
# File 'lib/villein/client.rb', line 88

def get_tags
  me = members(name: self.name)[0]
  me["tags"]
end

#join(addr, replay: false) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/villein/client.rb', line 44

def join(addr, replay: false)
  options = []

  if replay
    options << '-replay'
  end

  call_serf 'join', *options, addr
end

#leaveObject



54
55
56
# File 'lib/villein/client.rb', line 54

def leave
  call_serf 'leave'
end

#members(status: nil, name: nil, tags: {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/villein/client.rb', line 62

def members(status: nil, name: nil, tags: {})
  options = ['-format', 'json']

  options.push('-status', status.to_s) if status
  options.push('-name', name.to_s) if name

  tags.each do |tag, val|
    options.push('-tag', "#{tag}=#{val}")
  end

  json = call_serf('members', *options)
  response = JSON.parse(json)

  response["members"]
end

#set_tag(key, val) ⇒ Object

Set tag to the agent. Using Villein::Client#tags method is recommended. It provides high-level API via Villein::Tags.



103
104
105
# File 'lib/villein/client.rb', line 103

def set_tag(key, val)
  call_serf 'tags', '-set', "#{key}=#{val}"
end

#silence?Boolean

Returns:

  • (Boolean)


29
# File 'lib/villein/client.rb', line 29

def silence?() !!@silence; end

#tagsObject

Returns Villein::Tags object for the current agent. Villein::Tags provides high-level API for tagging agents.



81
82
83
# File 'lib/villein/client.rb', line 81

def tags
  @tags ||= Tags.new(self)
end