Class: Ciborg::Sobo

Inherits:
Object
  • Object
show all
Defined in:
lib/ciborg/sobo.rb

Defined Under Namespace

Classes: CommandFailed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, key, user = 'ubuntu') ⇒ Sobo

Returns a new instance of Sobo.



10
11
12
13
14
# File 'lib/ciborg/sobo.rb', line 10

def initialize(ip, key, user='ubuntu')
  @ip = ip
  @key = key
  @user = user
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



7
8
9
# File 'lib/ciborg/sobo.rb', line 7

def ip
  @ip
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/ciborg/sobo.rb', line 7

def key
  @key
end

#timeoutObject



16
17
18
# File 'lib/ciborg/sobo.rb', line 16

def timeout
  @timeout || 10000
end

#userObject (readonly)

Returns the value of attribute user.



7
8
9
# File 'lib/ciborg/sobo.rb', line 7

def user
  @user
end

Instance Method Details

#backtick(command) ⇒ Object



29
30
31
# File 'lib/ciborg/sobo.rb', line 29

def backtick(command)
  ssh_popen4!(command)[0]
end

#ssh_popen4!(command, streaming_output = false) ⇒ Object



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
67
68
69
70
71
72
73
# File 'lib/ciborg/sobo.rb', line 33

def ssh_popen4!(command, streaming_output = false)
  ssh = Net::SSH.start(ip, user, :keys => [key], :timeout => timeout, paranoid: false)
  stdout_data = ""
  stderr_data = ""
  exit_code = nil
  exit_signal = nil

  ssh.open_channel do |channel|
    channel.exec("/bin/bash -lc #{Shellwords.escape(command)}") do |ch, success|
      unless success
        raise "FAILED: couldn't execute command (ssh.channel.exec)"
      end

      channel.on_data do |ch,data|
        if streaming_output
          STDOUT << data
          STDOUT.flush
        end
        stdout_data += data
      end

      channel.on_extended_data do |ch,type,data|
        if streaming_output
          STDERR << data
          STDERR.flush
        end
        stderr_data += data
      end

      channel.on_request("exit-status") do |ch,data|
        exit_code = data.read_long
      end

      channel.on_request("exit-signal") do |ch, data|
        exit_signal = data.read_long
      end
    end
  end
  ssh.loop
  [stdout_data, stderr_data, exit_code, exit_signal]
end

#system(command) ⇒ Object



20
21
22
# File 'lib/ciborg/sobo.rb', line 20

def system(command)
  ssh_popen4!(command, true)[2]
end

#system!(command) ⇒ Object

Raises:



24
25
26
27
# File 'lib/ciborg/sobo.rb', line 24

def system!(command)
  result = ssh_popen4!(command, true)
  raise(CommandFailed, "Failed: #{command}\n#{result[0]}\n\n#{result[1]}") unless result[2] == 0
end

#upload(from, to, opts = "--exclude .git") ⇒ Object



75
76
77
# File 'lib/ciborg/sobo.rb', line 75

def upload(from, to, opts = "--exclude .git")
  Kernel.system(%Q{rsync --rsh="ssh -o 'StrictHostKeyChecking no' -i #{key}" --archive --compress --delete #{from} #{user}@#{ip}:#{to} #{opts}})
end