Class: Bcome::Ssh

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands) ⇒ Ssh

Returns a new instance of Ssh.



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

def initialize(commands)
  @commands = commands
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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ssh.rb', line 17

def execute!
  @commands.each do |command|
    instance = command.instance
    ssh_user = command.ssh_user
    ssh_keys = command.ssh_keys
    proxy = command.proxy

    ::Net::SSH.start(instance.ip_address, ssh_user, :proxy => proxy, :keys => ssh_keys, :paranoid => false) do |ssh|
      ssh_exec!(ssh, command)
        output_append("\n(#{instance.identifier})$".cyan + ">\s#{command.command} (#{command.pretty_result})\n")  
        output_append("#{command.output}")
      ssh.close
    end
    print_output
  end
end

#output_append(output_string) ⇒ Object



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

def output_append(output_string)
  @output_string = "#{@output_string}#{output_string}"
end


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

def print_output
  print "#{@output_string}\n"
end

#ssh_exec!(ssh, command) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ssh.rb', line 34

def ssh_exec!(ssh, command)
  ssh.open_channel do |channel|
    channel.exec(command.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