lib/backup/remote/command.rb in backup-remote-0.0.16 vs lib/backup/remote/command.rb in backup-remote-0.0.17

- old
+ new

@@ -9,17 +9,28 @@ class Command include SSHKit::DSL - def run_ssh_cmd(hostname, ssh_user, ssh_pass, cmd) - #cmd = "bash -c 'whoami'" + def self.build_sshkit_host(hostname, ssh_options) + puts "DEBUG: #{ssh_options}" + ssh_user = ssh_options[:user] + ssh_pass = ssh_options[:password] + ssh_key = ssh_options[:key] - #puts "run ssh cmd: #{hostname}, #{ssh_user}, #{ssh_pass}, #{cmd}" host = SSHKit::Host.new({hostname: hostname, user: ssh_user}) - host.password = ssh_pass + host.port = ssh_options[:port] || 22 + host.key = ssh_key if ssh_key + host.password = ssh_pass if ssh_pass + host + end + + def run_ssh_cmd(hostname, ssh_options, cmd) + #puts "run ssh cmd: #{hostname}, #{ssh_user}, #{ssh_pass}, #{cmd}" + host = Command.build_sshkit_host(hostname, ssh_options) + #srv = ssh_user+'@'+hostname all_servers = [host] output = '' @@ -41,11 +52,11 @@ # return { res: 1, output: output } rescue => e - #puts "ssh error: #{e.message}, #{e.backtrace}" + puts "ssh error: #{e.message}, #{e.backtrace}" { res: 0, output: output, error: e.message @@ -80,16 +91,21 @@ } end - def ssh_download_file(hostname, ssh_user, ssh_pass, remote_filename, dest_filename) - return ssh_download_file_sshkit(hostname, ssh_user, ssh_pass, remote_filename, dest_filename) + def ssh_download_file(hostname, ssh_options, remote_filename, dest_filename) + return ssh_download_file_sshkit(hostname, ssh_options, remote_filename, dest_filename) #return ssh_download_file_scp(hostname, ssh_user, ssh_pass, remote_filename, dest_filename) end - def ssh_download_file_scp(hostname, ssh_user, ssh_pass, remote_filename, dest_filename) + def ssh_download_file_scp(hostname, ssh_options, remote_filename, dest_filename) + ssh_user = ssh_options[:ssh_user] + ssh_pass = ssh_options[:ssh_password] + ssh_key = ssh_options[:ssh_key] + + # work Net::SCP.download!(hostname, ssh_user, remote_filename, dest_filename, :ssh => { :password => ssh_pass }) # return {res: 1, output: ""} @@ -99,13 +115,12 @@ error: e.message } end # !! NOT work on big files > 4Gb - def ssh_download_file_sshkit(hostname, ssh_user, ssh_pass, remote_filename, dest_filename) - host = SSHKit::Host.new("#{ssh_user}@#{hostname}") - host.password = ssh_pass + def ssh_download_file_sshkit(hostname, ssh_options, remote_filename, dest_filename) + host = Command.build_sshkit_host(hostname, ssh_options) on host do |host| download! remote_filename, dest_filename end @@ -117,12 +132,11 @@ res: 0, error: e.message } end - def ssh_upload_file(hostname, ssh_user, ssh_pass, source_file, dest_file, handler=nil) - host = SSHKit::Host.new("#{ssh_user}@#{hostname}") - host.password = ssh_pass + def ssh_upload_file(hostname, ssh_options, source_file, dest_file, handler=nil) + host = Command.build_sshkit_host(hostname, ssh_options) # scp f_temp = "/tmp/#{SecureRandom.uuid}" # sshkit