Class: Tenderloin::SSH
- Inherits:
-
Object
- Object
- Tenderloin::SSH
- Defined in:
- lib/tenderloin/ssh.rb
Constant Summary collapse
- SCRIPT =
File.join(File.dirname(__FILE__), '..', '..', 'script', 'tenderloin-ssh-expect.sh')
Class Method Summary collapse
- .cmd_ssh_opts ⇒ Object
- .connect(ip) ⇒ Object
- .execute(ip) ⇒ Object
- .net_ssh_opts ⇒ Object
- .options ⇒ Object
- .rsync(ip, src, dst) ⇒ Object
- .up?(ip) ⇒ Boolean
- .upload!(ip, from, to) ⇒ Object
Class Method Details
.cmd_ssh_opts ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/tenderloin/ssh.rb', line 52 def cmd_ssh_opts if .key "ssh -i #{.key} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p #{.port}" else "#{SCRIPT} #{.username} #{.password} #{.host} #{.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 .key Kernel.exec "#{cmd_ssh_opts} #{.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_opts ⇒ Object
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 |
.options ⇒ Object
48 49 50 |
# File 'lib/tenderloin/ssh.rb', line 48 def 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} #{.username}@#{ip}:#{dst}" `#{cmd}` end |
.up?(ip) ⇒ 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 |