diff options
Diffstat (limited to 'os_net_config')
-rw-r--r-- | os_net_config/__init__.py | 6 | ||||
-rw-r--r-- | os_net_config/impl_ifcfg.py | 46 | ||||
-rw-r--r-- | os_net_config/objects.py | 14 | ||||
-rw-r--r-- | os_net_config/tests/test_impl_ifcfg.py | 32 |
4 files changed, 83 insertions, 15 deletions
diff --git a/os_net_config/__init__.py b/os_net_config/__init__.py index 066ebb5..e2232e4 100644 --- a/os_net_config/__init__.py +++ b/os_net_config/__init__.py @@ -29,8 +29,14 @@ class NetConfig(object): def addInterface(self, interface): raise NotImplemented("addInterface is not implemented.") + def addVlan(self, bridge): + raise NotImplemented("addVlan is not implemented.") + def addBridge(self, bridge): raise NotImplemented("addBridge is not implemented.") + def addBond(self, bridge): + raise NotImplemented("addBond is not implemented.") + def apply(self): raise NotImplemented("apply is not implemented.") diff --git a/os_net_config/impl_ifcfg.py b/os_net_config/impl_ifcfg.py index e9715b8..78adc2e 100644 --- a/os_net_config/impl_ifcfg.py +++ b/os_net_config/impl_ifcfg.py @@ -68,7 +68,19 @@ class IfcfgNetConfig(os_net_config.NetConfig): data += "OVSBOOTPROTO=dhcp\n" if base_opt.members: members = [member.name for member in base_opt.members] - data += ("OVSDHCPINTERFACES=%s\n" % " ".join(members)) + data += ("OVSDHCPINTERFACES=\"%s\"\n" % " ".join(members)) + if base_opt.ovs_options: + data += "OVS_OPTIONS=\"%s\"\n" % base_opt.ovs_options + elif isinstance(base_opt, objects.OvsBond): + data += "DEVICETYPE=ovs\n" + data += "TYPE=OVSBond\n" + if base_opt.use_dhcp: + data += "OVSBOOTPROTO=dhcp\n" + if base_opt.members: + members = [member.name for member in base_opt.members] + data += ("BOND_IFACES=\"%s\"\n" % " ".join(members)) + if base_opt.ovs_options: + data += "OVS_OPTIONS=\"%s\"\n" % base_opt.ovs_options else: if base_opt.use_dhcp: data += "BOOTPROTO=dhcp\n" @@ -98,6 +110,19 @@ class IfcfgNetConfig(os_net_config.NetConfig): data += "IPV6ADDR=%s\n" % first_v6.ip return data + def _addRoutes(self, interface_name, routes=[]): + data = "" + first_line = "" + for route in routes: + if route.default: + first_line = "default via %s dev %s\n" % (route.next_hop, + interface_name) + else: + data += "%s via %s dev %s\n" % (route.ip_netmask, + route.next_hop, + interface_name) + self.routes[interface_name] = first_line + data + def addInterface(self, interface): data = self._addCommon(interface) self.interfaces[interface.name] = data @@ -116,18 +141,13 @@ class IfcfgNetConfig(os_net_config.NetConfig): if bridge.routes: self._addRoutes(bridge.name, bridge.routes) - def _addRoutes(self, interface_name, routes=[]): - data = "" - first_line = "" - for route in routes: - if route.default: - first_line = "default via %s dev %s\n" % (route.next_hop, - interface_name) - else: - data += "%s via %s dev %s\n" % (route.ip_netmask, - route.next_hop, - interface_name) - self.routes[interface_name] = first_line + data + def addBond(self, bond): + data = self._addCommon(bond) + #print 'bond name == %s' % bond.name + #print 'bond data == %s' % str(data) + self.interfaces[bond.name] = data + if bond.routes: + self._addRoutes(bond.name, bond.routes) def apply(self): restart_interfaces = [] diff --git a/os_net_config/objects.py b/os_net_config/objects.py index 81f441b..45824de 100644 --- a/os_net_config/objects.py +++ b/os_net_config/objects.py @@ -100,10 +100,22 @@ class OvsBridge(_BaseOpts): """Base class for OVS bridges.""" def __init__(self, name, use_dhcp=False, use_dhcpv6=False, addresses=[], - routes=[], members=[], mtu=1500): + routes=[], members=[], mtu=1500, ovs_options=None): super(OvsBridge, self).__init__(name, use_dhcp, use_dhcpv6, addresses, routes, mtu) self.members = members + self.ovs_options = ovs_options for member in self.members: member.bridge_name = name member.ovs_port = True + + +class OvsBond(_BaseOpts): + """Base class for OVS bonds.""" + + def __init__(self, name, use_dhcp=False, use_dhcpv6=False, addresses=[], + routes=[], members=[], mtu=1500, ovs_options=None): + super(OvsBond, self).__init__(name, use_dhcp, use_dhcpv6, addresses, + routes, mtu) + self.members = members + self.ovs_options = ovs_options diff --git a/os_net_config/tests/test_impl_ifcfg.py b/os_net_config/tests/test_impl_ifcfg.py index fa1efdb..cf1f0c0 100644 --- a/os_net_config/tests/test_impl_ifcfg.py +++ b/os_net_config/tests/test_impl_ifcfg.py @@ -60,7 +60,7 @@ HOTPLUG=no DEVICETYPE=ovs TYPE=OVSBridge OVSBOOTPROTO=dhcp -OVSDHCPINTERFACES=em1 +OVSDHCPINTERFACES="em1" """ _BASE_VLAN = """DEVICE=vlan5 @@ -70,6 +70,7 @@ VLAN=yes PHYSDEV=em1 """ + _VLAN_NO_IP = _BASE_VLAN + "BOOTPROTO=none\n" @@ -86,6 +87,16 @@ BOOTPROTO=none """ +_OVS_BOND_DHCP = """DEVICE=bond0 +ONBOOT=yes +HOTPLUG=no +DEVICETYPE=ovs +TYPE=OVSBond +OVSBOOTPROTO=dhcp +BOND_IFACES="em1 em2" +""" + + class TestIfcfgNetConfig(base.TestCase): def setUp(self): @@ -163,6 +174,25 @@ class TestIfcfgNetConfig(base.TestCase): self.provider.addBridge(bridge) self.assertEqual(_VLAN_OVS_BRIDGE, self.get_interface_config('vlan5')) + def test_ovs_bond(self): + interface1 = objects.Interface('em1') + interface2 = objects.Interface('em2') + bond = objects.OvsBond('bond0', use_dhcp=True, + members=[interface1, interface2]) + self.provider.addInterface(interface1) + self.provider.addInterface(interface2) + self.provider.addBond(bond) + self.assertEqual(_NO_IP, self.get_interface_config('em1')) + + em2_config = """DEVICE=em2 +ONBOOT=yes +HOTPLUG=no +BOOTPROTO=none +""" + self.assertEqual(em2_config, self.get_interface_config('em2')) + self.assertEqual(_OVS_BOND_DHCP, + self.get_interface_config('bond0')) + class TestIfcfgNetConfigApply(base.TestCase): |