~openstack-charmers-next/charms/xenial/neutron-gateway/trunk

« back to all changes in this revision

Viewing changes to hooks/quantum_contexts.py

  • Committer: James Page
  • Date: 2015-05-07 09:27:07 UTC
  • Revision ID: james.page@ubuntu.com-20150507092707-gxxl6kmzv6aq4p3i
Rename charm to neutron-gateway including code

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: set ts=4:et
2
 
import os
3
 
import uuid
4
 
import socket
5
 
from charmhelpers.core.hookenv import (
6
 
    config,
7
 
    unit_get,
8
 
    cached
9
 
)
10
 
from charmhelpers.fetch import (
11
 
    apt_install,
12
 
)
13
 
from charmhelpers.contrib.openstack.context import (
14
 
    OSContextGenerator,
15
 
    NeutronAPIContext,
16
 
)
17
 
from charmhelpers.contrib.openstack.utils import (
18
 
    get_os_codename_install_source
19
 
)
20
 
from charmhelpers.contrib.hahelpers.cluster import(
21
 
    eligible_leader
22
 
)
23
 
from charmhelpers.contrib.network.ip import (
24
 
    get_address_in_network,
25
 
)
26
 
 
27
 
DB_USER = "quantum"
28
 
QUANTUM_DB = "quantum"
29
 
NOVA_DB_USER = "nova"
30
 
NOVA_DB = "nova"
31
 
 
32
 
QUANTUM_OVS_PLUGIN = \
33
 
    "quantum.plugins.openvswitch.ovs_quantum_plugin.OVSQuantumPluginV2"
34
 
QUANTUM_NVP_PLUGIN = \
35
 
    "quantum.plugins.nicira.nicira_nvp_plugin.QuantumPlugin.NvpPluginV2"
36
 
NEUTRON_OVS_PLUGIN = \
37
 
    "neutron.plugins.openvswitch.ovs_neutron_plugin.OVSNeutronPluginV2"
38
 
NEUTRON_ML2_PLUGIN = \
39
 
    "neutron.plugins.ml2.plugin.Ml2Plugin"
40
 
NEUTRON_NVP_PLUGIN = \
41
 
    "neutron.plugins.nicira.nicira_nvp_plugin.NeutronPlugin.NvpPluginV2"
42
 
NEUTRON_N1KV_PLUGIN = \
43
 
    "neutron.plugins.cisco.n1kv.n1kv_neutron_plugin.N1kvNeutronPluginV2"
44
 
NEUTRON_NSX_PLUGIN = "vmware"
45
 
 
46
 
NEUTRON = 'neutron'
47
 
QUANTUM = 'quantum'
48
 
 
49
 
 
50
 
def networking_name():
51
 
    ''' Determine whether neutron or quantum should be used for name '''
52
 
    if get_os_codename_install_source(config('openstack-origin')) >= 'havana':
53
 
        return NEUTRON
54
 
    else:
55
 
        return QUANTUM
56
 
 
57
 
OVS = 'ovs'
58
 
NVP = 'nvp'
59
 
N1KV = 'n1kv'
60
 
NSX = 'nsx'
61
 
 
62
 
CORE_PLUGIN = {
63
 
    QUANTUM: {
64
 
        OVS: QUANTUM_OVS_PLUGIN,
65
 
        NVP: QUANTUM_NVP_PLUGIN,
66
 
    },
67
 
    NEUTRON: {
68
 
        OVS: NEUTRON_OVS_PLUGIN,
69
 
        NVP: NEUTRON_NVP_PLUGIN,
70
 
        N1KV: NEUTRON_N1KV_PLUGIN,
71
 
        NSX: NEUTRON_NSX_PLUGIN
72
 
    },
73
 
}
74
 
 
75
 
 
76
 
def remap_plugin(plugin):
77
 
    ''' Remaps plugin name for renames/switches in packaging '''
78
 
    release = get_os_codename_install_source(config('openstack-origin'))
79
 
    if plugin == 'nvp' and release >= 'icehouse':
80
 
        plugin = 'nsx'
81
 
    elif plugin == 'nsx' and release < 'icehouse':
82
 
        plugin = 'nvp'
83
 
    return plugin
84
 
 
85
 
 
86
 
def core_plugin():
87
 
    plugin = remap_plugin(config('plugin'))
88
 
    if (get_os_codename_install_source(config('openstack-origin'))
89
 
            >= 'icehouse'
90
 
            and plugin == OVS):
91
 
        return NEUTRON_ML2_PLUGIN
92
 
    else:
93
 
        return CORE_PLUGIN[networking_name()][plugin]
94
 
 
95
 
 
96
 
class L3AgentContext(OSContextGenerator):
97
 
 
98
 
    def __call__(self):
99
 
        api_settings = NeutronAPIContext()()
100
 
        ctxt = {}
101
 
        if config('run-internal-router') == 'leader':
102
 
            ctxt['handle_internal_only_router'] = eligible_leader(None)
103
 
 
104
 
        if config('run-internal-router') == 'all':
105
 
            ctxt['handle_internal_only_router'] = True
106
 
 
107
 
        if config('run-internal-router') == 'none':
108
 
            ctxt['handle_internal_only_router'] = False
109
 
 
110
 
        if config('external-network-id'):
111
 
            ctxt['ext_net_id'] = config('external-network-id')
112
 
        if config('plugin'):
113
 
            ctxt['plugin'] = config('plugin')
114
 
        if api_settings['enable_dvr']:
115
 
            ctxt['agent_mode'] = 'dvr_snat'
116
 
        else:
117
 
            ctxt['agent_mode'] = 'legacy'
118
 
        return ctxt
119
 
 
120
 
 
121
 
class QuantumGatewayContext(OSContextGenerator):
122
 
 
123
 
    def __call__(self):
124
 
        api_settings = NeutronAPIContext()()
125
 
        ctxt = {
126
 
            'shared_secret': get_shared_secret(),
127
 
            'local_ip':
128
 
            get_address_in_network(config('os-data-network'),
129
 
                                   get_host_ip(unit_get('private-address'))),
130
 
            'core_plugin': core_plugin(),
131
 
            'plugin': config('plugin'),
132
 
            'debug': config('debug'),
133
 
            'verbose': config('verbose'),
134
 
            'instance_mtu': config('instance-mtu'),
135
 
            'l2_population': api_settings['l2_population'],
136
 
            'enable_dvr': api_settings['enable_dvr'],
137
 
            'enable_l3ha': api_settings['enable_l3ha'],
138
 
            'overlay_network_type':
139
 
            api_settings['overlay_network_type'],
140
 
        }
141
 
 
142
 
        mappings = config('bridge-mappings')
143
 
        if mappings:
144
 
            ctxt['bridge_mappings'] = ','.join(mappings.split())
145
 
 
146
 
        flat_providers = config('flat-network-providers')
147
 
        if flat_providers:
148
 
            ctxt['network_providers'] = ','.join(flat_providers.split())
149
 
 
150
 
        vlan_ranges = config('vlan-ranges')
151
 
        if vlan_ranges:
152
 
            ctxt['vlan_ranges'] = ','.join(vlan_ranges.split())
153
 
 
154
 
        net_dev_mtu = api_settings['network_device_mtu']
155
 
        if net_dev_mtu:
156
 
            ctxt['network_device_mtu'] = net_dev_mtu
157
 
            ctxt['veth_mtu'] = net_dev_mtu
158
 
 
159
 
        return ctxt
160
 
 
161
 
 
162
 
@cached
163
 
def get_host_ip(hostname=None):
164
 
    try:
165
 
        import dns.resolver
166
 
    except ImportError:
167
 
        apt_install('python-dnspython', fatal=True)
168
 
        import dns.resolver
169
 
    hostname = hostname or unit_get('private-address')
170
 
    try:
171
 
        # Test to see if already an IPv4 address
172
 
        socket.inet_aton(hostname)
173
 
        return hostname
174
 
    except socket.error:
175
 
        answers = dns.resolver.query(hostname, 'A')
176
 
        if answers:
177
 
            return answers[0].address
178
 
 
179
 
 
180
 
SHARED_SECRET = "/etc/{}/secret.txt"
181
 
 
182
 
 
183
 
def get_shared_secret():
184
 
    secret = None
185
 
    _path = SHARED_SECRET.format(networking_name())
186
 
    if not os.path.exists(_path):
187
 
        secret = str(uuid.uuid4())
188
 
        with open(_path, 'w') as secret_file:
189
 
            secret_file.write(secret)
190
 
    else:
191
 
        with open(_path, 'r') as secret_file:
192
 
            secret = secret_file.read().strip()
193
 
    return secret