Class: SmartMachine::SSH

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

Instance Method Summary collapse

Methods inherited from Base

#machine_has_engine_installed?, #platform_on_machine?, #user_bash

Methods included from Logger

configure_logger_for, included, #logger, logger_for

Constructor Details

#initializeSSH

Returns a new instance of SSH.



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

def initialize
end

Instance Method Details

#loginObject



68
69
70
# File 'lib/smart_machine/ssh.rb', line 68

def 
  exec "ssh -p #{SmartMachine.credentials.machine[:port]} #{SmartMachine.credentials.machine[:username]}@#{SmartMachine.credentials.machine[:address]}"
end

#run(*commands) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/smart_machine/ssh.rb', line 8

def run(*commands)
  commands.flatten!
  status = {}

  Net::SSH.start(SmartMachine.credentials.machine[:address], SmartMachine.credentials.machine[:username], { port: SmartMachine.credentials.machine[:port], password: SmartMachine.credentials.machine[:password] }) do |ssh|
    commands.each do |command|

      puts "\e[1m" + "$ " + command + "\e[0m"

      channel = ssh.open_channel do |channel|

        channel.on_open_failed do |channel, code, description|
          raise "could not open channel (#{description}, ##{code})"
        end

        ssh.listen_to(STDIN) do |stdin|
          input = stdin.readpartial(1024)
          channel.send_data(input) unless input.empty?
        end

        channel.request_pty :modes => { Net::SSH::Connection::Term::ECHO => 0 } do |channel, success|
          raise "could not obtain pty" unless success

          channel.exec command do |channel, success|
            raise "could not execute command: #{command.inspect}" unless success

            if status
              channel.on_request("exit-status") do |ch2, data|
                status[:exit_code] = data.read_long
              end

              channel.on_request("exit-signal") do |ch2, data|
                status[:exit_signal] = data.read_long
              end
            end

            channel.on_data do |ch2, data|
              $stdout.print data

              if (data[/\[sudo\]|Password/i])
                channel.send_data "#{SmartMachine.credentials.machine[:password]}\n"
              end
            end

            channel.on_extended_data do |ch2, type, data|
              $stderr.print data
            end
          end
        end
      end

      channel.wait

      break if status[:exit_code] != 0
    end
  end

  status
end