Class: Hussh::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/hussh/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, user) ⇒ Session

Returns a new instance of Session.



5
6
7
8
# File 'lib/hussh/session.rb', line 5

def initialize(host, user)
  @host = host
  @user = user
end

Instance Method Details

#closeObject



91
92
93
94
# File 'lib/hussh/session.rb', line 91

def close
  @real_session.close if @real_session
  @real_session = nil
end

#exec!(command) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/hussh/session.rb', line 35

def exec!(command)
  Hussh.commands_run << command
  if self.has_response?(command)
    self.response_for(command)
  else
    response = real_session.exec!(command)
    self.update_recording(command, response)
    response
  end
end

#has_response?(command) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
# File 'lib/hussh/session.rb', line 14

def has_response?(command)
  Hussh.stubbed_responses.fetch(@host, {}).fetch(@user, {})
    .has_key?(command) ||
    Hussh.recorded_responses.fetch(@host, {}).fetch(@user, {})
    .has_key?(command)
end

#open_channel(&block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hussh/session.rb', line 46

def open_channel(&block)
  @channel = Channel.new(self)
  block.call(@channel)
  Hussh.commands_run << @channel.command
  if self.has_response?(@channel.command)
    if @channel.exec_block.respond_to?(:call)
      @channel.exec_block.call(@channel, true)
    end

    if @channel.on_data_block.respond_to?(:call)
      @channel.on_data_block.call(@channel, self.response_for(@channel.command))
    end
  else
    self.real_session.open_channel do |ch|

      if @channel.requested_pty?
        ch.request_pty do |ch, success|
          if @channel.request_pty_block.respond_to?(:call)
            @channel.request_pty_block.call(ch, success)
          end
        end
      end

      ch.exec(@channel.command) do |ch, success|
        @channel.exec_block.call(@channel, success) if @channel.exec_block

        ch.on_data do |ch, output|
          if @channel.on_data_block.respond_to?(:call)
            @channel.on_data_block.call(@channel, output)
            @on_data = output
            self.update_recording(@channel.command, @on_data)
          end
        end

        ch.on_extended_data do |ch, output|
          if @channel.on_extended_data_block.respond_to?(:call)
            @channel.on_extended_data_block.call(@channel, output)
          end
        end

      end
    end
  end
end

#real_sessionObject



10
11
12
# File 'lib/hussh/session.rb', line 10

def real_session
  @real_session ||= Net::SSH.start_without_hussh(@host, @user)
end

#response_for(command) ⇒ Object



21
22
23
24
25
26
# File 'lib/hussh/session.rb', line 21

def response_for(command)
  Hussh.stubbed_responses.fetch(@host, {}).fetch(@user, {}).fetch(
    command,
    Hussh.recorded_responses.fetch(@host, {}).fetch(@user, {})[command]
  )
end

#update_recording(command, response) ⇒ Object



28
29
30
31
32
33
# File 'lib/hussh/session.rb', line 28

def update_recording(command, response)
  Hussh.recorded_responses[@host] ||= {}
  Hussh.recorded_responses[@host][@user] ||= {}
  Hussh.recorded_responses[@host][@user][command] = response
  Hussh.recording_changed!
end