From d6abe1a78fd4dd06849bede15af3a87f02a51971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 29 Jun 2016 12:41:31 +0300 Subject: [PATCH] file transfer method was defined --- opt/ahenk/base/file/file_transfer_manager.py | 30 ++++++++++++ opt/ahenk/base/file/http_file_transfer.py | 48 ++++++++++++++++++ opt/ahenk/base/file/ssh_file_transfer.py | 51 ++++++++++++++------ 3 files changed, 115 insertions(+), 14 deletions(-) diff --git a/opt/ahenk/base/file/file_transfer_manager.py b/opt/ahenk/base/file/file_transfer_manager.py index e69de29..418f1b5 100644 --- a/opt/ahenk/base/file/file_transfer_manager.py +++ b/opt/ahenk/base/file/file_transfer_manager.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Author: Volkan Şahin + +from base.Scope import Scope +from base.file.ssh_file_transfer import Ssh +from base.file.http_file_transfer import Http + + +class FileTransferManager(object): + def __init__(self, protocol, parameter_map): + scope = Scope().getInstance() + self.logger = scope.getLogger() + self.configuration_manager = scope.getConfigurationManager() + self.transporter = self.get_instance(protocol, parameter_map) + + def get_instance(self, protocol, parameter_map): + try: + transporter = None + if str(protocol).lower() == 'ssh': + transporter = Ssh(parameter_map) + elif str(protocol).lower() == 'http': + transporter = Http(parameter_map) + else: + raise Exception('Unsupported file transfer protocol: {}'.format(str(protocol))) + return transporter + + except Exception as e: + self.logger.error('[FileTransferManager] A problem occurred while getting instance of related protocol. Error Message: {}'.format(str(e))) + return None diff --git a/opt/ahenk/base/file/http_file_transfer.py b/opt/ahenk/base/file/http_file_transfer.py index e69de29..329b303 100644 --- a/opt/ahenk/base/file/http_file_transfer.py +++ b/opt/ahenk/base/file/http_file_transfer.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Author: Volkan Şahin +from base.util.util import Util +from base.system.system import System +import urllib.request + +from base.Scope import Scope + + +class Http(object): + def __init__(self, parameter_map): + + scope = Scope().getInstance() + self.logger = scope.getLogger() + self.configuration_manager = scope.getConfigurationManager() + try: + self.url = parameter_map['url'] + 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): + pass + + def get_file(self): + + self.logger.debug('[FileTransfer] Getting file ...') + file_md5 = None + try: + tmp_file_name = str(Util.generate_uuid()) + local_full_path = System.Ahenk.received_dir_path() + tmp_file_name + urllib.request.urlretrieve(self.url, 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.url)) + except Exception as e: + self.logger.error('[FileTransfer] A problem occurred while downloading file. Exception message: {}'.format(str(e))) + raise + return file_md5 + + def disconnect(self): + pass + + def is_connected(self): + pass + + def connect(self): + pass diff --git a/opt/ahenk/base/file/ssh_file_transfer.py b/opt/ahenk/base/file/ssh_file_transfer.py index 96b8c67..c257c90 100644 --- a/opt/ahenk/base/file/ssh_file_transfer.py +++ b/opt/ahenk/base/file/ssh_file_transfer.py @@ -2,24 +2,34 @@ # -*- coding: utf-8 -*- # Author: Volkan Şahin +from base.util.util import Util +from base.system.system import System from base.Scope import Scope import paramiko -class FileTransfer(object): - def __init__(self, hostname, port, username, password, pkey=None): +class Ssh(object): + def __init__(self, parameter_map): scope = Scope().getInstance() self.logger = scope.getLogger() self.configuration_manager = scope.getConfigurationManager() - self.target_hostname = hostname - self.port = port - self.target_username = username - self.target_password = password - self.p_key = pkey - self.connection = None + try: + self.target_hostname = parameter_map['hostname'] + self.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: + self.target_password = parameter_map['password'] + else: + self.p_key = parameter_map['pkey'] + except Exception as e: + 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') def send_file(self, local_path, remote_path): @@ -35,18 +45,21 @@ class FileTransfer(object): self.connection.close() self.logger.debug('[FileTransfer] Connection is closed successfully') - def get_file(self, local_path, remote_path): + def get_file(self): self.logger.debug('[FileTransfer] Getting file ...') + file_md5 = None try: + tmp_file_name = str(Util.generate_uuid()) + local_full_path = System.Ahenk.received_dir_path() + tmp_file_name sftp = paramiko.SFTPClient.from_transport(self.connection) - sftp.get(remote_path, local_path) - self.logger.debug('[FileTransfer] File was downloaded to {} from {}'.format(local_path, remote_path)) + 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)) except Exception as e: self.logger.error('[FileTransfer] A problem occurred while downloading file. Exception message: {}'.format(str(e))) raise - finally: - self.connection.close() - self.logger.debug('[FileTransfer] Connection is closed successfully') + return file_md5 def connect(self): self.logger.debug('[FileTransfer] Connecting to {} via {}'.format(self.target_hostname, self.port)) @@ -57,3 +70,13 @@ class FileTransfer(object): self.logger.debug('[FileTransfer] Connected.') except Exception as e: self.logger.error('[FileTransfer] A problem occurred while connecting to {} . Exception message: {}'.format(self.target_hostname, str(e))) + + def disconnect(self): + self.connection.close() + self.logger.debug('[FileTransfer] Connection is closed.') + # TODO + pass + + def is_connected(self): + # TODO + pass