def create(self, request, *args, **kwargs): """Create a details report.""" # pylint: disable=unused-argument # Validate incoming request body has_errors, validation_result = validate_details_report_json( request.data) if has_errors: return Response(validation_result, status=status.HTTP_400_BAD_REQUEST) # Create FC model and save data details_report = create_details_report(request.data) scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_FINGERPRINT, details_report=details_report) scan_job.save() scan_job.queue() runner = ScanJobRunner(scan_job) runner.run() if scan_job.status != ScanTask.COMPLETED: # pylint: disable=no-member error_json = {'error': scan_job.tasks.first().status_message} return Response(error_json, status=status.HTTP_400_BAD_REQUEST) scan_job = ScanJob.objects.get(pk=scan_job.id) details_report = DetailsReport.objects.get(pk=details_report.id) # Prepare REST response body serializer = self.get_serializer(details_report) result = serializer.data return Response(result, status=status.HTTP_201_CREATED)
def sync_merge_reports(request): """Merge reports synchronously.""" # pylint: disable=too-many-locals error = {'reports': []} details_report_json = _convert_ids_to_json(request.data) has_errors, validation_result = validate_details_report_json( details_report_json, True) if has_errors: message = _(messages.REPORT_MERGE_NO_RESULTS % validation_result) error.get('reports').append(message) raise ValidationError(error) # Create FC model and save data details_report_json = _reconcile_source_versions(details_report_json) report_version = details_report_json.get('report_version', None) details_report = create_details_report(report_version, details_report_json) merge_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_FINGERPRINT, details_report=details_report) merge_job.save() merge_job.queue() runner = ScanJobRunner(merge_job) runner.run() if merge_job.status != ScanTask.COMPLETED: raise Exception(merge_job.status_message) merge_job.refresh_from_db() details_report = DetailsReport.objects.get(pk=details_report.id) # Prepare REST response body serializer = DetailsReportSerializer(details_report) result = serializer.data return Response(result, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): """Create a details report.""" # pylint: disable=unused-argument # Validate incoming request body has_errors, validation_result = validate_details_report_json( request.data, True) if has_errors: return Response(validation_result, status=status.HTTP_400_BAD_REQUEST) report_version = request.data.get('report_version', None) warn_deprecated = False if not report_version: warn_deprecated = True report_version = create_report_version() # Create FC model and save data details_report = create_details_report(create_report_version(), request.data) scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_FINGERPRINT, details_report=details_report) scan_job.save() if warn_deprecated: scan_job.log_message(_(messages.FC_MISSING_REPORT_VERSION), log_level=logging.WARNING) scan_job.queue() runner = ScanJobRunner(scan_job) runner.run() if scan_job.status != ScanTask.COMPLETED: # pylint: disable=no-member error_json = {'error': scan_job.tasks.first().status_message} return Response(error_json, status=status.HTTP_400_BAD_REQUEST) scan_job = ScanJob.objects.get(pk=scan_job.id) details_report = DetailsReport.objects.get(pk=details_report.id) # Prepare REST response body serializer = self.get_serializer(details_report) result = serializer.data return Response(result, status=status.HTTP_201_CREATED)
def sync_merge_reports(request): """Merge reports synchronously.""" error = { 'reports': [] } reports = validate_merge_report(request.data) sources = [] for report in reports: sources = sources + report.get_sources() details_report_json = {'sources': sources} has_errors, validation_result = validate_details_report_json( details_report_json) if has_errors: message = _(messages.REPORT_MERGE_NO_RESULTS % validation_result) error.get('reports').append(message) raise ValidationError(error) # Create FC model and save data details_report = create_details_report(details_report_json) scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_FINGERPRINT, details_report=details_report) scan_job.save() scan_job.queue() runner = ScanJobRunner(scan_job) runner.run() if scan_job.status != ScanTask.COMPLETED: raise Exception(scan_job.status_message) scan_job = ScanJob.objects.get(pk=scan_job.id) details_report = DetailsReport.objects.get(pk=details_report.id) # Prepare REST response body serializer = DetailsReportSerializer(details_report) result = serializer.data return Response(result, status=status.HTTP_201_CREATED)
def restart_incomplete_scansjobs(self): """Look for incomplete scans and restart.""" logger.info('%s: Searching for incomplete scans', SCAN_MANAGER_LOG_PREFIX) incomplete_scans = ScanJob.objects.filter( Q(status=ScanTask.RUNNING) | Q(status=ScanTask.PENDING) | Q(status=ScanTask.CREATED)).order_by('-status') restarted_scan_count = 0 for scanjob in incomplete_scans: scanner = ScanJobRunner(scanjob) logger.info('%s: Adding scan job(id=%d, status=%s, scan_type=%s)', SCAN_MANAGER_LOG_PREFIX, scanjob.id, scanjob.status, scanjob.scan_type) if scanjob.status == ScanTask.CREATED: scanjob.queue() self.put(scanner) restarted_scan_count += 1 if restarted_scan_count == 0: logger.info('%s: No running or pending scan jobs to start', SCAN_MANAGER_LOG_PREFIX)
def scan_restart(sender, instance, **kwargs): """Restart a scan. :param sender: Class that was saved :param instance: ScanJob that was saved :param kwargs: Other args :returns: None """ instance.log_message(_(messages.SIGNAL_STATE_CHANGE) % ('RESTART')) scanner = ScanJobRunner(instance) if not manager.SCAN_MANAGER.is_alive(): logger.error('%s: %s', manager.SCAN_MANAGER_LOG_PREFIX, _(messages.SIGNAL_SCAN_MANAGER_CRASH)) manager.SCAN_MANAGER = manager.Manager() logger.error('%s: %s', manager.SCAN_MANAGER_LOG_PREFIX, _(messages.SIGNAL_SCAN_MANAGER_RESTART)) manager.SCAN_MANAGER.start() # Don't add the scan as it will be picked up # by the manager startup, looking for pending/running scans. else: manager.SCAN_MANAGER.put(scanner)