~pwlars/charms/trusty/snappy-device-agent/first-iteration

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/neutron.py

  • Committer: Paul Larson
  • Date: 2015-08-14 20:10:33 UTC
  • Revision ID: paul.larson@canonical.com-20150814201033-e83hz07rh4u18kcw
snappy-device-agent charm for deploying provisioning kits to work with SPI

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
# Various utilies for dealing with Neutron and the renaming from Quantum.
 
18
 
 
19
from subprocess import check_output
 
20
 
 
21
from charmhelpers.core.hookenv import (
 
22
    config,
 
23
    log,
 
24
    ERROR,
 
25
)
 
26
 
 
27
from charmhelpers.contrib.openstack.utils import os_release
 
28
 
 
29
 
 
30
def headers_package():
 
31
    """Ensures correct linux-headers for running kernel are installed,
 
32
    for building DKMS package"""
 
33
    kver = check_output(['uname', '-r']).decode('UTF-8').strip()
 
34
    return 'linux-headers-%s' % kver
 
35
 
 
36
QUANTUM_CONF_DIR = '/etc/quantum'
 
37
 
 
38
 
 
39
def kernel_version():
 
40
    """ Retrieve the current major kernel version as a tuple e.g. (3, 13) """
 
41
    kver = check_output(['uname', '-r']).decode('UTF-8').strip()
 
42
    kver = kver.split('.')
 
43
    return (int(kver[0]), int(kver[1]))
 
44
 
 
45
 
 
46
def determine_dkms_package():
 
47
    """ Determine which DKMS package should be used based on kernel version """
 
48
    # NOTE: 3.13 kernels have support for GRE and VXLAN native
 
49
    if kernel_version() >= (3, 13):
 
50
        return []
 
51
    else:
 
52
        return ['openvswitch-datapath-dkms']
 
53
 
 
54
 
 
55
# legacy
 
56
 
 
57
 
 
58
def quantum_plugins():
 
59
    from charmhelpers.contrib.openstack import context
 
60
    return {
 
61
        'ovs': {
 
62
            'config': '/etc/quantum/plugins/openvswitch/'
 
63
                      'ovs_quantum_plugin.ini',
 
64
            'driver': 'quantum.plugins.openvswitch.ovs_quantum_plugin.'
 
65
                      'OVSQuantumPluginV2',
 
66
            'contexts': [
 
67
                context.SharedDBContext(user=config('neutron-database-user'),
 
68
                                        database=config('neutron-database'),
 
69
                                        relation_prefix='neutron',
 
70
                                        ssl_dir=QUANTUM_CONF_DIR)],
 
71
            'services': ['quantum-plugin-openvswitch-agent'],
 
72
            'packages': [[headers_package()] + determine_dkms_package(),
 
73
                         ['quantum-plugin-openvswitch-agent']],
 
74
            'server_packages': ['quantum-server',
 
75
                                'quantum-plugin-openvswitch'],
 
76
            'server_services': ['quantum-server']
 
77
        },
 
78
        'nvp': {
 
79
            'config': '/etc/quantum/plugins/nicira/nvp.ini',
 
80
            'driver': 'quantum.plugins.nicira.nicira_nvp_plugin.'
 
81
                      'QuantumPlugin.NvpPluginV2',
 
82
            'contexts': [
 
83
                context.SharedDBContext(user=config('neutron-database-user'),
 
84
                                        database=config('neutron-database'),
 
85
                                        relation_prefix='neutron',
 
86
                                        ssl_dir=QUANTUM_CONF_DIR)],
 
87
            'services': [],
 
88
            'packages': [],
 
89
            'server_packages': ['quantum-server',
 
90
                                'quantum-plugin-nicira'],
 
91
            'server_services': ['quantum-server']
 
92
        }
 
93
    }
 
94
 
 
95
NEUTRON_CONF_DIR = '/etc/neutron'
 
96
 
 
97
 
 
98
def neutron_plugins():
 
99
    from charmhelpers.contrib.openstack import context
 
100
    release = os_release('nova-common')
 
101
    plugins = {
 
102
        'ovs': {
 
103
            'config': '/etc/neutron/plugins/openvswitch/'
 
104
                      'ovs_neutron_plugin.ini',
 
105
            'driver': 'neutron.plugins.openvswitch.ovs_neutron_plugin.'
 
106
                      'OVSNeutronPluginV2',
 
107
            'contexts': [
 
108
                context.SharedDBContext(user=config('neutron-database-user'),
 
109
                                        database=config('neutron-database'),
 
110
                                        relation_prefix='neutron',
 
111
                                        ssl_dir=NEUTRON_CONF_DIR)],
 
112
            'services': ['neutron-plugin-openvswitch-agent'],
 
113
            'packages': [[headers_package()] + determine_dkms_package(),
 
114
                         ['neutron-plugin-openvswitch-agent']],
 
115
            'server_packages': ['neutron-server',
 
116
                                'neutron-plugin-openvswitch'],
 
117
            'server_services': ['neutron-server']
 
118
        },
 
119
        'nvp': {
 
120
            'config': '/etc/neutron/plugins/nicira/nvp.ini',
 
121
            'driver': 'neutron.plugins.nicira.nicira_nvp_plugin.'
 
122
                      'NeutronPlugin.NvpPluginV2',
 
123
            'contexts': [
 
124
                context.SharedDBContext(user=config('neutron-database-user'),
 
125
                                        database=config('neutron-database'),
 
126
                                        relation_prefix='neutron',
 
127
                                        ssl_dir=NEUTRON_CONF_DIR)],
 
128
            'services': [],
 
129
            'packages': [],
 
130
            'server_packages': ['neutron-server',
 
131
                                'neutron-plugin-nicira'],
 
132
            'server_services': ['neutron-server']
 
133
        },
 
134
        'nsx': {
 
135
            'config': '/etc/neutron/plugins/vmware/nsx.ini',
 
136
            'driver': 'vmware',
 
137
            'contexts': [
 
138
                context.SharedDBContext(user=config('neutron-database-user'),
 
139
                                        database=config('neutron-database'),
 
140
                                        relation_prefix='neutron',
 
141
                                        ssl_dir=NEUTRON_CONF_DIR)],
 
142
            'services': [],
 
143
            'packages': [],
 
144
            'server_packages': ['neutron-server',
 
145
                                'neutron-plugin-vmware'],
 
146
            'server_services': ['neutron-server']
 
147
        },
 
148
        'n1kv': {
 
149
            'config': '/etc/neutron/plugins/cisco/cisco_plugins.ini',
 
150
            'driver': 'neutron.plugins.cisco.network_plugin.PluginV2',
 
151
            'contexts': [
 
152
                context.SharedDBContext(user=config('neutron-database-user'),
 
153
                                        database=config('neutron-database'),
 
154
                                        relation_prefix='neutron',
 
155
                                        ssl_dir=NEUTRON_CONF_DIR)],
 
156
            'services': [],
 
157
            'packages': [[headers_package()] + determine_dkms_package(),
 
158
                         ['neutron-plugin-cisco']],
 
159
            'server_packages': ['neutron-server',
 
160
                                'neutron-plugin-cisco'],
 
161
            'server_services': ['neutron-server']
 
162
        },
 
163
        'Calico': {
 
164
            'config': '/etc/neutron/plugins/ml2/ml2_conf.ini',
 
165
            'driver': 'neutron.plugins.ml2.plugin.Ml2Plugin',
 
166
            'contexts': [
 
167
                context.SharedDBContext(user=config('neutron-database-user'),
 
168
                                        database=config('neutron-database'),
 
169
                                        relation_prefix='neutron',
 
170
                                        ssl_dir=NEUTRON_CONF_DIR)],
 
171
            'services': ['calico-felix',
 
172
                         'bird',
 
173
                         'neutron-dhcp-agent',
 
174
                         'nova-api-metadata'],
 
175
            'packages': [[headers_package()] + determine_dkms_package(),
 
176
                         ['calico-compute',
 
177
                          'bird',
 
178
                          'neutron-dhcp-agent',
 
179
                          'nova-api-metadata']],
 
180
            'server_packages': ['neutron-server', 'calico-control'],
 
181
            'server_services': ['neutron-server']
 
182
        }
 
183
    }
 
184
    if release >= 'icehouse':
 
185
        # NOTE: patch in ml2 plugin for icehouse onwards
 
186
        plugins['ovs']['config'] = '/etc/neutron/plugins/ml2/ml2_conf.ini'
 
187
        plugins['ovs']['driver'] = 'neutron.plugins.ml2.plugin.Ml2Plugin'
 
188
        plugins['ovs']['server_packages'] = ['neutron-server',
 
189
                                             'neutron-plugin-ml2']
 
190
        # NOTE: patch in vmware renames nvp->nsx for icehouse onwards
 
191
        plugins['nvp'] = plugins['nsx']
 
192
    return plugins
 
193
 
 
194
 
 
195
def neutron_plugin_attribute(plugin, attr, net_manager=None):
 
196
    manager = net_manager or network_manager()
 
197
    if manager == 'quantum':
 
198
        plugins = quantum_plugins()
 
199
    elif manager == 'neutron':
 
200
        plugins = neutron_plugins()
 
201
    else:
 
202
        log("Network manager '%s' does not support plugins." % (manager),
 
203
            level=ERROR)
 
204
        raise Exception
 
205
 
 
206
    try:
 
207
        _plugin = plugins[plugin]
 
208
    except KeyError:
 
209
        log('Unrecognised plugin for %s: %s' % (manager, plugin), level=ERROR)
 
210
        raise Exception
 
211
 
 
212
    try:
 
213
        return _plugin[attr]
 
214
    except KeyError:
 
215
        return None
 
216
 
 
217
 
 
218
def network_manager():
 
219
    '''
 
220
    Deals with the renaming of Quantum to Neutron in H and any situations
 
221
    that require compatability (eg, deploying H with network-manager=quantum,
 
222
    upgrading from G).
 
223
    '''
 
224
    release = os_release('nova-common')
 
225
    manager = config('network-manager').lower()
 
226
 
 
227
    if manager not in ['quantum', 'neutron']:
 
228
        return manager
 
229
 
 
230
    if release in ['essex']:
 
231
        # E does not support neutron
 
232
        log('Neutron networking not supported in Essex.', level=ERROR)
 
233
        raise Exception
 
234
    elif release in ['folsom', 'grizzly']:
 
235
        # neutron is named quantum in F and G
 
236
        return 'quantum'
 
237
    else:
 
238
        # ensure accurate naming for all releases post-H
 
239
        return 'neutron'