def test_subnet_edge(self):
     secondaddress = network.allocate_ip("netuser", "project0",
                             utils.generate_mac())
     hostname = "toomany-hosts"
     for project in range(1,5):
         project_id = "project%s" % (project)
         mac = utils.generate_mac()
         mac2 = utils.generate_mac()
         mac3 = utils.generate_mac()
         address = network.allocate_ip(
                 "netuser", project_id, mac)
         address2 = network.allocate_ip(
                 "netuser", project_id, mac2)
         address3 = network.allocate_ip(
                 "netuser", project_id, mac3)
         self.assertEqual(False, is_in_project(address, "project0"))
         self.assertEqual(False, is_in_project(address2, "project0"))
         self.assertEqual(False, is_in_project(address3, "project0"))
         rv = network.deallocate_ip(address)
         rv = network.deallocate_ip(address2)
         rv = network.deallocate_ip(address3)
         net = network.get_project_network(project_id, "default")
         self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name)
         self.dnsmasq.release_ip(mac2, address2, hostname, net.bridge_name)
         self.dnsmasq.release_ip(mac3, address3, hostname, net.bridge_name)
     net = network.get_project_network("project0", "default")
     rv = network.deallocate_ip(secondaddress)
     self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name)
    def test_range_allocation(self):
        mac = utils.generate_mac()
        secondmac = utils.generate_mac()
        hostname = "test-host"
        address = network.allocate_ip(
                    "netuser", "project0", mac)
        secondaddress = network.allocate_ip(
                "netuser", "project1", secondmac)
        net = network.get_project_network("project0", "default")
        secondnet = network.get_project_network("project1", "default")

        self.assertEqual(True, is_in_project(address, "project0"))
        self.assertEqual(True, is_in_project(secondaddress, "project1"))
        self.assertEqual(False, is_in_project(address, "project1"))

        # Addresses are allocated before they're issued
        self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name)
        self.dnsmasq.issue_ip(secondmac, secondaddress,
                                hostname, secondnet.bridge_name)

        rv = network.deallocate_ip(address)
        self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name)
        self.assertEqual(False, is_in_project(address, "project0"))

        # First address release shouldn't affect the second
        self.assertEqual(True, is_in_project(secondaddress, "project1"))

        rv = network.deallocate_ip(secondaddress)
        self.dnsmasq.release_ip(secondmac, secondaddress,
                                hostname, secondnet.bridge_name)
        self.assertEqual(False, is_in_project(secondaddress, "project1"))
Esempio n. 3
0
 def test_range_allocation(self):
     address = network.allocate_ip(
             "netuser", "project0", utils.generate_mac())
     secondaddress = network.allocate_ip(
             "netuser", "project1", utils.generate_mac())
     self.assertEqual(True,
                      address in self._get_project_addresses("project0"))
     self.assertEqual(True,
                      secondaddress in self._get_project_addresses("project1"))
     self.assertEqual(False, address in self._get_project_addresses("project1"))
     rv = network.deallocate_ip(address)
     self.assertEqual(False, address in self._get_project_addresses("project0"))
     rv = network.deallocate_ip(secondaddress)
     self.assertEqual(False,
                      secondaddress in self._get_project_addresses("project1"))
Esempio n. 4
0
 def test_allocate_deallocate_ip(self):
     address = network.allocate_ip(
             "netuser", "project0", utils.generate_mac())
     logging.debug("Was allocated %s" % (address))
     self.assertEqual(True, address in self._get_project_addresses("project0"))
     rv = network.deallocate_ip(address)
     self.assertEqual(False, address in self._get_project_addresses("project0"))
Esempio n. 5
0
 def run_instances(self, context, **kwargs):
     # make sure user can access the image
     # vpn image is private so it doesn't show up on lists
     if kwargs['image_id'] != FLAGS.vpn_image_id:
         image = self._get_image(context, kwargs['image_id'])
     logging.debug("Going to run instances...")
     reservation_id = utils.generate_uid('r')
     launch_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
     key_data = None
     if kwargs.has_key('key_name'):
         key_pair = context.user.get_key_pair(kwargs['key_name'])
         if not key_pair:
             raise exception.ApiError('Key Pair %s not found' %
                                      kwargs['key_name'])
         key_data = key_pair.public_key
     # TODO: Get the real security group of launch in here
     security_group = "default"
     bridge_name = network.BridgedNetwork.get_network_for_project(
         context.user.id, context.project.id, security_group)['bridge_name']
     for num in range(int(kwargs['max_count'])):
         inst = self.instdir.new()
         # TODO(ja): add ari, aki
         inst['image_id'] = kwargs['image_id']
         if 'kernel_id' in kwargs:
             inst['kernel_id'] = kwargs['kernel_id']
         if 'ramdisk_id' in kwargs:
             inst['ramdisk_id'] = kwargs['ramdisk_id']
         inst['user_data'] = kwargs.get('user_data', '')
         inst['instance_type'] = kwargs.get('instance_type', 'm1.small')
         inst['reservation_id'] = reservation_id
         inst['launch_time'] = launch_time
         inst['key_data'] = key_data or ''
         inst['key_name'] = kwargs.get('key_name', '')
         inst['user_id'] = context.user.id
         inst['project_id'] = context.project.id
         inst['mac_address'] = utils.generate_mac()
         inst['ami_launch_index'] = num
         inst['bridge_name'] = bridge_name
         if inst['image_id'] == FLAGS.vpn_image_id:
             address = network.allocate_vpn_ip(inst['user_id'],
                                               inst['project_id'],
                                               mac=inst['mac_address'])
         else:
             address = network.allocate_ip(inst['user_id'],
                                           inst['project_id'],
                                           mac=inst['mac_address'])
         inst['private_dns_name'] = str(address)
         # TODO: allocate expresses on the router node
         inst.save()
         rpc.cast(
             FLAGS.compute_topic, {
                 "method": "run_instance",
                 "args": {
                     "instance_id": inst.instance_id
                 }
             })
         logging.debug("Casting to node for %s's instance with IP of %s" %
                       (context.user.name, inst['private_dns_name']))
     # TODO: Make the NetworkComputeNode figure out the network name from ip.
     return defer.succeed(self._format_instances(context, reservation_id))
Esempio n. 6
0
 def test_range_allocation(self):
     address = network.allocate_ip("netuser", "project0",
                                   utils.generate_mac())
     secondaddress = network.allocate_ip("netuser", "project1",
                                         utils.generate_mac())
     self.assertEqual(True, address
                      in self._get_project_addresses("project0"))
     self.assertEqual(
         True, secondaddress in self._get_project_addresses("project1"))
     self.assertEqual(False, address
                      in self._get_project_addresses("project1"))
     rv = network.deallocate_ip(address)
     self.assertEqual(False, address
                      in self._get_project_addresses("project0"))
     rv = network.deallocate_ip(secondaddress)
     self.assertEqual(
         False, secondaddress in self._get_project_addresses("project1"))
Esempio n. 7
0
 def test_too_many_projects(self):
     for i in range(0, 30):
         name = 'toomany-project%s' % i
         self.manager.create_project(name, 'netuser', name)
         address = network.allocate_ip("netuser", name,
                                       utils.generate_mac())
         rv = network.deallocate_ip(address)
         self.manager.delete_project(name)
Esempio n. 8
0
 def test_too_many_projects(self):
     for i in range(0, 30):
         name = 'toomany-project%s' % i
         self.manager.create_project(name, 'netuser', name)
         address = network.allocate_ip(
                 "netuser", name, utils.generate_mac())
         rv = network.deallocate_ip(address)
         self.manager.delete_project(name)
Esempio n. 9
0
 def test_allocate_deallocate_ip(self):
     address = network.allocate_ip("netuser", "project0",
                                   utils.generate_mac())
     logging.debug("Was allocated %s" % (address))
     self.assertEqual(True, address
                      in self._get_project_addresses("project0"))
     rv = network.deallocate_ip(address)
     self.assertEqual(False, address
                      in self._get_project_addresses("project0"))
Esempio n. 10
0
File: cloud.py Progetto: sorenh/cc
 def run_instances(self, context, **kwargs):
     # make sure user can access the image
     # vpn image is private so it doesn't show up on lists
     if kwargs['image_id'] != FLAGS.vpn_image_id:
         image = self._get_image(context, kwargs['image_id'])
     logging.debug("Going to run instances...")
     reservation_id = utils.generate_uid('r')
     launch_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
     key_data = None
     if kwargs.has_key('key_name'):
         key_pair = context.user.get_key_pair(kwargs['key_name'])
         if not key_pair:
             raise exception.ApiError('Key Pair %s not found' %
                                      kwargs['key_name'])
         key_data = key_pair.public_key
     # TODO: Get the real security group of launch in here
     security_group = "default"
     bridge_name = network.BridgedNetwork.get_network_for_project(context.user.id, context.project.id, security_group)['bridge_name']
     for num in range(int(kwargs['max_count'])):
         inst = self.instdir.new()
         # TODO(ja): add ari, aki
         inst['image_id'] = kwargs['image_id']
         if 'kernel_id' in kwargs:
             inst['kernel_id'] = kwargs['kernel_id']
         if 'ramdisk_id' in kwargs:
             inst['ramdisk_id'] = kwargs['ramdisk_id']
         inst['user_data'] = kwargs.get('user_data', '')
         inst['instance_type'] = kwargs.get('instance_type', 'm1.small')
         inst['reservation_id'] = reservation_id
         inst['launch_time'] = launch_time
         inst['key_data'] = key_data or ''
         inst['key_name'] = kwargs.get('key_name', '')
         inst['user_id'] = context.user.id
         inst['project_id'] = context.project.id
         inst['mac_address'] = utils.generate_mac()
         inst['ami_launch_index'] = num
         inst['bridge_name'] = bridge_name
         if inst['image_id'] == FLAGS.vpn_image_id:
             address = network.allocate_vpn_ip(
                     inst['user_id'], inst['project_id'], mac=inst['mac_address'])
         else:
             address = network.allocate_ip(
                     inst['user_id'], inst['project_id'], mac=inst['mac_address'])
         inst['private_dns_name'] = str(address)
         # TODO: allocate expresses on the router node
         inst.save()
         rpc.cast(FLAGS.compute_topic,
              {"method": "run_instance",
               "args": {"instance_id" : inst.instance_id}})
         logging.debug("Casting to node for %s's instance with IP of %s" %
                   (context.user.name, inst['private_dns_name']))
     # TODO: Make the NetworkComputeNode figure out the network name from ip.
     return defer.succeed(self._format_instances(
                             context, reservation_id))
Esempio n. 11
0
 def test_subnet_edge(self):
     secondaddress = network.allocate_ip("netuser", "project0",
                                         utils.generate_mac())
     for project in range(1, 5):
         project_id = "project%s" % (project)
         address = network.allocate_ip("netuser", project_id,
                                       utils.generate_mac())
         address2 = network.allocate_ip("netuser", project_id,
                                        utils.generate_mac())
         address3 = network.allocate_ip("netuser", project_id,
                                        utils.generate_mac())
         self.assertEqual(
             False, address in self._get_project_addresses("project0"))
         self.assertEqual(
             False, address2 in self._get_project_addresses("project0"))
         self.assertEqual(
             False, address3 in self._get_project_addresses("project0"))
         rv = network.deallocate_ip(address)
         rv = network.deallocate_ip(address2)
         rv = network.deallocate_ip(address3)
     rv = network.deallocate_ip(secondaddress)
Esempio n. 12
0
 def test_subnet_edge(self):
     secondaddress = network.allocate_ip("netuser", "project0",
                             utils.generate_mac())
     for project in range(1,5):
         project_id = "project%s" % (project)
         address = network.allocate_ip(
                 "netuser", project_id, utils.generate_mac())
         address2 = network.allocate_ip(
                 "netuser", project_id, utils.generate_mac())
         address3 = network.allocate_ip(
                 "netuser", project_id, utils.generate_mac())
         self.assertEqual(False,
                          address in self._get_project_addresses("project0"))
         self.assertEqual(False,
                          address2 in self._get_project_addresses("project0"))
         self.assertEqual(False,
                          address3 in self._get_project_addresses("project0"))
         rv = network.deallocate_ip(address)
         rv = network.deallocate_ip(address2)
         rv = network.deallocate_ip(address3)
     rv = network.deallocate_ip(secondaddress)
    def test_allocate_deallocate_ip(self):
        address = network.allocate_ip(
                "netuser", "project0", utils.generate_mac())
        logging.debug("Was allocated %s" % (address))
        net = network.get_project_network("project0", "default")
        self.assertEqual(True, is_in_project(address, "project0"))
        mac = utils.generate_mac()
        hostname = "test-host"
        self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name)
        rv = network.deallocate_ip(address)

        # Doesn't go away until it's dhcp released
        self.assertEqual(True, is_in_project(address, "project0"))

        self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name)
        self.assertEqual(False, is_in_project(address, "project0"))
    def test_too_many_addresses(self):
        """
        Network size is 32, there are 5 addresses reserved for VPN.
        So we should get 23 usable addresses
        """
        net = network.get_project_network("project0", "default")
        hostname = "toomany-hosts"
        macs = {}
        addresses = {}
        for i in range(0, 22):
            macs[i] = utils.generate_mac()
            addresses[i] = network.allocate_ip("netuser", "project0", macs[i])
            self.dnsmasq.issue_ip(macs[i], addresses[i], hostname, net.bridge_name)

        self.assertRaises(NoMoreAddresses, network.allocate_ip, "netuser", "project0", utils.generate_mac())

        for i in range(0, 22):
            rv = network.deallocate_ip(addresses[i])
            self.dnsmasq.release_ip(macs[i], addresses[i], hostname, net.bridge_name)
Esempio n. 15
0
 def build_server_instance(self, env, context):
     reservation = utils.generate_uid("r")
     ltime = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
     inst = self.instdir.new()
     inst["name"] = env["server"]["name"]
     inst["image_id"] = env["server"]["imageId"]
     inst["instance_type"] = env["server"]["flavorId"]
     inst["user_id"] = context["user"].id
     inst["project_id"] = context["project"].id
     inst["reservation_id"] = reservation
     inst["launch_time"] = ltime
     inst["mac_address"] = utils.generate_mac()
     address = network.allocate_ip(inst["user_id"], inst["project_id"], mac=inst["mac_address"])
     inst["private_dns_name"] = str(address)
     inst["bridge_name"] = network.BridgedNetwork.get_network_for_project(
         inst["user_id"], inst["project_id"], "default"  # security group
     )["bridge_name"]
     # key_data, key_name, ami_launch_index
     # TODO(todd): key data or root password
     inst.save()
     return inst