示例#1
0
def init_compressed_csar(csar_name: str, inputs: typing.Optional[dict], storage: Storage, clean_storage: bool):
    if storage.exists("root_file"):
        if clean_storage:
            storage.remove_all()
        else:
            print("Looks like service template or CSAR has already been initialized. "
                  "Use the --clean/-c flag to clear the storage.")
            return

    if inputs is None:
        inputs = {}
    storage.write_json(inputs, "inputs")

    csars_dir = Path(storage.path) / "csars"
    csars_dir.mkdir(exist_ok=True)

    csar = CloudServiceArchive.create(PurePath(csar_name))
    csar.validate_csar()
    tosca_service_template = csar.get_entrypoint()

    # unzip csar and save the path to storage
    csar_dir = csars_dir / Path("csar")
    ZipFile(csar_name, "r").extractall(csar_dir)
    csar_tosca_service_template_path = csar_dir / tosca_service_template
    storage.write(str(csar_tosca_service_template_path), "root_file")

    # try to initiate service template from csar
    ast = tosca.load(Path(csar_dir), Path(tosca_service_template))
    template = ast.get_template(inputs)
    template.instantiate(storage)
示例#2
0
def package(input_dir: PurePath, csar_output: str,
            service_template_path: Optional[PurePath],
            csar_format: str) -> str:
    csar = CloudServiceArchive.create(input_dir)
    result: str = csar.package_csar(csar_output, service_template_path,
                                    csar_format)
    return result
示例#3
0
def deploy_compressed_csar(csar_path: PurePath, inputs: typing.Optional[dict],
                           storage: Storage, verbose_mode: bool,
                           num_workers: int, delete_existing_state: bool):
    if delete_existing_state:
        storage.remove("instances")

    if inputs is None:
        inputs = {}
    storage.write_json(inputs, "inputs")

    csars_dir = Path(storage.path) / "csars"
    csars_dir.mkdir(exist_ok=True)

    csar = CloudServiceArchive.create(csar_path)
    csar.validate_csar()
    tosca_service_template = csar.get_entrypoint()

    # unzip csar, save the path to storage and set workdir
    csar_dir = csars_dir / Path("csar")
    ZipFile(csar_path, "r").extractall(csar_dir)  # pylint: disable=consider-using-with
    csar_tosca_service_template_path = csar_dir / tosca_service_template
    storage.write(str(csar_tosca_service_template_path), "root_file")
    workdir = str(csar_dir)

    # initialize service template from CSAR and deploy
    ast = tosca.load(Path(csar_dir), Path(tosca_service_template))
    template = ast.get_template(inputs)
    topology = template.instantiate(storage)
    topology.deploy(verbose_mode, workdir, num_workers)
示例#4
0
def validate_csar(csar_path: PurePath, inputs: typing.Optional[dict],
                  storage: Storage, verbose: bool, executors: bool):
    if inputs is None:
        inputs = {}

    csar = CloudServiceArchive.create(csar_path)
    csar.validate_csar()
    entrypoint = csar.get_entrypoint()

    if entrypoint is not None:
        if isinstance(csar, DirCloudServiceArchive):
            workdir = Path(csar_path)
            ast = tosca.load(workdir, entrypoint)
            template = ast.get_template(inputs)
            if executors:
                topology = template.instantiate(storage)
                topology.validate(verbose, workdir, 1)
        else:
            with TemporaryDirectory() as csar_validation_dir:
                csar.unpackage_csar(csar_validation_dir)
                workdir = Path(csar_validation_dir)
                ast = tosca.load(workdir, entrypoint)
                template = ast.get_template(inputs)
                if executors:
                    topology = template.instantiate(storage)
                    topology.validate(verbose, csar_validation_dir, 1)
示例#5
0
def initialize_compressed_csar(csar_name: str, inputs: typing.Optional[dict],
                               storage: Storage):
    if inputs is None:
        inputs = {}
    storage.write_json(inputs, "inputs")

    csars_dir = Path(storage.path) / "csars"
    csars_dir.mkdir(exist_ok=True)

    # validate csar
    csar = CloudServiceArchive(csar_name, csars_dir)
    tosca_service_template = csar.validate_csar()

    # unzip csar and save the path to storage
    csar_dir = csars_dir / Path("csar")
    ZipFile(csar_name, 'r').extractall(csar_dir)
    csar_tosca_service_template_path = csar_dir / tosca_service_template
    storage.write(str(csar_tosca_service_template_path), "root_file")

    # try to initiate service template from csar
    ast = tosca.load(Path(csar_dir), Path(tosca_service_template))
    template = ast.get_template(inputs)
    template.instantiate(storage)
示例#6
0
def validate_compressed_csar(csar_name: str, inputs: typing.Optional[dict]):
    if inputs is None:
        inputs = {}

    with TemporaryDirectory() as csar_validation_dir:
        csar = CloudServiceArchive.create(PurePath(csar_name))
        csar.validate_csar()
        tosca_service_template = csar.get_entrypoint()

        # unzip csar to temporary folder
        ZipFile(csar_name, "r").extractall(csar_validation_dir)

        # try to initiate service template from csar
        ast = tosca.load(Path(csar_validation_dir), Path(tosca_service_template))
        ast.get_template(inputs)
示例#7
0
def info(csar_or_rootdir: Optional[PurePath], storage: Storage) -> dict:
    info_dict: Dict[str, Optional[str]] = dict(service_template=None,
                                               content_root=None,
                                               inputs=None,
                                               status=None)

    # stateless autodetect first if possible,
    # which can then be overwritten via state
    if csar_or_rootdir is not None:
        csar = CloudServiceArchive.create(csar_or_rootdir)
        try:
            csar.validate_csar()
            info_dict["content_root"] = str(csar_or_rootdir)

            meta = csar.parse_csar_meta()
            if meta is not None:
                info_dict["service_template"] = meta.entry_definitions
        except OperaError:
            pass

    if storage.exists("root_file"):
        service_template = storage.read("root_file")
        info_dict["service_template"] = service_template

        if storage.exists("inputs"):
            info_dict["inputs"] = str(storage.path / "inputs")
            inputs = yaml.safe_load(storage.read("inputs"))
        else:
            inputs = {}

        if storage.exists("csars/csar"):
            csar_dir = Path(storage.path) / "csars" / "csar"
            info_dict["content_root"] = str(csar_dir)
            ast = tosca.load(Path(csar_dir),
                             PurePath(service_template).relative_to(csar_dir))
        else:
            ast = tosca.load(Path.cwd(), PurePath(service_template))

        if storage.exists("instances"):
            template = ast.get_template(inputs)
            # We need to instantiate the template in order
            # to get access to the instance state.
            topology = template.instantiate(storage)
            info_dict["status"] = topology.get_info()
        else:
            info_dict["status"] = "initialized"

    return info_dict
示例#8
0
def info(csar_or_rootdir: Optional[PurePath], storage: Storage) -> dict:  # pylint: disable=too-many-statements
    info_dict: Dict[str, Optional[Union[str, dict, bool]]] = dict(
        service_template=None,
        content_root=None,
        inputs=None,
        status=None,
        csar_metadata=None,
        service_template_metadata=None,
        csar_valid=None)

    # stateless autodetect first if possible,
    # which can then be overwritten via state
    if csar_or_rootdir is not None:
        csar = CloudServiceArchive.create(csar_or_rootdir)
        try:
            # this validates CSAR and the entrypoint's (if it exists) metadata
            # failure to validate here means no static information will be available at all
            csar.validate_csar()
            info_dict["csar_valid"] = True

            info_dict["content_root"] = str(csar_or_rootdir)
            if csar.get_entrypoint() is not None:
                info_dict["service_template"] = str(csar.get_entrypoint())

                try:
                    service_template_meta = csar.parse_service_template_meta(
                        csar.get_entrypoint())
                    if service_template_meta:
                        info_dict[
                            "service_template_metadata"] = service_template_meta.to_dict(
                            )
                except OperaError:
                    pass

            try:
                csar_meta = csar.parse_csar_meta()
                if csar_meta:
                    info_dict["csar_metadata"] = csar_meta.to_dict()
            except OperaError:
                pass
        except OperaError:
            # anything that fails because of validation can be ignored, we can use state
            # we mark the CSAR as invalid because it's useful to know
            info_dict["csar_valid"] = False

    # detection from state, overrides stateless
    if storage.exists("root_file"):
        service_template_path = PurePath(storage.read("root_file"))
        info_dict["service_template"] = str(service_template_path)

        if storage.exists("inputs"):
            inputs = yaml.safe_load(storage.read("inputs"))
        else:
            inputs = {}
        info_dict["inputs"] = inputs

        if storage.exists("csars/csar"):
            csar_dir = Path(storage.path) / "csars" / "csar"
            info_dict["content_root"] = str(csar_dir)
            ast = tosca.load(Path(csar_dir),
                             service_template_path.relative_to(csar_dir))

            try:
                csar = CloudServiceArchive.create(csar_dir)
                csar.validate_csar()
                info_dict["csar_valid"] = True

                try:
                    service_template_meta = csar.parse_service_template_meta(
                        csar.get_entrypoint())
                    if service_template_meta:
                        info_dict[
                            "service_template_metadata"] = service_template_meta.to_dict(
                            )
                except OperaError:
                    pass

                try:
                    csar_meta = csar.parse_csar_meta()
                    if csar_meta:
                        info_dict["csar_metadata"] = csar_meta.to_dict()
                except OperaError:
                    pass

            except OperaError:
                info_dict["csar_valid"] = False
        else:
            ast = tosca.load(Path(service_template_path.parent),
                             PurePath(service_template_path.name))

        if storage.exists("instances"):
            template = ast.get_template(inputs)
            # We need to instantiate the template in order
            # to get access to the instance state.
            topology = template.instantiate(storage)
            info_dict["status"] = topology.status()
        else:
            info_dict["status"] = "initialized"

    return info_dict
示例#9
0
def unpackage(csar_input: str, output_dir: str):
    csar = CloudServiceArchive.create(PurePath(csar_input))
    # validate CSAR before unpacking it
    csar.validate_csar()
    # unpack the CSAR to the specified location
    csar.unpackage_csar(output_dir)
示例#10
0
def package(input_dir: str, csar_output: str, service_template: str, csar_format: str) -> str:
    csar = CloudServiceArchive.create(PurePath(input_dir))
    result: str = csar.package_csar(csar_output, service_template, csar_format)
    return result