166 lines
5.1 KiB
Python
Raw Normal View History

2014-06-05 11:59:23 +01:00
from charmhelpers.core.hookenv import (
2014-06-19 10:56:25 +01:00
relation_ids,
related_units,
relation_get,
config,
unit_get,
2014-06-05 11:59:23 +01:00
)
from charmhelpers.core.strutils import bool_from_string
2014-06-05 11:59:23 +01:00
from charmhelpers.contrib.openstack import context
2015-02-11 18:48:41 +00:00
from charmhelpers.core.host import (
service_running,
service_start,
service_restart,
)
2014-07-14 14:47:38 +03:00
from charmhelpers.contrib.network.ovs import add_bridge, add_bridge_port
2014-06-05 11:59:23 +01:00
from charmhelpers.contrib.openstack.utils import get_host_ip
2014-06-27 14:04:10 +01:00
from charmhelpers.contrib.network.ip import get_address_in_network
2015-02-27 18:25:49 +00:00
from charmhelpers.contrib.openstack.neutron import (
parse_bridge_mappings,
parse_data_port_mappings,
)
from charmhelpers.contrib.openstack.context import (
NeutronPortContext,
)
from charmhelpers.core.host import (
get_nic_hwaddr,
)
2014-06-05 11:59:23 +01:00
OVS_BRIDGE = 'br-int'
2014-06-19 10:56:25 +01:00
def _neutron_api_settings():
2014-06-05 11:59:23 +01:00
'''
Inspects current neutron-plugin relation
2014-06-05 11:59:23 +01:00
'''
neutron_settings = {
2014-09-04 15:18:06 +00:00
'neutron_security_groups': False,
'l2_population': True,
'overlay_network_type': 'gre',
}
2015-02-11 13:43:17 +00:00
for rid in relation_ids('neutron-plugin-api'):
2014-06-05 11:59:23 +01:00
for unit in related_units(rid):
rdata = relation_get(rid=rid, unit=unit)
2014-09-04 13:26:03 +00:00
if 'l2-population' not in rdata:
continue
neutron_settings = {
'l2_population': bool_from_string(rdata['l2-population']),
'overlay_network_type': rdata['overlay-network-type'],
'neutron_security_groups': bool_from_string(
rdata['neutron-security-groups']
),
}
2015-02-11 13:43:17 +00:00
2015-02-10 10:51:33 +00:00
net_dev_mtu = rdata.get('network-device-mtu')
2015-02-27 18:25:49 +00:00
if net_dev_mtu:
2015-02-10 10:51:33 +00:00
neutron_settings['network_device_mtu'] = net_dev_mtu
2015-02-10 09:54:40 +00:00
return neutron_settings
return neutron_settings
2014-06-05 11:59:23 +01:00
2014-06-19 10:56:25 +01:00
2015-02-27 18:25:49 +00:00
class DataPortContext(NeutronPortContext):
2015-02-11 18:48:41 +00:00
2015-02-27 18:25:49 +00:00
def __call__(self):
ports = config('data-port')
if ports:
portmap = parse_data_port_mappings(ports)
ports = portmap.values()
resolved = self.resolve_ports(ports)
normalized = {get_nic_hwaddr(port): port for port in resolved
if port not in ports}
normalized.update({port: port for port in resolved
if port in ports})
if resolved:
2015-03-03 13:55:30 +08:00
return {bridge: normalized[port] for bridge, port in
2015-02-27 18:25:49 +00:00
portmap.iteritems() if port in normalized.keys()}
2015-02-11 18:48:41 +00:00
2015-02-27 18:25:49 +00:00
return None
2015-02-11 18:48:41 +00:00
2014-06-05 11:59:23 +01:00
class OVSPluginContext(context.NeutronContext):
interfaces = []
@property
def plugin(self):
return 'ovs'
@property
def network_manager(self):
return 'neutron'
@property
def neutron_security_groups(self):
neutron_api_settings = _neutron_api_settings()
2014-09-04 15:18:06 +00:00
return neutron_api_settings['neutron_security_groups']
2014-06-05 11:59:23 +01:00
def _ensure_bridge(self):
if not service_running('openvswitch-switch'):
service_start('openvswitch-switch')
2015-02-11 18:48:41 +00:00
2014-06-05 11:59:23 +01:00
add_bridge(OVS_BRIDGE)
2015-02-27 18:25:49 +00:00
portmaps = DataPortContext()()
bridgemaps = parse_bridge_mappings(config('bridge-mappings'))
for provider, br in bridgemaps.iteritems():
2015-02-11 18:48:41 +00:00
add_bridge(br)
2015-02-27 18:25:49 +00:00
2015-03-03 13:55:30 +08:00
if not portmaps or br not in portmaps:
2015-02-27 18:25:49 +00:00
continue
2015-03-03 13:55:30 +08:00
add_bridge_port(br, portmaps[br], promisc=True)
2015-02-11 18:48:41 +00:00
service_restart('os-charm-phy-nic-mtu')
2014-06-05 11:59:23 +01:00
def ovs_ctxt(self):
# In addition to generating config context, ensure the OVS service
# is running and the OVS bridge exists. Also need to ensure
# local_ip points to actual IP, not hostname.
ovs_ctxt = super(OVSPluginContext, self).ovs_ctxt()
if not ovs_ctxt:
return {}
self._ensure_bridge()
2014-06-05 14:16:54 +00:00
conf = config()
2014-06-27 14:04:10 +01:00
ovs_ctxt['local_ip'] = \
get_address_in_network(config('os-data-network'),
get_host_ip(unit_get('private-address')))
neutron_api_settings = _neutron_api_settings()
2014-09-04 15:18:06 +00:00
ovs_ctxt['neutron_security_groups'] = self.neutron_security_groups
ovs_ctxt['l2_population'] = neutron_api_settings['l2_population']
2014-10-02 14:02:42 +00:00
ovs_ctxt['overlay_network_type'] = \
neutron_api_settings['overlay_network_type']
2014-06-23 12:49:58 +01:00
# TODO: We need to sort out the syslog and debug/verbose options as a
# general context helper
2014-06-05 14:16:54 +00:00
ovs_ctxt['use_syslog'] = conf['use-syslog']
2014-06-17 15:31:03 +01:00
ovs_ctxt['verbose'] = conf['verbose']
ovs_ctxt['debug'] = conf['debug']
2015-02-10 15:25:42 +00:00
2015-02-11 13:43:17 +00:00
net_dev_mtu = neutron_api_settings.get('network_device_mtu')
2015-02-10 15:25:42 +00:00
if net_dev_mtu:
2015-02-11 14:24:39 +00:00
# neutron.conf
ovs_ctxt['network_device_mtu'] = net_dev_mtu
# ml2 conf
2015-02-10 15:25:42 +00:00
ovs_ctxt['veth_mtu'] = net_dev_mtu
2015-02-11 16:31:04 +00:00
mappings = config('bridge-mappings')
if mappings:
ovs_ctxt['bridge_mappings'] = mappings
2014-06-05 11:59:23 +01:00
return ovs_ctxt
2015-02-10 13:06:32 +00:00
2015-02-10 19:09:25 +00:00
class PhyNICMTUContext(context.NeutronPortContext):
2015-02-10 13:06:32 +00:00
def __call__(self):
ctxt = {}
2015-02-10 19:09:25 +00:00
port = config('phy-nics')
2015-02-10 13:06:32 +00:00
if port:
2015-02-11 15:48:19 +00:00
ctxt = {"devs": port.replace(' ', '\\n')}
2015-02-10 13:06:32 +00:00
mtu = config('phy-nic-mtu')
if mtu:
2015-02-10 19:09:25 +00:00
ctxt['mtu'] = mtu
2015-02-10 13:06:32 +00:00
return ctxt