def pretty_print_smartstack_backends_for_locations(service_instance, tasks, locations, expected_count, verbose): """ Pretty prints the status of smartstack backends of a specified service and instance in the specified locations """ rows = [(" Name", "LastCheck", "LastChange", "Status")] expected_count_per_location = int(expected_count / len(locations)) for location in sorted(locations): hosts = locations[location] # arbitrarily choose the first host with a given attribute to query for replication stats synapse_host = hosts[0] sorted_backends = sorted( get_backends(service_instance, synapse_host=synapse_host, synapse_port=DEFAULT_SYNAPSE_PORT), key=lambda backend: backend['status'], reverse=True ) # Specify reverse so that backends in 'UP' are placed above 'MAINT' matched_tasks = match_backends_and_tasks(sorted_backends, tasks) running_count = sum(1 for backend, task in matched_tasks if backend and backend_is_up(backend)) rows.append( " %s - %s" % (location, haproxy_backend_report( expected_count_per_location, running_count))) # If verbose mode is specified, show status of individual backends if verbose: for backend, task in matched_tasks: if backend is not None: rows.append( format_haproxy_backend_row(backend, task is not None)) return format_table(rows)
def pretty_print_smartstack_backends_for_locations(service_instance, tasks, locations, expected_count, verbose, synapse_port, synapse_haproxy_url_format): """ Pretty prints the status of smartstack backends of a specified service and instance in the specified locations """ rows = [(" Name", "LastCheck", "LastChange", "Status")] if verbose else [] expected_count_per_location = int(expected_count / len(locations)) for location in sorted(locations): hosts = locations[location] # arbitrarily choose the first host with a given attribute to query for replication stats synapse_host = hosts[0] sorted_backends = sorted( get_backends( service_instance, synapse_host=synapse_host, synapse_port=synapse_port, synapse_haproxy_url_format=synapse_haproxy_url_format, ), key=lambda backend: backend['status'], reverse=True, # Specify reverse so that backends in 'UP' are placed above 'MAINT' ) matched_tasks = match_backends_and_tasks(sorted_backends, tasks) running_count = sum(1 for backend, task in matched_tasks if backend and backend_is_up(backend)) rows.append(" %s - %s" % (location, haproxy_backend_report(expected_count_per_location, running_count))) # If verbose mode is specified, show status of individual backends if verbose: for backend, task in matched_tasks: if backend is not None: rows.append(format_haproxy_backend_row(backend, task is not None)) return format_table(rows)
def test_match_backends_and_tasks(): backends = [ { "pxname": "servicename.main", "svname": "10.50.2.4:31000_box4", "status": "UP" }, { "pxname": "servicename.main", "svname": "10.50.2.5:31001_box5", "status": "UP" }, { "pxname": "servicename.main", "svname": "10.50.2.6:31001_box6", "status": "UP" }, { "pxname": "servicename.main", "svname": "10.50.2.6:31002_box7", "status": "UP" }, { "pxname": "servicename.main", "svname": "10.50.2.8:31000_box8", "status": "UP" }, ] hostnames = { 'box4': '10.50.2.4', 'box5': '10.50.2.5', 'box6': '10.50.2.6', 'box7': '10.50.2.7', 'box8': '10.50.2.8', } good_task1 = mock.Mock(host='box4', ports=[31000]) good_task2 = mock.Mock(host='box5', ports=[31001]) bad_task = mock.Mock(host='box7', ports=[31000]) tasks = [good_task1, good_task2, bad_task] with mock.patch( 'paasta_tools.monitoring.replication_utils.' 'socket.gethostbyname', side_effect=lambda x: hostnames[x], ): expected = [ (backends[0], good_task1), (backends[1], good_task2), (None, bad_task), (backends[2], None), (backends[3], None), (backends[4], None), ] actual = match_backends_and_tasks(backends, tasks) assert sorted(actual) == sorted(expected)
def test_match_backends_and_tasks(): backends = [ {"pxname": "servicename.main", "svname": "10.50.2.4:31000_box4", "status": "UP"}, {"pxname": "servicename.main", "svname": "10.50.2.5:31001_box5", "status": "UP"}, {"pxname": "servicename.main", "svname": "10.50.2.6:31001_box6", "status": "UP"}, {"pxname": "servicename.main", "svname": "10.50.2.6:31002_box7", "status": "UP"}, {"pxname": "servicename.main", "svname": "10.50.2.8:31000_box8", "status": "UP"}, ] hostnames = { 'box4': '10.50.2.4', 'box5': '10.50.2.5', 'box6': '10.50.2.6', 'box7': '10.50.2.7', 'box8': '10.50.2.8', } good_task1 = mock.Mock(host='box4', ports=[31000]) good_task2 = mock.Mock(host='box5', ports=[31001]) bad_task = mock.Mock(host='box7', ports=[31000]) tasks = [good_task1, good_task2, bad_task] with mock.patch( 'paasta_tools.monitoring.replication_utils.' 'socket.gethostbyname', side_effect=lambda x: hostnames[x], ): expected = [ (backends[0], good_task1), (backends[1], good_task2), (None, bad_task), (backends[2], None), (backends[3], None), (backends[4], None), ] actual = match_backends_and_tasks(backends, tasks) assert sorted(actual) == sorted(expected)