mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-11-22 10:52:17 +03:00
some fixing about file transfer
This commit is contained in:
parent
411460058a
commit
5ee0f142f7
2 changed files with 22 additions and 17 deletions
|
@ -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):
|
||||
|
|
|
@ -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.')
|
||||
|
|
Loading…
Reference in a new issue