Class: Flare::Tools::Cli::Ping

Inherits:
SubCommand show all
Includes:
Util::Conversion, Util::Logging
Defined in:
lib/flare/tools/cli/ping.rb

Constant Summary

Constants inherited from SubCommand

SubCommand::S_NG, SubCommand::S_OK

Constants included from Util::Interruption

Util::Interruption::InterruptionTargets

Constants included from Util::Constant

Util::Constant::DefalutBwlimit, Util::Constant::DefaultIndexServerName, Util::Constant::DefaultIndexServerPort, Util::Constant::DefaultNodePort, Util::Constant::DefaultTimeout, Util::Constant::STATUS_NG, Util::Constant::STATUS_OK

Instance Attribute Summary

Attributes included from Option

#optp

Instance Method Summary collapse

Methods included from Util::Logging

#debug, #error, #fatal, #info, logger, #puts, set_logger, #trace, #warn

Methods included from Util::Conversion

#short_desc_of_second

Methods inherited from SubCommand

desc, #execute_subcommand, myname, #myname, to_s, to_sym, usage

Methods included from Util::Interruption

included, #initialize_interruption, #interrupt, #interrupt_, interrupt_all, #interrupted?, #interruptible, #interruptible?

Methods included from Option

#option_init, #parse_options, #set_option_dry_run, #set_option_force, #set_option_global, #set_option_index_server

Constructor Details

#initializePing

Returns a new instance of Ping.



28
29
30
31
# File 'lib/flare/tools/cli/ping.rb', line 28

def initialize
  super
  @wait = false
end

Instance Method Details

#execute(config, args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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/flare/tools/cli/ping.rb', line 33

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

#setupObject



23
24
25
26
# File 'lib/flare/tools/cli/ping.rb', line 23

def setup
  super
  @optp.on('--wait',            "wait for OK responses from nodes") {@wait = true}
end