def detect_tags_for_submitter()

in api-server/tesazure/backends/common/__init__.py [0:0]


def detect_tags_for_submitter(task):
    """
    Uses properties of the submitted TES task to attempt to identify the
    submitting software, if any.

    Outputs a dict with information about the submitter.
    """
    tags = {}

    # Attempt to detect if Cromwell is submitting this task
    try:
        # Cromwell places workflow UUID in the description
        description_uuid = task.description.split(':')[0]
        uuid.UUID(description_uuid)
    except ValueError:
        description_uuid = False

    # any cromwell task is going to have a 'stderr', 'stdout' and 'rc' output
    output_path_is_url = all([output.path == output.url for output in task.outputs])
    has_rc_output = False
    has_stdout_output = False
    has_stderr_output = False
    match = False
    for output in task.outputs:
        if output.path.endswith('execution/rc'):
            # rc output has crmowell uuid in path
            uuid_regex = "[0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12}"
            match = re.match(rf"/tes-wd(?:/.*)?/([^/]+)/({uuid_regex})/call-([^/]+)(?:/.*)?/execution/rc", output.path)
        has_rc_output = True
        has_stdout_output = has_stdout_output or output.path.endswith('execution/stdout')
        has_stderr_output = has_stderr_output or output.path.endswith('execution/stderr')

    if output_path_is_url and has_rc_output and has_stdout_output and has_stderr_output and match:
        workflow_name, workflow_id, call_name = match.groups()
        # to confirm this isn't a strange coincidence, confirm the UUID in output path for rc matches the description's UUID
        if workflow_id == description_uuid:
            # confirmed cromwell
            current_app.logger.info(f"Detected 'cromwell' as task submitter, task is part of workflow with ID '{workflow_id}'")
            tags['ms-submitter-name'] = 'cromwell'
            tags['ms-submitter-workflow-id'] = workflow_id
            tags['ms-submitter-workflow-name'] = workflow_name
            tags['ms-submitter-workflow-stage'] = call_name
            tags['ms-submitter-cromwell-executiondir'] = str(pathlib.PurePosixPath(output.path).parent)
    return tags