Class: Flare::Tools::Cli::Activate

Inherits:
SubCommand show all
Includes:
IndexServerConfig, Flare::Tools::Common, Util::Constant, Util::Conversion, Util::Logging
Defined in:
lib/flare/tools/cli/activate.rb

Constant Summary

Constants included from IndexServerConfig

IndexServerConfig::Entity, IndexServerConfig::FLARE_INDEX_SERVER, IndexServerConfig::FLARE_INDEX_SERVERS

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

Constants included from Flare::Tools::Common

Flare::Tools::Common::NodeListFormat, Flare::Tools::Common::NodeListHeader

Constants inherited from SubCommand

SubCommand::S_NG, SubCommand::S_OK

Constants included from Util::Interruption

Util::Interruption::InterruptionTargets

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 Flare::Tools::Common

#address_of_hostname, #fetch_cluster, #hostname_of_address, #nodekey_of, #string_of_nodelist, #user_confirmed, #wait_for_master_construction, #wait_for_servers, #wait_for_slave_construction

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

#initializeActivate

Returns a new instance of Activate.



38
39
40
41
# File 'lib/flare/tools/cli/activate.rb', line 38

def initialize
  super
  @force = false
end

Instance Method Details

#execute(config, args) ⇒ Object



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/flare/tools/cli/activate.rb', line 43

def execute(config, args)
  parse_index_server(config, args)
  return S_NG if args.size < 1

  hosts = args.map {|x| x.split(':')}
  hosts.each do |x|
    if x.size != 2
      puts "invalid argument '#{x.join(':')}'."
      return S_NG
    end
  end

  Flare::Tools::IndexServer.open(config[:index_server_hostname], config[:index_server_port], @timeout) do |s|
    cluster = Flare::Tools::Cluster.new(s.host, s.port, s.stats_nodes)
    nodes = s.stats_nodes.sort_by{|key, val| [val['partition'], val['role'], key]}

    hosts.each do |hostname,port|
      nodekey = nodekey_of hostname, port
      ipaddr = address_of_hostname(hostname)

      unless cluster.has_nodekey? nodekey
        error "invalid 'hostname:port' pair: #{nodekey}"
        return S_NG
      end

      node = cluster.node_stat(nodekey)

      exec = @force
      if exec
      elsif node['state'] == 'active'
        warn "#{ipaddr}:#{port} is already active."
      else
        STDERR.print "turning node up (node=#{ipaddr}:#{port}, state=#{node['state']} -> activate) (y/n): "
        exec = interruptible {
          (gets.chomp.upcase == "Y")
        }
      end
      if exec && !@dry_run
        if @force
          begin
            s.set_state(hostname, port, 'active')
          rescue Timeout::Error => e
            error "failed to activate #{nodekey} (timeout)"
            raise e
          end
        else
          resp = false
          until resp
            resp = s.set_state(hostname, port, 'active')
            unless resp
              STDERR.print "turning node up (node=#{ipaddr}:#{port}, state=#{node['state']} -> activate) (y/n): "
              exec = interruptible {
                (gets.chomp.upcase == "Y")
              }
            end
          end
        end
      end
    end

    STDOUT.puts string_of_nodelist(s.stats_nodes, hosts.map {|x| "#{x[0]}:#{x[1]}"})
  end

  S_OK
end

#setupObject



31
32
33
34
35
36
# File 'lib/flare/tools/cli/activate.rb', line 31

def setup
  super
  set_option_index_server
  set_option_dry_run
  set_option_force
end