Ejemplo n.º 1
0
Archivo: submit.py Proyecto: yueri/Cook
def print_submit_result(cluster, response):
    """
    Parses a submission response from cluster and returns a corresponding message. Note that
    Cook Scheduler returns text when the submission was successful, and JSON when the submission
    failed. Also, in the case of failure, there are different possible shapes for the failure payload.
    """
    cluster_name = cluster['name']
    if response.status_code == 201:
        text = response.text.strip('"')
        if ' submitted groups' in text:
            group_index = text.index(' submitted groups')
            text = text[:group_index]
        uuids = [p for p in text.split() if is_valid_uuid(p)]
        print_info(submit_succeeded_message(cluster_name, uuids),
                   '\n'.join(uuids))
    else:
        try:
            data = response.json()
            if 'errors' in data:
                reason = json.dumps(data['errors'])
            elif 'error' in data:
                reason = data['error']
            else:
                reason = json.dumps(data)
        except json.decoder.JSONDecodeError:
            reason = '%s\n' % response.text
        print_info(submit_failed_message(cluster_name, reason))
Ejemplo n.º 2
0
def parse_entity_ref(ref_string, cluster_url_to_name_fn):
    """
    Returns a list of entity ref maps, where each map has the following shape:

      {'cluster': ..., 'type': ..., 'uuid': ...}

    The cluster field is the name of a cluster, the type field can be either job, instance, or group,
    and the uuid field is the uuid of the entity in question.

    An entity ref string can either be simply a UUID, in which case all configured clusters and all
    three types will be queried, or it can be a Cook Scheduler URL that represents the way to retrieve
    that specific entity.

    Some examples of valid entity ref strings follow:

      5ab383b1-5b8b-4483-b2b2-45126923f4df
      http://cluster2.example.com/jobs/5ab383b1-5b8b-4483-b2b2-45126923f4df
      http://cluster2.example.com/jobs?uuid=5ab383b1-5b8b-4483-b2b2-45126923f4df
      http://cluster3.example.com/instances/5ab383b1-5b8b-4483-b2b2-45126923f4df
      http://cluster3.example.com/instances?uuid=5ab383b1-5b8b-4483-b2b2-45126923f4df
      http://cluster4.example.com/groups/5ab383b1-5b8b-4483-b2b2-45126923f4df
      http://cluster4.example.com/groups?uuid=5ab383b1-5b8b-4483-b2b2-45126923f4df

    Throws if an invalid entity ref string is encountered.
    """
    result = urlparse(ref_string)

    if not result.path:
        raise Exception(f'{ref_string} is not a valid entity reference.')

    if not result.netloc:
        if not is_valid_uuid(result.path):
            raise Exception(f'{result.path} is not a valid UUID.')

        return [{'cluster': Clusters.ALL, 'type': Types.ALL, 'uuid': result.path}]

    path_parts = result.path.split('/')
    num_path_parts = len(path_parts)
    cluster_url = (f'{result.scheme}://' if result.scheme else '') + result.netloc

    if num_path_parts < 2:
        raise Exception(f'Unable to determine entity type and UUID from {ref_string}.')

    if num_path_parts == 2 and not result.query:
        raise Exception(f'Unable to determine UUID from {ref_string}.')

    cluster_name = cluster_url_to_name_fn(cluster_url)
    entity_type = resource_to_entity_type(path_parts[1])

    if num_path_parts > 2:
        return [{'cluster': cluster_name, 'type': entity_type, 'uuid': path_parts[2]}]

    query_args = parse_qs(result.query)

    if 'uuid' not in query_args:
        raise Exception(f'Unable to determine UUID from {ref_string}.')

    return [{'cluster': cluster_name, 'type': entity_type, 'uuid': uuid} for uuid in query_args['uuid']]
Ejemplo n.º 3
0
Archivo: submit.py Proyecto: yueri/Cook
def valid_uuid(s):
    """Allows argparse to flag user-provided job uuids as valid or not"""
    if is_valid_uuid(s):
        return str(uuid.UUID(s))
    else:
        raise argparse.ArgumentTypeError('%s is not a valid UUID' % s)