~bjornt/charms/trusty/quantum-gateway-hardcode-eth1/trunk

« back to all changes in this revision

Viewing changes to templates/evacuate_unit.py

  • Committer: James Page
  • Date: 2013-10-15 08:27:59 UTC
  • mfrom: (33.1.31 quantum-gateway)
  • Revision ID: james.page@canonical.com-20131015082759-he6nc740v1ublr6y
Merge of python-redux work for havana cycle

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
import subprocess
4
 
 
5
 
 
6
 
def log(priority, message):
7
 
    print "{}: {}".format(priority, message)
8
 
 
9
 
DHCP_AGENT = "DHCP Agent"
10
 
L3_AGENT = "L3 Agent"
11
 
 
12
 
 
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 }}',
21
 
                            auth_url=auth_url,
22
 
                            region_name='{{ region }}')
23
 
 
24
 
    agents = quantum.list_agents(agent_type=DHCP_AGENT)
25
 
    dhcp_agents = []
26
 
    l3_agents = []
27
 
    networks = {}
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:
32
 
            for network in \
33
 
                quantum.list_networks_on_dhcp_agent(agent['id'])['networks']:
34
 
                networks[network['id']] = agent['id']
35
 
 
36
 
    agents = quantum.list_agents(agent_type=L3_AGENT)
37
 
    routers = {}
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:
42
 
            for router in \
43
 
                quantum.list_routers_on_l3_agent(agent['id'])['routers']:
44
 
                routers[router['id']] = agent['id']
45
 
 
46
 
    index = 0
47
 
    for router_id in routers:
48
 
        agent = index % len(l3_agents)
49
 
        log('INFO',
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],
53
 
                                            router_id=router_id)
54
 
        quantum.add_router_to_l3_agent(l3_agent=l3_agents[agent],
55
 
                                       body={'router_id': router_id})
56
 
        index += 1
57
 
 
58
 
    index = 0
59
 
    for network_id in networks:
60
 
        agent = index % len(dhcp_agents)
61
 
        log('INFO',
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})
68
 
        index += 1
69
 
 
70
 
evacuate_unit(subprocess.check_output(['hostname', '-f']).strip())