6
def log(priority, message):
7
print "{}: {}".format(priority, message)
9
DHCP_AGENT = "DHCP Agent"
13
def evacuate_unit(unit):
14
''' Use agent scheduler API to detect down agents and re-schedule '''
15
from quantumclient.v2_0 import client
16
# TODO: Fixup for https keystone
17
auth_url = 'http://{{ keystone_host }}:{{ auth_port }}/v2.0'
18
quantum = client.Client(username='{{ service_username }}',
19
password='{{ service_password }}',
20
tenant_name='{{ service_tenant }}',
22
region_name='{{ region }}')
24
agents = quantum.list_agents(agent_type=DHCP_AGENT)
28
for agent in agents['agents']:
29
if agent['alive'] and agent['host'] != unit:
30
dhcp_agents.append(agent['id'])
31
elif agent['host'] == unit:
33
quantum.list_networks_on_dhcp_agent(agent['id'])['networks']:
34
networks[network['id']] = agent['id']
36
agents = quantum.list_agents(agent_type=L3_AGENT)
38
for agent in agents['agents']:
39
if agent['alive'] and agent['host'] != unit:
40
l3_agents.append(agent['id'])
41
elif agent['host'] == unit:
43
quantum.list_routers_on_l3_agent(agent['id'])['routers']:
44
routers[router['id']] = agent['id']
47
for router_id in routers:
48
agent = index % len(l3_agents)
50
'Moving router %s from %s to %s' % \
51
(router_id, routers[router_id], l3_agents[agent]))
52
quantum.remove_router_from_l3_agent(l3_agent=routers[router_id],
54
quantum.add_router_to_l3_agent(l3_agent=l3_agents[agent],
55
body={'router_id': router_id})
59
for network_id in networks:
60
agent = index % len(dhcp_agents)
62
'Moving network %s from %s to %s' % \
63
(network_id, networks[network_id], dhcp_agents[agent]))
64
quantum.remove_network_from_dhcp_agent(dhcp_agent=networks[network_id],
65
network_id=network_id)
66
quantum.add_network_to_dhcp_agent(dhcp_agent=dhcp_agents[agent],
67
body={'network_id': network_id})
70
evacuate_unit(subprocess.check_output(['hostname', '-f']).strip())