Пример #1
0
def create_workflow_from_json(
    name,
    access_token,
    workflow_json=None,
    workflow_file=None,
    parameters=None,
    workflow_engine="yadage",
    outputs=None,
):
    """Create a workflow from json specification.

    :param name: name or UUID of the workflow to be started.
    :param access_token: access token of the current user.
    :param workflow_json: workflow specification in json format.
    :param workflow_file: workflow specification file path.
                          Ignores ``workflow_json`` if provided.
    :param parameters: workflow input parameters dictionary.
    :param workflow_engine: one of the workflow engines (yadage, serial, cwl)
    :param outputs: dictionary with expected workflow outputs.

    :Example:

      .. code:: python

        create_workflow_from_json(
            workflow_json=workflow_json,
            name='workflow_name.1',
            access_token='access_token',
            parameters={'files': ['file.txt'], 'parameters': {'key': 'value'}},
            workflow_engine='serial')
    """
    validate_workflow_name(name)
    if is_uuid_v4(name):
        raise ValueError("Workflow name cannot be a valid UUIDv4")
    if not access_token:
        raise Exception(ERROR_MESSAGES["missing_access_token"])
    if os.environ.get("REANA_SERVER_URL") is None:
        raise Exception("Environment variable REANA_SERVER_URL is not set")
    workflow_engine = workflow_engine.lower()
    if workflow_engine not in REANA_WORKFLOW_ENGINES:
        raise Exception("Workflow engine - {} not found. You must use one of "
                        "these engines - {}".format(workflow_engine,
                                                    REANA_WORKFLOW_ENGINES))
    try:
        reana_yaml = dict(workflow={})
        if workflow_file:
            reana_yaml["workflow"]["file"] = workflow_file
        else:
            reana_yaml["workflow"]["specification"] = workflow_json
        reana_yaml["workflow"]["type"] = workflow_engine
        if parameters:
            reana_yaml["inputs"] = parameters
        if outputs:
            reana_yaml["outputs"] = outputs
        _validate_reana_yaml(reana_yaml)
        reana_specification = reana_yaml
        (response, http_response) = current_rs_api_client.api.create_workflow(
            reana_specification=json.loads(
                json.dumps(reana_specification, sort_keys=True)),
            workflow_name=name,
            access_token=access_token,
        ).result()
        if http_response.status_code == 201:
            return response
        else:
            raise Exception(
                "Expected status code 201 but replied with "
                "{status_code}".format(status_code=http_response.status_code))

    except HTTPError as e:
        logging.debug("Workflow creation failed: "
                      "\nStatus: {}\nReason: {}\n"
                      "Message: {}".format(e.response.status_code,
                                           e.response.reason,
                                           e.response.json()["message"]))
        raise Exception(e.response.json()["message"])
    except Exception as e:
        raise e
Пример #2
0
def create_workflow_from_json(workflow_json, name, access_token,
                              parameters=None, workflow_engine='yadage',
                              outputs=None):
    """Create a workflow from json specification.

    :param workflow_json: workflow specification in json format.
    :param name: name or UUID of the workflow to be started.
    :param access_token: Access token of the current user.
    :param parameters: workflow input parameters dictionary.
    :param workflow_engine: one of the workflow engines (yadage, serial, cwl)
    :param outputs: dictionary with expected workflow outputs.

    :Example:

      .. code:: python

        create_workflow_from_json(
            workflow_json=workflow_json,
            name='workflow_name.1',
            access_token='access_token',
            parameters={'files': ['file.txt'], 'parameters': {'key': 'value'}},
            workflow_engine='serial')
    """
    if is_uuid_v4(name):
        raise ValueError('Workflow name cannot be a valid UUIDv4')
    if not access_token:
        raise Exception(ERROR_MESSAGES['missing_access_token'])
    if os.environ.get('REANA_SERVER_URL') is None:
        raise Exception('Environment variable REANA_SERVER_URL is not set')
    workflow_engine = workflow_engine.lower()
    if workflow_engine not in WORKFLOW_ENGINES:
        raise Exception('Workflow engine - {} not found. You must use one of '
                        'these engines - {}'.format(workflow_engine,
                                                    WORKFLOW_ENGINES))
    try:
        reana_yaml = {}
        reana_yaml['workflow'] = {'specification': workflow_json}
        reana_yaml['workflow']['type'] = workflow_engine
        if parameters:
            reana_yaml['inputs'] = parameters
        if outputs:
            reana_yaml['outputs'] = outputs
        _validate_reana_yaml(reana_yaml)
        reana_specification = reana_yaml
        (response,
            http_response) = current_rs_api_client.api.create_workflow(
                reana_specification=json.loads(json.dumps(
                    reana_specification, sort_keys=True)),
                workflow_name=name,
                access_token=access_token).result()
        if http_response.status_code == 201:
            return response
        else:
            raise Exception(
                "Expected status code 201 but replied with "
                "{status_code}".format(
                    status_code=http_response.status_code))

    except HTTPError as e:
        logging.debug(
            'Workflow creation failed: '
            '\nStatus: {}\nReason: {}\n'
            'Message: {}'.format(e.response.status_code,
                                 e.response.reason,
                                 e.response.json()['message']))
        raise Exception(e.response.json()['message'])
    except Exception as e:
        raise e