class Sporf

Public Class Methods

execute_query(fqdn) click to toggle source
# File lib/sporf/sporf.rb, line 5
def execute_query fqdn
  begin
    timeout @timeout_interval do
      Net::SSH.start(fqdn, 'root', paranoid: false, forward_agent: true) do |ssh|
        output = ssh_exec!(ssh, @command)
        parse_output output, fqdn
      end
    end
  rescue TimeoutError, Errno::ETIMEDOUT, SocketError, Errno::EHOSTUNREACH => e
    timed_out "#{fqdn}: #{e.message}"
  rescue Exception => e
    timed_out "#{fqdn}: #{e.inspect}"
  end
end
failed(reason) click to toggle source
# File lib/sporf/helpers.rb, line 5
def failed(reason)
  failed_output_stream.push reason
  print_fail
end
failed_output_stream() click to toggle source
# File lib/sporf/helpers.rb, line 24
def failed_output_stream
  @failed_output_stream ||= Array.new
end
invert?() click to toggle source
# File lib/sporf/helpers.rb, line 36
def invert?
  @invert
end
parse_output(output, fqdn) click to toggle source
# File lib/sporf/sporf.rb, line 20
def parse_output output, fqdn
  if output[2] == 0 && !invert?
    succeeded "#{fqdn}: #{output[0]}"
  elsif output[2] != 0 && invert?
    succeeded "#{fqdn} returned #{output[2]}: #{output[1]} #{output[0]}"
  else
    failed "#{fqdn} returned #{output[2]}: #{output[1]} #{output[0]}"
  end
end
print_fail() click to toggle source
print_failed_stream() click to toggle source
print_success() click to toggle source
print_success_stream() click to toggle source
print_summary() click to toggle source
print_timeout() click to toggle source
print_timeout_stream() click to toggle source
ssh_exec!(ssh, command) click to toggle source
# File lib/sporf/ssh.rb, line 3
def ssh_exec!(ssh, command)
  # I am not awesome enough to have made this method myself
  # Originally submitted by 'flitzwald' over here: http://stackoverflow.com/a/3386375
  stdout_data = ''
  stderr_data = ''
  exit_code = nil

  ssh.open_channel do |channel|
    channel.exec(command) do |ch, success|
      unless success
        abort "FAILED: couldn't execute command (ssh.channel.exec)"
      end
      channel.on_data do |ch,data|
        stdout_data+=data
      end

      channel.on_extended_data do |ch,type,data|
        stderr_data+=data
      end

      channel.on_request('exit-status') do |ch,data|
        exit_code = data.read_long
      end
    end
  end
  ssh.loop
  [stdout_data, stderr_data, exit_code]
end
stdout() click to toggle source
# File lib/sporf/chef.rb, line 8
def stdout
  @stdout_hack ||= ::File.new('/dev/null', 'w')
end
succeeded(reason) click to toggle source
# File lib/sporf/helpers.rb, line 10
def succeeded(reason)
  success_output_stream.push reason
  print_success
end
success_output_stream() click to toggle source
# File lib/sporf/helpers.rb, line 28
def success_output_stream
  @success_output_stream ||= Array.new
end
test(command, query, timeout_interval, verbose_success, invert) click to toggle source
# File lib/sporf.rb, line 11
def test command, query, timeout_interval, verbose_success, invert
  @command = command
  @query = query
  @timeout_interval = timeout_interval
  @verbose_success = verbose_success
  @invert = invert

  knife_search(@query).peach(5) do |fqdn|
    execute_query fqdn
  end

  print_summary
end
timed_out(reason) click to toggle source
# File lib/sporf/helpers.rb, line 15
def timed_out(reason)
  timeout_output_stream.push reason
  print_timeout
end
timeout_output_stream() click to toggle source
# File lib/sporf/helpers.rb, line 20
def timeout_output_stream
  @timeout_output_stream ||= Array.new
end
verbose_success?() click to toggle source
# File lib/sporf/helpers.rb, line 32
def verbose_success?
  @verbose_success
end