def _get_running_services(filter_microservice_name, filter_env, filter_app_id, filter_local): if filter_local: local_microservices_ids = set(consul_query('agent/services').keys()) if filter_microservice_name: names = list(consul_query('catalog/services').keys()) microservices_names = fnmatch.filter(names, filter_microservice_name) else: microservices_names = list(consul_query('catalog/services').keys()) start_timestamps = kv.kv_get_recurse('start_timestamp/') or {} single_active_instances = kv.kv_get_recurse('single_active_instance/') if single_active_instances: single_active_instances_list = single_active_instances.keys() else: single_active_instances_list = [] services_list_from_catalog = {} for microservice_name in microservices_names: if microservice_name == 'consul': continue query = 'health/service/{microservice_name}'.format(**locals()) instances = consul_query(query) for instance in instances: microservice_checks_statuses = set(check['Status'] for check in (instance['Checks'] or [])) microservice_computed_status = '-' for possible_status in ['passing', 'warning', 'critical']: if possible_status in microservice_checks_statuses: microservice_computed_status = possible_status microservice_ip = instance['Node']['Address'] microservice_port = str(instance['Service']['Port']) microservice_id = instance['Service']['ID'] container_id = microservice_id.split(':')[0] microservice_tags = instance['Service']['Tags'] or [] microservice_tags_dict = __create_dict_from_tags(microservice_tags) 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 and (not filter_local or microservice_id in local_microservices_ids)): microservice_address = microservice_ip + ':' + microservice_port microservice_start_timestamp = start_timestamps.get(container_id, None) single_active_instance = microservice_id in single_active_instances_list microservice_dict = { 'name': microservice_name, 'address': microservice_address, 'microservice_id': microservice_id, 'container_id': container_id, 'status': microservice_computed_status, 'tags': microservice_tags_dict, 'start_timestamp': microservice_start_timestamp, 'single_active_instance': single_active_instance, } services_list_from_catalog[microservice_id] = microservice_dict return services_list_from_catalog
def main(): setup_sentry() args = _parse_args() saved_containers_path = args.saved_containers_path if not args.force and not _is_recovery_completed(): get_logger().info( 'Recovery is not completed. Aborting saving running containers.') return try: wait_for_consul_ready() services_key = 'services/{}'.format(get_ship_name()) containers_parameters = kv.kv_get_recurse(services_key, strip_keys=False) if not containers_parameters: get_logger().info( 'Aborted saving container because list is empty.') return _save_containers_parameters_list_in_file(containers_parameters, saved_containers_path) get_logger().info( 'Containers have been saved to {}.'.format(saved_containers_path)) except Exception as e: get_logger().exception(e) sys.exit(1)
def _get_services_list(filter_microservice_name, filter_env, filter_app_id, filter_local): consul_key = 'services' if filter_local: consul_key = '{}/{}'.format(consul_key, get_ship_name()) services_by_ship = kv.kv_get_recurse(consul_key) if not services_by_ship: return {} return _parse_single_ship(services_by_ship, filter_microservice_name, filter_env, filter_app_id)
def _get_services_list(filter_microservice_name, filter_env, filter_app_id, filter_local): consul_key = 'containers_parameters_list' if filter_local: consul_key = '{}/{}'.format(consul_key, get_ship_name()) services_by_ship = kv.kv_get_recurse(consul_key) if not services_by_ship: return {} result = {} for services_dict in services_by_ship.values(): result.update(_parse_single_ship(services_dict, filter_microservice_name, filter_env, filter_app_id)) return result
def _get_services_list(filter_microservice_name, filter_env, filter_app_id, filter_local): consul_key = 'containers_parameters_list' if filter_local: consul_key = '{}/{}'.format(consul_key, get_ship_name()) services_by_ship = kv.kv_get_recurse(consul_key) if not services_by_ship: return {} result = {} for services_dict in services_by_ship.values(): result.update( _parse_single_ship(services_dict, filter_microservice_name, filter_env, filter_app_id)) return result