|
19 | 19 | from blackduck.HubRestApi import HubInstance, object_id
|
20 | 20 |
|
21 | 21 | class ScanMonitor(object):
|
| 22 | + SUCCESS = 0 |
| 23 | + FAILURE = 1 |
| 24 | + TIMED_OUT = 2 |
| 25 | + |
22 | 26 | def __init__(self, hub, scan_location_name, max_checks=10, check_delay=5, snippet_scan=False, start_time = None):
|
23 | 27 | self.hub = hub
|
24 | 28 | self.scan_location_name = scan_location_name
|
@@ -48,24 +52,28 @@ def wait_for_scan_completion(self):
|
48 | 52 | while remaining_checks > 0:
|
49 | 53 | scans = self.hub.execute_get(scans_url).json().get('items', [])
|
50 | 54 |
|
51 |
| - newer_scans = list(filter(lambda s: arrow.get(s['createdAt']) > self.start_time, scans)) |
| 55 | + newer_scans = list(filter(lambda s: arrow.get(s['updatedAt']) > self.start_time, scans)) |
52 | 56 | logging.debug(f"Found {len(newer_scans)} newer scans")
|
53 | 57 |
|
54 | 58 | expected_scans_seen = len(newer_scans) == number_expected_newer_scans
|
55 | 59 | logging.debug(f"expected_scans_seen: {expected_scans_seen}")
|
56 | 60 |
|
57 |
| - if expected_scans_seen and all([s['status'] == 'COMPLETE' for s in newer_scans]): |
| 61 | + if expected_scans_seen and all([s['status'] in ['COMPLETE', 'FAILURE'] for s in newer_scans]): |
58 | 62 | logging.info("Scans have finished processing")
|
59 |
| - break |
| 63 | + if all([s['status'] == 'COMPLETE' for s in newer_scans]): |
| 64 | + return ScanMonitor.SUCCESS |
| 65 | + else: |
| 66 | + return ScanMonitor.FAILURE |
60 | 67 | else:
|
61 | 68 | remaining_checks -= 1
|
62 | 69 | logging.debug(f"Sleeping for {self.check_delay} seconds before checking again. {remaining_checks} remaining")
|
63 | 70 | time.sleep(self.check_delay)
|
64 | 71 |
|
65 |
| - # TODO: Check for success/failure and return appropriate exit status? |
| 72 | + return ScanMonitor.TIMED_OUT |
| 73 | + |
66 | 74 |
|
67 | 75 | if __name__ == "__main__":
|
68 |
| - parser = argparse.ArgumentParser("Wait for scan processing to complete for a given code (scan) location/name") |
| 76 | + parser = argparse.ArgumentParser("Wait for scan processing to complete for a given code (scan) location/name and provide an exit status - 0 successful, 1 failed, and 2 timed-out") |
69 | 77 | parser.add_argument("scan_location_name", help="The scan location name")
|
70 | 78 | parser.add_argument('-m', '--max_checks', type=int, default=10, help="Set the maximum number of checks before quitting")
|
71 | 79 | parser.add_argument('-t', '--time_between_checks', type=int, default=5, help="Set the number of seconds to wait in-between checks")
|
|
0 commit comments