net-ssh/net-scp

File remains locked if scp returns non-zero

burnzoire opened this issue · 0 comments

I'm using the upload! method to push zip archives to a remote location. After getting a SCP did not finish successfully (1): (Net::SCP::Error) I am finding that the file I tried to upload is locked.

Here is the method I am using for my Rails API application:

def copy_to_remote (local_path, remote_path, ssh)  
    session_options = {
      :password => ssh[:password],
      #:verbose => Logger::DEBUG,
      :timeout => 10
    }

    begin  
      # copy to remote location
      Net::SCP.start(ssh[:host], ssh[:user], session_options) do |scp|
        scp.upload!(local_path, remote_path)
      end

    rescue Net::SSH::AuthenticationFailed
      raise Exceptions::ArchiveAuthenticationFailed, "Could not authenticate with remote location"

    rescue Net::SSH::ConnectionTimeout
      raise Exceptions::ArchiveConnectionTimeout, "Connection timed out with remote location"

    rescue Net::SSH::Disconnect
      raise Exceptions::ArchiveDisconnect, "Lost connection with remote location"

    rescue Net::SCP::Error => e
      raise Exceptions::ArchiveCopyError, "Could not copy archive to remote location - #{e.message}"

    ensure
      # can't remove as file is locked. 
      FileUtils.rm_rf(local_path) if File::exists?(local_path)        
    end

  end

I have tried all kinds of permutations of calling upload, I have also tried manually closing the channel but I can't seem to get it to lose its grip on my file.