Sets a hostname for instances that properly resolves and cleans up network classes

This commit is contained in:
Vishvananda Ishaya
2010-08-14 21:52:51 +00:00
committed by Tarmac
4 changed files with 27 additions and 16 deletions

View File

@@ -69,10 +69,8 @@ def init_leases(interface):
"""Get the list of hosts for an interface."""
net = model.get_network_by_interface(interface)
res = ""
for host_name in net.hosts:
res += "%s\n" % linux_net.host_dhcp(net,
host_name,
net.hosts[host_name])
for address in net.address_objs:
res += "%s\n" % linux_net.host_dhcp(address)
return res

View File

@@ -124,12 +124,16 @@ class BasicModel(object):
yield cls(identifier)
@classmethod
@absorb_connection_error
def associated_to(cls, foreign_type, foreign_id):
redis_set = cls._redis_association_name(foreign_type, foreign_id)
for identifier in Redis.instance().smembers(redis_set):
for identifier in cls.associated_keys(foreign_type, foreign_id):
yield cls(identifier)
@classmethod
@absorb_connection_error
def associated_keys(cls, foreign_type, foreign_id):
redis_set = cls._redis_association_name(foreign_type, foreign_id)
return Redis.instance().smembers(redis_set) or []
@classmethod
def _redis_set_name(cls, kls_name):
# stupidly pluralize (for compatiblity with previous codebase)
@@ -138,7 +142,7 @@ class BasicModel(object):
@classmethod
def _redis_association_name(cls, foreign_type, foreign_id):
return cls._redis_set_name("%s:%s:%s" %
(foreign_type, foreign_id, cls.__name__))
(foreign_type, foreign_id, cls._redis_name()))
@property
def identifier(self):

View File

@@ -125,6 +125,12 @@ class CloudController(object):
}
else:
keys = ''
address_record = network_model.Address(i['private_dns_name'])
if address_record:
hostname = address_record['hostname']
else:
hostname = 'ip-%s' % i['private_dns_name'].replace('.', '-')
data = {
'user-data': base64.b64decode(i['user_data']),
'meta-data': {
@@ -137,17 +143,17 @@ class CloudController(object):
'root': '/dev/sda1',
'swap': 'sda3'
},
'hostname': i['private_dns_name'], # is this public sometimes?
'hostname': hostname,
'instance-action': 'none',
'instance-id': i['instance_id'],
'instance-type': i.get('instance_type', ''),
'local-hostname': i['private_dns_name'],
'local-hostname': hostname,
'local-ipv4': i['private_dns_name'], # TODO: switch to IP
'kernel-id': i.get('kernel_id', ''),
'placement': {
'availaibility-zone': i.get('availability_zone', 'nova'),
},
'public-hostname': i.get('dns_name', ''),
'public-hostname': hostname,
'public-ipv4': i.get('dns_name', ''), # TODO: switch to IP
'public-keys': keys,
'ramdisk-id': i.get('ramdisk_id', ''),
@@ -563,14 +569,15 @@ class CloudController(object):
is_vpn = False
if image_id == FLAGS.vpn_image_id:
is_vpn = True
inst = self.instdir.new()
allocate_result = yield rpc.call(network_topic,
{"method": "allocate_fixed_ip",
"args": {"user_id": context.user.id,
"project_id": context.project.id,
"security_group": security_group,
"is_vpn": is_vpn}})
"is_vpn": is_vpn,
"hostname": inst.instance_id}})
allocate_data = allocate_result['result']
inst = self.instdir.new()
inst['image_id'] = image_id
inst['kernel_id'] = kernel_id
inst['ramdisk_id'] = ramdisk_id
@@ -584,6 +591,7 @@ class CloudController(object):
inst['project_id'] = context.project.id
inst['ami_launch_index'] = num
inst['security_group'] = security_group
inst['hostname'] = inst.instance_id
for (key, value) in allocate_data.iteritems():
inst[key] = value

View File

@@ -202,8 +202,8 @@ class NetworkTestCase(test.TrialTestCase):
secondmac = result['mac_address']
secondaddress = result['private_dns_name']
self.assertEqual(address, secondaddress)
self.service.deallocate_fixed_ip(secondaddress)
issue_ip(secondmac, secondaddress, hostname, net.bridge_name)
self.service.deallocate_fixed_ip(secondaddress)
release_ip(secondmac, secondaddress, hostname, net.bridge_name)
def test_available_ips(self):
@@ -218,7 +218,7 @@ class NetworkTestCase(test.TrialTestCase):
services (network, gateway, CloudPipe, broadcast)
"""
net = model.get_project_network(self.projects[0].id, "default")
num_preallocated_ips = len(net.hosts.keys())
num_preallocated_ips = len(net.assigned)
net_size = flags.FLAGS.network_size
num_available_ips = net_size - (net.num_bottom_reserved_ips +
num_preallocated_ips +
@@ -254,7 +254,7 @@ class NetworkTestCase(test.TrialTestCase):
def is_in_project(address, project_id):
"""Returns true if address is in specified project"""
return address in model.get_project_network(project_id).list_addresses()
return address in model.get_project_network(project_id).assigned
def binpath(script):
@@ -272,6 +272,7 @@ def issue_ip(mac, private_ip, hostname, interface):
(out, err) = utils.execute(cmd, addl_env=env)
logging.debug("ISSUE_IP: %s, %s ", out, err)
def release_ip(mac, private_ip, hostname, interface):
"""Run del command on dhcpbridge"""
cmd = "%s del %s %s %s" % (binpath('nova-dhcpbridge'),