def undeploy(storage: Storage, verbose_mode: bool, num_workers: int): """ Undeploy a deployment. :raises ParseError: :raises DataError: """ if storage.exists("inputs"): inputs = storage.read_json("inputs") else: inputs = {} if storage.exists("root_file"): service_template = storage.read("root_file") workdir = str(Path.cwd()) if storage.exists("csars"): csar_dir = Path(storage.path) / "csars" / "csar" workdir = 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)) template = ast.get_template(inputs) topology = template.instantiate(storage) topology.undeploy(verbose_mode, workdir, num_workers) else: print("There is no root_file in storage.")
def deploy(service_template: str, inputs: typing.Optional[dict], storage: Storage, verbose_mode: bool, num_workers: int, delete_existing_state: bool): """ :raises ParseError: :raises DataError: """ if delete_existing_state: storage.remove("instances") if inputs is None: if storage.exists("inputs"): inputs = yaml.safe_load(storage.read("inputs")) else: inputs = {} storage.write_json(inputs, "inputs") storage.write(service_template, "root_file") workdir = str(Path.cwd()) if storage.exists("csars"): csar_dir = Path(storage.path) / "csars" / "csar" workdir = 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)) template = ast.get_template(inputs) topology = template.instantiate(storage) topology.deploy(verbose_mode, workdir, num_workers)
def deploy_service_template(service_template_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: if storage.exists("inputs"): inputs = yaml.safe_load(storage.read("inputs")) else: inputs = {} storage.write_json(inputs, "inputs") storage.write(str(service_template_path), "root_file") # set workdir and check if service template/CSAR has been initialized workdir = Path(service_template_path.parent) if storage.exists("csars"): csar_dir = Path(storage.path) / "csars" / "csar" workdir = csar_dir ast = tosca.load(workdir, service_template_path.relative_to(csar_dir)) else: ast = tosca.load(workdir, PurePath(service_template_path.name)) # initialize service template and deploy template = ast.get_template(inputs) topology = template.instantiate(storage) topology.deploy(verbose_mode, workdir, num_workers)
def outputs(storage: Storage) -> dict: """ Get deployment outputs. :raises ParseError: :raises DataError: """ if storage.exists("inputs"): inputs = storage.read_json("inputs") else: inputs = {} if storage.exists("root_file"): service_template_path = PurePath(storage.read("root_file")) if storage.exists("csars"): csar_dir = Path(storage.path) / "csars" / "csar" ast = tosca.load(Path(csar_dir), service_template_path.relative_to(csar_dir)) else: ast = tosca.load(Path(service_template_path.parent), PurePath(service_template_path.name)) template = ast.get_template(inputs) # We need to instantiate the template in order # to get access to the instance state. template.instantiate(storage) result: Dict = template.get_outputs() return result else: print("There is no root_file in storage.") return {}
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
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)
def notify(storage: Storage, verbose_mode: bool, trigger_name_or_event: str, notification_file_contents: typing.Optional[str]): if storage.exists("inputs"): inputs = yaml.safe_load(storage.read("inputs")) else: inputs = {} if storage.exists("root_file"): service_template = storage.read("root_file") workdir = str(Path.cwd()) if storage.exists("csars"): csar_dir = Path(storage.path) / "csars" / "csar" workdir = 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)) template = ast.get_template(inputs) # check if specified trigger or event name exists in template if trigger_name_or_event: trigger_name_or_event_exists = False for policy in template.policies: for trigger in policy.triggers.values(): if trigger_name_or_event in (trigger.name, trigger.event.data): trigger_name_or_event_exists = True break if not trigger_name_or_event_exists: raise DataError( "The provided trigger or event name does not exist: {}.". format(trigger_name_or_event)) topology = template.instantiate(storage) topology.notify(verbose_mode, workdir, trigger_name_or_event, notification_file_contents) else: print("There is no root_file in storage.")
def info(storage: Storage) -> dict: """ :raises ParseError: :raises DataError: """ info_dict = dict(service_template=None, content_root=None, inputs=None, status=None) 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
def init_service_template(service_template: 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 --clean/-c flag to clear the storage.") return if inputs is None: inputs = {} storage.write_json(inputs, "inputs") storage.write(service_template, "root_file") ast = tosca.load(Path.cwd(), PurePath(service_template)) template = ast.get_template(inputs) template.instantiate(storage)
def outputs(storage: Storage) -> dict: """ :raises ParseError: :raises DataError: """ service_template = storage.read("root_file") inputs = storage.read_json("inputs") if storage.exists("csars"): csar_dir = Path(storage.path) / "csars" / "csar" ast = tosca.load(Path(csar_dir), PurePath(service_template).relative_to(csar_dir)) else: ast = tosca.load(Path.cwd(), PurePath(service_template)) template = ast.get_template(inputs) # We need to instantiate the template in order # to get access to the instance state. template.instantiate(storage) return template.get_outputs()
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