Class: Bcome::Ssh

Inherits:
Object show all
Defined in:
lib/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands, proxy, node) ⇒ Ssh

Returns a new instance of Ssh.



5
6
7
8
9
# File 'lib/ssh.rb', line 5

def initialize(commands, proxy, node)
  @commands = commands
  @proxy = proxy
  @node = node
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



3
4
5
# File 'lib/ssh.rb', line 3

def commands
  @commands
end

Instance Method Details

#execute!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ssh.rb', line 11

def execute!
  ::Net::SSH.start(@node.ip_address, @node.ssh_user, :proxy => @proxy, :keys => @node.keys, :paranoid => false) do |ssh|
    @commands.each do |command|
      puts "(#{@node.identifier}) #{@user}$ #{command.raw_command}".command
      ssh_exec!(ssh, command)

      puts command.output

      print command.pretty_result
      puts "\n"
    end
    ssh.close
  end
end

#ssh_exec!(ssh, command) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ssh.rb', line 26

def ssh_exec!(ssh, command)
  ssh.open_channel do |channel|
    channel.exec(command.raw_command) do |ch, success|
      unless success
        abort "FAILED: couldn't execute command (ssh.channel.exec)"
      end
      channel.on_data do |ch,data|
        command.stdout +=data
      end
      channel.on_extended_data do |ch,type,data|
        command.stderr +=data
      end
      channel.on_request("exit-status") do |ch,data|
        command.exit_code = data.read_long
      end
      channel.on_request("exit-signal") do |ch, data|
        command.exit_signal = data.read_long
      end
    end
  end
  ssh.loop
end