Class: MCollective::Discovery::Delegate
- Inherits:
-
Object
- Object
- MCollective::Discovery::Delegate
- Defined in:
- lib/mcollective/discovery/delegate.rb
Class Method Summary collapse
- .binary_name ⇒ Object
- .discover(filter, timeout, limit, client) ⇒ Object
- .run_discover(cmd, timeout) ⇒ Object
Class Method Details
.binary_name ⇒ Object
4 5 6 |
# File 'lib/mcollective/discovery/delegate.rb', line 4 def self.binary_name "choria" end |
.discover(filter, timeout, limit, client) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mcollective/discovery/delegate.rb', line 8 def self.discover(filter, timeout, limit, client) @config = Config.instance raise("Cannot find the choria binary in your path") unless Util.command_in_path?("choria") cmd = [binary_name, "discover", "-j", "--silent"] config = client..fetch(:config) cmd << "--config" << config if config cmd << "-T" << filter["collective"] if filter["collective"] filter.fetch("identity", []).each do |i| cmd << "-I" << i end filter.fetch("cf_class", []).each do |c| cmd << "-C" << c end filter.fetch("fact", []).each do |f| cmd << "-F" << "%s%s%s" % [f[:fact], f[:operator], f[:value]] end filter.fetch("agent", []).each do |a| cmd << "-A" << a end filter.fetch("compound", []).each do |c| next unless c.is_a?(Array) cmd << "-S" << c.first["expr"] end cmd << "--federations" << @config.federations.join(",") unless @config.federations.empty? client..fetch(:discovery_options, []).each do |opt| cmd << "--do" << opt end cmd << "--dm" << (client..fetch(:discovery_method, "broadcast") rescue "broadcast") run_discover(cmd, timeout) end |
.run_discover(cmd, timeout) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mcollective/discovery/delegate.rb', line 53 def self.run_discover(cmd, timeout) nodes = [] Log.debug("Executing choria for discovery using: %s" % cmd.join(" ")) Open3.popen3(ENV, *cmd) do |stdin, stdout, stderr, wait_thr| stdin.close begin Timeout.timeout(timeout + 0.5) do out = stdout.read status = wait_thr.value raise("Choria discovery failed: %s" % stderr.read) unless status.exitstatus == 0 nodes.concat(JSON.parse(out)) end rescue Timeout::Error Log.warn("Timeout waiting for Choria to perform discovery") Process.kill("KILL", wait_thr[:pid]) raise("Choria failed to complete discovery within %d timeout" % timeout) end end nodes end |