Skip to content

Commit 4372b42

Browse files
authored
Merge pull request #45 from GaneshManal/PNDA-2282
PNDA-2282: Pass more specific reason back in case of bad health for deployment manager - Added exception handling changes.
2 parents 8b716e0 + 403c582 commit 4372b42

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

api/src/main/resources/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import application_summary_registrar
3838
import deployment_manager
3939
from deployer_system_test import DeployerRestClientTester
40-
from exceptiondef import NotFound, ConflictingState, FailedValidation, FailedCreation
40+
from exceptiondef import NotFound, ConflictingState, FailedValidation, FailedCreation, FailedConnection
4141
from async_dispatcher import AsyncDispatcher
4242
from package_repo_rest_client import PackageRepoRestClient
4343

@@ -82,6 +82,10 @@ def finish():
8282
logging.info(ex.msg)
8383
self.set_status(500)
8484
self.finish(ex.msg)
85+
elif isinstance(ex, FailedConnection):
86+
logging.info(ex.msg)
87+
self.set_status(503)
88+
self.finish(ex.msg)
8589
else:
8690
self.set_status(500)
8791
if "information" in str(ex):

api/src/main/resources/exceptiondef.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, arg):
3131
def __str__(self):
3232
return str(self.msg)
3333

34+
3435
class NotFound(DmException):
3536

3637
def __init__(self, arg):
@@ -57,3 +58,12 @@ class FailedCreation(DmException):
5758
def __init__(self, arg):
5859
super(FailedCreation, self).__init__(arg)
5960
self.msg = arg
61+
62+
63+
class FailedConnection(DmException):
64+
65+
def __init__(self, arg):
66+
super(FailedConnection, self).__init__(arg)
67+
self.msg = arg
68+
69+

api/src/main/resources/package_registrar.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
from package_parser import PackageParser
3030
from deployer_utils import HDFS
3131

32+
from exceptiondef import FailedConnection
33+
34+
3235
class HbasePackageRegistrar(object):
3336
COLUMN_DEPLOY_STATUS = "cf:deploy_status"
3437

@@ -130,20 +133,25 @@ def get_package_deploy_status(self, package_name):
130133
def list_packages(self):
131134
logging.debug("List all packages")
132135

133-
connection = happybase.Connection(self._hbase_host)
136+
connection = None
134137
try:
138+
connection = happybase.Connection(self._hbase_host)
135139
table = connection.table(self._table_name)
136140
result = [key for key, _ in table.scan(columns=['cf:name'])]
141+
except Exception as e:
142+
logging.debug(str(e))
143+
raise FailedConnection('Unable to connect to the HBase master')
137144
finally:
138-
connection.close()
145+
if connection:
146+
connection.close()
139147
return result
140148

141149
def generate_record(self, metadata):
142150
return metadata["package_name"], {
143151
'cf:name': '-'.join(metadata["package_name"].split("-")[:-1]),
144152
'cf:version': metadata["package_name"].split("-")[-1],
145153
'cf:metadata': json.dumps(metadata),
146-
'cf:package_data': "%s/%s" % (self._package_hdfs_dir_path, metadata["package_name"])
154+
'cf:package_data': "%s/%s" % (self._package_hdfs_dir_path, metadata["package_name"])
147155
}
148156

149157
def _read_from_db(self, key, columns):

api/src/main/resources/package_repo_rest_client.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import json
2222
import logging
2323
import requests
24-
from exceptiondef import NotFound
24+
import re
25+
from exceptiondef import FailedConnection
26+
from requests.exceptions import RequestException
2527

2628

2729
class PackageRepoRestClient(object):
@@ -69,14 +71,33 @@ def get_package_list(self, recency=None):
6971
response = self.make_rest_get_request(url)
7072
return json.loads(response.content)
7173

74+
@staticmethod
75+
def parse_error_msg_from_html_response(html_str):
76+
title_tag = re.search('<title>(.+?)<.*/title>', html_str)
77+
if title_tag:
78+
cause_msg = re.sub('<[A-Za-z\/][^>]*>', '', title_tag.group())
79+
return cause_msg
80+
return html_str
81+
7282
def make_rest_get_request(self, path, expected_codes=None):
7383
if not expected_codes:
7484
expected_codes = [200]
7585
url = self.api_url + path
7686
logging.debug("GET: " + url)
77-
response = requests.get(url, timeout=120)
87+
88+
try:
89+
response = requests.get(url, timeout=120)
90+
except RequestException as e:
91+
logging.debug("Request error: " + str(e))
92+
error_msg = 'Unable to connect to the Package Repository Manager'
93+
raise FailedConnection(error_msg)
94+
7895
logging.debug("response code: " + str(response.status_code))
79-
if (404 not in expected_codes) and (response.status_code == 404):
80-
raise NotFound(path)
81-
assert response.status_code in expected_codes
96+
97+
if response.status_code not in expected_codes:
98+
error_msg = PackageRepoRestClient.parse_error_msg_from_html_response(response.text)
99+
error_msg = "Package Repository Manager - {} (request path = {})".format(error_msg, path)
100+
logging.debug("Server error: " + str(error_msg))
101+
assert response.status_code in expected_codes, error_msg
102+
82103
return response

0 commit comments

Comments
 (0)