def format_source(json_source): """Format source with credentials and most recent connection scan. :param json_source: JSON source data from serializer :returns: JSON data """ expand_credential(json_source) source_id = json_source.get('id') scan_task_qs = ScanTask.objects.filter( source__id=source_id, scan_type=ScanTask.SCAN_TYPE_CONNECT) if scan_task_qs.count() > 0: scan_task = scan_task_qs.latest('start_time') scan_job = ScanJob.objects.filter( tasks__id=scan_task.id).latest('start_time') json_scan_job = {'id': scan_job.id, 'start_time': scan_job.start_time, 'end_time': scan_job.end_time, 'status': scan_job.status, 'systems_count': scan_task.systems_count, 'systems_scanned': scan_task.systems_scanned, 'systems_failed': scan_task.systems_failed} json_source['connection'] = json_scan_job return json_source
def format_source(json_source): """Format source with credentials and most recent connection scan. :param json_source: JSON source data from serializer :returns: JSON data """ expand_credential(json_source) conn_job_id = json_source.pop('most_recent_connect_scan', None) if conn_job_id: scan_job = ScanJob.objects.get(pk=conn_job_id) json_scan_job = expand_scanjob_with_times(scan_job, connect_only=True) source_id = json_source.get('id') task_for_source = scan_job.tasks.filter(source=source_id).first() if task_for_source is not None: if task_for_source.systems_count is not None: json_scan_job['source_systems_count'] = \ task_for_source.systems_count else: json_scan_job['source_systems_count'] = 0 if task_for_source.systems_scanned is not None: json_scan_job['source_systems_scanned'] = \ task_for_source.systems_scanned else: json_scan_job['source_systems_scanned'] = 0 if task_for_source.systems_failed is not None: json_scan_job['source_systems_failed'] = \ task_for_source.systems_failed else: json_scan_job['source_systems_failed'] = 0 if task_for_source.systems_unreachable is not None: json_scan_job['source_systems_unreachable'] = \ task_for_source.systems_unreachable else: json_scan_job['source_systems_unreachable'] = 0 json_source['connection'] = json_scan_job return json_source
def log_ansible_result(result, scan_task): """Log the results of a shell command. :param result: the Ansible result object, as a Python dict. :param scan_task: the ScanTask of this Ansible task. """ # pylint: disable=protected-access args = result._task.args source_json = SourceSerializer(scan_task.source).data view_util.expand_credential(source_json) scan_jobs = list(scan_task.scanjob_set.all()) # len(scan_jobs) should be 1 if len(scan_jobs) > 1: logger.warning('ScanTask %s associated to multiple ScanJobs %s', scan_task.id, [job.id for job in scan_jobs]) if RAW_PARAMS in args: log_fact(result._host.name, { 'type': 'shell', 'command': args[RAW_PARAMS] }, result._result, scan_jobs[0].id, scan_task.id, source_json)