Skip to content

Commit

Permalink
Merge branch 'master' into issues/195
Browse files Browse the repository at this point in the history
  • Loading branch information
kholdaway committed Nov 28, 2017
2 parents 073cac2 + 2731b16 commit 01efdcf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
9 changes: 5 additions & 4 deletions quipucords/scanner/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ def v2_runner_on_unreachable(self, result):
"""Print a json representation of the result."""
result_obj = _construct_result(result)
self.results.append(result_obj)
self._update_reachable_hosts(result_obj)
self.scanjob.failed_scans += 1
self.scanjob.status = ScanJob.FAILED
self.scanjob.save()
if self.scanjob is not None:
self._update_reachable_hosts(result_obj)
self.scanjob.failed_scans += 1
self.scanjob.status = ScanJob.FAILED
self.scanjob.save()
logger.warning('%s', result_obj)

def _update_reachable_hosts(self, result_obj):
Expand Down
30 changes: 27 additions & 3 deletions quipucords/scanner/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
os.path.join(os.path.curdir, 'playbooks', 'host_scan_playbook.yaml')))


class ScannerException(Exception):
"""Exception for issues detected during scans."""

def __init__(self, message=''):
"""Exception for issues detected during scans.
:param message: An error message describing the problem encountered
during scan.
"""
self.message = 'Scan failed. Error: {}'.format(message)
super().__init__(self.message)


class HostScanner(DiscoveryScanner):
"""Scan target systems to collect facts.
Expand Down Expand Up @@ -66,6 +79,10 @@ def host_scan(self):
num_total = num_remaining + num_completed
num_failed = len(failed)

if num_total == 0:
msg = 'Inventory provided no reachable hosts.'
raise ScannerException(msg)

if self.scan_restart:
logger.info('Host scan restarted for %s.',
self.scanjob)
Expand Down Expand Up @@ -185,8 +202,7 @@ def send_facts(self, facts):
data = response.json()
msg = 'Failed to obtain fact_collection_id when reporting facts.'
if response.status_code != 201:
logger.error('Could not create facts. Errors: %s', data)
assert 'id' in data, msg
raise ScannerException('{} Error: {}'.format(msg, data))
return data['id']

def _store_host_scan_success(self, fact_collection_id):
Expand All @@ -207,6 +223,12 @@ def run(self):
facts = self.host_scan()

# Send facts to fact endpoint
fact_size = len(facts)
if facts is None or fact_size == 0:
msg = 'Fact set is empty. '\
'No results will be reported to fact endpoint.'
raise ScannerException(msg)

fact_collection_id = self.send_facts(facts)

# Save the fact collection id to scanjob
Expand All @@ -220,5 +242,7 @@ def run(self):
except AssertionError as assertion_error:
logger.error(assertion_error)
self._store_error(assertion_error)

except ScannerException as scan_error:
logger.error(scan_error)
self._store_error(scan_error)
return facts

0 comments on commit 01efdcf

Please sign in to comment.