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 update(storage_old: Storage, workdir_old: str, storage_new: Storage, workdir_new: str, instance_comparer: InstanceComparer, instance_diff: Diff, verbose_mode: bool, num_workers: int, overwrite: bool): template_old = get_template(storage_old) template_new = get_template(storage_new) topology_old = template_old.instantiate(storage_old) topology_new = template_new.instantiate(storage_new) if verbose_mode: print(format_outputs(instance_diff.outputs(), "json")) instance_comparer.prepare_update(topology_old, topology_new, instance_diff) topology_old.undeploy(verbose_mode, workdir_old, num_workers) topology_old.write_all() if overwrite: # swap storage topology_new.set_storage(storage_old) # rewrite inputs and root file storage_old.write_json(storage_new.read_json("inputs"), "inputs") storage_old.write(storage_new.read("root_file"), "root_file") topology_new.write_all() topology_new.deploy(verbose_mode, workdir_new, num_workers)
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 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(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 outputs(args): if args.instance_path and not path.isdir(args.instance_path): raise argparse.ArgumentTypeError( "Directory {0} is not a valid path!".format(args.instance_path)) storage = Storage(Path(args.instance_path).joinpath( ".opera")) if args.instance_path else Storage(Path(".opera")) root = storage.read("root_file") inputs = storage.read_json("inputs") try: ast = tosca.load(Path.cwd(), PurePath(root)) template = ast.get_template(inputs) topology = template.instantiate(storage) # We need to instantiate the template in order to get access to the # instance state. print(format_outputs(template.get_outputs(), args.format)) except ParseError as e: print("{}: {}".format(e.loc, e)) return 1 except DataError as e: print(str(e)) return 1 return 0
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 undeploy(args): storage = Storage(Path(".opera")) root = storage.read("root_file") inputs = storage.read_json("inputs") try: ast = tosca.load(Path.cwd(), PurePath(root)) template = ast.get_template(inputs) topology = template.instantiate(storage) topology.undeploy() except ParseError as e: print("{}: {}".format(e.loc, e)) return 1 except DataError as e: print(str(e)) return 1 return 0
def outputs(args): storage = Storage(Path(".opera")) root = storage.read("root_file") inputs = storage.read_json("inputs") try: ast = tosca.load(Path.cwd(), PurePath(root)) template = ast.get_template(inputs) topology = template.instantiate(storage) # We need to instantiate the template in order to get access to the # instance state. print(format_outputs(template.get_outputs(), args.format)) except ParseError as e: print("{}: {}".format(e.loc, e)) return 1 except DataError as e: print(str(e)) return 1 return 0
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