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_optsObject



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

def cmd_ssh_opts
  if options.key
    "ssh -i #{options.key} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p #{options.port}"
  else
    "#{SCRIPT} #{options.username} #{options.password} #{options.host} #{options.port}".strip
  end
end

.connect(ip) ⇒ Object



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

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

.execute(ip) ⇒ Object



14
15
16
17
18
# File 'lib/tenderloin/ssh.rb', line 14

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

.net_ssh_optsObject



60
61
62
63
64
65
66
67
# File 'lib/tenderloin/ssh.rb', line 60

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][:key]] if Tenderloin.config[:ssh][:key]
  opts
end

.optionsObject



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

def options
  Tenderloin.config.ssh
end

.rsync(ip, src, dst) ⇒ Object



27
28
29
30
# File 'lib/tenderloin/ssh.rb', line 27

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

.up?(ip) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tenderloin/ssh.rb', line 32

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



20
21
22
23
24
25
# File 'lib/tenderloin/ssh.rb', line 20

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