Class: Patriot::Controller::WorkerAdminController
- Inherits:
-
Object
- Object
- Patriot::Controller::WorkerAdminController
- 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
-
#controll_worker_at(host, cmd) ⇒ Object
execute a worker command at a remote host.
-
#do_upgrade_at(host) ⇒ Object
execute upgrade commands at a remote host.
-
#get_worker_status(host, port) ⇒ String
get status of a worker.
-
#initialize(config) ⇒ WorkerAdminController
constructor
constructor.
-
#put_worker_status(host, port, new_status) ⇒ Object
change state of a worker.
-
#request_to_target_hosts(options = {}, &blk) ⇒ Hash
execute block for each target hosts.
-
#restart_worker(options = {}) ⇒ Object
restart target workers.
-
#sleep_worker(options = {}) ⇒ Object
sleep target workers.
-
#start_worker(options = {}) ⇒ Object
start target workers.
-
#status(options = {}) ⇒ Hash
get status of a worker or workers.
-
#stop_worker(options = {}) ⇒ Object
stop target workers.
-
#upgrade_worker(options = {}) ⇒ Object
upgrade libraries for target workers.
-
#wake_worker(options = {}) ⇒ Object
wake up target workers.
Methods included from Util::Logger
Methods included from Util::Config
Constructor Details
#initialize(config) ⇒ WorkerAdminController
constructor
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
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
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
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
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
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( = {}, &blk) hosts = [] port = .has_key?(:port) ? [:port] : @default_port if .has_key?(:host) hosts = [[:host]] elsif .has_key?(:hosts) hosts = [:hosts] hosts = hosts.split(",") unless hosts.is_a?(Array) elsif [: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
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( = {}) = {:interval => 60}.merge() target_nodes = request_to_target_hosts(){|h,p| controll_worker_at(h,'stop')} target_nodes.keys.each{|host| target_nodes[host] = true} port = .has_key?(:port) ? [: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 [:interval] if target_nodes.has_value?(true) end end |
#sleep_worker(options = {}) ⇒ Object
sleep target workers
84 85 86 |
# File 'lib/patriot/controller/worker_admin_controller.rb', line 84 def sleep_worker( = {}) return request_to_target_hosts(){|h,p| put_worker_status(h,p,Patriot::Worker::Status::SLEEP)} end |
#start_worker(options = {}) ⇒ Object
start target workers
104 105 106 |
# File 'lib/patriot/controller/worker_admin_controller.rb', line 104 def start_worker( = {}) return request_to_target_hosts(){|h,p| controll_worker_at(h,'start')} end |
#status(options = {}) ⇒ Hash
get status of a worker or workers
66 67 68 |
# File 'lib/patriot/controller/worker_admin_controller.rb', line 66 def status( = {}) return request_to_target_hosts(){|h,p| get_worker_status(h,p)} end |
#stop_worker(options = {}) ⇒ Object
stop target workers
110 111 112 |
# File 'lib/patriot/controller/worker_admin_controller.rb', line 110 def stop_worker( = {}) return request_to_target_hosts(){|h,p| controll_worker_at(h,'stop')} end |
#upgrade_worker(options = {}) ⇒ Object
upgrade libraries for target workers
152 153 154 |
# File 'lib/patriot/controller/worker_admin_controller.rb', line 152 def upgrade_worker( = {}) return request_to_target_hosts(){|h,p| do_upgrade_at(h)} end |