示例#1
0
    def _stop_service(self, container_id):
        ship = get_ship_name()
        service_dict = None
        service_list = kv_list('ships/{}/service/'.format(ship))
        if service_list:
            key = fnmatch.filter(service_list, '*/{}'.format(container_id))
            service_dict = kv_get(key[0]) if key else None
        if service_dict and service_dict['Status'] in ['crashed', 'not-recovered']:
            kv_remove(key[0])
        else:
            run_command_in_container('supervisorctl stop armada_agent', container_id)

            # TODO: Compatibility with old microservice images. Should be removed in future armada version.
            run_command_in_container('supervisorctl stop register_in_service_discovery', container_id)

            docker_api = docker_client.api()
            last_exception = None
            try:
                deregister_services(container_id)
            except:
                traceback.print_exc()
            for i in range(3):
                try:
                    docker_api.stop(container_id)
                    kv_remove(key[0])
                except Exception as e:
                    last_exception = e
                    traceback.print_exc()
                if not is_container_running(container_id):
                    break
            if is_container_running(container_id):
                get_logger().error('Could not stop container: {}'.format(container_id))
                raise last_exception
示例#2
0
def _get_services_list(filter_microservice_name, filter_env, filter_app_id,
                       filter_local):
    if filter_local:
        ship_list = ['containers_parameters_list/{}'.format(get_ship_name())]
    else:
        ship_list = kv.kv_list('containers_parameters_list/')
    services_dict = {}
    if not ship_list:
        return {}
    for ship in ship_list:
        containers = kv.kv_get(ship)
        if containers:
            services_dict.update(containers)
    services_list = services_dict.keys()

    result = {}
    if not services_list:
        return result

    if filter_microservice_name:
        services_list = fnmatch.filter(
            services_list,
            'ships/*/service/{}/*'.format(filter_microservice_name))

    for service in services_list:
        service_dict = services_dict[service]
        microservice_name = service_dict['ServiceName']
        microservice_status = service_dict['Status']
        microservice_id = service_dict['ServiceID']
        container_id = service_dict['container_id']
        microservice_start_timestamp = service_dict['start_timestamp']
        not_available = 'n/a'

        microservice_tags_dict = {}
        if service_dict['params']['microservice_env']:
            microservice_tags_dict['env'] = service_dict['params'][
                'microservice_env']
        if service_dict['params']['microservice_app_id']:
            microservice_tags_dict['app_id'] = service_dict['params'][
                'microservice_app_id']

        matches_env = (filter_env is None) or (
            filter_env == microservice_tags_dict.get('env'))
        matches_app_id = (filter_app_id is None) or (
            filter_app_id == microservice_tags_dict.get('app_id'))

        if matches_env and matches_app_id:
            microservice_dict = {
                'name': microservice_name,
                'status': microservice_status,
                'address': not_available,
                'microservice_id': microservice_id,
                'container_id': container_id,
                'tags': microservice_tags_dict,
                'start_timestamp': microservice_start_timestamp,
            }
            result[microservice_id] = microservice_dict
    return result
示例#3
0
 def POST(self):
     ship_name, error = self.get_post_parameter('name')
     if error:
         return self.status_error(error)
     other_ship_names = [get_ship_name(ip) for ip in get_other_ship_ips()]
     name_taken = ship_name in other_ship_names
     if not ship_name or ship_name == 'None' or name_taken:
         return self.status_error('Incorrect ship name: {}'.format(ship_name))
     set_ship_name(ship_name)
     return self.status_ok()
示例#4
0
 def POST(self):
     ship_name, error = self.get_post_parameter('name')
     if error:
         return self.status_error(error)
     other_ship_names = [get_ship_name(ip) for ip in get_other_ship_ips()]
     name_taken = ship_name in other_ship_names
     if not ship_name or ship_name == 'None' or name_taken:
         return self.status_error(
             'Incorrect ship name: {}'.format(ship_name))
     set_ship_name(ship_name)
     return self.status_ok()
示例#5
0
    def _start_container(self, long_container_id):
        docker_api = docker_client.api()
        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_service(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
示例#6
0
    def POST(self):
        consul_host, error = self.get_post_parameter('host')
        if error:
            return self.status_error(error)

        ship = get_ship_name()
        local_services = kv.kv_list('ships/{}/service/'.format(ship)) or []
        local_services_data = {key: kv.kv_get(key) for key in local_services}

        armada_size = _get_armada_size()
        if armada_size > 1:
            return self.status_error(
                'Currently only single ship armadas can join the others. '
                'Your armada has size: {0}.'.format(armada_size))

        try:
            agent_self_dict = consul_query(
                'agent/self', consul_address='{0}:8500'.format(consul_host))
            datacenter = agent_self_dict['Config']['Datacenter']
        except:
            return self.status_error(
                'Could not read remote host datacenter address.')

        current_consul_mode = _get_current_consul_mode()
        if current_consul_mode == consul_config.ConsulMode.BOOTSTRAP:
            override_runtime_settings(
                consul_mode=consul_config.ConsulMode.CLIENT,
                ship_ips=[consul_host],
                datacenter=datacenter)
        else:
            override_runtime_settings(ship_ips=[consul_host] +
                                      get_other_ship_ips(),
                                      datacenter=datacenter)

        if _restart_consul():
            supervisor_server = xmlrpclib.Server('http://localhost:9001/RPC2')
            hermes_init_output = supervisor_server.supervisor.startProcessGroup(
                'hermes_init')
            get_logger().info(
                'hermes_init start: {}'.format(hermes_init_output))
            set_ship_name(ship)
            for key, data in local_services_data.items():
                kv.kv_set(key, data)
            return self.status_ok()
        return self.status_error('Waiting for armada restart timed out.')
示例#7
0
    def _start_container(self, long_container_id):
        docker_api = docker_client.api()
        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_service(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
示例#8
0
    def POST(self):
        ship_name, error = self.get_post_parameter('name')
        if error:
            return self.status_error(error)
        if not ship_name or ship_name == 'None':
            try:
                ship_name = get_ship_name()
            except:
                ship_name = str(random.randrange(1000000))

        current_consul_mode = _get_current_consul_mode()
        if current_consul_mode == consul_config.ConsulMode.BOOTSTRAP:
            override_runtime_settings(ship_name=ship_name,
                                      ship_ips=[],
                                      datacenter='dc-' + str(random.randrange(1000000)))
        else:
            override_runtime_settings(ship_name=ship_name)

        if _restart_consul():
            return self.status_ok()
        return self.status_error('Waiting for armada restart timed out.')
示例#9
0
    def POST(self):
        ship_name, error = self.get_post_parameter('name')
        if error:
            return self.status_error(error)
        if not ship_name or ship_name == 'None':
            try:
                ship_name = get_ship_name()
            except:
                ship_name = str(random.randrange(1000000))

        current_consul_mode = _get_current_consul_mode()
        if current_consul_mode == consul_config.ConsulMode.BOOTSTRAP:
            override_runtime_settings(ship_name=ship_name,
                                      ship_ips=[],
                                      datacenter='dc-' +
                                      str(random.randrange(1000000)))
        else:
            override_runtime_settings(ship_name=ship_name)

        if _restart_consul():
            return self.status_ok()
        return self.status_error('Waiting for armada restart timed out.')
示例#10
0
    def _stop_service(self, container_id):
        ship = get_ship_name()
        service_dict = None
        service_list = kv_list('ships/{}/service/'.format(ship))
        if service_list:
            key = fnmatch.filter(service_list, '*/{}'.format(container_id))
            service_dict = kv_get(key[0]) if key else None
        if service_dict and service_dict['Status'] in [
                'crashed', 'not-recovered'
        ]:
            kv_remove(key[0])
        else:
            run_command_in_container('supervisorctl stop armada_agent',
                                     container_id)

            # TODO: Compatibility with old microservice images. Should be removed in future armada version.
            run_command_in_container(
                'supervisorctl stop register_in_service_discovery',
                container_id)

            docker_api = docker_client.api()
            last_exception = None
            try:
                deregister_services(container_id)
            except:
                traceback.print_exc()
            for i in range(3):
                try:
                    docker_api.stop(container_id)
                    kv_remove(key[0])
                except Exception as e:
                    last_exception = e
                    traceback.print_exc()
                if not is_container_running(container_id):
                    break
            if is_container_running(container_id):
                get_logger().error(
                    'Could not stop container: {}'.format(container_id))
                raise last_exception
示例#11
0
    def POST(self):
        consul_host, error = self.get_post_parameter('host')
        if error:
            return self.status_error(error)

        ship = get_ship_name()
        local_services = kv.kv_list('ships/{}/service/'.format(ship)) or []
        local_services_data = {key: kv.kv_get(key) for key in local_services}

        armada_size = _get_armada_size()
        if armada_size > 1:
            return self.status_error('Currently only single ship armadas can join the others. '
                                     'Your armada has size: {0}.'.format(armada_size))

        try:
            agent_self_dict = consul_query('agent/self', consul_address='{0}:8500'.format(consul_host))
            datacenter = agent_self_dict['Config']['Datacenter']
        except:
            return self.status_error('Could not read remote host datacenter address.')

        current_consul_mode = _get_current_consul_mode()
        if current_consul_mode == consul_config.ConsulMode.BOOTSTRAP:
            override_runtime_settings(consul_mode=consul_config.ConsulMode.CLIENT,
                                      ship_ips=[consul_host],
                                      datacenter=datacenter)
        else:
            override_runtime_settings(ship_ips=[consul_host] + get_other_ship_ips(),
                                      datacenter=datacenter)

        if _restart_consul():
            supervisor_server = xmlrpclib.Server('http://localhost:9001/RPC2')
            hermes_init_output = supervisor_server.supervisor.startProcessGroup('hermes_init')
            get_logger().info('hermes_init start: {}'.format(hermes_init_output))
            set_ship_name(ship)
            for key, data in local_services_data.items():
                kv.kv_set(key, data)
            return self.status_ok()
        return self.status_error('Waiting for armada restart timed out.')
示例#12
0
 def GET(self):
     return get_ship_name()
示例#13
0
 def GET(self):
     return get_ship_name()