Ejemplo n.º 1
0
def retrieve_inspection_result(context):
    """Retrieve inspection from Thoth using Amun API."""
    response = get_inspection_build_log(context.amun_api_host, context.inspection_id)
    assert response
Ejemplo n.º 2
0
def sync_inspection_documents(
    document_ids: Optional[List[str]] = None,
    *,
    force: bool = False,
    graceful: bool = False,
    graph: Optional[GraphDatabase] = None,
    amun_api_url: str,
    only_ceph_sync: bool = False,
    only_graph_sync: bool = False,
    is_local: bool = False,
) -> tuple:
    """Sync observations made on Amun into graph database."""
    from amun import get_inspection_build_log
    from amun import get_inspection_job_log
    from amun import get_inspection_specification
    from amun import get_inspection_status
    from amun import has_inspection_job
    from amun import is_inspection_finished

    if is_local:
        raise NotImplementedError(
            "Cannot sync inspection documents from a local file")

    if only_graph_sync and only_ceph_sync:
        raise ValueError("At least one of Ceph or Graph should be performed")

    inspection_store = InspectionResultsStore()
    inspection_store.connect()

    dependency_monkey_reports_store = DependencyMonkeyReportsStore()
    dependency_monkey_reports_store.connect()

    if not graph:
        graph = GraphDatabase()
        graph.connect()

    processed, synced, skipped, failed = 0, 0, 0, 0
    for inspection_id in document_ids or dependency_monkey_reports_store.iterate_inspection_ids(
    ):
        processed += 1
        if force or not inspection_store.document_exists(inspection_id):
            finished = is_inspection_finished(amun_api_url, inspection_id)

            if finished:
                _LOGGER.info(
                    "Obtaining results from Amun API for inspection %r",
                    inspection_id)
                try:
                    specification, created = get_inspection_specification(
                        amun_api_url, inspection_id)
                    build_log = get_inspection_build_log(
                        amun_api_url, inspection_id)
                    status = get_inspection_status(amun_api_url, inspection_id)
                    job_log = None
                    if has_inspection_job(amun_api_url, inspection_id):
                        job_log = get_inspection_job_log(
                            amun_api_url, inspection_id)

                    document = {
                        "specification": specification,
                        "created": created,
                        "build_log": build_log,
                        "job_log": job_log,
                        "inspection_id": inspection_id,
                        "status": status,
                    }

                    # First we store results into graph database and then onto
                    # Ceph. This way in the next run we can sync documents that
                    # failed to sync to graph - see if statement that is asking
                    # for Ceph document presents first.
                    if not only_ceph_sync:
                        _LOGGER.info(
                            f"Syncing inspection {inspection_id!r} to graph")
                        graph.sync_inspection_result(document)

                    if not only_graph_sync:
                        _LOGGER.info(
                            f"Syncing inspection {inspection_id!r} to {inspection_store.ceph.host}"
                        )
                        inspection_store.store_document(document)

                    synced += 1
                except Exception as exc:
                    if not graceful:
                        raise

                    _LOGGER.exception(f"Failed to sync inspection %r: %s",
                                      inspection_id, str(exc))
                    failed += 1
            else:
                _LOGGER.info(
                    f"Skipping inspection {inspection_id!r} - not finised yet")
                skipped += 1
        else:
            _LOGGER.info(
                f"Skipping inspection {inspection_id!r} - the given inspection is already synced"
            )
            skipped += 1

    return processed, synced, skipped, failed
Ejemplo n.º 3
0
def sync_inspection_documents(amun_api_url: str,
                              document_ids: list = None,
                              force_sync: bool = False,
                              graceful: bool = False) -> tuple:
    """Sync observations made on Amun into graph databaes."""
    inspection_store = InspectionResultsStore()
    inspection_store.connect()

    dependency_mokey_reports_store = DependencyMonkeyReportsStore()
    dependency_mokey_reports_store.connect()

    graph = GraphDatabase()
    graph.connect()

    processed, synced, skipped, failed = 0, 0, 0, 0
    for inspection_id in document_ids or dependency_mokey_reports_store.iterate_inspection_ids(
    ):
        processed += 1
        if force_sync or not inspection_store.document_exists(inspection_id):
            try:
                finished = is_inspection_finished(amun_api_url, inspection_id)
            except Exception as exc:
                _LOGGER.error(
                    "Failed to obtain inspection status from Amun: %s",
                    str(exc))
                continue

            if finished:
                _LOGGER.info(
                    "Obtaining results from Amun API for inspection %r",
                    inspection_id)
                try:
                    specification = get_inspection_specification(
                        amun_api_url, inspection_id)
                    build_log = get_inspection_build_log(
                        amun_api_url, inspection_id)
                    status = get_inspection_status(amun_api_url, inspection_id)
                    job_log = None
                    if has_inspection_job(amun_api_url, inspection_id):
                        job_log = get_inspection_job_log(
                            amun_api_url, inspection_id)

                    document = {
                        'specification': specification,
                        'build_log': build_log,
                        'job_log': job_log,
                        'inspection_id': inspection_id,
                        'status': status
                    }

                    # First we store results into graph database and then onto
                    # Ceph. This way in the next run we can sync documents that
                    # failed to sync to graph - see if statement that is asking
                    # for Ceph document presents first.
                    _LOGGER.info(
                        f"Syncing inspection {inspection_id!r} to {graph.hosts}"
                    )
                    graph.sync_inspection_result(document)
                    _LOGGER.info(
                        f"Syncing inspection {inspection_id!r} to {inspection_store.ceph.host}"
                    )
                    inspection_store.store_document(document)
                    synced += 1
                except Exception as exc:
                    if not graceful:
                        raise

                    _LOGGER.exception(f"Failed to sync inspection %r: %s",
                                      inspection_id, str(exc))
                    failed += 1
            else:
                _LOGGER.info(
                    f"Skipping inspection {inspection_id!r} - not finised yet")
                skipped += 1
        else:
            _LOGGER.info(
                f"Skipping inspection {inspection_id!r} - the given inspection is already synced"
            )
            skipped += 1

    return processed, synced, skipped, failed