def main():
    args = _parse_args()
    container_id = socket.gethostname()
    docker_inspect = get_docker_inspect(container_id)

    port_and_protocol = _get_port_and_protocol(args.port)

    service_port = int(docker_inspect['NetworkSettings']['Ports']
                       [port_and_protocol][0]['HostPort'])
    service_filename = microservice_name = os.environ.get('MICROSERVICE_NAME')

    while True:
        try:
            agent_self_dict = consul_query('agent/self')
            if agent_self_dict['Config']:
                break
        except:
            traceback.print_exc()
        time.sleep(1)

    service_id = container_id
    full_service_name = microservice_name
    if args.subservice:
        service_id += ':' + args.subservice
        full_service_name += ':' + args.subservice
        service_filename += '-' + args.subservice

    consul_service_data = {
        'ID': service_id,
        'Name': full_service_name,
        'Port': service_port,
        'Check': {
            'TTL': '15s',
        }
    }
    tags = _create_tags()
    if tags:
        consul_service_data['Tags'] = tags

    print_err('consul_service_data:\n{0}\n'.format(
        json.dumps(consul_service_data)))

    _create_service_file(service_filename, full_service_name, service_id,
                         args.port, args.health_check)
    while True:
        try:
            _register_service(service_id, consul_service_data)
        except:
            print_err('ERROR on registering service:')
            traceback.print_exc()

        try:
            _store_start_timestamp(container_id, docker_inspect["Created"])
        except:
            print_err('ERROR on storing timestamp:')
            traceback.print_exc()

        time.sleep(10 + random.uniform(-2, 2))
示例#2
0
 def get_advertise_address(self):
     if self.advertise_address:
         return self.advertise_address
     try:
         agent_self_dict = consul_query('agent/self')
         self.advertise_address = agent_self_dict['Config']['AdvertiseAddr']
     except:
         raise AddressAdapterException('Could not acquire advertise IP address.')
     return self.advertise_address
def main():
    args = _parse_args()
    container_id = socket.gethostname()
    docker_inspect = get_docker_inspect(container_id)

    port_and_protocol = _get_port_and_protocol(args.port)

    service_port = int(docker_inspect['NetworkSettings']['Ports'][port_and_protocol][0]['HostPort'])
    service_filename = microservice_name = os.environ.get('MICROSERVICE_NAME')

    while True:
        try:
            agent_self_dict = consul_query('agent/self')
            if agent_self_dict['Config']:
                break
        except:
            traceback.print_exc()
        time.sleep(1)

    service_id = container_id
    full_service_name = microservice_name
    if args.subservice:
        service_id += ':' + args.subservice
        full_service_name += ':' + args.subservice
        service_filename += "-" + args.subservice

    consul_service_data = {
        'ID': service_id,
        'Name': full_service_name,
        'Port': service_port,
        'Check': {
            'TTL': '15s',
        }
    }
    tags = _create_tags()
    if tags:
        consul_service_data['Tags'] = tags

    print_err('consul_service_data:\n{0}\n'.format(json.dumps(consul_service_data)))

    _create_service_file(service_filename, full_service_name, service_id, args.port, args.health_check)
    while True:
        try:
            _register_service(service_id, consul_service_data)
        except:
            print_err('ERROR on registering service:')
            traceback.print_exc()

        try:
            _store_start_timestamp(container_id, docker_inspect["Created"])
        except:
            print_err('ERROR on storing timestamp:')
            traceback.print_exc()

        time.sleep(10 + random.uniform(-2, 2))
示例#4
0
 def get_subservice_address(self, subservice):
     if subservice in self.subservices:
         return self.subservices[subservice]
     container_id = socket.gethostname()
     if subservice:
         service_id = '{container_id}:{subservice}'.format(**locals())
     else:
         service_id = container_id
     services_dict = consul_query('agent/services')
     if service_id in services_dict:
         advertise_address = self.get_advertise_address()
         subservice_port = services_dict[service_id]['Port']
         subservice_address = '{advertise_address}:{subservice_port}'.format(**locals())
         self.subservices[subservice] = subservice_address
         return subservice_address
     else:
         raise AddressAdapterException('Could not find subservice: {subservice}.'.format(**locals()))
def _exists_service(service_id):
    try:
        return service_id in consul_query('agent/services')
    except:
        return False
def _exists_service(service_id):
    try:
        return service_id in consul_query('agent/services')
    except:
        return False
示例#7
0
def _wait_for_consul():
    agent_self_dict = consul_query('agent/self')
    if 'Config' not in agent_self_dict:
        raise Exception('Consul not ready yet')
示例#8
0
def _wait_for_consul():
    agent_self_dict = consul_query('agent/self')
    if 'Config' not in agent_self_dict:
        raise Exception('Consul not ready yet')