def pull_file(self, remote_source, local_dir): ''' Transport file on the remote side to a local directory Args: - remote_source (string): remote_source - local_dir (string): Local directory to copy to Returns: - str: Local path to file Raises: - FileExists : Name collision at local directory. - FileCopyException : FileCopy failed. ''' local_dest = local_dir + '/' + os.path.basename(remote_source) try: os.makedirs(local_dir) except OSError as e: if e.errno != errno.EEXIST: logger.exception( "Failed to create local_dir: {0}".format(local_dir)) raise BadScriptPath(e, self.hostname) try: self._valid_sftp_client().get(remote_source, local_dest) except Exception as e: logger.exception("File pull failed") raise FileCopyException(e, self.hostname) return local_dest
def push_file(self, source, dest_dir): ''' If the source files dirpath is the same as dest_dir, a copy is not necessary, and nothing is done. Else a copy is made. Args: - source (string) : Path to the source file - dest_dir (string) : Path to the directory to which the files is to be copied Returns: - destination_path (String) : Absolute path of the destination file Raises: - FileCopyException : If file copy failed. ''' local_dest = os.path.join(dest_dir, os.path.basename(source)) # Only attempt to copy if the target dir and source dir are different if os.path.dirname(source) != dest_dir: try: shutil.copyfile(source, local_dest) os.chmod(local_dest, 0o777) except OSError as e: raise FileCopyException(e, self.hostname) else: os.chmod(local_dest, 0o777) return local_dest
def push_file(self, local_source, remote_dir): ''' Transport a local file to a directory on a remote machine Args: - local_source (string): Path - remote_dir (string): Remote path Returns: - str: Path to copied file on remote machine Raises: - BadScriptPath : if script path on the remote side is bad - BadPermsScriptPath : You do not have perms to make the channel script dir - FileCopyException : FileCopy failed. ''' remote_dest = remote_dir + '/' + os.path.basename(local_source) try: self.makedirs(remote_dir, exist_ok=True) except IOError as e: logger.exception("Pushing {0} to {1} failed".format( local_source, remote_dir)) if e.errno == 2: raise BadScriptPath(e, self.hostname) elif e.errno == 13: raise BadPermsScriptPath(e, self.hostname) else: logger.exception("File push failed due to SFTP client failure") raise FileCopyException(e, self.hostname) try: self._valid_sftp_client().put(local_source, remote_dest, confirm=True) # Set perm because some systems require the script to be executable self._valid_sftp_client().chmod(remote_dest, 0o777) except Exception as e: logger.exception( "File push from local source {} to remote destination {} failed" .format(local_source, remote_dest)) raise FileCopyException(e, self.hostname) return remote_dest
def pull_file(self, remote_source, local_dir): ''' Transport file on the remote side to a local directory Args: - remote_source (string): remote_source - local_dir (string): Local directory to copy to Returns: - str: Local path to file Raises: - FileExists : Name collision at local directory. - FileCopyException : FileCopy failed. ''' local_dest = local_dir + '/' + os.path.basename(remote_source) try: os.makedirs(local_dir) except OSError as e: if e.errno != errno.EEXIST: logger.exception( "Failed to create local_dir: {0}".format(local_dir)) raise BadScriptPath(e, self.hostname) # Easier to check this than to waste time trying to pull file and # realize there's a problem. if os.path.exists(local_dest): logger.exception( "Remote file copy will overwrite a local file:{0}".format( local_dest)) raise FileExists(None, self.hostname, filename=local_dest) try: self.sftp_client.get(remote_source, local_dest) except Exception as e: logger.exception("File pull failed") raise FileCopyException(e, self.hostname) return local_dest