Class: OctofactsUpdater::Service::ENC

Inherits:
Object
  • Object
show all
Defined in:
lib/octofacts_updater/service/enc.rb

Class Method Summary collapse

Class Method Details

.run_enc(hostname, config) ⇒ Object

Execute the external node classifier script. This expects the value of “path” to be set in the configuration.

hostname - A String with the FQDN of the host. config - A Hash with configuration data.

Returns a Hash consisting of the parsed output of the ENC.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/octofacts_updater/service/enc.rb', line 17

def self.run_enc(hostname, config)
  unless config["enc"].is_a?(Hash)
    raise ArgumentError, "The ENC configuration must be defined"
  end

  unless config["enc"]["path"].is_a?(String)
    raise ArgumentError, "The ENC path must be defined"
  end

  unless File.file?(config["enc"]["path"])
    raise Errno::ENOENT, "The ENC script could not be found at #{config['enc']['path'].inspect}"
  end

  command = [config["enc"]["path"], hostname].map { |x| Shellwords.escape(x) }.join(" ")
  stdout, stderr, exitstatus = Open3.capture3(command)
  unless exitstatus.exitstatus == 0
    output = { "stdout" => stdout, "stderr" => stderr, "exitstatus" => exitstatus.exitstatus }
    raise "Error executing #{command.inspect}: #{output.to_yaml}"
  end

  YAML.load(stdout)
end