Class MCollective::Client
In: lib/mcollective/client.rb
Parent: Object

Helpers for writing clients that can talk to agents, do discovery and so forth

Methods

Attributes

discoverer  [RW] 
options  [RW] 
stats  [RW] 

Public Class methods

[Source]

    # File lib/mcollective/client.rb, line 6
 6:     def initialize(configfile)
 7:       @config = Config.instance
 8:       @config.loadconfig(configfile) unless @config.configured
 9: 
10:       @connection = PluginManager["connector_plugin"]
11:       @security = PluginManager["security_plugin"]
12: 
13:       @security.initiated_by = :client
14:       @options = nil
15:       @subscriptions = {}
16: 
17:       @discoverer = Discovery.new(self)
18:       @connection.connect
19:     end

Public Instance methods

Returns the configured main collective if no specific collective is specified as options

[Source]

    # File lib/mcollective/client.rb, line 23
23:     def collective
24:       if @options[:collective].nil?
25:         @config.main_collective
26:       else
27:         @options[:collective]
28:       end
29:     end

Disconnects cleanly from the middleware

[Source]

    # File lib/mcollective/client.rb, line 32
32:     def disconnect
33:       Log.debug("Disconnecting from the middleware")
34:       @connection.disconnect
35:     end

Performs a discovery of nodes matching the filter passed returns an array of nodes

An integer limit can be supplied this will have the effect of the discovery being cancelled soon as it reached the requested limit of hosts

[Source]

     # File lib/mcollective/client.rb, line 115
115:     def discover(filter, timeout, limit=0)
116:       discovered = @discoverer.discover(filter, timeout, limit)
117:     end

[Source]

     # File lib/mcollective/client.rb, line 173
173:     def discovered_req(body, agent, options=false)
174:       raise "Client#discovered_req has been removed, please port your agent and client to the SimpleRPC framework"
175:     end

Prints out the stats returns from req and discovered_req in a nice way

[Source]

     # File lib/mcollective/client.rb, line 178
178:     def display_stats(stats, options=false, caption="stomp call summary")
179:       options = @options unless options
180: 
181:       if options[:verbose]
182:         puts("\n---- #{caption} ----")
183: 
184:         if stats[:discovered]
185:           puts("           Nodes: #{stats[:discovered]} / #{stats[:responses]}")
186:         else
187:           puts("           Nodes: #{stats[:responses]}")
188:         end
189: 
190:         printf("      Start Time: %s\n", Time.at(stats[:starttime]))
191:         printf("  Discovery Time: %.2fms\n", stats[:discoverytime] * 1000)
192:         printf("      Agent Time: %.2fms\n", stats[:blocktime] * 1000)
193:         printf("      Total Time: %.2fms\n", stats[:totaltime] * 1000)
194: 
195:       else
196:         if stats[:discovered]
197:           printf("\nFinished processing %d / %d hosts in %.2f ms\n\n", stats[:responses], stats[:discovered], stats[:blocktime] * 1000)
198:         else
199:           printf("\nFinished processing %d hosts in %.2f ms\n\n", stats[:responses], stats[:blocktime] * 1000)
200:         end
201:       end
202: 
203:       if stats[:noresponsefrom].size > 0
204:         puts("\nNo response from:\n")
205: 
206:         stats[:noresponsefrom].each do |c|
207:           puts if c % 4 == 1
208:           printf("%30s", c)
209:         end
210: 
211:         puts
212:       end
213:     end

Blocking call that waits for ever for a message to arrive.

If you give it a requestid this means you‘ve previously send a request with that ID and now you just want replies that matches that id, in that case the current connection will just ignore all messages not directed at it and keep waiting for more till it finds a matching message.

[Source]

     # File lib/mcollective/client.rb, line 85
 85:     def receive(requestid = nil)
 86:       reply = nil
 87: 
 88:       begin
 89:         reply = @connection.receive
 90:         reply.type = :reply
 91:         reply.expected_msgid = requestid
 92: 
 93:         reply.decode!
 94: 
 95:         reply.payload[:senderid] = Digest::MD5.hexdigest(reply.payload[:senderid]) if ENV.include?("MCOLLECTIVE_ANON")
 96: 
 97:         raise(MsgDoesNotMatchRequestID, "Message reqid #{requestid} does not match our reqid #{reply.requestid}") unless reply.requestid == requestid
 98:       rescue SecurityValidationFailed => e
 99:         Log.warn("Ignoring a message that did not pass security validations")
100:         retry
101:       rescue MsgDoesNotMatchRequestID => e
102:         Log.debug("Ignoring a message for some other client")
103:         retry
104:       end
105: 
106:       reply
107:     end

Send a request, performs the passed block for each response

times = req("status", "mcollectived", options, client) {|resp|

  pp resp

}

It returns a hash of times and timeouts for discovery and total run is taken from the options hash which in turn is generally built using MCollective::Optionparser

[Source]

     # File lib/mcollective/client.rb, line 127
127:     def req(body, agent=nil, options=false, waitfor=0)
128:       if body.is_a?(Message)
129:         agent = body.agent
130:         options = body.options
131:         waitfor = body.discovered_hosts.size || 0
132:       end
133: 
134:       stat = {:starttime => Time.now.to_f, :discoverytime => 0, :blocktime => 0, :totaltime => 0}
135: 
136:       timeout = @discoverer.discovery_timeout(@options[:timeout], @options[:filter])
137: 
138:       STDOUT.sync = true
139: 
140:       hosts_responded = 0
141:       reqid = nil
142: 
143:       begin
144:         Timeout.timeout(timeout) do
145:           reqid = sendreq(body, agent, @options[:filter])
146: 
147:           loop do
148:             resp = receive(reqid)
149: 
150:             hosts_responded += 1
151: 
152:             yield(resp.payload)
153: 
154:             break if (waitfor != 0 && hosts_responded >= waitfor)
155:           end
156:         end
157:       rescue Interrupt => e
158:       rescue Timeout::Error => e
159:       ensure
160:         unsubscribe(agent, :reply)
161:       end
162: 
163:       stat[:totaltime] = Time.now.to_f - stat[:starttime]
164:       stat[:blocktime] = stat[:totaltime] - stat[:discoverytime]
165:       stat[:responses] = hosts_responded
166:       stat[:noresponsefrom] = []
167:       stat[:requestid] = reqid
168: 
169:       @stats = stat
170:       return stat
171:     end

Sends a request and returns the generated request id, doesn‘t wait for responses and doesn‘t execute any passed in code blocks for responses

[Source]

    # File lib/mcollective/client.rb, line 39
39:     def sendreq(msg, agent, filter = {})
40:       if msg.is_a?(Message)
41:         request = msg
42:         agent = request.agent
43:       else
44:         ttl = @options[:ttl] || @config.ttl
45:         request = Message.new(msg, nil, {:agent => agent, :type => :request, :collective => collective, :filter => filter, :ttl => ttl})
46:         request.reply_to = @options[:reply_to] if @options[:reply_to]
47:       end
48: 
49:       request.encode!
50: 
51:       Log.debug("Sending request #{request.requestid} to the #{request.agent} agent with ttl #{request.ttl} in collective #{request.collective}")
52: 
53:       subscribe(agent, :reply) unless request.reply_to
54: 
55:       request.publish
56: 
57:       request.requestid
58:     end

[Source]

    # File lib/mcollective/client.rb, line 60
60:     def subscribe(agent, type)
61:       unless @subscriptions.include?(agent)
62:         subscription = Util.make_subscriptions(agent, type, collective)
63:         Log.debug("Subscribing to #{type} target for agent #{agent}")
64: 
65:         Util.subscribe(subscription)
66:         @subscriptions[agent] = 1
67:       end
68:     end

[Source]

    # File lib/mcollective/client.rb, line 70
70:     def unsubscribe(agent, type)
71:       if @subscriptions.include?(agent)
72:         subscription = Util.make_subscriptions(agent, type, collective)
73:         Log.debug("Unsubscribing #{type} target for #{agent}")
74: 
75:         Util.unsubscribe(subscription)
76:         @subscriptions.delete(agent)
77:       end
78:     end

[Validate]