Class: Patriot::Controller::WorkerAdminController

Inherits:
Object
  • Object
show all
Includes:
Util::Config, Util::Logger
Defined in:
lib/patriot/controller/worker_admin_controller.rb

Overview

Controller class for remote management of workers

Constant Summary collapse

WORKER_COMMAND =

a command line used for start/stop workers

File.join($home || Dir.pwd,'bin', 'patriot worker')
UPGRADE_COMMAND =

a command line used for upgrade

File.join($home || Dir.pwd,'bin', 'patriot upgrade')

Constants included from Util::Config

Util::Config::ADMIN_USER_KEY, Util::Config::DEFAULT_CONFIG, Util::Config::DEFAULT_PLUGIN_DIR, Util::Config::INFO_SERVER_PORT_KEY, Util::Config::PASSWORD_KEY, Util::Config::PLUGIN_DIR_KEY, Util::Config::PLUGIN_INIT_SCRIPT, Util::Config::PLUGIN_KEY, Util::Config::PLUGIN_LIB_DIR, Util::Config::USERNAME_KEY, Util::Config::WORKER_HOST_KEY, Util::Config::WORKER_USER_KEY

Instance Method Summary collapse

Methods included from Util::Logger

#create_logger

Methods included from Util::Config

#load_config, #load_plugins

Constructor Details

#initialize(config) ⇒ WorkerAdminController

constructor

Parameters:



18
19
20
21
22
23
24
25
# File 'lib/patriot/controller/worker_admin_controller.rb', line 18

def initialize(config)
  @config = config
  @logger = create_logger(config)
  set_default_values
  username  = config.get(Patriot::Util::Config::USERNAME_KEY, "")
  password  = config.get(Patriot::Util::Config::PASSWORD_KEY, "")
  @auth = 'Basic ' + Base64.encode64("#{username}:#{password}").chomp
end

Instance Method Details

#controll_worker_at(host, cmd) ⇒ Object

execute a worker command at a remote host

Parameters:

  • host (String)

    host name of the target host



143
144
145
146
147
148
# File 'lib/patriot/controller/worker_admin_controller.rb', line 143

def controll_worker_at(host, cmd)
  sudoer = @worker_user.nil? ? "" : "-u #{@worker_user}"
  ssh_cmd = "ssh -l #{@user} #{host} sudo #{sudoer} #{WORKER_COMMAND} #{cmd}"
  @logger.info ssh_cmd
  puts `#{ssh_cmd}`
end

#do_upgrade_at(host) ⇒ Object

execute upgrade commands at a remote host

Parameters:

  • host (String)

    host name of the target host



158
159
160
161
162
# File 'lib/patriot/controller/worker_admin_controller.rb', line 158

def do_upgrade_at(host)
  ssh_cmd = "ssh -l #{@user} #{host} sudo #{UPGRADE_COMMAND}"
  @logger.info ssh_cmd
  puts `#{ssh_cmd}`
end

#get_worker_status(host, port) ⇒ String

get status of a worker

Parameters:

  • host (String)

    host name of the target host

  • port (String)

    port number of the worker process on the target host

Returns:



74
75
76
77
78
79
80
# File 'lib/patriot/controller/worker_admin_controller.rb', line 74

def get_worker_status(host, port)
  begin
    return RestClient.get("http://#{host}:#{port}/worker")
  rescue Errno::ECONNREFUSED, SocketError
    return nil
  end
end

#put_worker_status(host, port, new_status) ⇒ Object

change state of a worker

Parameters:

  • host (String)

    host name of the target host

  • port (String)

    port number of the worker process on the target host



97
98
99
100
# File 'lib/patriot/controller/worker_admin_controller.rb', line 97

def put_worker_status(host, port, new_status)
  resource = RestClient::Resource.new("http://#{host}:#{port}/worker")
  return resource.put({:status => new_status}, :Authorization => @auth )
end

#request_to_target_hosts(options = {}, &blk) ⇒ Hash

execute block for each target hosts

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :host (Object)

    a target host

  • :hosts (Object)

    a comma separated value of target hosts

  • :all (Object)

    set true to target all hosts in the configuration

Returns:

  • (Hash)

    a hash from host name to the result of the block



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/patriot/controller/worker_admin_controller.rb', line 42

def request_to_target_hosts(options = {}, &blk)
  hosts = []
  port = options.has_key?(:port) ? options[:port] : @default_port
  if options.has_key?(:host)
    hosts = [options[:host]]
  elsif options.has_key?(:hosts)
    hosts = options[:hosts]
    hosts = hosts.split(",") unless hosts.is_a?(Array)
  elsif options[:all] == true
    hosts = @default_hosts
    hosts = [hosts] unless hosts.is_a?(Array)
  else
    raise "any host is not set"
  end
  results = {}
  hosts.each{|h| results[h] = yield(h,port) }
  return results
end

#restart_worker(options = {}) ⇒ Object

restart target workers

Parameters:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/patriot/controller/worker_admin_controller.rb', line 116

def restart_worker(options = {})
  options = {:interval => 60}.merge(options)
  target_nodes = request_to_target_hosts(options){|h,p| controll_worker_at(h,'stop')}
  target_nodes.keys.each{|host| target_nodes[host] = true}

  port = options.has_key?(:port) ? options[:port] : @default_port
  while(target_nodes.has_value?(true))
    target_nodes.keys.each do |host|
      next unless target_nodes[host] # skip already started
      res = get_worker_status(host,port)
      if res.nil?
        controll_worker_at(host,'start')
        target_nodes[host] = false
      else
        if res.code == 200
          @logger.info "status code from #{host} : #{res.code}"
        else
          @logger.warn "status code from #{host} : #{res.code}"
        end
      end
    end
    sleep options[:interval] if target_nodes.has_value?(true)
  end
end

#sleep_worker(options = {}) ⇒ Object

sleep target workers

Parameters:



84
85
86
# File 'lib/patriot/controller/worker_admin_controller.rb', line 84

def sleep_worker(options = {})
  return request_to_target_hosts(options){|h,p| put_worker_status(h,p,Patriot::Worker::Status::SLEEP)}
end

#start_worker(options = {}) ⇒ Object

start target workers

Parameters:



104
105
106
# File 'lib/patriot/controller/worker_admin_controller.rb', line 104

def start_worker(options = {})
  return request_to_target_hosts(options){|h,p| controll_worker_at(h,'start')}
end

#status(options = {}) ⇒ Hash

get status of a worker or workers

Parameters:

Returns:

  • (Hash)

    status of worker in String. nil for an unresponsive worker



66
67
68
# File 'lib/patriot/controller/worker_admin_controller.rb', line 66

def status(options = {})
  return request_to_target_hosts(options){|h,p| get_worker_status(h,p)}
end

#stop_worker(options = {}) ⇒ Object

stop target workers

Parameters:



110
111
112
# File 'lib/patriot/controller/worker_admin_controller.rb', line 110

def stop_worker(options = {})
  return request_to_target_hosts(options){|h,p| controll_worker_at(h,'stop')}
end

#upgrade_worker(options = {}) ⇒ Object

upgrade libraries for target workers

Parameters:



152
153
154
# File 'lib/patriot/controller/worker_admin_controller.rb', line 152

def upgrade_worker(options = {})
  return request_to_target_hosts(options){|h,p| do_upgrade_at(h)}
end

#wake_worker(options = {}) ⇒ Object

wake up target workers

Parameters:



90
91
92
# File 'lib/patriot/controller/worker_admin_controller.rb', line 90

def wake_worker(options = {})
  return request_to_target_hosts(options){|h,p| put_worker_status(h,p,Patriot::Worker::Status::ACTIVE)}
end