Ejemplo n.º 1
0
 def _get_manifest(self, date_time):
     dates = utils.month_date_range(date_time)
     directory = f"{REPORTS_DIR}/{self.cluster_id}/{dates}"
     msg = f"Looking for manifest at {directory}"
     LOG.info(log_json(self.request_id, msg, self.context))
     report_meta = utils.get_report_details(directory)
     return report_meta
Ejemplo n.º 2
0
    def test_get_report_details(self):
        """Test that we handle manifest files properly."""
        with tempfile.TemporaryDirectory() as manifest_path:
            manifest_file = f"{manifest_path}/manifest.json"
            with self.assertLogs("masu.util.ocp.common",
                                 level="INFO") as logger:
                expected = f"INFO:masu.util.ocp.common:No manifest available at {manifest_file}"
                utils.get_report_details(manifest_path)
                self.assertIn(expected, logger.output)

            with open(manifest_file, "w") as f:
                data = {"key": "value"}
                json.dump(data, f)
            utils.get_report_details(manifest_path)

            with patch("masu.util.ocp.common.open") as mock_open:
                mock_open.side_effect = OSError
                with self.assertLogs("masu.util.ocp.common",
                                     level="INFO") as logger:
                    expected = "ERROR:masu.util.ocp.common:Unable to extract manifest data"
                    utils.get_report_details(manifest_path)
                    self.assertIn(expected, logger.output[0])
Ejemplo n.º 3
0
def extract_payload(url):
    """
    Extract OCP usage report payload into local directory structure.

    Payload is expected to be a .tar.gz file that contains:
    1. manifest.json - dictionary containing usage report details needed
        for report processing.
        Dictionary Contains:
            file - .csv usage report file name
            date - DateTime that the payload was created
            uuid - uuid for payload
            cluster_id  - OCP cluster ID.
    2. *.csv - Actual usage report for the cluster.  Format is:
        Format is: <uuid>_report_name.csv

    On successful completion the report and manifest will be in a directory
    structure that the OCPReportDownloader is expecting.

    Ex: /var/tmp/insights_local/my-ocp-cluster-1/20181001-20181101

    Args:
        url (String): URL path to payload in the Insights upload service..

    Returns:
        (Dict): keys: value
            "file: String,
             cluster_id: String,
             payload_date: DateTime,
             manifest_path: String,
             uuid: String,
             manifest_path: String"

    """
    # Create temporary directory for initial file staging and verification in the
    # OpenShift PVC directory so that any failures can be triaged in the event
    # the pod goes down.
    os.makedirs(Config.TMP_DIR, exist_ok=True)
    temp_dir = tempfile.mkdtemp(dir=Config.TMP_DIR)

    # Download file from quarntine bucket as tar.gz
    try:
        download_response = requests.get(url)
        download_response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        shutil.rmtree(temp_dir)
        raise KafkaMsgHandlerError('Unable to download file. Error: ', str(err))

    temp_file = '{}/{}'.format(temp_dir, 'usage.tar.gz')
    try:
        temp_file_hdl = open('{}/{}'.format(temp_dir, 'usage.tar.gz'), 'wb')
        temp_file_hdl.write(download_response.content)
        temp_file_hdl.close()
    except (OSError, IOError) as error:
        shutil.rmtree(temp_dir)
        raise KafkaMsgHandlerError('Unable to write file. Error: ', str(error))

    # Extract tarball into temp directory
    try:
        mytar = TarFile.open(temp_file)
        mytar.extractall(path=temp_dir)
        files = mytar.getnames()
        manifest_path = [manifest for manifest in files if 'manifest.json' in manifest]
    except ReadError as error:
        LOG.error('Unable to untar file. Reason: %s', str(error))
        shutil.rmtree(temp_dir)
        raise KafkaMsgHandlerError('Extraction failure.')

    # Open manifest.json file and build the payload dictionary.
    full_manifest_path = '{}/{}'.format(temp_dir, manifest_path[0])
    report_meta = utils.get_report_details(os.path.dirname(full_manifest_path))

    # Create directory tree for report.
    usage_month = utils.month_date_range(report_meta.get('date'))
    destination_dir = '{}/{}/{}'.format(Config.INSIGHTS_LOCAL_REPORT_DIR,
                                        report_meta.get('cluster_id'),
                                        usage_month)
    os.makedirs(destination_dir, exist_ok=True)

    # Copy manifest
    manifest_destination_path = '{}/{}'.format(destination_dir,
                                               os.path.basename(report_meta.get('manifest_path')))
    shutil.copy(report_meta.get('manifest_path'), manifest_destination_path)

    # Copy report payload
    for report_file in report_meta.get('files'):
        subdirectory = os.path.dirname(full_manifest_path)
        payload_source_path = '{}/{}'.format(subdirectory, report_file)
        payload_destination_path = '{}/{}'.format(destination_dir, report_file)
        try:
            shutil.copy(payload_source_path, payload_destination_path)
        except FileNotFoundError as error:
            LOG.error('Unable to find file in payload. %s', str(error))
            raise KafkaMsgHandlerError('Missing file in payload')
    LOG.info('Successfully extracted OCP for %s/%s', report_meta.get('cluster_id'), usage_month)
    # Remove temporary directory and files
    shutil.rmtree(temp_dir)
    return report_meta
Ejemplo n.º 4
0
 def _get_manifest(self, date_time):
     dates = utils.month_date_range(date_time)
     directory = '{}/{}/{}'.format(REPORTS_DIR, self.cluster_id, dates)
     LOG.info('Looking for manifest at %s', directory)
     report_meta = utils.get_report_details(directory)
     return report_meta
Ejemplo n.º 5
0
def extract_payload(url, request_id, context={}):  # noqa: C901
    """
    Extract OCP usage report payload into local directory structure.

    Payload is expected to be a .tar.gz file that contains:
    1. manifest.json - dictionary containing usage report details needed
        for report processing.
        Dictionary Contains:
            files - names of .csv usage reports for the manifest
            date - DateTime that the payload was created
            uuid - uuid for payload
            cluster_id  - OCP cluster ID.
    2. *.csv - Actual usage report for the cluster.  Format is:
        Format is: <uuid>_report_name.csv

    On successful completion the report and manifest will be in a directory
    structure that the OCPReportDownloader is expecting.

    Ex: /var/tmp/insights_local/my-ocp-cluster-1/20181001-20181101

    Once the files are extracted:
    1. Provider account is retrieved for the cluster id.  If no account is found we return.
    2. Manifest database record is created which will establish the assembly_id and number of files
    3. Report stats database record is created and is used as a filter to determine if the file
       has already been processed.
    4. All report files that have not been processed will have the local path to that report file
       added to the report_meta context dictionary for that file.
    5. Report file context dictionaries that require processing is added to a list which will be
       passed to the report processor.  All context from report_meta is used by the processor.

    Args:
        url (String): URL path to payload in the Insights upload service..
        request_id (String): Identifier associated with the payload
        context (Dict): Context for logging (account, etc)

    Returns:
        [dict]: keys: value
                files: [String],
                date: DateTime,
                cluster_id: String
                manifest_path: String,
                provider_uuid: String,
                provider_type: String
                schema_name: String
                manifest_id: Integer
                current_file: String

    """
    temp_dir, temp_file_path, temp_file = download_payload(request_id, url, context)
    manifest_path = extract_payload_contents(request_id, temp_dir, temp_file_path, temp_file, context)

    # Open manifest.json file and build the payload dictionary.
    full_manifest_path = f"{temp_dir}/{manifest_path[0]}"
    report_meta = utils.get_report_details(os.path.dirname(full_manifest_path))

    # Filter and get account from payload's cluster-id
    cluster_id = report_meta.get("cluster_id")
    if context:
        context["cluster_id"] = cluster_id
    account = get_account_from_cluster_id(cluster_id, request_id, context)
    if not account:
        msg = f"Recieved unexpected OCP report from {cluster_id}"
        LOG.error(log_json(request_id, msg, context))
        shutil.rmtree(temp_dir)
        return None
    schema_name = account.get("schema_name")
    provider_type = account.get("provider_type")
    context["account"] = schema_name[4:]
    context["provider_type"] = provider_type
    report_meta["provider_uuid"] = account.get("provider_uuid")
    report_meta["provider_type"] = provider_type
    report_meta["schema_name"] = schema_name
    report_meta["account"] = schema_name[4:]
    report_meta["request_id"] = request_id

    # Create directory tree for report.
    usage_month = utils.month_date_range(report_meta.get("date"))
    destination_dir = f"{Config.INSIGHTS_LOCAL_REPORT_DIR}/{report_meta.get('cluster_id')}/{usage_month}"
    os.makedirs(destination_dir, exist_ok=True)

    # Copy manifest
    manifest_destination_path = f"{destination_dir}/{os.path.basename(report_meta.get('manifest_path'))}"
    shutil.copy(report_meta.get("manifest_path"), manifest_destination_path)

    # Save Manifest
    report_meta["manifest_id"] = create_manifest_entries(report_meta, request_id, context)

    # Copy report payload
    report_metas = []
    for report_file in report_meta.get("files"):
        current_meta = report_meta.copy()
        subdirectory = os.path.dirname(full_manifest_path)
        payload_source_path = f"{subdirectory}/{report_file}"
        payload_destination_path = f"{destination_dir}/{report_file}"
        try:
            shutil.copy(payload_source_path, payload_destination_path)
            current_meta["current_file"] = payload_destination_path
            if not record_report_status(report_meta["manifest_id"], report_file, request_id, context):
                msg = f"Successfully extracted OCP for {report_meta.get('cluster_id')}/{usage_month}"
                LOG.info(log_json(request_id, msg, context))
                create_daily_archives(
                    request_id,
                    report_meta["account"],
                    report_meta["provider_uuid"],
                    report_file,
                    payload_destination_path,
                    report_meta["manifest_id"],
                    report_meta["date"],
                    context,
                )
                report_metas.append(current_meta)
            else:
                # Report already processed
                pass
        except FileNotFoundError:
            msg = f"File {str(report_file)} has not downloaded yet."
            LOG.debug(log_json(request_id, msg, context))

    # Remove temporary directory and files
    shutil.rmtree(temp_dir)
    return report_metas
Ejemplo n.º 6
0
def extract_payload(url):  # noqa: C901
    """
    Extract OCP usage report payload into local directory structure.

    Payload is expected to be a .tar.gz file that contains:
    1. manifest.json - dictionary containing usage report details needed
        for report processing.
        Dictionary Contains:
            files - names of .csv usage reports for the manifest
            date - DateTime that the payload was created
            uuid - uuid for payload
            cluster_id  - OCP cluster ID.
    2. *.csv - Actual usage report for the cluster.  Format is:
        Format is: <uuid>_report_name.csv

    On successful completion the report and manifest will be in a directory
    structure that the OCPReportDownloader is expecting.

    Ex: /var/tmp/insights_local/my-ocp-cluster-1/20181001-20181101

    Once the files are extracted:
    1. Provider account is retrieved for the cluster id.  If no account is found we return.
    2. Manifest database record is created which will establish the assembly_id and number of files
    3. Report stats database record is created and is used as a filter to determine if the file
       has already been processed.
    4. All report files that have not been processed will have the local path to that report file
       added to the report_meta context dictionary for that file.
    5. Report file context dictionaries that require processing is added to a list which will be
       passed to the report processor.  All context from report_meta is used by the processor.

    Args:
        url (String): URL path to payload in the Insights upload service..

    Returns:
        [dict]: keys: value
                files: [String],
                date: DateTime,
                cluster_id: String
                manifest_path: String,
                provider_uuid: String,
                provider_type: String
                schema_name: String
                manifest_id: Integer
                current_file: String

    """
    # Create temporary directory for initial file staging and verification in the
    # OpenShift PVC directory so that any failures can be triaged in the event
    # the pod goes down.
    os.makedirs(Config.PVC_DIR, exist_ok=True)
    temp_dir = tempfile.mkdtemp(dir=Config.PVC_DIR)

    # Download file from quarantine bucket as tar.gz
    try:
        download_response = requests.get(url)
        download_response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        shutil.rmtree(temp_dir)
        raise KafkaMsgHandlerError("Unable to download file. Error: ", str(err))

    temp_file = "{}/{}".format(temp_dir, "usage.tar.gz")
    try:
        temp_file_hdl = open("{}/{}".format(temp_dir, "usage.tar.gz"), "wb")
        temp_file_hdl.write(download_response.content)
        temp_file_hdl.close()
    except (OSError, IOError) as error:
        shutil.rmtree(temp_dir)
        raise KafkaMsgHandlerError("Unable to write file. Error: ", str(error))

    # Extract tarball into temp directory
    try:
        mytar = TarFile.open(temp_file)
        mytar.extractall(path=temp_dir)
        files = mytar.getnames()
        manifest_path = [manifest for manifest in files if "manifest.json" in manifest]
    except (ReadError, EOFError, OSError) as error:
        LOG.warning("Unable to untar file. Reason: %s", str(error))
        shutil.rmtree(temp_dir)
        raise KafkaMsgHandlerError("Extraction failure.")

    if not manifest_path:
        raise KafkaMsgHandlerError("No manifest found in payload.")
    # Open manifest.json file and build the payload dictionary.
    full_manifest_path = "{}/{}".format(temp_dir, manifest_path[0])
    report_meta = utils.get_report_details(os.path.dirname(full_manifest_path))

    # Filter and get account from payload's cluster-id
    account = get_account_from_cluster_id(report_meta.get("cluster_id"))
    if not account:
        LOG.error(f"Recieved unexpected OCP report from {report_meta.get('cluster_id')}")
        shutil.rmtree(temp_dir)
        return None

    report_meta["provider_uuid"] = account.get("provider_uuid")
    report_meta["provider_type"] = account.get("provider_type")
    report_meta["schema_name"] = account.get("schema_name")

    # Create directory tree for report.
    usage_month = utils.month_date_range(report_meta.get("date"))
    destination_dir = "{}/{}/{}".format(Config.INSIGHTS_LOCAL_REPORT_DIR, report_meta.get("cluster_id"), usage_month)
    os.makedirs(destination_dir, exist_ok=True)

    # Copy manifest
    manifest_destination_path = "{}/{}".format(destination_dir, os.path.basename(report_meta.get("manifest_path")))
    shutil.copy(report_meta.get("manifest_path"), manifest_destination_path)

    # Save Manifest
    report_meta["manifest_id"] = create_manifest_entries(report_meta)

    # Copy report payload
    report_metas = []
    for report_file in report_meta.get("files"):
        current_meta = report_meta.copy()
        subdirectory = os.path.dirname(full_manifest_path)
        payload_source_path = f"{subdirectory}/{report_file}"
        payload_destination_path = f"{destination_dir}/{report_file}"
        try:
            shutil.copy(payload_source_path, payload_destination_path)
            current_meta["current_file"] = payload_destination_path
            if not record_report_status(report_meta["manifest_id"], report_file):
                LOG.info("Successfully extracted OCP for %s/%s", report_meta.get("cluster_id"), usage_month)
                report_metas.append(current_meta)
            else:
                # Report already processed
                pass
        except FileNotFoundError:
            LOG.debug("File %s has not downloaded yet.", str(report_file))

    # Remove temporary directory and files
    shutil.rmtree(temp_dir)
    return report_metas
Ejemplo n.º 7
0
 def _get_manifest(self, date_time):
     dates = utils.month_date_range(date_time)
     directory = f"{REPORTS_DIR}/{self.cluster_id}/{dates}"
     LOG.info("Looking for manifest at %s", directory)
     report_meta = utils.get_report_details(directory)
     return report_meta