From 5ee0f142f74eebfd3c6358fe762234e32b339ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Fri, 1 Jul 2016 16:58:20 +0300 Subject: [PATCH] some fixing about file transfer --- opt/ahenk/base/file/http_file_transfer.py | 2 +- opt/ahenk/base/file/ssh_file_transfer.py | 37 +++++++++++++---------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/opt/ahenk/base/file/http_file_transfer.py b/opt/ahenk/base/file/http_file_transfer.py index 329b303..225f238 100644 --- a/opt/ahenk/base/file/http_file_transfer.py +++ b/opt/ahenk/base/file/http_file_transfer.py @@ -19,7 +19,7 @@ class Http(object): except Exception as e: self.logger.error('[Http] A problem occurred while parsing parameter map. Error Message: {}'.format(str(e))) - def send_file(self, local_path, remote_path): + def send_file(self, local_path, md5): pass def get_file(self): diff --git a/opt/ahenk/base/file/ssh_file_transfer.py b/opt/ahenk/base/file/ssh_file_transfer.py index e08bd03..5a34146 100644 --- a/opt/ahenk/base/file/ssh_file_transfer.py +++ b/opt/ahenk/base/file/ssh_file_transfer.py @@ -17,12 +17,12 @@ class Ssh(object): try: self.target_hostname = parameter_map['host'] - self.port =parameter_map['port'] + self.target_port = parameter_map['port'] self.target_username = parameter_map['username'] self.target_path = parameter_map['path'] self.target_password = None self.p_key = None - if parameter_map['password'] is not None: + if Util.has_attr_json(parameter_map, 'password'): self.target_password = parameter_map['password'] else: self.p_key = parameter_map['pkey'] @@ -30,23 +30,28 @@ class Ssh(object): self.logger.error('[Ssh] A problem occurred while parsing ssh connection parameters. Error Message: {}'.format(str(e))) self.connection = None - self.logger.debug('[FileTransfer] Parameters set up') + self.logger.debug('[Ssh] Parameters set up') - def send_file(self, local_path): - self.logger.debug('[FileTransfer] Sending file ...') + def send_file(self, local_path, md5): + self.logger.debug('[Ssh] {0} is sending to {1}'.format(local_path, self.target_path + md5)) try: sftp = paramiko.SFTPClient.from_transport(self.connection) - sftp.put(local_path, self.target_path) - self.logger.debug('[FileTransfer] File was sent to {} from {}'.format(local_path, self.target_path)) + try: + sftp.chdir(self.target_path) # Test if remote_path exists + except IOError: + sftp.mkdir() # Create remote_path + sftp.chdir(self.target_path) + + sftp.put(local_path, self.target_path + md5) + self.logger.debug('[Ssh] File was sent to {0} from {1}'.format(local_path, self.target_path)) + return True except Exception as e: - self.logger.error('[FileTransfer] A problem occurred while sending file. Exception message: {}'.format(str(e))) - finally: - self.connection.close() - self.logger.debug('[FileTransfer] Connection is closed successfully') + self.logger.error('[Ssh] A problem occurred while sending file. Exception message: {}'.format(str(e))) + return False def get_file(self): - self.logger.debug('[FileTransfer] Getting file ...') + self.logger.debug('[Ssh] Getting file ...') file_md5 = None try: tmp_file_name = str(Util.generate_uuid()) @@ -55,16 +60,16 @@ class Ssh(object): sftp.get(self.target_path, local_full_path) file_md5 = str(Util.get_md5_file(local_full_path)) Util.rename_file(local_full_path, System.Ahenk.received_dir_path() + file_md5) - self.logger.debug('[FileTransfer] File was downloaded to {0} from {1}'.format(local_full_path, self.target_path)) + self.logger.debug('[Ssh] File was downloaded to {0} from {1}'.format(local_full_path, self.target_path)) except Exception as e: - self.logger.error('[FileTransfer] A problem occurred while downloading file. Exception message: {}'.format(str(e))) + self.logger.error('[Ssh] A problem occurred while downloading file. Exception message: {}'.format(str(e))) raise return file_md5 def connect(self): - self.logger.debug('[FileTransfer] Connecting to {} via {}'.format(self.target_hostname, self.port)) + self.logger.debug('[FileTransfer] Connecting to {} via {}'.format(self.target_hostname, self.target_port)) try: - connection = paramiko.Transport((self.target_hostname, int(self.port))) + connection = paramiko.Transport(self.target_hostname, int(self.target_port)) connection.connect(username=self.target_username, password=self.target_password, pkey=self.p_key) self.connection = connection self.logger.debug('[FileTransfer] Connected.')