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: InsufficientVersionError, LengthExceedsLimitError, SerfCommandNotFound, 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.



29
30
31
32
33
34
35
36
# File 'lib/villein/client.rb', line 29

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

  retrieve_name unless @name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



41
42
43
# File 'lib/villein/client.rb', line 41

def name
  @name
end

#rpc_addrObject (readonly)

Returns the value of attribute rpc_addr.



41
42
43
# File 'lib/villein/client.rb', line 41

def rpc_addr
  @rpc_addr
end

#serfObject (readonly)

Returns the value of attribute serf.



41
42
43
# File 'lib/villein/client.rb', line 41

def serf
  @serf
end

#silence=(value) ⇒ Object (writeonly)

Sets the attribute silence

Parameters:

  • value

    the value to set the attribute silence to.



39
40
41
# File 'lib/villein/client.rb', line 39

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.



144
145
146
# File 'lib/villein/client.rb', line 144

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

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



52
53
54
55
56
57
58
59
60
# File 'lib/villein/client.rb', line 52

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

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

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

#force_leave(node) ⇒ Object



106
107
108
# File 'lib/villein/client.rb', line 106

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.



136
137
138
139
# File 'lib/villein/client.rb', line 136

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

#infoObject

Returns a result of ‘serf info`. This may raise InsufficientVersionError when `serf info` is not supported.



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

def info
  JSON.parse call_serf('info', '-format', 'json')
rescue SerfCommandNotFound
  raise InsufficientVersionError, 'serf v0.6.0 or later is required to run `serf info`.'
end

#join(addr, replay: false) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/villein/client.rb', line 92

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

  if replay
    options << '-replay'
  end

  call_serf 'join', *options, addr
end

#leaveObject



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

def leave
  call_serf 'leave'
end

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/villein/client.rb', line 110

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

#query(name, payload, node: nil, tag: nil, timeout: nil, no_ack: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/villein/client.rb', line 62

def query(name, payload, node: nil, tag: nil, timeout: nil, no_ack: false)
  # TODO: version check
  options = ['-format', 'json']

  if node
    node = [node] unless node.respond_to?(:each)
    node.each do |n|
      options << "-node=#{n}"
    end
  end

  if tag
    tag = [tag] unless tag.respond_to?(:each)
    tag.each do |t|
      options << "-tag=#{t}"
    end
  end

  if timeout
    options << "-timeout=#{timeout}"
  end

  if no_ack
    options << "-no-ack"
  end

  out = call_serf('query', *options, name, payload)
  JSON.parse(out)
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.



151
152
153
# File 'lib/villein/client.rb', line 151

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

#silence?Boolean

Returns:

  • (Boolean)


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

def silence?() !!@silence; end

#tagsObject

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



129
130
131
# File 'lib/villein/client.rb', line 129

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