~james-page/charms/trusty/quantum-gateway/nsx-update

« back to all changes in this revision

Viewing changes to hooks/quantum_utils.py

  • Committer: Adam Gandelman
  • Date: 2013-06-03 17:35:03 UTC
  • mfrom: (25.1.32 quantum-gateway)
  • Revision ID: adamg@canonical.com-20130603173503-qg646m67re20r3yn
Support for Grizzly. Support for HA.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import subprocess
2
 
from utils import juju_log as log
 
2
import os
 
3
import uuid
 
4
import base64
 
5
import apt_pkg as apt
 
6
from lib.utils import (
 
7
    juju_log as log,
 
8
    configure_source,
 
9
    config_get
 
10
    )
3
11
 
4
12
 
5
13
OVS = "ovs"
6
 
NVP = "nvp"
7
14
 
8
15
OVS_PLUGIN = \
9
16
    "quantum.plugins.openvswitch.ovs_quantum_plugin.OVSQuantumPluginV2"
10
 
NVP_PLUGIN = \
11
 
    "quantum.plugins.nicira.nicira_nvp_plugin.QuantumPlugin.NvpPluginV2"
12
17
CORE_PLUGIN = {
13
18
    OVS: OVS_PLUGIN,
14
 
    NVP: NVP_PLUGIN
15
19
    }
16
20
 
17
21
OVS_PLUGIN_CONF = \
18
22
    "/etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini"
19
 
NVP_PLUGIN_CONF = \
20
 
    "/etc/quantum/plugins/nicira/nvp.ini"
21
23
PLUGIN_CONF = {
22
24
    OVS: OVS_PLUGIN_CONF,
23
 
    NVP: NVP_PLUGIN_CONF
24
25
    }
25
26
 
26
27
GATEWAY_PKGS = {
28
29
        "quantum-plugin-openvswitch-agent",
29
30
        "quantum-l3-agent",
30
31
        "quantum-dhcp-agent",
31
 
        'python-mysqldb'
 
32
        'python-mysqldb',
 
33
        "nova-api-metadata"
32
34
        ],
33
 
    NVP: [
34
 
        "quantum-plugin-nicira"
35
 
        ]
36
35
    }
37
36
 
38
37
GATEWAY_AGENTS = {
39
38
    OVS: [
40
39
        "quantum-plugin-openvswitch-agent",
41
40
        "quantum-l3-agent",
42
 
        "quantum-dhcp-agent"
43
 
        ]
 
41
        "quantum-dhcp-agent",
 
42
        "nova-api-metadata"
 
43
        ],
44
44
    }
45
45
 
 
46
EXT_PORT_CONF = '/etc/init/ext-port.conf'
 
47
 
 
48
 
 
49
def get_os_version(package=None):
 
50
    apt.init()
 
51
    cache = apt.Cache()
 
52
    pkg = cache[package or 'quantum-common']
 
53
    if pkg.current_ver:
 
54
        return apt.upstream_version(pkg.current_ver.ver_str)
 
55
    else:
 
56
        return None
 
57
 
 
58
 
 
59
if get_os_version('quantum-common') >= "2013.1":
 
60
    for plugin in GATEWAY_AGENTS:
 
61
        GATEWAY_AGENTS[plugin].append("quantum-metadata-agent")
 
62
 
46
63
DB_USER = "quantum"
47
64
QUANTUM_DB = "quantum"
48
65
KEYSTONE_SERVICE = "quantum"
 
66
NOVA_DB_USER = "nova"
 
67
NOVA_DB = "nova"
49
68
 
50
69
QUANTUM_CONF = "/etc/quantum/quantum.conf"
51
70
L3_AGENT_CONF = "/etc/quantum/l3_agent.ini"
52
71
DHCP_AGENT_CONF = "/etc/quantum/dhcp_agent.ini"
 
72
METADATA_AGENT_CONF = "/etc/quantum/metadata_agent.ini"
 
73
NOVA_CONF = "/etc/nova/nova.conf"
 
74
 
 
75
RESTART_MAP = {
 
76
    QUANTUM_CONF: [
 
77
        'quantum-l3-agent',
 
78
        'quantum-dhcp-agent',
 
79
        'quantum-metadata-agent',
 
80
        'quantum-plugin-openvswitch-agent'
 
81
        ],
 
82
    DHCP_AGENT_CONF: [
 
83
        'quantum-dhcp-agent'
 
84
        ],
 
85
    L3_AGENT_CONF: [
 
86
        'quantum-l3-agent'
 
87
        ],
 
88
    METADATA_AGENT_CONF: [
 
89
        'quantum-metadata-agent'
 
90
        ],
 
91
    OVS_PLUGIN_CONF: [
 
92
        'quantum-plugin-openvswitch-agent'
 
93
        ],
 
94
    NOVA_CONF: [
 
95
        'nova-api-metadata'
 
96
        ]
 
97
    }
53
98
 
54
99
RABBIT_USER = "nova"
55
100
RABBIT_VHOST = "nova"
90
135
            'Deleting port {} from bridge {}'.format(port, name))
91
136
        subprocess.check_call(["ovs-vsctl", "del-port", name, port])
92
137
        subprocess.check_call(["ip", "link", "set", port, "down"])
 
138
 
 
139
 
 
140
SHARED_SECRET = "/etc/quantum/secret.txt"
 
141
 
 
142
 
 
143
def get_shared_secret():
 
144
    secret = None
 
145
    if not os.path.exists(SHARED_SECRET):
 
146
        secret = str(uuid.uuid4())
 
147
        with open(SHARED_SECRET, 'w') as secret_file:
 
148
            secret_file.write(secret)
 
149
    else:
 
150
        with open(SHARED_SECRET, 'r') as secret_file:
 
151
            secret = secret_file.read().strip()
 
152
    return secret
 
153
 
 
154
 
 
155
def flush_local_configuration():
 
156
    if os.path.exists('/usr/bin/quantum-netns-cleanup'):
 
157
        cmd = [
 
158
            "quantum-netns-cleanup",
 
159
            "--config-file=/etc/quantum/quantum.conf"
 
160
            ]
 
161
        for agent_conf in ['l3_agent.ini', 'dhcp_agent.ini']:
 
162
            agent_cmd = list(cmd)
 
163
            agent_cmd.append('--config-file=/etc/quantum/{}'\
 
164
                                .format(agent_conf))
 
165
            subprocess.call(agent_cmd)
 
166
 
 
167
 
 
168
def install_ca(ca_cert):
 
169
    with open('/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt',
 
170
              'w') as crt:
 
171
        crt.write(base64.b64decode(ca_cert))
 
172
    subprocess.check_call(['update-ca-certificates', '--fresh'])
 
173
 
 
174
DHCP_AGENT = "DHCP Agent"
 
175
L3_AGENT = "L3 Agent"
 
176
 
 
177
 
 
178
def reassign_agent_resources(env):
 
179
    ''' Use agent scheduler API to detect down agents and re-schedule '''
 
180
    from quantumclient.v2_0 import client
 
181
    # TODO: Fixup for https keystone
 
182
    auth_url = 'http://%(keystone_host)s:%(auth_port)s/v2.0' % env
 
183
    quantum = client.Client(username=env['service_username'],
 
184
                            password=env['service_password'],
 
185
                            tenant_name=env['service_tenant'],
 
186
                            auth_url=auth_url,
 
187
                            region_name=env['region'])
 
188
 
 
189
    agents = quantum.list_agents(agent_type=DHCP_AGENT)
 
190
    dhcp_agents = []
 
191
    l3_agents = []
 
192
    networks = {}
 
193
    for agent in agents['agents']:
 
194
        if not agent['alive']:
 
195
            log('INFO', 'DHCP Agent %s down' % agent['id'])
 
196
            for network in \
 
197
                quantum.list_networks_on_dhcp_agent(agent['id'])['networks']:
 
198
                networks[network['id']] = agent['id']
 
199
        else:
 
200
            dhcp_agents.append(agent['id'])
 
201
 
 
202
    agents = quantum.list_agents(agent_type=L3_AGENT)
 
203
    routers = {}
 
204
    for agent in agents['agents']:
 
205
        if not agent['alive']:
 
206
            log('INFO', 'L3 Agent %s down' % agent['id'])
 
207
            for router in \
 
208
                quantum.list_routers_on_l3_agent(agent['id'])['routers']:
 
209
                routers[router['id']] = agent['id']
 
210
        else:
 
211
            l3_agents.append(agent['id'])
 
212
 
 
213
    index = 0
 
214
    for router_id in routers:
 
215
        agent = index % len(l3_agents)
 
216
        log('INFO',
 
217
            'Moving router %s from %s to %s' % \
 
218
            (router_id, routers[router_id], l3_agents[agent]))
 
219
        quantum.remove_router_from_l3_agent(l3_agent=routers[router_id],
 
220
                                            router_id=router_id)
 
221
        quantum.add_router_to_l3_agent(l3_agent=l3_agents[agent],
 
222
                                       body={'router_id': router_id})
 
223
        index += 1
 
224
 
 
225
    index = 0
 
226
    for network_id in networks:
 
227
        agent = index % len(dhcp_agents)
 
228
        log('INFO',
 
229
            'Moving network %s from %s to %s' % \
 
230
            (network_id, networks[network_id], dhcp_agents[agent]))
 
231
        quantum.remove_network_from_dhcp_agent(dhcp_agent=networks[network_id],
 
232
                                               network_id=network_id)
 
233
        quantum.add_network_to_dhcp_agent(dhcp_agent=dhcp_agents[agent],
 
234
                                          body={'network_id': network_id})
 
235
        index += 1
 
236
 
 
237
def do_openstack_upgrade():
 
238
    configure_source()
 
239
    plugin = config_get('plugin')
 
240
    pkgs = []
 
241
    if plugin in GATEWAY_PKGS.keys():
 
242
        pkgs += GATEWAY_PKGS[plugin]
 
243
        if plugin == OVS:
 
244
            pkgs.append('openvswitch-datapath-dkms')
 
245
    cmd = ['apt-get', '-y',
 
246
           '--option', 'Dpkg::Options::=--force-confold',
 
247
           '--option', 'Dpkg::Options::=--force-confdef',
 
248
           'install'] + pkgs
 
249
    subprocess.check_call(cmd)