Class: MCollective::Discovery::Delegate

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/discovery/delegate.rb

Class Method Summary collapse

Class Method Details

.binary_nameObject



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
# File 'lib/mcollective/discovery/delegate.rb', line 8

def self.discover(filter, timeout, limit, client)
  raise("Cannot find the choria binary in your path") unless Util.command_in_path?("choria")

  cmd = [binary_name, "discover", "-j", "--silent"]

  config = client.options.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

  client.options.fetch(:discovery_options, []).each do |opt|
    cmd << "--do" << opt
  end

  cmd << "--dm" << (client.options.fetch(:discovery_method, "broadcast") rescue "broadcast")

  run_discover(cmd, timeout)
end

.run_discover(cmd, timeout) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mcollective/discovery/delegate.rb', line 49

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