예제 #1
0
def decompile_marketplace_bp(name, version, app_source, bp_name, project,
                             with_secrets, bp_dir):
    """decompiles marketplace blueprint"""

    if not version:
        LOG.info("Fetching latest version of Marketplace Blueprint {} ".format(
            name))
        version = get_mpi_latest_version(name=name, app_source=app_source)
        LOG.info(version)

    LOG.info("Converting MPI into blueprint")
    bp_payload = convert_mpi_into_blueprint(name=name,
                                            version=version,
                                            project_name=project,
                                            app_source=app_source)
    del bp_payload["status"]

    client = get_api_client()
    blueprint_uuid = bp_payload["metadata"]["uuid"]
    res, err = client.blueprint.export_file(blueprint_uuid)
    if err:
        LOG.error("[{}] - {}".format(err["code"], err["error"]))
        sys.exit(-1)

    bp_payload = res.json()
    blueprint = bp_payload["spec"]["resources"]
    blueprint_name = get_valid_identifier(bp_name or name)

    if not bp_dir:
        bp_dir_suffix = bp_name or "mpi_bp_{}_v{}".format(
            blueprint_name, version)
        bp_dir = os.path.join(os.getcwd(), bp_dir_suffix)

    blueprint_description = bp_payload["spec"].get("description", "")
    LOG.info("Decompiling marketplace blueprint {}".format(name))
    for sub_obj in blueprint.get("substrate_definition_list"):
        sub_type = sub_obj.get("type", "") or "AHV_VM"
        if sub_type == "K8S_POD":
            raise NotImplementedError(
                "Decompilation for k8s pod is not supported right now")
        elif sub_type != "AHV_VM":
            LOG.warning(
                "Decompilation support for providers other than AHV is experimental."
            )
            break

    bp_cls = BlueprintType.decompile(blueprint)
    bp_cls.__name__ = blueprint_name
    bp_cls.__doc__ = blueprint_description

    create_bp_dir(bp_cls=bp_cls, bp_dir=bp_dir, with_secrets=with_secrets)
    click.echo(
        "\nSuccessfully decompiled. Directory location: {}. Blueprint location: {}"
        .format(get_bp_dir(), os.path.join(get_bp_dir(), "blueprint.py")))
예제 #2
0
파일: bps.py 프로젝트: tuxtof/calm-dsl
def _decompile_bp(bp_payload, with_secrets=False, prefix="", bp_dir=None):
    """decompiles the blueprint from payload"""

    blueprint = bp_payload["spec"]["resources"]
    blueprint_name = bp_payload["spec"].get("name", "DslBlueprint")
    blueprint_description = bp_payload["spec"].get("description", "")

    blueprint_metadata = bp_payload["metadata"]

    # POP unnecessary keys
    blueprint_metadata.pop("creation_time", None)
    blueprint_metadata.pop("last_update_time", None)

    metadata_obj = MetadataType.decompile(blueprint_metadata)

    # Copying dsl_name_map to global client_attrs
    if bp_payload["spec"]["resources"]["client_attrs"].get("None", {}):
        init_dsl_metadata_map(bp_payload["spec"]["resources"]["client_attrs"]["None"])

    LOG.info("Decompiling blueprint {}".format(blueprint_name))

    for sub_obj in blueprint.get("substrate_definition_list"):
        sub_type = sub_obj.get("type", "") or "AHV_VM"
        if sub_type == "K8S_POD":
            raise NotImplementedError(
                "Decompilation for k8s pod is not supported right now"
            )
        elif sub_type != "AHV_VM":
            LOG.warning(
                "Decompilation support for providers other than AHV is experimental."
            )
            break

    prefix = get_valid_identifier(prefix)
    bp_cls = BlueprintType.decompile(blueprint, prefix=prefix)
    bp_cls.__name__ = get_valid_identifier(blueprint_name)
    bp_cls.__doc__ = blueprint_description

    create_bp_dir(
        bp_cls=bp_cls,
        with_secrets=with_secrets,
        metadata_obj=metadata_obj,
        bp_dir=bp_dir,
    )
    click.echo(
        "\nSuccessfully decompiled. Directory location: {}. Blueprint location: {}".format(
            get_bp_dir(), os.path.join(get_bp_dir(), "blueprint.py")
        )
    )
예제 #3
0
파일: bps.py 프로젝트: jkntnx/calm-dsl
def _decompile_bp(bp_payload, with_secrets=False):
    """decompiles the blueprint from payload"""

    blueprint = bp_payload["spec"]["resources"]
    blueprint_name = bp_payload["spec"].get("name", "DslBlueprint")
    blueprint_description = bp_payload["spec"].get("description", "")

    # Copying dsl_name_map to global client_attrs
    if bp_payload["spec"]["resources"]["client_attrs"].get("None", {}):
        init_dsl_metadata_map(bp_payload["spec"]["resources"]["client_attrs"]["None"])

    LOG.info("Decompiling blueprint {}".format(blueprint_name))

    for sub_obj in blueprint.get("substrate_definition_list"):
        sub_type = sub_obj.get("type", "") or "AHV_VM"
        if sub_type == "K8S_POD":
            raise NotImplementedError(
                "Decompilation for k8s pod is not supported right now"
            )
        elif sub_type != "AHV_VM":
            LOG.warning(
                "Decompilation support for providers other than AHV is experimental/best effort"
            )
            break

    bp_cls = BlueprintType.decompile(blueprint)
    bp_cls.__name__ = get_valid_identifier(blueprint_name)
    bp_cls.__doc__ = blueprint_description

    create_bp_dir(bp_cls=bp_cls, with_secrets=with_secrets)
    click.echo("\nSuccessfully decompiled. Directory location: {}".format(get_bp_dir()))
예제 #4
0
    def test_bp_payload_roundtrip(self):
        """Tests whether compiled payload of decompiled blueprint is same as normal blueprint"""

        runner = CliRunner()

        bp_file_location = os.path.join(os.getcwd(), BP_FILE)
        LOG.info("Compiling Blueprint file at {}".format(bp_file_location))
        result = runner.invoke(
            cli, ["-vv", "compile", "bp", "-f", bp_file_location])
        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))
        bp_json = json.loads(result.output)

        # Fetch project used for compilation
        project_name = bp_json["metadata"]["project_reference"].get("name")

        # Creating BP
        bp_name = "Test_BP_{}".format(str(uuid.uuid4()))
        LOG.info("Creating Blueprint '{}' from file at {}".format(
            bp_name, bp_file_location))
        result = runner.invoke(
            cli, ["create", "bp", "-f", bp_file_location, "-n", bp_name])
        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))

        # Update project in context to be used for decompilation
        ContextObj = get_context()
        ContextObj.update_project_context(project_name=project_name)

        self.created_bp_list.append(bp_name)
        # Decompiling the created bp and storing secrets in file
        LOG.info("Decompiling Blueprint {}".format(bp_name))
        cli_inputs = [CRED_PASSWORD, CRED_PASSWORD, CRED_PASSWORD]
        result = runner.invoke(
            cli,
            ["decompile", "bp", bp_name, "--with_secrets"],
            input="\n".join(cli_inputs),
        )

        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))

        self.bp_dir_list.append(get_bp_dir())
        # TODO add interface check tests
        decompiled_bp_file_location = os.path.join(get_bp_dir(),
                                                   "blueprint.py")
        LOG.info("Compiling decompiled blueprint file at {}".format(
            decompiled_bp_file_location))
        result = runner.invoke(
            cli, ["-vv", "compile", "bp", "-f", decompiled_bp_file_location])
        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))

        decompiled_bp_json = json.loads(result.output)
        # We don't have paramter to pass name of blueprint during compile right now
        # So compare everythin else other than blueprint name
        decompiled_bp_json["metadata"]["name"] = bp_json["metadata"]["name"]
        decompiled_bp_json["spec"]["name"] = bp_json["spec"]["name"]

        LOG.info("Comparing original and decompiled blueprint json")
        assert bp_json == decompiled_bp_json
        LOG.info("Success")

        # Deleting old bp directory
        shutil.rmtree(self.bp_dir_list.pop())

        # Resetting context for decompiling
        init_decompile_context()

        self._test_decompile_with_prefix(bp_name)
예제 #5
0
    def _test_decompile_with_prefix(self, bp_name):

        runner = CliRunner()

        # Decompiling the created bp and storing secrets in file with prefix
        prefix = get_valid_identifier("_{}".format(str(uuid.uuid4())[:10]))
        LOG.info(
            "Decompiling Blueprint {} with prefix flag set to '{}'".format(
                bp_name, prefix))

        cli_inputs = [CRED_PASSWORD, CRED_PASSWORD, CRED_PASSWORD]
        result = runner.invoke(
            cli,
            ["decompile", "bp", bp_name, "--with_secrets", "-p", prefix],
            input="\n".join(cli_inputs),
        )

        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))

        self.bp_dir_list.append(get_bp_dir())
        decompiled_bp_file_location = os.path.join(get_bp_dir(),
                                                   "blueprint.py")

        user_bp_module = get_blueprint_module_from_file(
            decompiled_bp_file_location)
        UserBlueprint = get_blueprint_class_from_module(user_bp_module)

        LOG.info("Asserting prefix in the entity names")
        for svc in UserBlueprint.services:
            assert svc.__name__.startswith(prefix)

        for pkg in UserBlueprint.packages:
            assert pkg.__name__.startswith(prefix)

        for sub in UserBlueprint.substrates:
            assert sub.__name__.startswith(prefix)
            assert sub.provider_spec.__name__.startswith(prefix)
            assert sub.provider_spec.resources.__name__.startswith(prefix)

        for pfl in UserBlueprint.profiles:
            assert pfl.__name__.startswith(prefix)

            for dep in pfl.deployments:
                assert dep.__name__.startswith(prefix)

        LOG.info("Success")

        # Resetting context for decompiling
        init_decompile_context

        # On applying prefix, name of dsl classes will be changed but UI name will be preserved
        # So compiled payload should be same

        bp_file_location = os.path.join(os.getcwd(), BP_FILE)
        LOG.info(
            "Compiling original blueprint file at {}".format(bp_file_location))
        result = runner.invoke(
            cli, ["-vv", "compile", "bp", "-f", bp_file_location])
        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))
        bp_json = json.loads(result.output)

        LOG.info(
            "Compiling decompiled blueprint file having entity names with prefix at {}"
            .format(decompiled_bp_file_location))
        result = runner.invoke(
            cli, ["-vv", "compile", "bp", "-f", decompiled_bp_file_location])
        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))

        decompiled_bp_json = json.loads(result.output)
        # We don't have paramter to pass name of blueprint during compile right now
        # So compare everythin else other than blueprint name
        decompiled_bp_json["metadata"]["name"] = bp_json["metadata"]["name"]
        decompiled_bp_json["spec"]["name"] = bp_json["spec"]["name"]

        # POP out the client attrs, as they will be changed due to prefix
        bp_json["spec"]["resources"].pop("client_attrs", {})
        decompiled_bp_json["spec"]["resources"].pop("client_attrs", {})

        LOG.info("Comparing original and decompiled blueprint json")
        assert bp_json == decompiled_bp_json
        LOG.info("Success")

        # Reseting context
        ContextObj = get_context()
        ContextObj.reset_configuration()
예제 #6
0
    def test_bp_payload_roundtrip(self):
        """Tests whether compiled payload of decompiled blueprint is same as normal blueprint"""

        runner = CliRunner()

        bp_file_location = os.path.join(os.getcwd(), BP_FILE)
        LOG.info("Creating Blueprint at {}".format(bp_file_location))
        result = runner.invoke(
            cli, ["-vvvvv", "compile", "bp", "-f", bp_file_location])
        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))
        bp_json = json.loads(result.output)

        # Creating BP
        bp_name = "Test_BP_{}".format(str(uuid.uuid4()))
        result = runner.invoke(
            cli, ["create", "bp", "-f", bp_file_location, "-n", bp_name])
        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))

        self.created_bp_list.append(bp_name)
        # Decompiling the cerated bp and storing secrets in file
        LOG.info("Decompiling Blueprint {}".format(bp_name))
        input = [CRED_PASSWORD]
        result = runner.invoke(cli,
                               ["decompile", "bp", bp_name, "--with_secrets"],
                               input="\n".join(input))

        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))

        self.bp_dir_list.append(get_bp_dir())
        # TODO add interface check tests
        decompiled_bp_file_location = os.path.join(get_bp_dir(),
                                                   "blueprint.py")
        LOG.info(
            "Compiling Blueprint at {}".format(decompiled_bp_file_location))
        result = runner.invoke(
            cli,
            ["-vvvvv", "compile", "bp", "-f", decompiled_bp_file_location])
        if result.exit_code:
            cli_res_dict = {
                "Output": result.output,
                "Exception": str(result.exception)
            }
            LOG.debug("Cli Response: {}".format(
                json.dumps(cli_res_dict, indent=4, separators=(",", ": "))))
            LOG.debug("Traceback: \n{}".format("".join(
                traceback.format_tb(result.exc_info[2]))))

        decompiled_bp_json = json.loads(result.output)
        # We don't have paramter to pass name of blueprint during compile right now
        # So compare everythin else other than blueprint name
        decompiled_bp_json["metadata"]["name"] = bp_json["metadata"]["name"]
        decompiled_bp_json["spec"]["name"] = bp_json["spec"]["name"]

        assert bp_json == decompiled_bp_json