Class: Tenderloin::SSH

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

Constant Summary collapse

SCRIPT =
File.join(File.dirname(__FILE__), '..', '..', 'script', 'tenderloin-ssh-expect.sh')

Class Method Summary collapse

Class Method Details

.cmd_ssh_opts(ip = nil) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/tenderloin/ssh.rb', line 78

def cmd_ssh_opts(ip=nil)
  if options.keys
    keyopts = options.keys.map {|k| "-i #{File.expand_path(k)}"}.join(' ')
    "ssh #{keyopts} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p #{options.port}"
  else
    "#{SCRIPT} #{options.username} #{options.password} #{options.host || ip} #{options.port}".strip
  end
end

.connect(ip, command = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/tenderloin/ssh.rb', line 6

def connect(ip, command = nil)
  if command
    remote_exec(ip, command)
  else
    ssh_connect(ip)
  end
end

.execute(ip) ⇒ Object



40
41
42
43
44
# File 'lib/tenderloin/ssh.rb', line 40

def execute(ip)
  Net::SSH.start(ip, Tenderloin.config[:ssh][:username], net_ssh_opts) do |ssh|
    yield ssh
  end
end

.net_ssh_optsObject



87
88
89
90
91
92
93
94
# File 'lib/tenderloin/ssh.rb', line 87

def net_ssh_opts
  opts = {}
  opts[:port] = Tenderloin.config.ssh.port
  opts[:password] = Tenderloin.config.ssh.password
  opts[:timeout] = Tenderloin.config.ssh.timeout
  opts[:keys] = Tenderloin.config[:ssh][:keys] if Tenderloin.config[:ssh][:keys]
  opts
end

.optionsObject



74
75
76
# File 'lib/tenderloin/ssh.rb', line 74

def options
  Tenderloin.config.ssh
end

.remote_exec(ip, command) ⇒ Object



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

def remote_exec(ip, command)
  execute(ip) do |ssh|
    ssh.open_channel do |channel|
      channel.exec command do |ch, success|
        raise "could not execute remote command: #{command}" unless success

        ch.on_data do |c, data|
          STDOUT.print data
        end

        ch.on_extended_data do |c, type, data|
          STDERR.print data
        end
      end
    end
  end
end

.rsync(ip, src, dst) ⇒ Object



53
54
55
56
# File 'lib/tenderloin/ssh.rb', line 53

def rsync(ip,src,dst)
  cmd = "rsync -avz --delete -e \"#{cmd_ssh_opts}\" #{src} #{options.username}@#{ip}:#{dst}"
  `#{cmd}`
end

.ssh_connect(ip) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/tenderloin/ssh.rb', line 32

def ssh_connect(ip)
  if options.keys
    Kernel.exec "#{cmd_ssh_opts} #{options.username}@#{ip}"
  else
    Kernel.exec cmd_ssh_opts(ip).strip
  end
end

.up?(ip) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tenderloin/ssh.rb', line 58

def up?(ip)
  check_thread = Thread.new do
    begin
      Thread.current[:result] = false
      Net::SSH.start(ip, Tenderloin.config.ssh.username, net_ssh_opts) do |ssh|
        Thread.current[:result] = true
      end
    rescue Errno::ECONNREFUSED, Net::SSH::Disconnect
      # False, its defaulted above
    end
  end

  check_thread.join(Tenderloin.config.ssh.timeout)
  return check_thread[:result]
end

.upload!(ip, from, to) ⇒ Object



46
47
48
49
50
51
# File 'lib/tenderloin/ssh.rb', line 46

def upload!(ip, from, to)
  execute(ip) do |ssh|
    scp = Net::SCP.new(ssh)
    scp.upload!(from, to)
  end
end