How to use list_routers_on_l3_agent method in tempest

Best Python code snippet using tempest_python

neutron-ha-tool.py

Source:neutron-ha-tool.py Github

copy

Full Screen

...133 LOG.info("No rebalancing required for 1 or fewer agents")134 return135 for l3_agent in agents:136 l3_agent_dict[l3_agent['id']] = \137 list_routers_on_l3_agent(qclient, l3_agent['id'])138 ordered_l3_agent_dict = OrderedDict(sorted(l3_agent_dict.items(),139 key=lambda t: len(t[0])))140 ordered_l3_agent_list = list(ordered_l3_agent_dict)141 num_agents = len(ordered_l3_agent_list)142 LOG.info("Agent list: %s", ordered_l3_agent_list[0:(num_agents-1/2)+1])143 i = 0144 for agent in ordered_l3_agent_list[0:num_agents-1/2]:145 low_agent_id = ordered_l3_agent_list[i]146 hgh_agent_id = ordered_l3_agent_list[-(i+1)]147 # do nothing if we end up comparing the same router148 if low_agent_id == hgh_agent_id:149 continue150 LOG.info("Examining low_agent=%s, high_agent=%s",151 low_agent_id, hgh_agent_id)152 low_agent_router_count = len(l3_agent_dict[low_agent_id])153 hgh_agent_router_count = len(l3_agent_dict[hgh_agent_id])154 LOG.info("Low Count=%s, High Count=%s",155 low_agent_router_count, hgh_agent_router_count)156 for router_id in l3_agent_dict[hgh_agent_id]:157 if low_agent_router_count >= hgh_agent_router_count:158 break159 else:160 LOG.info("Migrating router=%s from agent=%s to agent=%s",161 router_id, hgh_agent_id, low_agent_id)162 try:163 if not noop:164 migrate_router(qclient, router_id, hgh_agent_id,165 low_agent_id)166 low_agent_router_count += 1167 hgh_agent_router_count -= 1168 except:169 LOG.exception("Failed to migrate router=%s from agent=%s "170 "to agent=%s", router_id, hgh_agent_id,171 low_agent_id)172 continue173 i += 1174def l3_agent_check(qclient):175 """176 Walk the l3 agents searching for agents that are offline. Show routers177 that are offline and where we would migrate them to.178 :param qclient: A neutronclient179 """180 migration_count = 0181 agent_list = list_agents(qclient)182 agent_dead_list = agent_dead_id_list(agent_list, 'L3 agent')183 agent_alive_list = agent_alive_id_list(agent_list, 'L3 agent')184 LOG.info("There are %s offline L3 agents and %s online L3 agents",185 len(agent_dead_list), len(agent_alive_list))186 if len(agent_dead_list) > 0:187 for agent_id in agent_dead_list:188 LOG.info("Querying agent_id=%s for routers to migrate", agent_id)189 router_id_list = list_routers_on_l3_agent(qclient, agent_id)190 for router_id in router_id_list:191 try:192 target_id = random.choice(agent_alive_list)193 except IndexError:194 LOG.warn("There are no l3 agents alive we could "195 "migrate routers onto.")196 target_id = None197 migration_count += 1198 LOG.warn("Would like to migrate router=%s to agent=%s",199 router_id, target_id)200 if migration_count > 0:201 sys.exit(2)202def l3_agent_migrate(qclient, noop=False, now=False):203 """204 Walk the l3 agents searching for agents that are offline. For those that205 are offline, we will retrieve a list of routers on them and migrate them to206 a random l3 agent that is online.207 :param qclient: A neutronclient208 :param noop: Optional noop flag209 :param now: Optional. If false (the default), we'll wait for a random210 amount of time (between 30 and 60 seconds) before migration. If211 true, routers are migrated immediately.212 """213 migration_count = 0214 agent_list = list_agents(qclient)215 agent_dead_list = agent_dead_id_list(agent_list, 'L3 agent')216 agent_alive_list = agent_alive_id_list(agent_list, 'L3 agent')217 LOG.info("There are %s offline L3 agents and %s online L3 agents",218 len(agent_dead_list), len(agent_alive_list))219 if len(agent_dead_list) > 0:220 if len(agent_alive_list) < 1:221 LOG.exception("There are no l3 agents alive to migrate "222 "routers onto")223 timeout = 0224 if not now:225 while timeout < TAKEOVER_DELAY:226 agent_list_new = list_agents(qclient)227 agent_dead_list_new = agent_dead_id_list(agent_list_new,228 'L3 agent')229 if len(agent_dead_list_new) < len(agent_dead_list):230 LOG.info("Skipping router failover since an agent came "231 "online while ensuring agents offline for %s "232 "seconds", TAKEOVER_DELAY)233 sys.exit(0)234 LOG.info("Agent found offline for seconds=%s but waiting "235 "seconds=%s before migration",236 timeout, TAKEOVER_DELAY)237 timeout += 1238 time.sleep(1)239 for agent_id in agent_dead_list:240 LOG.info("Querying agent_id=%s for routers to migrate", agent_id)241 router_id_list = list_routers_on_l3_agent(qclient, agent_id)242 for router_id in router_id_list:243 target_id = random.choice(agent_alive_list)244 LOG.info("Migrating router=%s to agent=%s",245 router_id, target_id)246 try:247 if not noop:248 migrate_router(qclient, router_id, agent_id, target_id)249 migration_count += 1250 except:251 LOG.exception("There was an error migrating a router")252 continue253 LOG.info("%s routers required migration from offline L3 agents",254 migration_count)255def replicate_dhcp(qclient, noop=False):256 """257 Retrieve a network list and then probe each DHCP agent to ensure258 they have that network assigned.259 :param qclient: A neutronclient260 :param noop: Optional noop flag261 """262 added = 0263 networks = list_networks(qclient)264 network_id_list = [n['id'] for n in networks]265 agents = list_agents(qclient, agent_type='DHCP agent')266 LOG.info("Replicating %s networks to %s DHCP agents", len(networks),267 len(agents))268 for dhcp_agent_id in [a['id'] for a in agents]:269 networks_on_agent = \270 qclient.list_networks_on_dhcp_agent(dhcp_agent_id)['networks']271 network_id_on_agent = [n['id'] for n in networks_on_agent]272 for network_id in network_id_list:273 if network_id not in network_id_on_agent:274 try:275 dhcp_body = {'network_id': network_id}276 if not noop:277 qclient.add_network_to_dhcp_agent(dhcp_agent_id,278 dhcp_body)279 LOG.info("Added missing network=%s to dhcp agent=%s",280 network_id, dhcp_agent_id)281 added += 1282 except:283 LOG.exception("Failed to add network_id=%s to"284 "dhcp_agent=%s", network_id, dhcp_agent_id)285 continue286 LOG.info("Added %s networks to DHCP agents", added)287def migrate_router(qclient, router_id, agent_id, target_id):288 """289 Returns nothing, and raises on exception290 :param qclient: A neutronclient291 :param router_id: The id of the router to migrate292 :param agent_id: The id of the l3 agent to migrate from293 :param target_id: The id of the l3 agent to migrate to294 """295 # N.B. The neutron API will return "success" even when there is a296 # subsequent failure during the add or remove process so we must check to297 # ensure the router has been added or removed298 # remove the router from the dead agent299 qclient.remove_router_from_l3_agent(agent_id, router_id)300 # ensure it is removed or log an error301 if router_id in list_routers_on_l3_agent(qclient, agent_id):302 LOG.exception("Failed to remove router_id=%s from agent_id=%s",303 router_id, agent_id)304 # add the router id to a live agent305 router_body = {'router_id': router_id}306 qclient.add_router_to_l3_agent(target_id, router_body)307 # ensure it is removed or log an error308 if router_id not in list_routers_on_l3_agent(qclient, target_id):309 LOG.exception("Failed to add router_id=%s from agent_id=%s",310 router_id, agent_id)311def list_networks(qclient):312 """313 Return a list of network objects314 :param qclient: A neutronclient315 """316 resp = qclient.list_networks()317 LOG.debug("list_networks: %s", resp)318 return resp['networks']319def list_dhcp_agent_networks(qclient, agent_id):320 """321 Return a list of network ids assigned to a particular DHCP agent322 :param qclient: A neutronclient323 :param agent_id: A DHCP agent id324 """325 resp = qclient.list_networks_on_dhcp_agent(agent_id)326 LOG.debug("list_networks_on_dhcp_agent: %s", resp)327 return [s['id'] for s in resp['networks']]328def list_routers(qclient):329 """330 Return a list of router objects331 :param qclient: A neutronclient332 # {'routers': [333 # {u'status': u'ACTIVE',334 # u'external_gateway_info':335 # {u'network_id': u'b970297c-d80e-4527-86d7-e49d2da9fdef'},336 # u'name': u'router1',337 # u'admin_state_up': True,338 # u'tenant_id': u'5603b97ee7f047ea999e25492c7fcb23',339 # u'routes': [],340 # u'id': u'0a122e5c-1623-412e-8c53-a1e21d1daff8'}341 # ]}342 """343 resp = qclient.list_routers()344 LOG.debug("list_routers: %s", resp)345 return resp['routers']346def list_routers_on_l3_agent(qclient, agent_id):347 """348 Return a list of router ids on an agent349 :param qclient: A neutronclient350 """351 resp = qclient.list_routers_on_l3_agent(agent_id)352 LOG.debug("list_routers_on_l3_agent: %s", resp)353 return [r['id'] for r in resp['routers']]354def list_agents(qclient, agent_type=None):355 """Return a list of agent objects356 :param qclient: A neutronclient357 # {u'agents': [358 # {u'binary': u'neutron-openvswitch-agent',359 # u'description': None,360 # u'admin_state_up': True,361 # u'heartbeat_timestamp': u'2013-07-02 22:20:25',362 # u'alive': True,363 # u'topic': u'N/A',364 # u'host': u'o3r3.int.san3.attcompute.com',365 # u'agent_type': u'Open vSwitch agent',...

Full Screen

Full Screen

quantum-ha-tool.py

Source:quantum-ha-tool.py Github

copy

Full Screen

...95 LOG.info("No rebalancing required for 1 or fewer agents")96 return97 for l3_agent in agents:98 num_routers=l3_agent['configurations']['routers']99 l3_agent_dict[l3_agent['id']] = list_routers_on_l3_agent(qclient, l3_agent['id'])100 ordered_l3_agent_dict = OrderedDict(sorted(l3_agent_dict.items(), key=lambda t: len(t[0])))101 ordered_l3_agent_list = list(ordered_l3_agent_dict)102 num_agents = len(ordered_l3_agent_list)103 LOG.info("Agent list: %s", ordered_l3_agent_list[0:(num_agents-1/2)+1])104 i=0105 for agent in ordered_l3_agent_list[0:num_agents-1/2]:106 low_agent_id=ordered_l3_agent_list[i]107 hgh_agent_id=ordered_l3_agent_list[-(i+1)]108 # do nothing if we end up comparing the same router109 if low_agent_id == hgh_agent_id:110 continue111 LOG.info("Examining low_agent=%s, high_agent=%s", low_agent_id, hgh_agent_id)112 low_agent_router_count = len(l3_agent_dict[low_agent_id])113 hgh_agent_router_count = len(l3_agent_dict[hgh_agent_id])114 LOG.info("Low Count=%s, High Count=%s", low_agent_router_count, hgh_agent_router_count)115 for router_id in l3_agent_dict[hgh_agent_id]:116 if low_agent_router_count >= hgh_agent_router_count:117 break118 else:119 LOG.info("Migrating router=%s from agent=%s to agent=%s", router_id, hgh_agent_id, low_agent_id)120 try:121 if not noop:122 migrate_router(qclient, router_id, hgh_agent_id, low_agent_id)123 low_agent_router_count += 1124 hgh_agent_router_count -= 1125 except:126 LOG.traceback("Failed to migrate router=%s from agent=%s to agent=%s" % (router_id, hgh_agent_id, low_agent_id))127 continue128 i+=1129def l3_agent_check(qclient, noop=False):130 """131 Walk the l3 agents searching for agents that are offline. Show routers132 that are offline and where we would migrate them too.133 :param qclient: A quantumclient134 :param noop: Optional noop flag135 """136 migration_count = 0137 agent_list = list_agents(qclient)138 agent_dead_list = agent_dead_id_list(agent_list, 'L3 agent')139 agent_alive_list = agent_alive_id_list(agent_list, 'L3 agent')140 LOG.info("There are %s offline L3 agents and %s online L3 agents", len(agent_dead_list), len(agent_alive_list))141 if len(agent_dead_list) > 0:142 for agent_id in agent_dead_list:143 LOG.info("Querying agent_id=%s for routers to migrate", agent_id)144 router_id_list = list_routers_on_l3_agent(qclient, agent_id)145 for router_id in router_id_list:146 try:147 target_id = choice(agent_alive_list)148 except:149 LOG.warn("There are no l3 agents alive we could migrate routers onto")150 target_id = None151 LOG.info("Would like to migrate router=%s to agent=%s", router_id, target_id)152def l3_agent_migrate(qclient, noop=False):153 """154 Walk the l3 agents searching for agents that are offline. For those that are155 offline, we will retrieve a list of routers on them and migrate them to a156 random l3 agent that is online.157 :param qclient: A quantumclient158 :param noop: Optional noop flag159 """160 migration_count = 0161 agent_list = list_agents(qclient)162 agent_dead_list = agent_dead_id_list(agent_list, 'L3 agent')163 agent_alive_list = agent_alive_id_list(agent_list, 'L3 agent')164 LOG.info("There are %s offline L3 agents and %s online L3 agents", len(agent_dead_list), len(agent_alive_list))165 if len(agent_dead_list) > 0:166 if len(agent_alive_list) < 1:167 LOG.exception("There are no l3 agents alive to migrate routers onto")168 for agent_id in agent_dead_list:169 LOG.info("Querying agent_id=%s for routers to migrate", agent_id)170 router_id_list = list_routers_on_l3_agent(qclient, agent_id)171 for router_id in router_id_list:172 target_id = choice(agent_alive_list)173 LOG.info("Migrating router=%s to agent=%s", router_id, target_id)174 router_body = {'router_id': router_id}175 try:176 if not noop:177 migrate_router(qclient, router_id, agent_id, target_id)178 migration_count+=1179 except:180 LOG.exception("There was an error migrating a router")181 continue182 LOG.info("%s routers required migration from offline L3 agents", migration_count)183def replicate_dhcp(qclient, noop=False):184 """185 Retrieve a network list and then probe each DHCP agent to ensure they have that186 network assigned.187 :param qclient: A quantumclient188 :param noop: Optional noop flag189 """190 added=0191 networks = list_networks(qclient)192 network_id_list = [n['id'] for n in networks]193 agents = list_agents(qclient, agent_type='DHCP agent')194 LOG.info("Replicating %s networks to %s DHCP agents", len(networks), len(agents))195 for dhcp_agent_id in [a['id'] for a in agents]:196 networks_on_agent = qclient.list_networks_on_dhcp_agent(dhcp_agent_id)['networks']197 network_id_on_agent = [n['id'] for n in networks_on_agent]198 for network_id in network_id_list:199 if network_id not in network_id_on_agent:200 try:201 dhcp_body = {'network_id': network_id}202 if not noop:203 qclient.add_network_to_dhcp_agent(dhcp_agent_id, dhcp_body)204 LOG.info("Added missing network=%s to dhcp agent=%s", network_id, dhcp_agent_id)205 added+=1206 except:207 LOG.exception("Failed to add network_id=%s to dhcp_agent=%s", network_id, dhcp_agent_id)208 continue209 LOG.info("Added %s networks to DHCP agents", added)210def migrate_router(qclient, router_id, agent_id, target_id):211 """212 Returns nothing, and raises on exception213 :param qclient: A quantumclient214 :param router_id: The id of the router to migrate215 :param agent_id: The id of the l3 agent to migrate from216 :param target_id: The id of the l3 agent to migrate to217 """218 # N.B. The quantum API will return "success" even when there is a subsequent219 # failure during the add or remove process so we must check to ensure the220 # router has been added or removed221 # remove the router from the dead agent222 qclient.remove_router_from_l3_agent(agent_id, router_id)223 # ensure it is removed or log an error224 if router_id in list_routers_on_l3_agent(qclient, agent_id):225 LOG.exception("Failed to remove router_id=%s from agent_id=%s", router_id, agent_id)226 # add the router id to a live agent227 router_body = {'router_id': router_id}228 qclient.add_router_to_l3_agent(target_id, router_body)229 # ensure it is removed or log an error230 if router_id not in list_routers_on_l3_agent(qclient, target_id):231 LOG.exception("Failed to add router_id=%s from agent_id=%s", router_id, agent_id)232def list_networks(qclient):233 """234 Return a list of network objects235 :param qclient: A quantumclient236 """237 resp = qclient.list_networks()238 LOG.debug("list_networks: %s", resp)239 return resp['networks']240def list_dhcp_agent_networks(qclient, agent_id):241 """242 Return a list of network ids assigned to a particular DHCP agent243 :param qclient: A quantumclient244 :param agent_id: A DHCP agent id245 """246 resp = qclient.list_networks_on_dhcp_agent(agent_id)247 LOG.debug("list_networks_on_dhcp_agent: %s", resp)248 return [s['id'] for s in resp['networks']]249def list_routers(qclient):250 """251 Return a list of router objects252 :param qclient: A quantumclient253 # {'routers': [{u'status': u'ACTIVE', u'external_gateway_info': {u'network_id': u'b970297c-d80e-4527-86d7-e49d2da9fdef'}, u'name': u'router1',254 # u'admin_state_up': True, u'tenant_id': u'5603b97ee7f047ea999e25492c7fcb23', u'routes': [], u'id': u'0a122e5c-1623-412e-8c53-a1e21d1daff8'},255 """256 resp = qclient.list_routers()257 LOG.debug("list_routers: %s", resp)258 return resp['routers']259def list_routers_on_l3_agent(qclient, agent_id):260 """261 Return a list of router ids on an agent262 :param qclient: A quantumclient263 """264 resp = qclient.list_routers_on_l3_agent(agent_id)265 LOG.debug("list_routers_on_l3_agent: %s", resp)266 return [r['id'] for r in resp['routers']]267def list_agents(qclient, agent_type=None):268 """269 Return a list of agent objects270 :param qclient: A quantumclient271 # openvswitch272 #273 # {u'agents': [{u'binary': u'quantum-openvswitch-agent', u'description': None, u'admin_state_up': True, u'heartbeat_timestamp': u'2013-07-02 22:20:25'274 # u'alive': True, u'topic': u'N/A', u'host': u'o3r3.int.san3.attcompute.com', u'agent_type': u'Open vSwitch agent', u'created_at': u'2013-07-02 14:50:57',275 # u'started_at': u'2013-07-02 14:50:57', u'id': u'3a577f1d-d86e-4f1a-a395-8d4c8e4df1e2', u'configurations': {u'devices': 10}},276 #277 # dhcp278 #...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful