lib/webtranslateit/safe/sftp.rb in webtranslateit-safe-0.4.3 vs lib/webtranslateit/safe/sftp.rb in webtranslateit-safe-0.4.4

- old
+ new

@@ -1,46 +1,63 @@ module WebTranslateIt + module Safe + class Sftp < Sink + MAX_RETRIES = 5 + protected def active? host && user end def path @path ||= expand(config[:sftp, :path] || config[:local, :path] || ':kind/:id') end - def save - raise RuntimeError, 'pipe-streaming not supported for SFTP.' unless @backup.path + def save # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity + raise 'pipe-streaming not supported for SFTP.' unless @backup.path puts "Uploading #{host}:#{full_path} via SFTP" if verbose? || dry_run? - unless dry_run? || local_only? - opts = {} - opts[:password] = password if password - opts[:port] = port if port - Net::SFTP.start(host, user, opts) do |sftp| - puts "Sending #{@backup.path} to #{full_path}" if verbose? - begin - sftp.upload! @backup.path, full_path - rescue Net::SFTP::StatusException - puts "Ensuring remote path (#{path}) exists" if verbose? - # mkdir -p - folders = path.split('/') - folders.each_index do |i| - folder = folders[0..i].join('/') - puts "Creating #{folder} on remote" if verbose? - sftp.mkdir!(folder) rescue Net::SFTP::StatusException + return if dry_run? || local_only? + + retries = 0 + opts = {} + opts[:password] = password if password + opts[:port] = port if port + Net::SFTP.start(host, user, opts) do |sftp| + puts "Sending #{@backup.path} to #{full_path}" if verbose? + begin + sftp.upload! @backup.path, full_path + rescue IO::TimeoutError + puts 'Upload timed out, retrying' + retries += 1 + if retries >= MAX_RETRIES + puts "Tried #{retries} times. Giving up." + else + retry unless retries >= MAX_RETRIES + end + rescue Net::SFTP::StatusException + puts "Ensuring remote path (#{path}) exists" if verbose? + # mkdir -p + folders = path.split('/') + folders.each_index do |i| + folder = folders[0..i].join('/') + puts "Creating #{folder} on remote" if verbose? + begin + sftp.mkdir!(folder) + rescue StandardError + Net::SFTP::StatusException end - retry end + retry end - puts '...done' if verbose? end + puts '...done' if verbose? end def cleanup return if local_only? || dry_run? @@ -51,15 +68,13 @@ opts[:password] = password if password opts[:port] = port if port Net::SFTP.start(host, user, opts) do |sftp| files = sftp.dir.glob(path, File.basename("#{base}*")) - puts files.collect {|x| x.name } if verbose? + puts files.collect(&:name) if verbose? - files = files. - collect {|x| x.name }. - sort + files = files.collect(&:name).sort cleanup_with_limit(files, keep) do |f| file = File.join(path, f) puts "removing sftp file #{host}:#{file}" if dry_run? || verbose? sftp.remove!(file) unless dry_run? || local_only? @@ -82,7 +97,9 @@ def port config[:sftp, :port] end end + end -end \ No newline at end of file + +end