diff options
| author | Vishvananda Ishaya <vishvananda@yahoo.com> | 2010-08-23 22:06:49 -0700 |
|---|---|---|
| committer | Vishvananda Ishaya <vishvananda@yahoo.com> | 2010-08-23 22:06:49 -0700 |
| commit | ce658b72aebe3d2caf41d5250c56e40474501014 (patch) | |
| tree | 59450773da86cc519848175bd55c501da278d8e5 | |
| parent | 14055e979fcfd7f085c16416f7344d146dd714dc (diff) | |
| download | nova-ce658b72aebe3d2caf41d5250c56e40474501014.tar.gz nova-ce658b72aebe3d2caf41d5250c56e40474501014.tar.xz nova-ce658b72aebe3d2caf41d5250c56e40474501014.zip | |
moving network code and fixing run_instances
| -rw-r--r-- | nova/db/api.py | 25 | ||||
| -rw-r--r-- | nova/db/sqlalchemy/api.py | 26 | ||||
| -rw-r--r-- | nova/endpoint/cloud.py | 49 | ||||
| -rw-r--r-- | nova/network/service.py | 12 |
4 files changed, 76 insertions, 36 deletions
diff --git a/nova/db/api.py b/nova/db/api.py index a3c54df24..b460859c4 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -106,12 +106,12 @@ def floating_ip_deallocate(context, address): #################### -def fixed_ip_allocate_address(context, network_id): +def fixed_ip_allocate(context, network_id): """Allocate free fixed ip and return the address. Raises if one is not available. """ - return _impl.fixed_ip_allocate_address(context, network_id) + return _impl.fixed_ip_allocate(context, network_id) def fixed_ip_get_by_address(context, address): @@ -163,20 +163,30 @@ def instance_get(context, instance_id): def instance_get_all(context): - """Gets all instances.""" + """Get all instances.""" return _impl.instance_get_all(context) +def instance_get_by_name(context, name): + """Get an instance by name.""" + return _impl.instance_get_by_project(context, name) + + def instance_get_by_project(context, project_id): - """Gets all instance belonging to a project.""" + """Get all instance belonging to a project.""" return _impl.instance_get_by_project(context, project_id) def instance_get_by_reservation(context, reservation_id): - """Gets all instance belonging to a reservation.""" + """Get all instance belonging to a reservation.""" return _impl.instance_get_by_reservation(context, reservation_id) +def instance_get_host(context, instance_id): + """Get the host that the instance is running on.""" + return _impl.instance_get_all(context, instance_id) + + def instance_state(context, instance_id, state, description=None): """Set the state of an instance.""" return _impl.instance_state(context, instance_id, state, description) @@ -234,6 +244,11 @@ def network_get_index(context, network_id): return _impl.network_get_index(context, network_id) +def network_get_vpn_ip(context, network_id): + """Gets non-conflicting index for network""" + return _impl.network_get_vpn_ip(context, network_id) + + def network_set_cidr(context, network_id, cidr): """Set the Classless Inner Domain Routing for the network""" return _impl.network_set_cidr(context, network_id, cidr) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index e05563c13..73833a9f3 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -91,7 +91,7 @@ def floating_ip_deallocate(context, address): ################### -def fixed_ip_allocate_address(context, network_id): +def fixed_ip_allocate(context, network_id): session = models.NovaBase.get_session() query = session.query(models.FixedIp).filter_by(network_id=network_id) query = query.filter_by(reserved=False).filter_by(allocated=False) @@ -104,7 +104,7 @@ def fixed_ip_allocate_address(context, network_id): fixed_ip_ref['allocated'] = True session.add(fixed_ip_ref) session.commit() - return fixed_ip_ref['ip_str'] + fixed_ip_ref def fixed_ip_get_by_address(context, address): @@ -150,6 +150,7 @@ def fixed_ip_instance_disassociate(context, address): def instance_create(context, values): instance_ref = models.Instance() for (key, value) in values.iteritems(): + print key instance_ref[key] = value instance_ref.save() return instance_ref.id @@ -168,6 +169,11 @@ def instance_get_all(context): return models.Instance.all() +def instance_get_by_name(context, name): + # NOTE(vish): remove the 'i-' + return models.Instance.find(name[2:]) + + def instance_get_by_project(context, project_id): session = models.NovaBase.get_session() query = session.query(models.Instance) @@ -184,6 +190,11 @@ def instance_get_by_reservation(context, reservation_id): return results +def instance_get_host(context, instance_id): + instance_ref = instance_get(context, instance_id) + return instance_ref['node_name'] + + def instance_state(context, instance_id, state, description=None): instance_ref = instance_get(context, instance_id) instance_ref.set_state(state, description) @@ -198,6 +209,7 @@ def instance_update(context, instance_id, values): ################### + # NOTE(vish): is there a better place for this logic? def network_allocate(context, project_id): """Set up the network""" @@ -219,6 +231,7 @@ def network_allocate(context, project_id): net['vpn_public_port'] = FLAGS.vpn_start + index db.network_update(context, network_id, net) db.network_create_fixed_ips(context, network_id, FLAGS.cnt_vpn_clients) + return network_ref def network_create(context, values): @@ -274,7 +287,8 @@ def network_get_vpn_ip(context, network_id): fixed_ip = fixed_ip_get_by_address(context, address) if fixed_ip['allocated']: raise db.AddressAlreadyAllocated() - db.fixed_ip_allocate(context, {'allocated': True}) + db.fixed_ip_update(context, fixed_ip['id'], {'allocated': True}) + return fixed_ip def network_get_host(context, network_id): @@ -340,10 +354,10 @@ def project_get_network(context, project_id): if not rv: raise exception.NotFound('No network for project: %s' % project_id) return rv - - + + ################### - + def queue_get_for(context, topic, physical_node_id): return "%s.%s" % (topic, physical_node_id) # FIXME(ja): this should be servername? diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index dd489cd95..db79c585e 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -522,16 +522,16 @@ class CloudController(object): @defer.inlineCallbacks def _get_network_topic(self, context): """Retrieves the network host for a project""" - host = network_service.get_host_for_project(context.project.id) + network_ref = db.project_get_network(context, context.project.id) + host = db.network_get_host(context, network_ref['id']) if not host: host = yield rpc.call(FLAGS.network_topic, {"method": "set_network_host", - "args": {"user_id": context.user.id, - "project_id": context.project.id}}) - defer.returnValue('%s.%s' %(FLAGS.network_topic, host)) + "args": {"project_id": context.project.id}}) + defer.returnValue(db.queue_get_for(FLAGS.network_topic, host)) @rbac.allow('projectmanager', 'sysadmin') - #@defer.inlineCallbacks + @defer.inlineCallbacks 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 @@ -571,11 +571,16 @@ class CloudController(object): for num in range(int(kwargs['max_count'])): inst = {} - inst['mac_address'] = utils.generate_mac() - inst['fixed_ip'] = db.fixed_ip_allocate_address(context, network_ref['id']) inst['image_id'] = image_id inst['kernel_id'] = kernel_id inst['ramdisk_id'] = ramdisk_id + instance_ref = db.instance_create(context, inst) + inst_id = instance_ref['id'] + if db.instance_is_vpn(instance_ref['id']): + fixed_ip = db.fixed_ip_allocate(context, network_ref['id']) + else: + fixed_ip = db.network_get_vpn_ip(context, network_ref['id']) + inst['mac_address'] = utils.generate_mac() inst['user_data'] = kwargs.get('user_data', '') inst['instance_type'] = kwargs.get('instance_type', 'm1.small') inst['reservation_id'] = reservation_id @@ -585,16 +590,23 @@ class CloudController(object): inst['project_id'] = context.project.id # FIXME(ja) inst['launch_index'] = num inst['security_group'] = security_group - # inst['hostname'] = inst.id # FIXME(ja): id isn't assigned until create + inst['hostname'] = inst_id # FIXME(ja): id isn't assigned until create + db.instance_update(context, inst_id, inst) + + + # TODO(vish): This probably should be done in the scheduler + # network is setup when host is assigned + network_topic = yield self.get_network_topic() + rpc.call(network_topic, + {"method": "setup_fixed_ip", + "args": {"fixed_ip": fixed_ip['id']}}) - inst_id = db.instance_create(context, inst) rpc.cast(FLAGS.compute_topic, {"method": "run_instance", "args": {"instance_id": inst_id}}) logging.debug("Casting to node for %s/%s's instance %s" % (context.project.name, context.user.name, inst_id)) - # defer.returnValue(self._format_instances(context, reservation_id)) - return self._format_run_instances(context, reservation_id) + defer.returnValue(self._format_instances(context, reservation_id)) @rbac.allow('projectmanager', 'sysadmin') @@ -605,13 +617,12 @@ class CloudController(object): for name in instance_id: logging.debug("Going to try and terminate %s" % name) try: - inst_id = name[2:] # remove the i- - instance_ref = db.instance_get(context, inst_id) + instance_ref = db.instance_get_by_name(context, name) except exception.NotFound: logging.warning("Instance %s was not found during terminate" % name) continue - + # FIXME(ja): where should network deallocate occur? # floating_ip = network_model.get_public_ip_for_instance(i) # if floating_ip: @@ -622,7 +633,7 @@ class CloudController(object): # rpc.cast(network_topic, # {"method": "disassociate_floating_ip", # "args": {"floating_ip": floating_ip}}) - # + # # fixed_ip = instance.get('private_dns_name', None) # if fixed_ip: # logging.debug("Deallocating address %s" % fixed_ip) @@ -633,14 +644,14 @@ class CloudController(object): # {"method": "deallocate_fixed_ip", # "args": {"fixed_ip": fixed_ip}}) - if instance_ref['physical_node_id'] is not None: + host = db.instance_get_host(context, instance_ref['id']) + if host is not None: # NOTE(joshua?): It's also internal default - rpc.cast(db.queue_get_for(context, FLAGS.compute_topic, - instance_ref['physical_node_id']), + rpc.cast(db.queue_get_for(context, FLAGS.compute_topic, host), {"method": "terminate_instance", "args": {"instance_id": name}}) else: - db.instance_destroy(context, inst_id) + db.instance_destroy(context, instance_ref['id']) # defer.returnValue(True) return True diff --git a/nova/network/service.py b/nova/network/service.py index 368d99cbd..7eed2c10a 100644 --- a/nova/network/service.py +++ b/nova/network/service.py @@ -206,11 +206,11 @@ class VlanNetworkService(BaseNetworkService): # admin command or fixtures db.network_ensure_indexes(None, FLAGS.num_networks) - def allocate_fixed_ip(self, project_id, instance_id, is_vpn=False, - context=None, *args, **kwargs): + def setup_fixed_ip(self, project_id, instance_id, context=None, + *args, **kwargs): """Gets a fixed ip from the pool""" network_ref = db.project_get_network(context, project_id) - if is_vpn: + if db.instance_is_vpn(context, instance_id): address = db.network_get_vpn_ip_address(context, network_ref['id']) logging.debug("Allocating vpn IP %s", address) @@ -225,8 +225,6 @@ class VlanNetworkService(BaseNetworkService): address = parent.allocate_fixed_ip(project_id, instance_id, context) - _driver.ensure_vlan_bridge(network_ref['vlan'], - network_ref['bridge']) return address def deallocate_fixed_ip(self, address, context=None): @@ -257,7 +255,9 @@ class VlanNetworkService(BaseNetworkService): def _on_set_network_host(self, context, network_id): """Called when this host becomes the host for a project""" - pass + network_ref = db.network_get(network_id) + _driver.ensure_vlan_bridge(network_ref['vlan'], + network_ref['bridge']) @classmethod |
