Example #1
0
    def plug(self,
             network_id,
             port_id,
             device_name,
             mac_address,
             bridge=None,
             namespace=None,
             prefix=None):
        """Plugin the interface."""
        if not ip_lib.device_exists(
                device_name, self.root_helper, namespace=namespace):
            ip = ip_lib.IPWrapper(self.root_helper)

            # Enable agent to define the prefix
            if prefix:
                tap_name = device_name.replace(prefix, 'tap')
            else:
                tap_name = device_name.replace(self.DEV_NAME_PREFIX, 'tap')
            root_veth, ns_veth = ip.add_veth(tap_name, device_name)
            ns_veth.link.set_address(mac_address)

            if self.conf.network_device_mtu:
                root_veth.link.set_mtu(self.conf.network_device_mtu)
                ns_veth.link.set_mtu(self.conf.network_device_mtu)

            if namespace:
                namespace_obj = ip.ensure_namespace(namespace)
                namespace_obj.add_device_to_namespace(ns_veth)

            root_veth.link.set_up()
            ns_veth.link.set_up()

        else:
            LOG.warn(_("Device %s already exists"), device_name)
Example #2
0
    def plug(self, network_id, port_id, device_name, mac_address,
             bridge=None, namespace=None, prefix=None):
        """Plugin the interface."""
        if not ip_lib.device_exists(device_name,
                                    self.root_helper,
                                    namespace=namespace):
            ip = ip_lib.IPWrapper(self.root_helper)

            # Enable agent to define the prefix
            if prefix:
                tap_name = device_name.replace(prefix, 'tap')
            else:
                tap_name = device_name.replace(self.DEV_NAME_PREFIX, 'tap')
            root_veth, ns_veth = ip.add_veth(tap_name, device_name)
            ns_veth.link.set_address(mac_address)

            if self.conf.network_device_mtu:
                root_veth.link.set_mtu(self.conf.network_device_mtu)
                ns_veth.link.set_mtu(self.conf.network_device_mtu)

            if namespace:
                namespace_obj = ip.ensure_namespace(namespace)
                namespace_obj.add_device_to_namespace(ns_veth)

            root_veth.link.set_up()
            ns_veth.link.set_up()

        else:
            LOG.warn(_("Device %s already exists"), device_name)
Example #3
0
    def _ensure_local_port(self, network_id, subnet_id,
                           network_type, ip_address):
        driver = importutils.import_object(self.conf.interface_driver,
                                           self.conf)

        host_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, socket.gethostname()))

        name = 'AKANDA:RUG:%s' % network_type.upper()

        query_dict = dict(device_owner=DEVICE_OWNER_RUG,
                          device_id=host_id,
                          name=name,
                          network_id=network_id)

        ports = self.api_client.list_ports(**query_dict)['ports']

        if ports:
            port = Port.from_dict(ports[0])
            LOG.info(_LI('already have local %s port, using %r'),
                     network_type, port)
        else:
            LOG.info(_LI('creating a new local %s port'), network_type)
            port_dict = {
                'admin_state_up': True,
                'network_id': network_id,
                'device_owner': DEVICE_OWNER_ROUTER_INT,  # lying here for IP
                'name': name,
                'device_id': host_id,
                'fixed_ips': [{
                    'ip_address': ip_address.split('/')[0],
                    'subnet_id': subnet_id
                }],
                'binding:host_id': socket.gethostname()
            }
            port = Port.from_dict(
                self.api_client.create_port(dict(port=port_dict))['port'])

            # remove lie that enabled us pick IP on slaac subnet
            self.api_client.update_port(
                port.id,
                {'port': {'device_owner': DEVICE_OWNER_RUG}}
            )
            port.device_owner = DEVICE_OWNER_RUG

            LOG.info(_LI('new local %s port: %r'), network_type, port)

        # create the tap interface if it doesn't already exist
        if not ip_lib.device_exists(driver.get_device_name(port)):
            driver.plug(
                port.network_id,
                port.id,
                driver.get_device_name(port),
                port.mac_address)

            # add sleep to ensure that port is setup before use
            time.sleep(1)

        driver.init_l3(driver.get_device_name(port), [ip_address])
        return port
Example #4
0
    def _ensure_local_port(self, network_id, subnet_id, network_type,
                           ip_address):
        driver = importutils.import_object(self.conf.interface_driver,
                                           self.conf)

        host_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, socket.gethostname()))

        query_dict = dict(device_owner=DEVICE_OWNER_RUG,
                          device_id=host_id,
                          network_id=network_id)

        ports = self.api_client.list_ports(**query_dict)['ports']

        if ports:
            port = Port.from_dict(ports[0])
            LOG.info('already have local %s port, using %r', network_type,
                     port)
        else:
            LOG.info('creating a new local %s port', network_type)
            port_dict = {
                'admin_state_up':
                True,
                'network_id':
                network_id,
                'device_owner':
                DEVICE_OWNER_ROUTER_INT,  # lying here for IP
                'device_id':
                host_id,
                'fixed_ips': [{
                    'ip_address': ip_address.split('/')[0],
                    'subnet_id': subnet_id
                }],
                'binding:host_id':
                socket.gethostname()
            }
            port = Port.from_dict(
                self.api_client.create_port(dict(port=port_dict))['port'])

            # remove lie that enabled us pick IP on slaac subnet
            self.api_client.update_port(
                port.id, {'port': {
                    'device_owner': DEVICE_OWNER_RUG
                }})
            port.device_owner = DEVICE_OWNER_RUG

            LOG.info('new local %s port: %r', network_type, port)

        # create the tap interface if it doesn't already exist
        if not ip_lib.device_exists(driver.get_device_name(port)):
            driver.plug(port.network_id, port.id, driver.get_device_name(port),
                        port.mac_address)

            # add sleep to ensure that port is setup before use
            time.sleep(1)

        driver.init_l3(driver.get_device_name(port), [ip_address])
        return port
Example #5
0
    def ensure_local_service_port(self):
        driver = importutils.import_object(self.conf.interface_driver,
                                           self.conf)

        host_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, socket.gethostname()))

        query_dict = dict(device_owner=DEVICE_OWNER_RUG,
                          device_id=host_id,
                          network_id=self.conf.management_network_id)

        ports = self.api_client.list_ports(**query_dict)['ports']

        ip_address = get_local_service_ip(self.conf)

        if ports:
            port = Port.from_dict(ports[0])
            LOG.info('already have local service port, using %r', port)
        else:
            LOG.info('creating a new local service port')
            # create the missing local port
            port_dict = dict(
                admin_state_up=True,
                network_id=self.conf.management_network_id,
                device_owner=DEVICE_OWNER_RUG,
                device_id=host_id,
                fixed_ips=[{
                    'ip_address': ip_address.split('/')[0],
                    'subnet_id': self.conf.management_subnet_id
                }]
            )

            port = Port.from_dict(
                self.api_client.create_port(dict(port=port_dict))['port'])
            LOG.info('new local service port: %r', port)

        # create the tap interface if it doesn't already exist
        if not ip_lib.device_exists(driver.get_device_name(port)):
            driver.plug(
                port.network_id,
                port.id,
                driver.get_device_name(port),
                port.mac_address)

            # add sleep to ensure that port is setup before use
            time.sleep(1)

        driver.init_l3(driver.get_device_name(port), [ip_address])

        return port
Example #6
0
    def plug(self,
             network_id,
             port_id,
             device_name,
             mac_address,
             bridge=None,
             namespace=None,
             prefix=None):
        """Plug in the interface."""
        if not bridge:
            bridge = self.conf.ovs_integration_bridge

        self.check_bridge_exists(bridge)

        if not ip_lib.device_exists(
                device_name, self.root_helper, namespace=namespace):

            ip = ip_lib.IPWrapper(self.root_helper)
            tap_name = self._get_tap_name(device_name, prefix)

            if self.conf.ovs_use_veth:
                root_dev, ns_dev = ip.add_veth(tap_name, device_name)

            internal = not self.conf.ovs_use_veth
            self._ovs_add_port(bridge,
                               tap_name,
                               port_id,
                               mac_address,
                               internal=internal)

            ns_dev = ip.device(device_name)
            ns_dev.link.set_address(mac_address)

            if self.conf.network_device_mtu:
                ns_dev.link.set_mtu(self.conf.network_device_mtu)
                if self.conf.ovs_use_veth:
                    root_dev.link.set_mtu(self.conf.network_device_mtu)

            if namespace:
                namespace_obj = ip.ensure_namespace(namespace)
                namespace_obj.add_device_to_namespace(ns_dev)

            ns_dev.link.set_up()
            if self.conf.ovs_use_veth:
                root_dev.link.set_up()
        else:
            LOG.warn(_("Device %s already exists"), device_name)
Example #7
0
    def plug(self, network_id, port_id, device_name, mac_address,
             bridge=None, namespace=None, prefix=None):
        """Plug in the interface."""
        if not bridge:
            bridge = self.conf.ovs_integration_bridge

        self.check_bridge_exists(bridge)

        if not ip_lib.device_exists(device_name,
                                    self.root_helper,
                                    namespace=namespace):

            ip = ip_lib.IPWrapper(self.root_helper)
            tap_name = self._get_tap_name(device_name, prefix)

            if self.conf.ovs_use_veth:
                root_dev, ns_dev = ip.add_veth(tap_name, device_name)

            internal = not self.conf.ovs_use_veth
            self._ovs_add_port(bridge, tap_name, port_id, mac_address,
                               internal=internal)

            ns_dev = ip.device(device_name)
            ns_dev.link.set_address(mac_address)

            if self.conf.network_device_mtu:
                ns_dev.link.set_mtu(self.conf.network_device_mtu)
                if self.conf.ovs_use_veth:
                    root_dev.link.set_mtu(self.conf.network_device_mtu)

            if namespace:
                namespace_obj = ip.ensure_namespace(namespace)
                namespace_obj.add_device_to_namespace(ns_dev)

            ns_dev.link.set_up()
            if self.conf.ovs_use_veth:
                root_dev.link.set_up()
        else:
            LOG.warn(_("Device %s already exists"), device_name)
 def test_device_does_not_exist(self):
     with mock.patch.object(ip_lib.IPDevice, '_execute') as _execute:
         _execute.return_value = ''
         _execute.side_effect = RuntimeError
         self.assertFalse(ip_lib.device_exists('eth0'))
 def test_device_exists(self):
     with mock.patch.object(ip_lib.IPDevice, '_execute') as _execute:
         _execute.return_value = LINK_SAMPLE[1]
         self.assertTrue(ip_lib.device_exists('eth0'))
         _execute.assert_called_once_with('o', 'link', ('show', 'eth0'))
Example #10
0
 def test_device_does_not_exist(self):
     with mock.patch.object(ip_lib.IPDevice, '_execute') as _execute:
         _execute.return_value = ''
         _execute.side_effect = RuntimeError
         self.assertFalse(ip_lib.device_exists('eth0'))
Example #11
0
 def test_device_exists(self):
     with mock.patch.object(ip_lib.IPDevice, '_execute') as _execute:
         _execute.return_value = LINK_SAMPLE[1]
         self.assertTrue(ip_lib.device_exists('eth0'))
         _execute.assert_called_once_with('o', 'link', ('show', 'eth0'))
Example #12
0
 def check_bridge_exists(self, bridge):
     if not ip_lib.device_exists(bridge):
         raise Exception('Bridge %s does not exist' % bridge)
Example #13
0
 def test_device_exists(self):
     with mock.patch.object(ip_lib.IPDevice, "_execute") as _execute:
         _execute.return_value = LINK_SAMPLE[1]
         self.assertTrue(ip_lib.device_exists("eth0"))
         _execute.assert_called_once_with("o", "link", ("show", "eth0"))
Example #14
0
 def check_bridge_exists(self, bridge):
     if not ip_lib.device_exists(bridge):
         raise Exception('Bridge %s does not exist' % bridge)