def _setup_network(self, instance, network_info): if not network_info: return container_id = self._find_container_by_name(instance['name']).get('id') if not container_id: return network_info = network_info[0]['network'] netns_path = '/var/run/netns' if not os.path.exists(netns_path): utils.execute( 'mkdir', '-p', netns_path, run_as_root=True) nspid = self._find_container_pid(container_id) if not nspid: msg = _('Cannot find any PID under container "{0}"') raise RuntimeError(msg.format(container_id)) netns_path = os.path.join(netns_path, container_id) utils.execute( 'ln', '-sf', '/proc/{0}/ns/net'.format(nspid), '/var/run/netns/{0}'.format(container_id), run_as_root=True) rand = random.randint(0, 100000) if_local_name = 'pvnetl{0}'.format(rand) if_remote_name = 'pvnetr{0}'.format(rand) bridge = network_info['bridge'] gateway = network.find_gateway(instance, network_info) ip = network.find_fixed_ip(instance, network_info) undo_mgr = utils.UndoManager() try: utils.execute( 'ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) # NOTE(samalba): Deleting the interface will delete all associated # resources (remove from the bridge, its pair, etc...) utils.execute( 'brctl', 'addif', bridge, if_local_name, run_as_root=True) utils.execute( 'ip', 'link', 'set', if_local_name, 'up', run_as_root=True) utils.execute( 'ip', 'link', 'set', if_remote_name, 'netns', nspid, run_as_root=True) utils.execute( 'ip', 'netns', 'exec', container_id, 'ifconfig', if_remote_name, ip, run_as_root=True) utils.execute( 'ip', 'netns', 'exec', container_id, 'ip', 'route', 'replace', 'default', 'via', gateway, 'dev', if_remote_name, run_as_root=True) except Exception: msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def _setup_network(self, instance, network_info): if not network_info: return container_id = self.find_container_by_name(instance['name']).get('id') if not container_id: return network_info = network_info[0]['network'] netns_path = '/var/run/netns' if not os.path.exists(netns_path): utils.execute('mkdir', '-p', netns_path, run_as_root=True) nspid = self._find_container_pid(container_id) if not nspid: msg = _('Cannot find any PID under container "{0}"') raise RuntimeError(msg.format(container_id)) netns_path = os.path.join(netns_path, container_id) utils.execute('ln', '-sf', '/proc/{0}/ns/net'.format(nspid), '/var/run/netns/{0}'.format(container_id), run_as_root=True) rand = random.randint(0, 100000) if_local_name = 'pvnetl{0}'.format(rand) if_remote_name = 'pvnetr{0}'.format(rand) bridge = network_info['bridge'] gateway = network.find_gateway(instance, network_info) ip = network.find_fixed_ip(instance, network_info) undo_mgr = utils.UndoManager() try: utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) # NOTE(samalba): Deleting the interface will delete all associated # resources (remove from the bridge, its pair, etc...) utils.execute('brctl', 'addif', bridge, if_local_name, run_as_root=True) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) utils.execute('ip', 'link', 'set', if_remote_name, 'netns', nspid, run_as_root=True) utils.execute('ip', 'netns', 'exec', container_id, 'ifconfig', if_remote_name, ip, run_as_root=True) utils.execute('ip', 'netns', 'exec', container_id, 'ip', 'route', 'replace', 'default', 'via', gateway, 'dev', if_remote_name, run_as_root=True) except Exception: msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def _setup_network(self, instance, network_info): if not network_info: return container_id = self.find_container_by_name(instance["name"]).get("id") if not container_id: return network_info = network_info[0]["network"] netns_path = "/var/run/netns" if not os.path.exists(netns_path): utils.execute("mkdir", "-p", netns_path, run_as_root=True) nspid = self._find_container_pid(container_id) if not nspid: msg = _('Cannot find any PID under container "{0}"') raise RuntimeError(msg.format(container_id)) netns_path = os.path.join(netns_path, container_id) utils.execute( "ln", "-sf", "/proc/{0}/ns/net".format(nspid), "/var/run/netns/{0}".format(container_id), run_as_root=True ) rand = random.randint(0, 100000) if_local_name = "pvnetl{0}".format(rand) if_remote_name = "pvnetr{0}".format(rand) bridge = network_info["bridge"] gateway = network.find_gateway(instance, network_info) ip = network.find_fixed_ip(instance, network_info) undo_mgr = utils.UndoManager() try: utils.execute( "ip", "link", "add", "name", if_local_name, "type", "veth", "peer", "name", if_remote_name, run_as_root=True, ) undo_mgr.undo_with(lambda: utils.execute("ip", "link", "delete", if_local_name, run_as_root=True)) # NOTE(samalba): Deleting the interface will delete all associated # resources (remove from the bridge, its pair, etc...) utils.execute("brctl", "addif", bridge, if_local_name, run_as_root=True) utils.execute("ip", "link", "set", if_local_name, "up", run_as_root=True) utils.execute("ip", "link", "set", if_remote_name, "netns", nspid, run_as_root=True) utils.execute("ip", "netns", "exec", container_id, "ifconfig", if_remote_name, ip, run_as_root=True) utils.execute( "ip", "netns", "exec", container_id, "ip", "route", "replace", "default", "via", gateway, "dev", if_remote_name, run_as_root=True, ) except Exception: msg = _("Failed to setup the network, rolling back") undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def test_find_gateway(self): instance = {'uuid': uuid.uuid4()} network_info = test_utils.get_test_network_info() first_net = network_info[0]['network'] first_net['subnets'][0]['gateway']['address'] = '10.0.0.1' self.assertEqual('10.0.0.1', network.find_gateway(instance, first_net))