Module: FileCopy
- Defined in:
- lib/file_copy.rb,
lib/file_copy/copy.rb,
lib/file_copy/version.rb
Constant Summary collapse
- VERSION =
"1.1.0"
Class Method Summary collapse
-
.sftp_copy(username, password, server, files_map) ⇒ Object
Simply copy map files using sftp server on dest_server.
- .sftp_mkdir_recursive(sftp, path) ⇒ Object
-
.ssh_connect(username, password, server) ⇒ Object
Creates ssh connection, assumes username and password or without password but with ssh keys.
Class Method Details
.sftp_copy(username, password, server, files_map) ⇒ Object
Simply copy map files using sftp server on dest_server. Note: stfp.upload support parallel uploads - default is 2. TODO(kolman): packing files, all, not all, determine by part of file.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/file_copy/copy.rb', line 33 def sftp_copy(username, password, server, files_map) ssh = FileCopy.ssh_connect(username, password, server) ssh.sftp.connect do |sftp| uploads = files_map.map { |from,to| remote_dir = File.dirname(to) sftp_mkdir_recursive(sftp, File.dirname(to)) Log.debug1 "Copying #{from} to #{to}" sftp.upload(from, to) } uploads.each { |u| u.wait } Log.debug1 "Done." end # ssh.sftp.connect end |
.sftp_mkdir_recursive(sftp, path) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/file_copy/copy.rb', line 48 def sftp_mkdir_recursive(sftp, path) dir_stat = nil begin Log.debug1 "Stat remote dir: #{path}." dir_stat = sftp.stat!(path).directory? Log.debug1 "Stat result #{dir_stat}." rescue Net::SFTP::StatusException end if !dir_stat Log.debug1 "Directory does not exists: #{path}." sftp_mkdir_recursive sftp, File.dirname(path) Log.debug1 "Making dir #{path}." response = sftp.mkdir!(path) Log.debug1 "Making dir ok:#{response.ok?}." end end |
.ssh_connect(username, password, server) ⇒ Object
Creates ssh connection, assumes username and password or without password but with ssh keys.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/file_copy/copy.rb', line 11 def ssh_connect(username, password, server) username = (username and username.length > 0) ? username : ENV['USER'] password = (password and password.length > 0) ? password : nil port = 22 # 22 is a standart SSH port raise "Undefined server" unless server Log.debug1 "Trying to connect(ssh): #{username}, #{password}, #{server}, #{port}." if (username and password) Net::SSH.start(server, username, :password => password, :port => port) elsif (username) Net::SSH.start(server, username, :port => port) else raise "Undefined username" end end |