class Patriot::Controller::WorkerAdminController

Controller class for remote management of workers

Constants

UPGRADE_COMMAND

a command line used for upgrade

WORKER_COMMAND

a command line used for start/stop workers

Public Class Methods

new(config) click to toggle source

constructor @param config [Patriot::Util::Config::Base] configuration of this controller

# 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

Public Instance Methods

controll_worker_at(host, cmd) click to toggle source

execute a worker command at a remote host @param host [String] host name of the target host

# 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) click to toggle source

execute upgrade commands at a remote host @param host [String] host name of the target host

# 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) click to toggle source

get status of a worker @param host [String] host name of the target host @param port [String] port number of the worker process on the target host @return [String] status of the server @see {Patriot::Worker::Base}

# 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) click to toggle source

change state of a worker @param host [String] host name of the target host @param port [String] port number of the worker process on the target host

# 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 = {}) { |h,port| ... } click to toggle source

execute block for each target hosts @param options [Hash] @option options :host a target host @option options :hosts a comma separated value of target hosts @option options :all set true to target all hosts in the configuration @return [Hash] a hash from host name to the result of the block

# 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 = {}) click to toggle source

restart target workers @param options @see {#request_to_target_hosts}

# 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 = {}) click to toggle source

sleep target workers @param options @see {#request_to_target_hosts}

# 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 = {}) click to toggle source

start target workers @param options @see {#request_to_target_hosts}

# 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 = {}) click to toggle source

get status of a worker or workers @param options @see {#request_to_target_hosts} @return [Hash]

status of worker in String.
nil for an unresponsive worker
# 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 = {}) click to toggle source

stop target workers @param options @see {#request_to_target_hosts}

# 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 = {}) click to toggle source

upgrade libraries for target workers @param options @see {#request_to_target_hosts}

# 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 = {}) click to toggle source

wake up target workers @param options @see {#request_to_target_hosts}

# 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

Private Instance Methods

set_default_values() click to toggle source

@private

# File lib/patriot/controller/worker_admin_controller.rb, line 28
def set_default_values
  @default_hosts = @config.get(WORKER_HOST_KEY) || []
  @default_port  = @config.get(INFO_SERVER_PORT_KEY)
  @user          = @config.get(ADMIN_USER_KEY)
  @worker_user   = @config.get(WORKER_USER_KEY)
end