Class | BoxGrinder::SFTPHelper |
In: |
lib/boxgrinder-build/helpers/sftp-helper.rb
lib/boxgrinder-build/helpers/sftp-helper.rb |
Parent: | Object |
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 7 7: def initialize(options={}) 8: @log = options[:log] || LogHelper.new 9: end
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 7 7: def initialize(options={}) 8: @log = options[:log] || LogHelper.new 9: end
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 11 11: def connect(host, username, options={}) 12: @log.info "Connecting to #{host}..." 13: @ssh = Net::SSH.start(host, username, options) 14: end
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 11 11: def connect(host, username, options={}) 12: @log.info "Connecting to #{host}..." 13: @ssh = Net::SSH.start(host, username, options) 14: end
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 16 16: def connected? 17: return true if !@ssh.nil? and !@ssh.closed? 18: false 19: end
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 16 16: def connected? 17: return true if !@ssh.nil? and !@ssh.closed? 18: false 19: end
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 21 21: def disconnect 22: @log.info "Disconnecting from host..." 23: @ssh.close if connected? 24: @ssh = nil 25: end
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 21 21: def disconnect 22: @log.info "Disconnecting from host..." 23: @ssh.close if connected? 24: @ssh = nil 25: end
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 27 27: def upload_files(path, default_permissions, overwrite, files = {}) 28: return if files.size == 0 29: 30: raise "You're not connected to server" unless connected? 31: 32: @log.debug "Files to upload:" 33: 34: files.each do |remote, local| 35: @log.debug "#{File.basename(local)} => #{path}/#{remote}" 36: end 37: 38: global_size = 0 39: 40: files.each_value do |file| 41: global_size += File.size(file) 42: end 43: 44: global_size_kb = global_size / 1024 45: global_size_mb = global_size_kb / 1024 46: 47: @log.info "#{files.size} files to upload (#{global_size_mb > 0 ? global_size_mb.to_s + "MB" : global_size_kb > 0 ? global_size_kb.to_s + "kB" : global_size.to_s})" 48: 49: @ssh.sftp.connect do |sftp| 50: begin 51: sftp.stat!(path) 52: rescue Net::SFTP::StatusException => e 53: raise unless e.code == 2 54: @ssh.exec!("mkdir -p #{path}") 55: end 56: 57: nb = 0 58: 59: files.each do |key, local| 60: name = File.basename(local) 61: remote = "#{path}/#{key}" 62: size_b = File.size(local) 63: size_kb = size_b / 1024 64: nb_of = "#{nb += 1}/#{files.size}" 65: 66: begin 67: sftp.stat!(remote) 68: 69: unless overwrite 70: 71: local_md5_sum = `md5sum #{local} | awk '{ print $1 }'`.strip 72: remote_md5_sum = @ssh.exec!("md5sum #{remote} | awk '{ print $1 }'").strip 73: 74: if (local_md5_sum.eql?(remote_md5_sum)) 75: @log.info "#{nb_of} #{name}: files are identical (md5sum: #{local_md5_sum}), skipping..." 76: next 77: end 78: end 79: 80: rescue Net::SFTP::StatusException => e 81: raise unless e.code == 2 82: end 83: 84: @ssh.exec!("mkdir -p #{File.dirname(remote) }") 85: 86: pbar = ProgressBar.new("#{nb_of} #{name}", size_b) 87: pbar.file_transfer_mode 88: 89: sftp.upload!(local, remote) do |event, uploader, * args| 90: case event 91: when :open then 92: when :put then 93: pbar.set(args[1]) 94: when :close then 95: when :mkdir then 96: when :finish then 97: pbar.finish 98: end 99: end 100: 101: sftp.setstat(remote, :permissions => default_permissions) 102: end 103: end 104: end
# File lib/boxgrinder-build/helpers/sftp-helper.rb, line 27 27: def upload_files(path, default_permissions, overwrite, files = {}) 28: return if files.size == 0 29: 30: raise "You're not connected to server" unless connected? 31: 32: @log.debug "Files to upload:" 33: 34: files.each do |remote, local| 35: @log.debug "#{File.basename(local)} => #{path}/#{remote}" 36: end 37: 38: global_size = 0 39: 40: files.each_value do |file| 41: global_size += File.size(file) 42: end 43: 44: global_size_kb = global_size / 1024 45: global_size_mb = global_size_kb / 1024 46: 47: @log.info "#{files.size} files to upload (#{global_size_mb > 0 ? global_size_mb.to_s + "MB" : global_size_kb > 0 ? global_size_kb.to_s + "kB" : global_size.to_s})" 48: 49: @ssh.sftp.connect do |sftp| 50: begin 51: sftp.stat!(path) 52: rescue Net::SFTP::StatusException => e 53: raise unless e.code == 2 54: @ssh.exec!("mkdir -p #{path}") 55: end 56: 57: nb = 0 58: 59: files.each do |key, local| 60: name = File.basename(local) 61: remote = "#{path}/#{key}" 62: size_b = File.size(local) 63: size_kb = size_b / 1024 64: nb_of = "#{nb += 1}/#{files.size}" 65: 66: begin 67: sftp.stat!(remote) 68: 69: unless overwrite 70: 71: local_md5_sum = `md5sum #{local} | awk '{ print $1 }'`.strip 72: remote_md5_sum = @ssh.exec!("md5sum #{remote} | awk '{ print $1 }'").strip 73: 74: if (local_md5_sum.eql?(remote_md5_sum)) 75: @log.info "#{nb_of} #{name}: files are identical (md5sum: #{local_md5_sum}), skipping..." 76: next 77: end 78: end 79: 80: rescue Net::SFTP::StatusException => e 81: raise unless e.code == 2 82: end 83: 84: @ssh.exec!("mkdir -p #{File.dirname(remote) }") 85: 86: pbar = ProgressBar.new("#{nb_of} #{name}", size_b) 87: pbar.file_transfer_mode 88: 89: sftp.upload!(local, remote) do |event, uploader, * args| 90: case event 91: when :open then 92: when :put then 93: pbar.set(args[1]) 94: when :close then 95: when :mkdir then 96: when :finish then 97: pbar.finish 98: end 99: end 100: 101: sftp.setstat(remote, :permissions => default_permissions) 102: end 103: end 104: end