def _clean_up_kv_store(): global next_kv_clean_up_timestamp if time.time() < next_kv_clean_up_timestamp: return get_logger().info('Cleaning up kv-store:') next_kv_clean_up_timestamp = get_next_kv_clean_up_timestamp() services = armada_api.get_json('list') valid_container_ids = set( service.get('container_id') for service in services) start_timestamp_keys = kv.kv_list('start_timestamp/') or [] for key in start_timestamp_keys: container_id = key.split('/')[-1] if container_id not in valid_container_ids: get_logger().info('Removing key: {}'.format(key)) kv.kv_remove(key) single_active_instance_keys = kv.kv_list('single_active_instance/') or [] for key in single_active_instance_keys: container_id = key.split('/')[-1].split(':')[0] if container_id not in valid_container_ids: get_logger().info('Removing key: {}'.format(key)) kv.kv_remove(key) get_logger().info('Finished cleaning up kv-store.')
def command_list(args): service_list = get_json('list', vars(args)) output_rows = None if not args.quiet: output_header = ('Name', 'Address', 'ID', 'Status', 'Tags') if args.uptime: output_header += ("Created (UTC)", ) output_rows = [output_header] for service in service_list: if args.quiet: print(str(service['container_id'])) else: service_tags_pretty = [ str(x) + ':' + str(service['tags'][x]) for x in sorted(service['tags']) ] if service['tags'] else '-' output_row = (service['name'], service['address'], service['container_id'], service['status'], service_tags_pretty) if args.uptime: creation_time = epoch_to_iso(service['start_timestamp']) output_row += (creation_time, ) output_rows.append(output_row) if not args.quiet: print_table([output_rows[0]] + sorted(output_rows[1:]))
def command_list(args): service_list = get_json('list', vars(args)) output_rows = None if not args.quiet: output_header = ('Name', 'Address', 'ID', 'Status', 'Tags') if args.uptime: output_header += ("Created (UTC)",) output_rows = [output_header] for service in service_list: if args.quiet: print(str(service['container_id'])) else: service_tags_pretty = [str(x) + ':' + str(service['tags'][x]) for x in sorted(service['tags'])] if service['tags'] else '-' output_row = (service['name'], service['address'], service['microservice_id'], service['status'], service_tags_pretty) if args.uptime: creation_time = epoch_to_iso(service['start_timestamp']) output_row += (creation_time,) output_rows.append(output_row) if not args.quiet: print_table([output_rows[0]] + sorted(output_rows[1:]))
def command_info(args): info = armada_api.get_json('info') output_header = ['Current', 'Ship name', 'Ship role', 'API address', 'API status', 'Version'] output_rows = [output_header] ship_role_counts = Counter() for ship in info: current_string = '->'.rjust(len(output_header[0])) if ship['is_current'] else '' if ship['status'] == 'passing': ship_role_counts[ship['role']] += 1 output_rows.append([current_string, ship['name'], ship['role'], ship['address'], ship['status'], ship['version']]) print_table(output_rows) if ship_role_counts['leader'] == 0: print('\nERROR: There is no active leader. Armada is not working!', file=sys.stderr) elif ship_role_counts['commander'] == 0: print('\nWARNING: We cannot survive leader leaving/failure.', file=sys.stderr) print('Such configuration should only be used in development environments.', file=sys.stderr) elif ship_role_counts['commander'] == 1: print('\nWARNING: We can survive leaving of commander but commander failure or leader leave/failure will be ' 'fatal.', file=sys.stderr) print('Such configuration should only be used in development environments.', file=sys.stderr) else: failure_tolerance = ship_role_counts['commander'] / 2 print('\nWe can survive failure of {0} {1} (including leader).'.format( failure_tolerance, 'commander' if failure_tolerance == 1 else 'commanders'), file=sys.stderr)
def command_list(args): service_list = get_json('list', vars(args)) output_rows = None if not args.quiet: output_header = ('Name', 'Address', 'ID', 'Status', 'Env', 'AppID') if args.uptime: output_header += ("Created (UTC)",) output_rows = [output_header] for service in service_list: if args.quiet: print(str(service['container_id'])) else: service_tags = service['tags'] output_row = (service['name'], service['address'], service['container_id'], service['status'], service_tags.get('env') or '-', service_tags.get('app_id') or '-') if args.uptime: creation_time = epoch_to_iso(service['start_timestamp']) output_row += (creation_time,) output_rows.append(output_row) if not args.quiet: print_table([output_rows[0]] + sorted(output_rows[1:]))
def command_list(args): service_list = get_json('list', vars(args)) output_rows = None if not args.quiet: output_header = ('Name', 'Address', 'ID', 'Status', 'Env', 'AppID') if args.uptime: output_header += ("Created (UTC)", ) output_rows = [output_header] for service in service_list: if args.quiet: print(str(service['container_id'])) else: service_tags = service['tags'] output_row = (service['name'], service['address'], service['container_id'], service['status'], service_tags.get('env') or '-', service_tags.get('app_id') or '-') if args.uptime: creation_time = epoch_to_iso(service['start_timestamp']) output_row += (creation_time, ) output_rows.append(output_row) if not args.quiet: print_table([output_rows[0]] + sorted(output_rows[1:]))
def _consul_discover(service_name): params = {'microservice_name': service_name} services = armada_api.get_json('list', params) service_addresses = set() for service in services: if service['status'] in ['passing','warning']: service_addresses.add(service['address']) return service_addresses
def _consul_discover(service_name): params = {'microservice_name': service_name} services = armada_api.get_json('list', params) service_addresses = set() for service in services: if service['status'] != 'critical': service_addresses.add(service['address']) return service_addresses
def _consul_discover(service_name): params = {'microservice_name': service_name} services = armada_api.get_json('list', params) service_addresses = set() for service in services: if service['status'] in ['passing', 'warning']: service_addresses.add(service['address']) return service_addresses
def wait_for_consul_ready(timeout_seconds=60): timeout_expiration = time.time() + timeout_seconds last_exception = None while time.time() < timeout_expiration: time.sleep(1) try: params = {'local': 'true', 'microservice_name': 'armada'} armada_instances = armada_api.get_json('list', params) if armada_instances[0]['status'] == 'passing': return True except Exception as e: last_exception = e if last_exception: logging.exception(last_exception) return False
def _clean_up_kv_store(): global next_kv_clean_up_timestamp if time.time() < next_kv_clean_up_timestamp: return get_logger().info('Cleaning up kv-store:') next_kv_clean_up_timestamp = get_next_kv_clean_up_timestamp() services = armada_api.get_json('list') valid_container_ids = set(service.get('container_id') for service in services) start_timestamp_keys = kv.kv_list('start_timestamp/') or [] for key in start_timestamp_keys: container_id = key.split('/')[-1] if container_id not in valid_container_ids: get_logger().info('Removing key: {}'.format(key)) kv.kv_remove(key) single_active_instance_keys = kv.kv_list('single_active_instance/') or [] for key in single_active_instance_keys: container_id = key.split('/')[-1].split(':')[0] if container_id not in valid_container_ids: get_logger().info('Removing key: {}'.format(key)) kv.kv_remove(key) get_logger().info('Finished cleaning up kv-store.')
def command_list(args): service_list = get_json('list', vars(args)) if args.quiet: for container_id in { service['container_id'] for service in service_list }: print(container_id) return output_header = ['Name', 'Address', 'ID', 'Status', 'Env', 'AppID'] if args.uptime: output_header.append("Created (UTC)") if args.microservice_version: output_header.append("Microservice Version") output_rows = [output_header] for service in service_list: service_tags = service['tags'] output_row = [ service['name'], service['address'], service['container_id'], service['status'], service_tags.get('env') or '-', service_tags.get('app_id') or '-' ] if args.uptime: creation_time = epoch_to_iso(service['start_timestamp']) output_row.append(creation_time) if args.microservice_version: microservice_version = service.get('microservice_version', '?') output_row.append(microservice_version) output_rows.append(output_row) print_table(output_rows)
def _get_running_armada_services(): return armada_api.get_json('list', {'microservice_name': 'armada'})
def _get_running_armada_services(): return armada_api.get_json("list", {"microservice_name": "armada"})