Class: Net::SSH::Shell

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

Defined Under Namespace

Classes: Process, Subshell

Constant Summary collapse

VERSION =
'0.4.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Shell.



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

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.



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

def channel
  @channel
end

#default_process_classObject

Returns the value of attribute default_process_class.



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

def default_process_class
  @default_process_class
end

#processesObject (readonly)

Returns the value of attribute processes.



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

def processes
  @processes
end

#sessionObject (readonly)

Returns the value of attribute session.



7
8
9
# File 'lib/net/ssh/shell.rb', line 7

def session
  @session
end

#shellObject (readonly)

Returns the value of attribute shell.



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

def shell
  @shell
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#busy?Boolean

Returns:



94
95
96
# File 'lib/net/ssh/shell.rb', line 94

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

#child_finished(child) ⇒ Object



106
107
108
109
110
# File 'lib/net/ssh/shell.rb', line 106

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

#close!Object



102
103
104
# File 'lib/net/ssh/shell.rb', line 102

def close!
  channel.close if channel
end

#closed?Boolean

Returns:



57
58
59
# File 'lib/net/ssh/shell.rb', line 57

def closed?
  state == :closed
end

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



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

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



88
89
90
91
92
# File 'lib/net/ssh/shell.rb', line 88

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

#on_channel_close(_channel) ⇒ Object



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

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

#on_process_run(&callback) ⇒ Object



65
66
67
# File 'lib/net/ssh/shell.rb', line 65

def on_process_run(&callback)
  @on_process_run = callback
end

#open(&callback) ⇒ Object



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

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



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

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

#open?Boolean

Returns:



53
54
55
# File 'lib/net/ssh/shell.rb', line 53

def open?
  state == :open
end

#opening?Boolean

Returns:



61
62
63
# File 'lib/net/ssh/shell.rb', line 61

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

#separatorObject



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

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



84
85
86
# File 'lib/net/ssh/shell.rb', line 84

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

#wait!Object



98
99
100
# File 'lib/net/ssh/shell.rb', line 98

def wait!
  session.loop { busy? }
end

#when_open(&callback) ⇒ Object



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

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