lib/wordmove/hosts/remote_host.rb in wordmove-0.0.7 vs lib/wordmove/hosts/remote_host.rb in wordmove-0.0.8
- old
+ new
@@ -1,7 +1,8 @@
require 'net/ssh'
require 'net/scp'
+require 'net/ssh/gateway'
module Wordmove
class RemoteHost < LocalHost
alias :locally_run :run
@@ -11,41 +12,71 @@
def initialize(options = {})
super
end
def session
- logger.verbose "Connecting to #{options.ssh.host}..." unless @session.present?
- @session ||= Net::SSH.start(options.ssh.host, options.ssh.username, @ssh_extras )
+ if options.ssh.nil?
+ raise Thor::Error, "No SSH credentials provided on Movefile!"
+ end
+
+ ssh_extras = {}
+ [ :port, :password ].each do |p|
+ ssh_extras.merge!( { p => options.ssh[p] } ) if options.ssh[p]
+ end
+
+ if options.ssh.gateway.nil?
+ logger.verbose "Connecting to #{options.ssh.host}..." unless @session.present?
+ @session ||= Net::SSH.start(options.ssh.host, options.ssh.username, ssh_extras)
+ else
+ logger.verbose "Connecting to #{options.ssh.host} through the gateway..." unless @session.present?
+ @session ||= gateway.ssh(options.ssh.host, options.ssh.username, ssh_extras)
+ end
+
+ @session
end
+ def gateway
+ if options.ssh.gateway.nil?
+ raise Thor::Error, "No SSH credentials provided on Movefile!"
+ end
+
+ ssh_extras = {}
+ [ :port, :password ].each do |p|
+ ssh_extras.merge!( { p => options.ssh.gateway[p] } ) if options.ssh.gateway[p]
+ end
+
+ logger.verbose "Connecting to #{options.ssh.gateway.host}..." unless @gateway.present?
+ @gateway ||= Net::SSH::Gateway.new(options.ssh.gateway.host, options.ssh.gateway.username, ssh_extras )
+
+ @gateway
+ end
+
def close
session.close
+ if options.ssh.gateway.present?
+ gateway.close(session.transport.port)
+ end
end
def upload_file(source_file, destination_file)
logger.verbose "Copying remote #{source_file} to #{destination_file}..."
- Net::SSH.start options.ssh.host, options.ssh.username, @ssh_extras do |ssh|
- ssh.scp.download! source_file, destination_file
- end
+ session.scp.download! source_file, destination_file
end
def download_file(source_file, destination_file)
logger.verbose "Copying local #{source_file} to #{destination_file}..."
- Net::SSH.start options.ssh.host, options.ssh.username, @ssh_extras do |ssh|
- ssh.scp.upload! source_file, destination_file
- end
+ session.scp.upload! source_file, destination_file
end
def download_dir(source_dir, destination_dir)
- destination_dir = "#{options.ssh.host}:#{destination_dir}"
+ destination_dir = ":#{destination_dir}"
destination_dir = "#{options.ssh.username}@#{destination_dir}" if options.ssh.username
rsync "#{source_dir}/", destination_dir
end
def upload_dir(source_dir, destination_dir)
- source_dir = "#{options.ssh.host}:#{source_dir}/"
- source_dir = "#{options.ssh.username}@#{source_dir}" if options.ssh.username
+ source_dir = ":#{source_dir}/"
rsync source_dir, destination_dir
end
def run(*args)
command = shell_command(*args)
@@ -59,22 +90,37 @@
exclude_file = Tempfile.new('exclude')
exclude_file.write(options.exclude.join("\n"))
exclude_file.close
- arguments = [ "-azLK" ]
+ arguments = [ "-azLKO" ]
- if options.ssh && (options.ssh.port || options.ssh.password)
+ if options.ssh && (options.ssh.port || options.ssh.password || options.ssh.gateway)
- remote_shell_arguments = [ "ssh" ]
+ remote_shell_arguments = []
+ if options.ssh.gateway
+ host = options.ssh.gateway.host
+ host = "#{options.ssh.gateway.username}@#{host}" if options.ssh.gateway.username
+ remote_shell_arguments << [ "ssh", host ]
+ if options.ssh.gateway.port
+ remote_shell_arguments << [ "-p", options.ssh.gateway.port ]
+ end
+ end
+
+ remote_shell_arguments << [ "ssh" ]
+
if options.ssh.port
remote_shell_arguments << [ "-p", options.ssh.port ]
end
if options.ssh.password
remote_shell_arguments = [ "sshpass", "-p", options.ssh.password ] + remote_shell_arguments
end
+
+ host = options.ssh.host
+ host = "#{options.ssh.username}@#{host}" if options.ssh.username
+ remote_shell_arguments << host
arguments << [ "-e", remote_shell_arguments.join(" ") ]
end
arguments << [ "--exclude-from=#{exclude_file.path}", "--delete", source_dir, destination_dir ]