Esempio n. 1
0
def ship_ip_to_name(ip):
    return kv.kv_get('ships/{}/name'.format(ip))
Esempio n. 2
0
def get_matched_containers(microservice_name_or_container_id_prefix):
    service_names = list(consul_query('catalog/services').keys())

    matched_containers_by_name = []
    matched_containers_by_id = []

    for service_name in service_names:
        query = 'catalog/service/{service_name}'.format(**locals())
        try:
            instances = consul_query(query)
        except Exception as e:
            print_err(
                'WARNING: query "{query}" failed ({exception_class}: {exception})'
                .format(query=query,
                        exception_class=type(e).__name__,
                        exception=e))
            instances = []

        for instance in instances:
            container_id = instance['ServiceID'].split(':')[0]
            service_name = instance['ServiceName']

            if microservice_name_or_container_id_prefix == service_name:
                matched_containers_by_name.append(instance)

            if container_id.startswith(microservice_name_or_container_id_prefix
                                       ) and ":" not in instance['ServiceID']:
                matched_containers_by_id.append(instance)

    services_list = kv.kv_list('ships/')
    if services_list:
        services_list = fnmatch.filter(services_list, 'ships/*/service/*')
    if services_list:
        for service in services_list:
            service_dict = kv.kv_get(service)
            if service_dict['Status'] not in [
                    'crashed', 'not-recovered', 'recovering'
            ]:
                continue
            container_id = service_dict['container_id']
            service_name = service_dict['ServiceName']

            if microservice_name_or_container_id_prefix == service_name:
                matched_containers_by_name.append(service_dict)

            if container_id.startswith(
                    microservice_name_or_container_id_prefix
            ) and ":" not in service_dict['ServiceID']:
                matched_containers_by_id.append(service_dict)

    matched_containers_by_name_count = len(matched_containers_by_name)
    matched_containers_by_id_count = len(matched_containers_by_id)

    if matched_containers_by_name_count and matched_containers_by_id_count:
        raise ArmadaCommandException(
            'Found matching containers with both microservice name ({matched_containers_by_name_count}) '
            'and container_id ({matched_containers_by_id_count}). '
            'Please provide more specific criteria.'.format(**locals()))
    if matched_containers_by_id_count > 1:
        raise ArmadaCommandException(
            'There are too many ({matched_containers_by_id_count}) matching containers. '
            'Please provide more specific container_id.'.format(**locals()))
    matched_containers = matched_containers_by_name + matched_containers_by_id

    matches_count = len(matched_containers)
    if matches_count == 0:
        raise ArmadaCommandException(
            'There are no running containers with microservice: '
            '{microservice_name_or_container_id_prefix}'.format(**locals()))

    return matched_containers
Esempio n. 3
0
def ship_name_to_ip(name):
    return kv.kv_get('ships/{}/ip'.format(name))
Esempio n. 4
0
def get_matched_containers(microservice_name_or_container_id_prefix):
    service_names = list(consul_query('catalog/services').keys())

    matched_containers_by_name = []
    matched_containers_by_id = []

    for service_name in service_names:
        query = 'catalog/service/{service_name}'.format(**locals())
        try:
            instances = consul_query(query)
        except Exception as e:
            print_err('WARNING: query "{query}" failed ({exception_class}: {exception})'.format(
                query=query, exception_class=type(e).__name__, exception=e))
            instances = []

        for instance in instances:
            container_id = instance['ServiceID'].split(':')[0]
            service_name = instance['ServiceName']

            if microservice_name_or_container_id_prefix == service_name:
                matched_containers_by_name.append(instance)

            if container_id.startswith(microservice_name_or_container_id_prefix) and ":" not in instance['ServiceID']:
                matched_containers_by_id.append(instance)

    services_list = kv.kv_list('ships/')
    if services_list:
        services_list = fnmatch.filter(services_list, 'ships/*/service/*')
    if services_list:
        for service in services_list:
            service_dict = kv.kv_get(service)
            container_id = service_dict['container_id']
            service_name = service_dict['ServiceName']

            if service_dict['Status'] == 'started':
                try:
                    instances = consul_query('catalog/service/{}'.format(service_name))
                    if container_id in [i['ServiceID'].split(':')[0] for i in instances]:
                        continue
                except Exception as e:
                    pass

            if microservice_name_or_container_id_prefix == service_name:
                matched_containers_by_name.append(service_dict)

            if container_id.startswith(microservice_name_or_container_id_prefix) and ":" not in service_dict['ServiceID']:
                matched_containers_by_id.append(service_dict)

    matched_containers_by_name_count = len(matched_containers_by_name)
    matched_containers_by_id_count = len(matched_containers_by_id)

    if matched_containers_by_name_count and matched_containers_by_id_count:
        raise ArmadaCommandException(
            'Found matching containers with both microservice name ({matched_containers_by_name_count}) '
            'and container_id ({matched_containers_by_id_count}). '
            'Please provide more specific criteria.'.format(**locals()))
    if matched_containers_by_id_count > 1:
        raise ArmadaCommandException(
            'There are too many ({matched_containers_by_id_count}) matching containers. '
            'Please provide more specific container_id.'.format(**locals()))
    matched_containers = matched_containers_by_name + matched_containers_by_id

    matches_count = len(matched_containers)
    if matches_count == 0:
        raise ArmadaCommandException(
            'There are no running containers with microservice: '
            '{microservice_name_or_container_id_prefix}'.format(**locals()))

    return matched_containers
Esempio n. 5
0
def ship_ip_to_name(ip):
    return kv.kv_get('ships/{}/name'.format(ip))
Esempio n. 6
0
def ship_name_to_ip(name):
    return kv.kv_get('ships/{}/ip'.format(name))
Esempio n. 7
0
def get_matched_containers(microservice_name_or_container_id_prefix):
    service_names = list(consul_query("catalog/services").keys())

    matched_containers_by_name = []
    matched_containers_by_id = []

    for service_name in service_names:
        query = "catalog/service/{service_name}".format(**locals())
        try:
            instances = consul_query(query)
        except Exception as e:
            print_err(
                'WARNING: query "{query}" failed ({exception_class}: {exception})'.format(
                    query=query, exception_class=type(e).__name__, exception=e
                )
            )
            instances = []

        for instance in instances:
            container_id = instance["ServiceID"].split(":")[0]
            service_name = instance["ServiceName"]

            if microservice_name_or_container_id_prefix == service_name:
                matched_containers_by_name.append(instance)

            if container_id.startswith(microservice_name_or_container_id_prefix) and ":" not in instance["ServiceID"]:
                matched_containers_by_id.append(instance)

    instances_kv = kv.kv_list("service/")

    if instances_kv:
        for instance in instances_kv:
            instance_dict = kv.kv_get(instance)
            container_id = (
                instance_dict["container_id"] if "container_id" in instance_dict else instance_dict["ServiceID"]
            )
            service_name = instance_dict["ServiceName"]

            if microservice_name_or_container_id_prefix == service_name:
                matched_containers_by_name.append(instance_dict)

            if (
                container_id.startswith(microservice_name_or_container_id_prefix)
                and ":" not in instance_dict["ServiceID"]
            ):
                matched_containers_by_id.append(instance_dict)

    matched_containers_by_name_count = len(matched_containers_by_name)
    matched_containers_by_id_count = len(matched_containers_by_id)

    if matched_containers_by_name_count and matched_containers_by_id_count:
        raise ArmadaCommandException(
            "Found matching containers with both microservice name ({matched_containers_by_name_count}) "
            "and container_id ({matched_containers_by_id_count}). "
            "Please provide more specific criteria.".format(**locals())
        )
    if matched_containers_by_id_count > 1:
        raise ArmadaCommandException(
            "There are too many ({matched_containers_by_id_count}) matching containers. "
            "Please provide more specific container_id.".format(**locals())
        )
    matched_containers = matched_containers_by_name + matched_containers_by_id

    matches_count = len(matched_containers)
    if matches_count == 0:
        raise ArmadaCommandException(
            "There are no running containers with microservice: "
            "{microservice_name_or_container_id_prefix}".format(**locals())
        )

    return matched_containers