Class: EventMachine::WinRM::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/em-winrm/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Returns a new instance of Session.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/em-winrm/session.rb', line 31

def initialize(options={})
  if options[:logger]
    WinRM::Log.logger = options[:logger]
  else
    log_level = options[:log_level] || :info
    WinRM::Log.level(log_level)
  end
  @servers = {}
  @commands = []
  @exit_codes = {}
  WinRM::Log.debug(":session => :init")
end

Instance Attribute Details

#exit_codesObject (readonly)

Returns the value of attribute exit_codes.



23
24
25
# File 'lib/em-winrm/session.rb', line 23

def exit_codes
  @exit_codes
end

Instance Method Details

#closeObject

clean up servers and stop the EventMachine event loop



148
149
150
151
152
153
154
# File 'lib/em-winrm/session.rb', line 148

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, exit_code) ⇒ Object

called by backend server when it completes a command



116
117
118
119
120
121
122
123
124
125
# File 'lib/em-winrm/session.rb', line 116

def command_complete(host, cid,exit_code)
  WinRM::Log.debug(":command_complete => #{host} with return code #{exit_code}")
  @commands.delete(cid)
  @on_command_complete.call(host) if @on_command_complete
  @exit_codes[host] = exit_code
  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

Yields:

  • (subsession)


90
91
92
93
94
95
# File 'lib/em-winrm/session.rb', line 90

def on(*new_servers)
  subsession = self.clone
  subsession.servers = new_servers & servers
  yield subsession if block_given?
  subsession
end

#on_close(&blk) ⇒ Object



29
# File 'lib/em-winrm/session.rb', line 29

def on_close(&blk); @on_close = blk; end

#on_command_complete(&blk) ⇒ Object



27
# File 'lib/em-winrm/session.rb', line 27

def on_command_complete(&blk); @on_command_complete = blk; end

#on_error(&blk) ⇒ Object



26
# File 'lib/em-winrm/session.rb', line 26

def on_error(&blk); @on_error = blk; end

#on_finish(&blk) ⇒ Object



28
# File 'lib/em-winrm/session.rb', line 28

def on_finish(&blk); @on_finish = blk; end

#on_output(&blk) ⇒ Object

Proxy Methods



25
# File 'lib/em-winrm/session.rb', line 25

def on_output(&blk); @on_output = blk; end

#relay_command(data) ⇒ Object

run command on all servers



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/em-winrm/session.rb', line 47

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



108
109
110
111
# File 'lib/em-winrm/session.rb', line 108

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



100
101
102
103
# File 'lib/em-winrm/session.rb', line 100

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

#serversObject

return an array of the current servers in the session



72
73
74
# File 'lib/em-winrm/session.rb', line 72

def servers
  @servers.values
end

#servers=(servers) ⇒ Object

set the current servers in the session



79
80
81
82
# File 'lib/em-winrm/session.rb', line 79

def servers=(servers)
  @servers = {}
  servers.each{|s| @servers[s.host] = s}
end

#unbindObject



127
128
129
130
131
132
133
# File 'lib/em-winrm/session.rb', line 127

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



135
136
137
138
139
140
141
142
# File 'lib/em-winrm/session.rb', line 135

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

#use(host, options) ⇒ Object

initialize connections to a server



65
66
67
# File 'lib/em-winrm/session.rb', line 65

def use(host, options)
  @servers[host] = Server.new(self, host, options)
end