class Flare::Tools::Cli::Ping

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/flare/tools/cli/ping.rb, line 27
def initialize
  super
  @wait = false
end

Public Instance Methods

execute(config, args) click to toggle source
# File lib/flare/tools/cli/ping.rb, line 32
def execute(config, args)

  hosts = args.map do |arg|
    hostname, port, rest = arg.split(':', 3)
    if !rest.nil? || hostname.nil? || hostname.empty? || port.nil? || port.empty?
      error "invalid argument '#{arg}'. it must be hostname:port."
      return S_NG
    end
    begin
      ipaddr = Resolv.getaddress(hostname)
    rescue Resolv::ResolvError
      error "unknown host '#{hostname}'"
      return S_NG
    end
    [hostname, port]
  end

  hosts.each do |hostname, port|
    resp = nil
    until resp
      begin
        debug "trying..."
        interruptible do
          Flare::Tools::Stats.open(hostname, port, @timeout) do |s|
            resp = s.ping
          end
        end
      rescue IOError
        return S_NG
      rescue
        unless @wait
          puts "#{hostname}:#{port} is down"
          return S_NG
        end
        interruptible {sleep 1}
      end
    end
  end

  puts "alive"
  S_OK
end
setup() click to toggle source
Calls superclass method
# File lib/flare/tools/cli/ping.rb, line 22
def setup
  super
  @optp.on('--wait',            "wait for OK responses from nodes") {@wait = true}
end