Class: Disloku::SessionManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/disloku/SessionManager.rb

Instance Method Summary collapse

Constructor Details

#initializeSessionManager

Returns a new instance of SessionManager.



10
11
12
13
14
# File 'lib/disloku/SessionManager.rb', line 10

def initialize()
	@sessions = {}

	at_exit { shutdown!() }
end

Instance Method Details

#get(connection, &block) ⇒ Object



16
17
18
19
# File 'lib/disloku/SessionManager.rb', line 16

def get(connection, &block)
	connection = getConnection(connection)
	block.call(connection)
end

#getConnection(connection) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/disloku/SessionManager.rb', line 21

def getConnection(connection)
	if (!@sessions.has_key?(connection.hash))
		Log.instance.info("creating new session #{connection.user}@#{connection.host} -> #{connection.hash}")
		session = Net::SSH.start(connection.host, connection.user, connection.options)
		sftp = Net::SFTP::Session.new(session).connect!
		@sessions[connection.hash] = { :sftp => sftp, :ssh => session }
	end

	return @sessions[connection.hash][:sftp]
end

#shutdown!Object



32
33
34
35
# File 'lib/disloku/SessionManager.rb', line 32

def shutdown!()
	Log.instance.info("closing all open sessions")
	@sessions.each {|key, value| value[:ssh].close() }
end