Exemple #1
0
 def test_find_fixed_ip(self):
     instance = {'uuid': uuid.uuid4()}
     network_info = test_utils.get_test_network_info()
     first_net = network_info[0]['network']
     first_net['subnets'][0]['cidr'] = '10.0.0.0/24'
     first_net['subnets'][0]['ips'][0]['type'] = 'fixed'
     first_net['subnets'][0]['ips'][0]['address'] = '10.0.1.13'
     self.assertEqual('10.0.1.13/24', network.find_fixed_ip(instance,
                                                            first_net))
Exemple #2
0
 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)
Exemple #3
0
 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)
Exemple #4
0
 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)