示例#1
0
def recreate_workflow(workflow_json):
    """Recreates a workflow from a JSON to prepare for it to be executed.

    Args:
        workflow_json (JSON dict): The input workflow JSON, with some other fields as well.

    Returns:
        (Workflow object, start_arguments): A tuple containing the reconstructed Workflow object, and the arguments to
            the start action.
    """
    uid = workflow_json['uid']
    del workflow_json['uid']
    execution_uid = workflow_json['execution_uid']
    del workflow_json['execution_uid']
    start = workflow_json['start']

    start_arguments = {}
    if 'start_arguments' in workflow_json:
        start_arguments = [Argument(**arg) for arg in workflow_json['start_arguments']]
        workflow_json.pop("start_arguments")

    workflow = Workflow.create(workflow_json)
    workflow.uid = uid
    workflow.set_execution_uid(execution_uid)
    workflow.start = start

    return workflow, start_arguments
示例#2
0
    def load_workflow(resource, workflow_name):
        """Loads a workflow from a file.

        Args:
            resource (str): Path to the workflow.
            workflow_name (str): Name of the workflow to load.

        Returns:
            True on success, False otherwise.
        """
        try:
            playbook_file = open(resource, 'r')
        except (IOError, OSError) as e:
            logger.error(
                'Could not load workflow from {0}. Reason: {1}'.format(
                    resource, format_exception_message(e)))
            return None
        else:
            with playbook_file:
                workflow_loaded = playbook_file.read()
                try:
                    playbook_json = json.loads(workflow_loaded)
                    playbook_name = playbook_json['name']
                    workflow_json = next(
                        (workflow for workflow in playbook_json['workflows']
                         if workflow['name'] == workflow_name), None)
                    if workflow_json is None:
                        logger.warning(
                            'Workflow {0} not found in playbook {0}. '
                            'Cannot load.'.format(workflow_name,
                                                  playbook_name))
                        return None
                    workflow = Workflow.create(workflow_json)
                    return playbook_name, workflow
                except ValueError as e:
                    logger.exception('Cannot parse {0}. Reason: {1}'.format(
                        resource, format_exception_message(e)))
                except (InvalidArgument, UnknownApp, UnknownAppAction,
                        UnknownTransform, UnknownCondition) as e:
                    logger.error(
                        'Error constructing workflow {0}. Reason: {1}'.format(
                            workflow_name, format_exception_message(e)))
                    return None
                except KeyError as e:
                    logger.error(
                        'Invalid Playbook JSON format. Details: {}'.format(e))
                    return None