Class: EventMachine::WinRM::Session
- Inherits:
-
Object
- Object
- EventMachine::WinRM::Session
- Defined in:
- lib/em-winrm/session.rb
Instance Method Summary collapse
-
#close ⇒ Object
clean up servers and stop the EventMachine event loop.
-
#command_complete(host, cid) ⇒ Object
called by backend server when it completes a command.
-
#initialize(options = {}) ⇒ Session
constructor
A new instance of Session.
-
#on(*new_servers) {|subsession| ... } ⇒ Object
returns a new EventMachine::WinRM::Session instance consisting of a specific sub-set of servers.
- #on_close(&blk) ⇒ Object
- #on_command_complete(&blk) ⇒ Object
- #on_error(&blk) ⇒ Object
- #on_finish(&blk) ⇒ Object
-
#on_output(&blk) ⇒ Object
Proxy Methods.
-
#relay_command(data) ⇒ Object
run command on all servers.
-
#relay_error_from_backend(host, data) ⇒ Object
relay error output from backend server to client.
-
#relay_output_from_backend(host, data) ⇒ Object
relay output from backend server to client.
-
#servers ⇒ Object
return an array of the current servers in the session.
-
#servers=(servers) ⇒ Object
set the current servers in the session.
- #unbind ⇒ Object
- #unbind_backend(host) ⇒ Object
-
#use(host, options) ⇒ Object
initialize connections to a server.
Constructor Details
#initialize(options = {}) ⇒ Session
Returns a new instance of Session.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/em-winrm/session.rb', line 30 def initialize(={}) if [:logger] WinRM::Log.logger = [:logger] else log_level = [:log_level] || :info WinRM::Log.level(log_level) end @servers = {} @commands = [] WinRM::Log.debug(":session => :init") end |
Instance Method Details
#close ⇒ Object
clean up servers and stop the EventMachine event loop
145 146 147 148 149 150 151 |
# File 'lib/em-winrm/session.rb', line 145 def close unbind # try to stop eventmachine loop EM.stop rescue @on_close.call if @on_close WinRM::Log.debug(":session => :close") end |
#command_complete(host, cid) ⇒ Object
called by backend server when it completes a command
114 115 116 117 118 119 120 121 122 |
# File 'lib/em-winrm/session.rb', line 114 def command_complete(host, cid) WinRM::Log.debug(":command_complete => #{host}") @commands.delete(cid) @on_command_complete.call(host) if @on_command_complete if @commands.compact.size.zero? @on_command_complete.call(:all) if @on_command_complete EM.stop end end |
#on(*new_servers) {|subsession| ... } ⇒ Object
returns a new EventMachine::WinRM::Session instance consisting of a specific sub-set of servers
inspired by Net::SSH::Multi::Session.on
88 89 90 91 92 93 |
# File 'lib/em-winrm/session.rb', line 88 def on(*new_servers) subsession = self.clone subsession.servers = new_servers & servers yield subsession if block_given? subsession end |
#on_close(&blk) ⇒ Object
28 |
# File 'lib/em-winrm/session.rb', line 28 def on_close(&blk); @on_close = blk; end |
#on_command_complete(&blk) ⇒ Object
26 |
# File 'lib/em-winrm/session.rb', line 26 def on_command_complete(&blk); @on_command_complete = blk; end |
#on_error(&blk) ⇒ Object
25 |
# File 'lib/em-winrm/session.rb', line 25 def on_error(&blk); @on_error = blk; end |
#on_finish(&blk) ⇒ Object
27 |
# File 'lib/em-winrm/session.rb', line 27 def on_finish(&blk); @on_finish = blk; end |
#on_output(&blk) ⇒ Object
Proxy Methods
24 |
# File 'lib/em-winrm/session.rb', line 24 def on_output(&blk); @on_output = blk; end |
#relay_command(data) ⇒ Object
run command on all servers
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/em-winrm/session.rb', line 45 def relay_command(data) EM.epoll EM.run do WinRM::Log.debug(":relay_to_servers => #{data}") servers ||= @servers.values.compact servers.each do |s| operation = proc do WinRM::Log.debug(":relayed => #{s.host}") @commands << s.run_command(data) end EM.defer(operation) end end end |
#relay_error_from_backend(host, data) ⇒ Object
relay error output from backend server to client
106 107 108 109 |
# File 'lib/em-winrm/session.rb', line 106 def relay_error_from_backend(host, data) WinRM::Log.debug(":relay_error_from_backend => #{[host, data]}") data = @on_error.call(host, data) if @on_error end |
#relay_output_from_backend(host, data) ⇒ Object
relay output from backend server to client
98 99 100 101 |
# File 'lib/em-winrm/session.rb', line 98 def relay_output_from_backend(host, data) WinRM::Log.debug(":relay_output_from_backend => #{[host, data]}") data = @on_output.call(host, data) if @on_output end |
#servers ⇒ Object
return an array of the current servers in the session
70 71 72 |
# File 'lib/em-winrm/session.rb', line 70 def servers @servers.values end |
#servers=(servers) ⇒ Object
set the current servers in the session
77 78 79 80 |
# File 'lib/em-winrm/session.rb', line 77 def servers=(servers) @servers = {} servers.each{|s| @servers[s.host] = s} end |
#unbind ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/em-winrm/session.rb', line 124 def unbind WinRM::Log.debug(":unbind => :connection") # terminate any unfinished connections @servers.values.compact.each do |s| s.unbind end end |
#unbind_backend(host) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/em-winrm/session.rb', line 132 def unbind_backend(host) WinRM::Log.debug(":unbind_backend => #{host}") @servers[host] = nil @on_finish.call(host) if @on_finish if @servers.values.compact.size.zero? @on_finish.call(:all) if @on_finish end end |