Exemplo n.º 1
0
def _add_running_services_at_startup():
    wait_for_consul_ready()
    try:
        ship_ip, ship_name = get_ship_ip_and_name()
        containers_saved_in_kv = get_local_services_from_kv_store()
        sleep(10)
        all_services = consul_query('agent/services')
        if 'consul' in all_services:
            del all_services['consul']
        for service_id, service_dict in six.iteritems(all_services):
            if ':' in service_id:
                continue
            if service_dict['Service'] == 'armada':
                continue
            key = create_consul_services_key(ship_name,
                                             service_dict['Service'],
                                             service_id)
            if not containers_saved_in_kv or key not in containers_saved_in_kv:
                save_container(ship_name,
                               service_id,
                               'started',
                               ship_ip=ship_ip)
                get_logger().info(
                    'Added running service: {}'.format(service_id))
    except Exception:
        get_logger().exception('Unable to add running services.')
Exemplo n.º 2
0
def _load_from_list(saved_containers, ship_name, ship_ip):
    wait_for_consul_ready()
    running_containers = _get_local_running_containers()
    containers_to_be_added = _multiset_difference(saved_containers, running_containers)
    for container_parameters in containers_to_be_added:
        get_logger().info('Added service: {}'.format(container_parameters))
        save_container(ship_name, _generate_id(), 'crashed', params=container_parameters, ship_ip=ship_ip)
Exemplo n.º 3
0
 def on_post(self, req, resp, microservice_id):
     try:
         input_json = req.json
         container_id = microservice_id.split(':')[0]
         microservice_name = input_json['microservice_name']
         microservice_data = {
             'microservice_id':
             microservice_id,
             'microservice_name':
             microservice_name,
             'single_active_instance':
             bool(input_json.get('single_active_instance')),
             'container_created_timestamp':
             input_json['container_created_timestamp'],
             'microservice_port':
             local_port_to_external_port(
                 container_id, input_json['microservice_local_port']),
         }
         microservice_tags = []
         if input_json.get('microservice_env'):
             microservice_tags.append('env:{}'.format(
                 input_json['microservice_env']))
         if input_json.get('microservice_app_id'):
             microservice_tags.append('app_id:{}'.format(
                 input_json['microservice_app_id']))
         if microservice_tags:
             microservice_data['microservice_tags'] = microservice_tags
         microservice_version = input_json.get('microservice_version')
         register_service_in_consul(microservice_data)
         if microservice_version and microservice_name != 'armada' and not is_subservice(
                 microservice_name):
             ship_ip, ship_name = get_ship_ip_and_name()
             save_container(ship_name,
                            container_id,
                            status='started',
                            start_timestamp=str(
                                input_json['container_created_timestamp']),
                            ship_ip=ship_ip)
             update_service_dict(ship_name, microservice_name, container_id,
                                 'microservice_version',
                                 microservice_version)
         resp.json = microservice_data
     except Exception as e:
         logging.exception(e)
         resp.json = {
             'error': 'Could not register service: {}'.format(repr(e))
         }
         resp.status = falcon.HTTP_400
Exemplo n.º 4
0
    def _start_container(self, long_container_id):
        docker_api = docker_client.api(timeout=30)
        docker_api.start(long_container_id)

        service_endpoints = {}
        agent_self_dict = consul_query('agent/self')
        service_ip = agent_self_dict['Config']['AdvertiseAddr']

        docker_inspect = docker_api.inspect_container(long_container_id)

        ship = get_ship_name()
        container_id = shorten_container_id(long_container_id)
        save_container(ship, container_id, status='started')

        for container_port, host_address in docker_inspect['NetworkSettings']['Ports'].items():
            service_endpoints['{0}:{1}'.format(service_ip, host_address[0]['HostPort'])] = container_port
        return service_endpoints
Exemplo n.º 5
0
    def _start_container(self, long_container_id):
        docker_api = docker_client.api(timeout=30)
        docker_api.start(long_container_id)

        service_endpoints = {}
        service_ip, ship_name = get_ship_ip_and_name()

        docker_inspect = docker_api.inspect_container(long_container_id)

        container_id = shorten_container_id(long_container_id)
        save_container(ship_name,
                       container_id,
                       status='started',
                       ship_ip=service_ip)

        for container_port, host_address in docker_inspect['NetworkSettings'][
                'Ports'].items():
            service_endpoints['{0}:{1}'.format(
                service_ip, host_address[0]['HostPort'])] = container_port
        return service_endpoints
Exemplo n.º 6
0
    def on_post(self, req, resp):
        try:
            microservice_data = req.json
            register_service_in_consul(microservice_data)
            result = {'microservice_data': microservice_data}

            ship_ip, ship_name = get_ship_ip_and_name()
            microservice_id = microservice_data['microservice_id']
            container_id = microservice_id.split(':')[0]
            if not is_subservice(microservice_data['microservice_name']):
                save_container(
                    ship_name,
                    container_id,
                    status='started',
                    start_timestamp=str(
                        microservice_data['container_created_timestamp']),
                    ship_ip=ship_ip)
        except Exception as e:
            return self.status_exception(resp, 'Could not register service.',
                                         e)
        return self.status_ok(resp, {'result': result})
Exemplo n.º 7
0
def _update_running_services():
    ship_ip = get_ship_ip()
    try:
        ship_name = get_ship_name(ship_ip)
    except:
        ship_name = ship_ip
    services = _get_local_services_from_catalog()
    running_containers_ids = _get_running_container_ids()
    local_services = get_local_services_from_kv_store()
    local_services_container_ids = [it.split('/')[-1] for it in local_services]
    for service_id in services.keys():
        container_id, is_subservice = _get_container_id_with_subservice(
            service_id)
        if container_id not in local_services_container_ids:
            save_container(ship_name, container_id, 'started', ship_ip=ship_ip)
            get_logger().info(
                'Saved container in kv-store: {container_id}'.format(
                    container_id=container_id))
        if container_id in running_containers_ids:
            continue
        if not is_subservice:
            name = services[service_id]['Service']
            update_container_status('crashed',
                                    ship=ship_name,
                                    service_name=name,
                                    container_id=container_id)
            get_logger().info('Set status to "crashed": {container_id}'.format(
                container_id=container_id))
        deregister_services(container_id)

    for service_key in local_services:
        container_id = service_key.split('/')[-1]
        if container_id not in running_containers_ids:
            update_container_status('crashed', key=service_key)
            get_logger().info('Set status to "crashed": {container_id}'.format(
                container_id=container_id))
            deregister_services(container_id)