Ejemplo n.º 1
0
 def register(self, project, domain, name, version):
     """
     :param Text project:
     :param Text domain:
     :param Text name:
     :param Text version:
     """
     self.validate()
     id_to_register = _identifier.Identifier(
         _identifier_model.ResourceType.WORKFLOW, project, domain, name,
         version)
     old_id = self.id
     self._id = id_to_register
     try:
         client = _flyte_engine.get_client()
         sub_workflows = self.get_sub_workflows()
         client.create_workflow(
             id_to_register,
             _admin_workflow_model.WorkflowSpec(
                 self,
                 sub_workflows,
             ),
         )
         self._id = id_to_register
         self._has_registered = True
         return str(id_to_register)
     except _user_exceptions.FlyteEntityAlreadyExistsException:
         pass
     except Exception:
         self._id = old_id
         raise
Ejemplo n.º 2
0
 def register(self, identifier):
     client = _FlyteClientManager(
         _platform_config.URL.get(),
         insecure=_platform_config.INSECURE.get()).client
     try:
         return client.create_workflow(
             identifier, _workflow_model.WorkflowSpec(self.sdk_workflow))
     except _user_exceptions.FlyteEntityAlreadyExistsException:
         pass
Ejemplo n.º 3
0
    def serialize(self):
        """
        Serializing a workflow should produce an object similar to what the registration step produces, in preparation
        for actual registration to Admin.

        :rtype: flyteidl.admin.workflow_pb2.WorkflowSpec
        """
        sub_workflows = self.get_sub_workflows()
        return _admin_workflow_model.WorkflowSpec(
            self,
            sub_workflows,
        ).to_flyte_idl()
Ejemplo n.º 4
0
def get_serializable_workflow(
    entity_mapping: OrderedDict,
    settings: SerializationSettings,
    entity: WorkflowBase,
) -> admin_workflow_models.WorkflowSpec:
    # Get node models
    upstream_node_models = [
        get_serializable(entity_mapping, settings, n)
        for n in entity.nodes
        if n.id != _common_constants.GLOBAL_INPUT_NODE_ID
    ]

    sub_wfs = []
    for n in entity.nodes:
        if isinstance(n.flyte_entity, WorkflowBase):
            if isinstance(n.flyte_entity, ReferenceEntity):
                raise Exception(
                    f"Sorry, reference subworkflows do not work right now, please use the launch plan instead for the "
                    f"subworkflow you're trying to invoke. Node: {n}"
                )
            sub_wf_spec = get_serializable(entity_mapping, settings, n.flyte_entity)
            if not isinstance(sub_wf_spec, admin_workflow_models.WorkflowSpec):
                raise Exception(
                    f"Serialized form of a workflow should be an admin.WorkflowSpec but {type(sub_wf_spec)} found instead"
                )
            sub_wfs.append(sub_wf_spec.template)
            sub_wfs.extend(sub_wf_spec.sub_workflows)

        if isinstance(n.flyte_entity, BranchNode):
            if_else: workflow_model.IfElseBlock = n.flyte_entity._ifelse_block
            # See comment in get_serializable_branch_node also. Again this is a List[Node] even though it's supposed
            # to be a List[workflow_model.Node]
            leaf_nodes: List[Node] = filter(  # noqa
                None,
                [
                    if_else.case.then_node,
                    *([] if if_else.other is None else [x.then_node for x in if_else.other]),
                    if_else.else_node,
                ],
            )
            for leaf_node in leaf_nodes:
                if isinstance(leaf_node.flyte_entity, WorkflowBase):
                    sub_wf_spec = get_serializable(entity_mapping, settings, leaf_node.flyte_entity)
                    sub_wfs.append(sub_wf_spec.template)
                    sub_wfs.extend(sub_wf_spec.sub_workflows)

    wf_id = _identifier_model.Identifier(
        resource_type=_identifier_model.ResourceType.WORKFLOW,
        project=settings.project,
        domain=settings.domain,
        name=entity.name,
        version=settings.version,
    )
    wf_t = workflow_model.WorkflowTemplate(
        id=wf_id,
        metadata=entity.workflow_metadata.to_flyte_model(),
        metadata_defaults=entity.workflow_metadata_defaults.to_flyte_model(),
        interface=entity.interface,
        nodes=upstream_node_models,
        outputs=entity.output_bindings,
    )

    return admin_workflow_models.WorkflowSpec(template=wf_t, sub_workflows=list(set(sub_wfs)))
Ejemplo n.º 5
0
def get_serializable_workflow(
    entity_mapping: OrderedDict,
    settings: SerializationSettings,
    entity: WorkflowBase,
    options: Optional[Options] = None,
) -> admin_workflow_models.WorkflowSpec:
    # TODO: Try to move up following config refactor - https://github.com/flyteorg/flyte/issues/2214
    from flytekit.remote.workflow import FlyteWorkflow

    # Get node models
    upstream_node_models = [
        get_serializable(entity_mapping, settings, n, options)
        for n in entity.nodes if n.id != _common_constants.GLOBAL_INPUT_NODE_ID
    ]

    sub_wfs = []
    for n in entity.nodes:
        if isinstance(n.flyte_entity, WorkflowBase):
            # We are currently not supporting reference workflows since these will
            # require a network call to flyteadmin to populate the WorkflowTemplate
            # object
            if isinstance(n.flyte_entity, ReferenceWorkflow):
                raise Exception(
                    "Reference sub-workflows are currently unsupported. Use reference launch plans instead."
                )
            sub_wf_spec = get_serializable(entity_mapping, settings,
                                           n.flyte_entity, options)
            if not isinstance(sub_wf_spec, admin_workflow_models.WorkflowSpec):
                raise TypeError(
                    f"Unexpected type for serialized form of workflow. Expected {admin_workflow_models.WorkflowSpec}, but got {type(sub_wf_spec)}"
                )
            sub_wfs.append(sub_wf_spec.template)
            sub_wfs.extend(sub_wf_spec.sub_workflows)

        if isinstance(n.flyte_entity, FlyteWorkflow):
            get_serializable(entity_mapping, settings, n.flyte_entity, options)
            sub_wfs.append(n.flyte_entity)
            sub_wfs.extend([s for s in n.flyte_entity.sub_workflows.values()])

        if isinstance(n.flyte_entity, BranchNode):
            if_else: workflow_model.IfElseBlock = n.flyte_entity._ifelse_block
            # See comment in get_serializable_branch_node also. Again this is a List[Node] even though it's supposed
            # to be a List[workflow_model.Node]
            leaf_nodes: List[Node] = filter(  # noqa
                None,
                [
                    if_else.case.then_node,
                    *([] if if_else.other is None else
                      [x.then_node for x in if_else.other]),
                    if_else.else_node,
                ],
            )
            for leaf_node in leaf_nodes:
                if isinstance(leaf_node.flyte_entity, WorkflowBase):
                    sub_wf_spec = get_serializable(entity_mapping, settings,
                                                   leaf_node.flyte_entity,
                                                   options)
                    sub_wfs.append(sub_wf_spec.template)
                    sub_wfs.extend(sub_wf_spec.sub_workflows)
                elif isinstance(leaf_node.flyte_entity, FlyteWorkflow):
                    get_serializable(entity_mapping, settings,
                                     leaf_node.flyte_entity, options)
                    sub_wfs.append(leaf_node.flyte_entity)
                    sub_wfs.extend([
                        s
                        for s in leaf_node.flyte_entity.sub_workflows.values()
                    ])

    wf_id = _identifier_model.Identifier(
        resource_type=_identifier_model.ResourceType.WORKFLOW,
        project=settings.project,
        domain=settings.domain,
        name=entity.name,
        version=settings.version,
    )
    wf_t = workflow_model.WorkflowTemplate(
        id=wf_id,
        metadata=entity.workflow_metadata.to_flyte_model(),
        metadata_defaults=entity.workflow_metadata_defaults.to_flyte_model(),
        interface=entity.interface,
        nodes=upstream_node_models,
        outputs=entity.output_bindings,
    )
    return admin_workflow_models.WorkflowSpec(
        template=wf_t,
        sub_workflows=sorted(set(sub_wfs), key=lambda x: x.short_string()))