Due to a scheduled upgrade to version 14.10, GitLab will be unavailabe on Monday 30.05., from 19:00 until 20:00.

Commit 0d251b54 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Fix ssh client connection.

parent 5707b44c
Pipeline #33633 passed with stages
in 1 minute and 50 seconds
......@@ -73,45 +73,13 @@ class MappingFileHandler:
def _retrieve_mapping_files(self, record_set_id: str) -> Dict[str, Union[str, bool, list]]:
try:
with self.ssh_client.connect(hostname=self.host,
port=self.port,
username=self.user,
password=self.password) as client:
with client.open_sftp() as sftp:
path = join(self.base_path, record_set_id, self.config_folder)
# Check mapping file
try:
mapping_file_path = join(path, self.mapping_file)
with sftp.open(mapping_file_path, 'r') as mapping:
file_contents_mapping = mapping.read()
except IOError as err:
return {
'status': 'FATAL',
'type': 'NoMappingFile',
'message': f'Could not open the mapping file at '
f'{mapping_file_path} (ERROR: {err}).'
}
# Check xslt file
try:
xslt_file_path = join(path, self.xslt_file)
sftp.stat(xslt_file_path)
with sftp.open(xslt_file_path, 'r') as xslt:
file_contents_xslt = xslt.read()
except IOError:
file_contents_xslt = ''
# Check local transform file
try:
local_transform_file_path = join(path, self.local_transform_file)
sftp.stat(local_transform_file_path)
with sftp.open(local_transform_file_path, 'r') as local_transform:
file_contents_local_transform = local_transform.read()
except IOError:
file_contents_local_transform = ''
self.ssh_client.connect(hostname=self.host,
port=self.port,
username=self.user,
password=self.password)
except BadHostKeyException as ex:
message = f"Could not connect to the server because of a bad host key: {ex}"
self.logger.error()
self.logger.error(message)
return {
'status': 'FATAL',
'type': 'BadHostKeyException',
......@@ -119,7 +87,7 @@ class MappingFileHandler:
}
except AuthenticationException:
message = "Could not authenticate with the sftp server with the given credentials."
self.logger.error()
self.logger.error(message)
return {
'status': 'FATAL',
'type': 'AuthenticationException',
......@@ -127,7 +95,7 @@ class MappingFileHandler:
}
except SSHException as ex:
message = f"SSH Exception: {ex}."
self.logger.error()
self.logger.error(message)
return {
'status': 'FATAL',
'type': 'SSHException',
......@@ -141,6 +109,44 @@ class MappingFileHandler:
'type': 'SocketError',
'messages': [message]
}
sftp = self.ssh_client.open_sftp()
path = join(self.base_path, record_set_id, self.config_folder)
# Check mapping file
mapping_file_path = join(path, self.mapping_file)
try:
with sftp.open(mapping_file_path, 'r') as mapping:
file_contents_mapping = mapping.read()
except IOError as err:
message = f'Could not open the mapping file at {mapping_file_path} (ERROR: {err}).'
sftp.close()
self.ssh_client.close()
self.logger.error(message)
return {
'status': 'FATAL',
'type': 'NoMappingFile',
'message': message
}
# Check xslt file
xslt_file_path = join(path, self.xslt_file)
try:
with sftp.open(xslt_file_path, 'r') as xslt:
file_contents_xslt = xslt.read()
except IOError:
file_contents_xslt = ''
# Check local transform file
local_transform_file_path = join(path, self.local_transform_file)
try:
with sftp.open(local_transform_file_path, 'r') as local_transform:
file_contents_local_transform = local_transform.read()
except IOError:
file_contents_local_transform = ''
sftp.close()
self.ssh_client.close()
return {
'status': 'SUCCESS',
'mapping_file': file_contents_mapping,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment