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
9
# File 'lib/hussh/session.rb', line 5

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

Instance Method Details

#channelsObject



51
52
53
# File 'lib/hussh/session.rb', line 51

def channels
  @channels ||= {}
end

#closeObject



65
66
67
68
69
70
71
72
# File 'lib/hussh/session.rb', line 65

def close
  channels.each do |id, channel|
    channel.close
  end
  if have_real_session?
    real_session.close
  end
end

#exec!(command) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/hussh/session.rb', line 40

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

#get_next_channel_idObject



55
56
57
# File 'lib/hussh/session.rb', line 55

def get_next_channel_id
  @channel_id_counter += 1
end

#has_response?(command) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/hussh/session.rb', line 19

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

#have_real_session?Boolean

Returns:

  • (Boolean)


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

def have_real_session?
  !!@real_session
end

#open_channel {|channel| ... } ⇒ Object

Yields:

  • (channel)


59
60
61
62
63
# File 'lib/hussh/session.rb', line 59

def open_channel(&block)
  channel = Channel.new(self)
  yield(channel) if block_given?
  channels[get_next_channel_id] = channel
end

#real_sessionObject



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

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

#response_for(command) ⇒ Object



26
27
28
29
30
31
# File 'lib/hussh/session.rb', line 26

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



33
34
35
36
37
38
# File 'lib/hussh/session.rb', line 33

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