Class: Net::SSH::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ssh/shell.rb,
lib/net/ssh/shell/process.rb,
lib/net/ssh/shell/version.rb,
lib/net/ssh/shell/subshell.rb

Defined Under Namespace

Classes: Process, Subshell

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, shell = :default) ⇒ Shell

Returns a new instance of Shell.



16
17
18
19
20
21
22
23
24
25
# File 'lib/net/ssh/shell.rb', line 16

def initialize(session, shell=:default)
  @session = session
  @shell = shell
  @state = :closed
  @processes = []
  @when_open = []
  @on_process_run = nil
  @default_process_class = Net::SSH::Shell::Process
  open
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



10
11
12
# File 'lib/net/ssh/shell.rb', line 10

def channel
  @channel
end

#default_process_classObject

Returns the value of attribute default_process_class.



14
15
16
# File 'lib/net/ssh/shell.rb', line 14

def default_process_class
  @default_process_class
end

#processesObject (readonly)

Returns the value of attribute processes.



13
14
15
# File 'lib/net/ssh/shell.rb', line 13

def processes
  @processes
end

#sessionObject (readonly)

Returns the value of attribute session.



9
10
11
# File 'lib/net/ssh/shell.rb', line 9

def session
  @session
end

#shellObject (readonly)

Returns the value of attribute shell.



12
13
14
# File 'lib/net/ssh/shell.rb', line 12

def shell
  @shell
end

#stateObject (readonly)

Returns the value of attribute state.



11
12
13
# File 'lib/net/ssh/shell.rb', line 11

def state
  @state
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/net/ssh/shell.rb', line 96

def busy?
  opening? || processes.any?
end

#child_finished(child) ⇒ Object



108
109
110
111
112
# File 'lib/net/ssh/shell.rb', line 108

def child_finished(child)
  channel.on_close(&method(:on_channel_close)) if !channel.nil?
  processes.delete(child)
  run_next_process
end

#close!Object



104
105
106
# File 'lib/net/ssh/shell.rb', line 104

def close!
  channel.close if channel
end

#closed?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/net/ssh/shell.rb', line 59

def closed?
  state == :closed
end

#execute(command, *args, &callback) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/net/ssh/shell.rb', line 71

def execute(command, *args, &callback)
  # The class is an optional second argument.
  klass = default_process_class
  klass = args.shift if args.first.is_a?(Class)

  # The properties are expected to be the next argument.
  props = {}
  props = args.shift if args.first.is_a?(Hash)

  process = klass.new(self, command, props, callback)
  processes << process
  run_next_process if processes.length == 1
  process
end

#execute!(command, &callback) ⇒ Object



90
91
92
93
94
# File 'lib/net/ssh/shell.rb', line 90

def execute!(command, &callback)
  process = execute(command, &callback)
  wait!
  process
end

#on_channel_close(channel) ⇒ Object



121
122
123
124
# File 'lib/net/ssh/shell.rb', line 121

def on_channel_close(channel)
  @state = :closed
  @channel = nil
end

#on_process_run(&callback) ⇒ Object



67
68
69
# File 'lib/net/ssh/shell.rb', line 67

def on_process_run(&callback)
  @on_process_run = callback
end

#open(&callback) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/net/ssh/shell.rb', line 27

def open(&callback)
  if closed?
    @state = :opening
    @channel = session.open_channel(&method(:open_succeeded))
    @channel.on_open_failed(&method(:open_failed))
    @channel.on_request('exit-status', &method(:on_exit_status))
  end
  when_open(&callback) if callback
  self
end

#open!Object



38
39
40
41
42
43
44
# File 'lib/net/ssh/shell.rb', line 38

def open!
  if !open?
    open if closed?
    session.loop { opening? }
  end
  self
end

#open?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/net/ssh/shell.rb', line 55

def open?
  state == :open
end

#opening?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/net/ssh/shell.rb', line 63

def opening?
  !open? && !closed?
end

#separatorObject



114
115
116
117
118
119
# File 'lib/net/ssh/shell.rb', line 114

def separator
  @separator ||= begin
    s = Digest::SHA1.hexdigest([session.object_id, object_id, Time.now.to_i, Time.now.usec, rand(0xFFFFFFFF)].join(":"))
    s << Digest::SHA1.hexdigest(s)
  end
end

#subshell(command, &callback) ⇒ Object



86
87
88
# File 'lib/net/ssh/shell.rb', line 86

def subshell(command, &callback)
  execute(command, Net::SSH::Shell::Subshell, &callback)
end

#wait!Object



100
101
102
# File 'lib/net/ssh/shell.rb', line 100

def wait!
  session.loop { busy? }
end

#when_open(&callback) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/net/ssh/shell.rb', line 46

def when_open(&callback)
  if open?
    yield self
  else
    @when_open << callback
  end
  self
end