diff options
| author | Vishvananda Ishaya <vishvananda@gmail.com> | 2010-08-03 17:27:29 -0700 |
|---|---|---|
| committer | Vishvananda Ishaya <vishvananda@gmail.com> | 2010-08-03 17:27:29 -0700 |
| commit | 13ec179c99012ed3d579e19094c0039ccb630796 (patch) | |
| tree | 7bc362707430c8551a9fd144975395243a73f390 | |
| parent | 576dade1d53814416977522637bea9e3c32e5483 (diff) | |
created assocaition between project and host, modified commands to get host async, simplified calls to network
| -rw-r--r-- | nova/network/service.py | 72 | ||||
| -rw-r--r-- | nova/tests/network_unittest.py | 30 |
2 files changed, 54 insertions, 48 deletions
diff --git a/nova/network/service.py b/nova/network/service.py index 72f7db126..45c2867ca 100644 --- a/nova/network/service.py +++ b/nova/network/service.py @@ -23,7 +23,6 @@ Network Nodes are responsible for allocating ips and setting up network import logging from nova import datastore -from nova import exception as nova_exception from nova import flags from nova import service from nova import utils @@ -52,6 +51,13 @@ flags.DEFINE_string('flat_network_broadcast', '192.168.0.255', flags.DEFINE_string('flat_network_dns', '8.8.4.4', 'Dns for simple network') +def get_host_for_project(project_id): + redis = datastore.Redis.instance() + host = redis.get(__host_key(project_id)) + +def __host_key(project_id): + return "network_host:%s" % project_id + class BaseNetworkService(service.Service): """Implements common network service functionality @@ -61,12 +67,18 @@ class BaseNetworkService(service.Service): def __init__(self, *args, **kwargs): self.network = model.PublicNetworkController() - def create_network(self, user_id, project_id, security_group='default', - *args, **kwargs): - """Subclass implements creating network and returns network data""" - raise NotImplementedError() + def get_network_host(self, user_id, project_id, *args, **kwargs): + """Safely becomes the host of the projects network""" + redis = datastore.Redis.instance() + key = __host_key(project_id) + if redis.setnx(key, FLAGS.node_name): + return FLAGS.node_name + else: + return redis.get(key) - def allocate_fixed_ip(self, user_id, project_id, *args, **kwargs): + def allocate_fixed_ip(self, user_id, project_id, + security_group='default', + *args, **kwargs): """Subclass implements getting fixed ip from the pool""" raise NotImplementedError() @@ -92,22 +104,11 @@ class BaseNetworkService(service.Service): class FlatNetworkService(BaseNetworkService): - def create_network(self, user_id, project_id, security_group='default', - *args, **kwargs): - """Creates network and returns bridge + """Basic network where no vlans are used""" - Flat network service simply returns a common bridge regardless of - project. - """ - return {'network_type': 'injected', - 'bridge_name': FLAGS.flat_network_bridge, - 'network_network': FLAGS.flat_network_network, - 'network_netmask': FLAGS.flat_network_netmask, - 'network_gateway': FLAGS.flat_network_gateway, - 'network_broadcast': FLAGS.flat_network_broadcast, - 'network_dns': FLAGS.flat_network_dns} - - def allocate_fixed_ip(self, user_id, project_id, *args, **kwargs): + def allocate_fixed_ip(self, user_id, project_id, + security_group='default', + *args, **kwargs): """Gets a fixed ip from the pool Flat network just grabs the next available ip from the pool @@ -119,23 +120,26 @@ class FlatNetworkService(BaseNetworkService): fixed_ip = redis.spop('ips') if not fixed_ip: raise exception.NoMoreAddresses() - return {'mac': utils.generate_mac(), - 'ip' : str(fixed_ip)} + return {'network_type': 'injected', + 'mac_address': utils.generate_mac(), + 'private_dns_name': str(fixed_ip), + 'bridge_name': FLAGS.flat_network_bridge, + 'network_network': FLAGS.flat_network_network, + 'network_netmask': FLAGS.flat_network_netmask, + 'network_gateway': FLAGS.flat_network_gateway, + 'network_broadcast': FLAGS.flat_network_broadcast, + 'network_dns': FLAGS.flat_network_dns} def deallocate_fixed_ip(self, fixed_ip, *args, **kwargs): """Returns an ip to the pool""" datastore.Redis.instance().sadd('ips', fixed_ip) class VlanNetworkService(BaseNetworkService): - """Allocates ips and sets up networks""" - def create_network(self, user_id, project_id, security_group='default', - *args, **kwargs): - """Creates network and returns bridge""" - net = model.get_project_network(project_id, security_group) - return {'network_type': 'dhcp', - 'bridge_name': net['bridge_name']} + """Vlan network with dhcp""" - def allocate_fixed_ip(self, user_id, project_id, vpn=False, *args, **kwargs): + def allocate_fixed_ip(self, user_id, project_id, + security_group='default', + vpn=False, *args, **kwargs): """Gets a fixed ip from the pool """ mac = utils.generate_mac() net = model.get_project_network(project_id) @@ -143,8 +147,10 @@ class VlanNetworkService(BaseNetworkService): fixed_ip = net.allocate_vpn_ip(user_id, project_id, mac) else: fixed_ip = net.allocate_ip(user_id, project_id, mac) - return {'mac': mac, - 'ip' : fixed_ip} + return {'network_type': 'dhcp', + 'bridge_name': net['bridge_name'], + 'mac_address': mac, + 'private_dns_name' : fixed_ip} def deallocate_fixed_ip(self, fixed_ip, *args, **kwargs): diff --git a/nova/tests/network_unittest.py b/nova/tests/network_unittest.py index 4b2f6c649..a9695f818 100644 --- a/nova/tests/network_unittest.py +++ b/nova/tests/network_unittest.py @@ -71,8 +71,8 @@ class NetworkTestCase(test.TrialTestCase): def test_allocate_deallocate_fixed_ip(self): result = self.service.allocate_fixed_ip( self.user.id, self.projects[0].id) - address = result['ip'] - mac = result['mac'] + address = result['private_dns_name'] + mac = result['mac_address'] logging.debug("Was allocated %s" % (address)) net = model.get_project_network(self.projects[0].id, "default") self.assertEqual(True, is_in_project(address, self.projects[0].id)) @@ -90,12 +90,12 @@ class NetworkTestCase(test.TrialTestCase): hostname = "test-host" result = self.service.allocate_fixed_ip( self.user.id, self.projects[0].id) - mac = result['mac'] - address = result['ip'] + mac = result['mac_address'] + address = result['private_dns_name'] result = self.service.allocate_fixed_ip( self.user, self.projects[1].id) - secondmac = result['mac'] - secondaddress = result['ip'] + secondmac = result['mac_address'] + secondaddress = result['private_dns_name'] net = model.get_project_network(self.projects[0].id, "default") secondnet = model.get_project_network(self.projects[1].id, "default") @@ -124,22 +124,22 @@ class NetworkTestCase(test.TrialTestCase): def test_subnet_edge(self): result = self.service.allocate_fixed_ip(self.user.id, self.projects[0].id) - firstaddress = result['ip'] + firstaddress = result['private_dns_name'] hostname = "toomany-hosts" for i in range(1,5): project_id = self.projects[i].id result = self.service.allocate_fixed_ip( self.user, project_id) - mac = result['mac'] - address = result['ip'] + mac = result['mac_address'] + address = result['private_dns_name'] result = self.service.allocate_fixed_ip( self.user, project_id) - mac2 = result['mac'] - address2 = result['ip'] + mac2 = result['mac_address'] + address2 = result['private_dns_name'] result = self.service.allocate_fixed_ip( self.user, project_id) - mac3 = result['mac'] - address3 = result['ip'] + mac3 = result['mac_address'] + address3 = result['private_dns_name'] self.assertEqual(False, is_in_project(address, self.projects[0].id)) self.assertEqual(False, is_in_project(address2, self.projects[0].id)) self.assertEqual(False, is_in_project(address3, self.projects[0].id)) @@ -194,8 +194,8 @@ class NetworkTestCase(test.TrialTestCase): addresses = {} for i in range(0, (num_available_ips - 1)): result = self.service.allocate_fixed_ip(self.user.id, self.projects[0].id) - macs[i] = result['mac'] - addresses[i] = result['ip'] + macs[i] = result['mac_address'] + addresses[i] = result['private_dns_name'] self.dnsmasq.issue_ip(macs[i], addresses[i], hostname, net.bridge_name) self.assertRaises(NoMoreAddresses, self.service.allocate_fixed_ip, self.user.id, self.projects[0].id) |
