From 5d00101e5c0e6ba5e4ecba49c132f74c18b5267c Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 3 Mar 2011 16:28:04 -0400 Subject: [PATCH 01/53] start of fanout --- nova/rpc.py | 20 ++++++++++++++++++++ nova/scheduler/zone_manager.py | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/nova/rpc.py b/nova/rpc.py index 8fe4565d..a02cdc90 100644 --- a/nova/rpc.py +++ b/nova/rpc.py @@ -218,6 +218,16 @@ class TopicPublisher(Publisher): super(TopicPublisher, self).__init__(connection=connection) +class FanoutPublisher(Publisher): + """Publishes messages to a fanout exchange.""" + exchange_type = "fanout" + + def __init__(self, topic, connection=None): + self.exchange = "%s_fanout" % topic + self.durable = False + super(FanoutPublisher, self).__init__(connection=connection) + + class DirectConsumer(Consumer): """Consumes messages directly on a channel specified by msg_id""" exchange_type = "direct" @@ -360,6 +370,16 @@ def cast(context, topic, msg): publisher.close() +def fanout_cast(context, topic, msg): + """Sends a message on a fanout exchange without waiting for a response""" + LOG.debug(_("Making asynchronous fanout cast...")) + _pack_context(msg, context) + conn = Connection.instance() + publisher = FanoutPublisher(topic, connection=conn) + publisher.send(msg) + publisher.close() + + def generic_response(message_data, message): """Logs a result and exits""" LOG.debug(_('response %s'), message_data) diff --git a/nova/scheduler/zone_manager.py b/nova/scheduler/zone_manager.py index edf9000c..eedc5c23 100644 --- a/nova/scheduler/zone_manager.py +++ b/nova/scheduler/zone_manager.py @@ -105,6 +105,7 @@ class ZoneManager(object): def __init__(self): self.last_zone_db_check = datetime.min self.zone_states = {} + self.compute_states = {} self.green_pool = greenpool.GreenPool() def get_zone_list(self): @@ -141,3 +142,6 @@ class ZoneManager(object): self.last_zone_db_check = datetime.now() self._refresh_from_db(context) self._poll_zones(context) + + def update_compute_capabilities(self): + logging.debug(_("****** UPDATE COMPUTE CAPABILITIES *******")) From 803b3b79a8cae9dea49cc74a5abaf5da0fa18ff2 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Sun, 20 Feb 2011 13:36:45 -0800 Subject: [PATCH 02/53] scheduler manager --- nova/scheduler_manager.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 nova/scheduler_manager.py diff --git a/nova/scheduler_manager.py b/nova/scheduler_manager.py new file mode 100644 index 00000000..c78b6fea --- /dev/null +++ b/nova/scheduler_manager.py @@ -0,0 +1,39 @@ +# Copyright 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +This module provides SchedulerDependentManager, a base class for +any Manager that has Capabilities that should be related to the +Scheduler. + +These Capabilities are hints that can help the scheduler route +requests to the appropriate service instance. +""" + +from nova import manager +from nova.scheduler import api + + +FLAGS = flags.FLAGS + + +def SchedulerDependentManage(manager.Manager): + def __init__(self, host=None, db_driver=None): + self.last_capabilities = {} + super(SchedulerDependentManager, self).__init__(host, db_driver) + + def periodic_tasks(self, context=None): + """Pass data back to the scheduler at a periodic interval""" + logging.debug(_("Notifying Schedulers of capabilities ...")) From dba165de077f6d79039955dc79cd5ff6e35b3acb Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Sun, 20 Feb 2011 16:40:08 -0800 Subject: [PATCH 03/53] service ping working --- nova/scheduler_manager.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/nova/scheduler_manager.py b/nova/scheduler_manager.py index c78b6fea..987ca8e9 100644 --- a/nova/scheduler_manager.py +++ b/nova/scheduler_manager.py @@ -22,18 +22,23 @@ These Capabilities are hints that can help the scheduler route requests to the appropriate service instance. """ +import sys + +from nova import flags from nova import manager from nova.scheduler import api +from nova import log as logging FLAGS = flags.FLAGS -def SchedulerDependentManage(manager.Manager): +class SchedulerDependentManager(manager.Manager): def __init__(self, host=None, db_driver=None): self.last_capabilities = {} super(SchedulerDependentManager, self).__init__(host, db_driver) def periodic_tasks(self, context=None): """Pass data back to the scheduler at a periodic interval""" - logging.debug(_("Notifying Schedulers of capabilities ...")) + logging.debug(_("*** Notifying Schedulers of capabilities ...")) + super(SchedulerDependentManager, self).periodic_tasks(context) From a06093b1f089d41f8ed306727400c3fb3f9850e7 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Sun, 20 Feb 2011 20:58:59 -0800 Subject: [PATCH 04/53] fanout kinda working --- nova/rpc.py | 51 +++++++++++++++++++++++++-------------- nova/scheduler/api.py | 7 ++++++ nova/scheduler_manager.py | 2 ++ 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/nova/rpc.py b/nova/rpc.py index a02cdc90..e0cf6d30 100644 --- a/nova/rpc.py +++ b/nova/rpc.py @@ -137,24 +137,7 @@ class Consumer(messaging.Consumer): return timer -class Publisher(messaging.Publisher): - """Publisher base class""" - pass - - -class TopicConsumer(Consumer): - """Consumes messages on a specific topic""" - exchange_type = "topic" - - def __init__(self, connection=None, topic="broadcast"): - self.queue = topic - self.routing_key = topic - self.exchange = FLAGS.control_exchange - self.durable = False - super(TopicConsumer, self).__init__(connection=connection) - - -class AdapterConsumer(TopicConsumer): +class AdapterConsumer(Consumer): """Calls methods on a proxy object based on method and args""" def __init__(self, connection=None, topic="broadcast", proxy=None): LOG.debug(_('Initing the Adapter Consumer for %s') % topic) @@ -207,6 +190,37 @@ class AdapterConsumer(TopicConsumer): return +class Publisher(messaging.Publisher): + """Publisher base class""" + pass + + +class TopicAdapterConsumer(AdapterConsumer): + """Consumes messages on a specific topic""" + exchange_type = "topic" + + def __init__(self, connection=None, topic="broadcast", proxy=None): + self.queue = topic + self.routing_key = topic + self.exchange = FLAGS.control_exchange + self.durable = False + super(TopicAdapterConsumer, self).__init__(connection=connection, + topic=topic, proxy=proxy) + + +class FanoutAdapterConsumer(AdapterConsumer): + """Consumes messages from a fanout exchange""" + exchange_type = "fanout" + + def __init__(self, connection=None, topic="broadcast", proxy=None): + self.exchange = "%s_fanout" % topic + self.routing_key = topic + self.queue = "ignored" + self.durable = False + super(FanoutAdapterConsumer, self).__init__(connection=connection, + topic=topic, proxy=proxy) + + class TopicPublisher(Publisher): """Publishes messages on a specific topic""" exchange_type = "topic" @@ -214,6 +228,7 @@ class TopicPublisher(Publisher): def __init__(self, connection=None, topic="broadcast"): self.routing_key = topic self.exchange = FLAGS.control_exchange + self.queue = "ignored" self.durable = False super(TopicPublisher, self).__init__(connection=connection) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 8491bf3a..53d72507 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -47,3 +47,10 @@ class API: for item in items: item['api_url'] = item['api_url'].replace('\\/', '/') return items + + @classmethod + def update_service_capabilities(cls, context, service_name, capabilities): + kwargs = dict(method='update_service_capabilities', + service_name=service_name, capabilities=capabilities) + return rpc.fanout_cast(context, 'scheduler', kwargs) + diff --git a/nova/scheduler_manager.py b/nova/scheduler_manager.py index 987ca8e9..a4530161 100644 --- a/nova/scheduler_manager.py +++ b/nova/scheduler_manager.py @@ -41,4 +41,6 @@ class SchedulerDependentManager(manager.Manager): def periodic_tasks(self, context=None): """Pass data back to the scheduler at a periodic interval""" logging.debug(_("*** Notifying Schedulers of capabilities ...")) + api.API.update_service_capabilities(context, 'compute', self.last_capabilities) + super(SchedulerDependentManager, self).periodic_tasks(context) From e860e7d5500941394bfdcefba510fdfc5143104f Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Sun, 20 Feb 2011 22:33:39 -0800 Subject: [PATCH 05/53] fanout works --- nova/rpc.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/nova/rpc.py b/nova/rpc.py index e0cf6d30..601e45b4 100644 --- a/nova/rpc.py +++ b/nova/rpc.py @@ -215,8 +215,12 @@ class FanoutAdapterConsumer(AdapterConsumer): def __init__(self, connection=None, topic="broadcast", proxy=None): self.exchange = "%s_fanout" % topic self.routing_key = topic - self.queue = "ignored" + unique = uuid.uuid4().hex + self.queue = "%s_fanout_%s" % (topic, unique) self.durable = False + LOG.info(_("Created '%(exchange)s' fanout exchange " + "with '%(key)s' routing key"), + dict(exchange=self.exchange, key=self.routing_key)) super(FanoutAdapterConsumer, self).__init__(connection=connection, topic=topic, proxy=proxy) @@ -228,7 +232,6 @@ class TopicPublisher(Publisher): def __init__(self, connection=None, topic="broadcast"): self.routing_key = topic self.exchange = FLAGS.control_exchange - self.queue = "ignored" self.durable = False super(TopicPublisher, self).__init__(connection=connection) @@ -239,7 +242,10 @@ class FanoutPublisher(Publisher): def __init__(self, topic, connection=None): self.exchange = "%s_fanout" % topic + self.queue = "%s_fanout" % topic self.durable = False + LOG.info(_("Writing to '%(exchange)s' fanout exchange"), + dict(exchange=self.exchange)) super(FanoutPublisher, self).__init__(connection=connection) From c7df2944f9128be2f9bd4456acf6ec2f4a982b90 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Wed, 23 Feb 2011 14:41:11 -0800 Subject: [PATCH 06/53] tests working again --- nova/tests/test_rpc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/tests/test_rpc.py b/nova/tests/test_rpc.py index 4820e04f..44d7c91e 100644 --- a/nova/tests/test_rpc.py +++ b/nova/tests/test_rpc.py @@ -36,7 +36,7 @@ class RpcTestCase(test.TestCase): super(RpcTestCase, self).setUp() self.conn = rpc.Connection.instance(True) self.receiver = TestReceiver() - self.consumer = rpc.AdapterConsumer(connection=self.conn, + self.consumer = rpc.TopicAdapterConsumer(connection=self.conn, topic='test', proxy=self.receiver) self.consumer.attach_to_eventlet() @@ -97,7 +97,7 @@ class RpcTestCase(test.TestCase): nested = Nested() conn = rpc.Connection.instance(True) - consumer = rpc.AdapterConsumer(connection=conn, + consumer = rpc.TopicAdapterConsumer(connection=conn, topic='nested', proxy=nested) consumer.attach_to_eventlet() From b46458d11ab47d85284472dcd6f3476f13680f74 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Wed, 23 Feb 2011 17:58:32 -0800 Subject: [PATCH 07/53] Tests all working again --- nova/flags.py | 2 +- nova/scheduler/api.py | 1 - nova/scheduler_manager.py | 22 ++++++++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/nova/flags.py b/nova/flags.py index 6f37c82f..7036180f 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -356,5 +356,5 @@ DEFINE_string('node_availability_zone', 'nova', 'availability zone of this node') DEFINE_string('zone_name', 'nova', 'name of this zone') -DEFINE_string('zone_capabilities', 'kypervisor:xenserver;os:linux', +DEFINE_string('zone_capabilities', 'hypervisor:xenserver;os:linux', 'Key/Value tags which represent capabilities of this zone') diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 53d72507..6a6bfc03 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -53,4 +53,3 @@ class API: kwargs = dict(method='update_service_capabilities', service_name=service_name, capabilities=capabilities) return rpc.fanout_cast(context, 'scheduler', kwargs) - diff --git a/nova/scheduler_manager.py b/nova/scheduler_manager.py index a4530161..65bd71c0 100644 --- a/nova/scheduler_manager.py +++ b/nova/scheduler_manager.py @@ -34,13 +34,27 @@ FLAGS = flags.FLAGS class SchedulerDependentManager(manager.Manager): - def __init__(self, host=None, db_driver=None): - self.last_capabilities = {} + + """Periodically send capability updates to the Scheduler services. + Services that need to update the Scheduler of their capabilities + should derive from this class. Otherwise they can derive from + manager.Manager directly. Updates are only sent after + update_service_capabilities is called with non-None values.""" + + def __init__(self, host=None, db_driver=None, service_name="undefined"): + self.last_capabilities = None + self.service_name = service_name super(SchedulerDependentManager, self).__init__(host, db_driver) + def update_service_capabilities(self, capabilities): + """Remember these capabilities to send on next periodic update.""" + self.last_capabilities = capabilities + def periodic_tasks(self, context=None): """Pass data back to the scheduler at a periodic interval""" - logging.debug(_("*** Notifying Schedulers of capabilities ...")) - api.API.update_service_capabilities(context, 'compute', self.last_capabilities) + if self.last_capabilities: + logging.debug(_("*** Notifying Schedulers of capabilities ...")) + api.API.update_service_capabilities(context, self.service_name, + self.last_capabilities) super(SchedulerDependentManager, self).periodic_tasks(context) From 3291423f09631a93e3e38aa1c3e0268242e79b6a Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 24 Feb 2011 01:53:01 -0800 Subject: [PATCH 08/53] capability aggregation working --- nova/scheduler/api.py | 15 +++++++++++++-- nova/scheduler/zone_manager.py | 35 +++++++++++++++++++++++++++++++--- nova/scheduler_manager.py | 6 ++---- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 6a6bfc03..ac38350e 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -43,13 +43,24 @@ class API: return rpc.call(context, queue, kwargs) def get_zone_list(self, context): + """Return a list of zones assoicated with this zone.""" items = self._call_scheduler('get_zone_list', context) for item in items: item['api_url'] = item['api_url'].replace('\\/', '/') return items + def get_zone_capabilities(self, context, service=None): + """Returns a dict of key, value capabilities for this zone, + or for a particular class of services running in this zone.""" + return self._call_scheduler('get_zone_capabilities', context=context, + params=dict(service=service)) + @classmethod - def update_service_capabilities(cls, context, service_name, capabilities): + def update_service_capabilities(cls, context, service_name, host, + capabilities): + """Send an update to all the scheduler services informing them + of the capabilities of this service.""" kwargs = dict(method='update_service_capabilities', - service_name=service_name, capabilities=capabilities) + args=dict(service_name=service_name, host=host, + capabilities=capabilities)) return rpc.fanout_cast(context, 'scheduler', kwargs) diff --git a/nova/scheduler/zone_manager.py b/nova/scheduler/zone_manager.py index eedc5c23..09c9811f 100644 --- a/nova/scheduler/zone_manager.py +++ b/nova/scheduler/zone_manager.py @@ -105,13 +105,37 @@ class ZoneManager(object): def __init__(self): self.last_zone_db_check = datetime.min self.zone_states = {} - self.compute_states = {} + self.service_states = {} # { : { : { cap k : v }}} self.green_pool = greenpool.GreenPool() def get_zone_list(self): """Return the list of zones we know about.""" return [zone.to_dict() for zone in self.zone_states.values()] + def get_zone_capabilities(self, context, service=None): + """Roll up all the individual host info to generic 'service' + capabilities. Each capability is aggregated into + _min and _max values.""" + service_dict = self.service_states + if service: + service_dict = dict(service_name=service, + hosts=self.service_states.get(service, {})) + + # TODO(sandy) - be smarter about fabricating this structure. + # But it's likely to change once we understand what the Best-Match + # code will need better. + combined = {} # { _ : (min, max), ... } + for service_name, host_dict in service_dict.iteritems(): + for host, caps_dict in host_dict.iteritems(): + for cap, value in caps_dict.iteritems(): + key = "%s_%s" % (service_name, cap) + min_value, max_value = combined.get(key, (value, value)) + min_value = min(min_value, value) + max_value = max(max_value, value) + combined[key] = (min_value, max_value) + + return combined + def _refresh_from_db(self, context): """Make our zone state map match the db.""" # Add/update existing zones ... @@ -143,5 +167,10 @@ class ZoneManager(object): self._refresh_from_db(context) self._poll_zones(context) - def update_compute_capabilities(self): - logging.debug(_("****** UPDATE COMPUTE CAPABILITIES *******")) + def update_service_capabilities(self, service_name, host, capabilities): + """Update the per-service capabilities based on this notification.""" + logging.debug(_("Received %(service_name)s service update from " + "%(host)s: %(capabilities)s") % locals()) + service_caps = self.service_states.get(service_name, {}) + service_caps[host] = capabilities + self.service_states[service_name] = service_caps diff --git a/nova/scheduler_manager.py b/nova/scheduler_manager.py index 65bd71c0..ca39b85d 100644 --- a/nova/scheduler_manager.py +++ b/nova/scheduler_manager.py @@ -34,13 +34,11 @@ FLAGS = flags.FLAGS class SchedulerDependentManager(manager.Manager): - """Periodically send capability updates to the Scheduler services. Services that need to update the Scheduler of their capabilities should derive from this class. Otherwise they can derive from manager.Manager directly. Updates are only sent after update_service_capabilities is called with non-None values.""" - def __init__(self, host=None, db_driver=None, service_name="undefined"): self.last_capabilities = None self.service_name = service_name @@ -53,8 +51,8 @@ class SchedulerDependentManager(manager.Manager): def periodic_tasks(self, context=None): """Pass data back to the scheduler at a periodic interval""" if self.last_capabilities: - logging.debug(_("*** Notifying Schedulers of capabilities ...")) + logging.debug(_("Notifying Schedulers of capabilities ...")) api.API.update_service_capabilities(context, self.service_name, - self.last_capabilities) + self.host, self.last_capabilities) super(SchedulerDependentManager, self).periodic_tasks(context) From 86d79a81a149b25e3c764f9b94b289b32e031df8 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 24 Feb 2011 14:32:25 -0800 Subject: [PATCH 09/53] service capabilities test --- nova/tests/test_zones.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/nova/tests/test_zones.py b/nova/tests/test_zones.py index 5a52a050..3ca71d5f 100644 --- a/nova/tests/test_zones.py +++ b/nova/tests/test_zones.py @@ -76,6 +76,34 @@ class ZoneManagerTestCase(test.TestCase): self.assertEquals(len(zm.zone_states), 1) self.assertEquals(zm.zone_states[1].username, 'user1') + def test_service_capabilities(self): + zm = zone_manager.ZoneManager() + caps = zm.get_zone_capabilities(self, None) + self.assertEquals(caps, {}) + + zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2)) + caps = zm.get_zone_capabilities(self, None) + self.assertEquals(caps, dict(svc1_a=(1, 1), svc1_b=(2, 2))) + + zm.update_service_capabilities("svc1", "host1", dict(a=2, b=3)) + caps = zm.get_zone_capabilities(self, None) + self.assertEquals(caps, dict(svc1_a=(2, 2), svc1_b=(3, 3))) + + zm.update_service_capabilities("svc1", "host2", dict(a=20, b=30)) + caps = zm.get_zone_capabilities(self, None) + self.assertEquals(caps, dict(svc1_a=(2, 20), svc1_b=(3, 30))) + + zm.update_service_capabilities("svc10", "host1", dict(a=99, b=99)) + caps = zm.get_zone_capabilities(self, None) + self.assertEquals(caps, dict(svc1_a=(2, 20), svc1_b=(3, 30), + svc10_a=(99, 99), svc10_b=(99, 99))) + + zm.update_service_capabilities("svc1", "host3", dict(c=5)) + caps = zm.get_zone_capabilities(self, None) + self.assertEquals(caps, dict(svc1_a=(2, 20), svc1_b=(3, 30), + svc1_c=(5, 5), svc10_a=(99, 99), + svc10_b=(99, 99))) + def test_refresh_from_db_replace_existing(self): zm = zone_manager.ZoneManager() zone_state = zone_manager.ZoneState() From 507de7900b9797c90a45d6ecfd249eff390aee0b Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 24 Feb 2011 15:23:15 -0800 Subject: [PATCH 10/53] new tests --- nova/scheduler/api.py | 39 ++++++++++++++++++---------------- nova/scheduler/zone_manager.py | 3 +-- nova/tests/test_zones.py | 6 ++++++ 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index ac38350e..fcff2f14 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -25,34 +25,37 @@ FLAGS = flags.FLAGS LOG = logging.getLogger('nova.scheduler.api') +def _call_scheduler(method, context, params=None): + """Generic handler for RPC calls to the scheduler. + + :param params: Optional dictionary of arguments to be passed to the + scheduler worker + + :retval: Result returned by scheduler worker + """ + if not params: + params = {} + queue = FLAGS.scheduler_topic + kwargs = {'method': method, 'args': params} + return rpc.call(context, queue, kwargs) + + class API: """API for interacting with the scheduler.""" - def _call_scheduler(self, method, context, params=None): - """Generic handler for RPC calls to the scheduler. - - :param params: Optional dictionary of arguments to be passed to the - scheduler worker - - :retval: Result returned by scheduler worker - """ - if not params: - params = {} - queue = FLAGS.scheduler_topic - kwargs = {'method': method, 'args': params} - return rpc.call(context, queue, kwargs) - - def get_zone_list(self, context): + @classmethod + def get_zone_list(cls, context): """Return a list of zones assoicated with this zone.""" - items = self._call_scheduler('get_zone_list', context) + items = _call_scheduler('get_zone_list', context) for item in items: item['api_url'] = item['api_url'].replace('\\/', '/') return items - def get_zone_capabilities(self, context, service=None): + @classmethod + def get_zone_capabilities(cls, context, service=None): """Returns a dict of key, value capabilities for this zone, or for a particular class of services running in this zone.""" - return self._call_scheduler('get_zone_capabilities', context=context, + return _call_scheduler('get_zone_capabilities', context=context, params=dict(service=service)) @classmethod diff --git a/nova/scheduler/zone_manager.py b/nova/scheduler/zone_manager.py index 09c9811f..c1a50dbc 100644 --- a/nova/scheduler/zone_manager.py +++ b/nova/scheduler/zone_manager.py @@ -118,8 +118,7 @@ class ZoneManager(object): _min and _max values.""" service_dict = self.service_states if service: - service_dict = dict(service_name=service, - hosts=self.service_states.get(service, {})) + service_dict = {service: self.service_states.get(service, {})} # TODO(sandy) - be smarter about fabricating this structure. # But it's likely to change once we understand what the Best-Match diff --git a/nova/tests/test_zones.py b/nova/tests/test_zones.py index 3ca71d5f..79d766f2 100644 --- a/nova/tests/test_zones.py +++ b/nova/tests/test_zones.py @@ -104,6 +104,12 @@ class ZoneManagerTestCase(test.TestCase): svc1_c=(5, 5), svc10_a=(99, 99), svc10_b=(99, 99))) + caps = zm.get_zone_capabilities(self, 'svc1') + self.assertEquals(caps, dict(svc1_a=(2, 20), svc1_b=(3, 30), + svc1_c=(5, 5))) + caps = zm.get_zone_capabilities(self, 'svc10') + self.assertEquals(caps, dict(svc10_a=(99, 99), svc10_b=(99, 99))) + def test_refresh_from_db_replace_existing(self): zm = zone_manager.ZoneManager() zone_state = zone_manager.ZoneState() From c1df9c5d11f205d76631a2b34b3ee685d2be43ac Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 24 Feb 2011 15:44:27 -0800 Subject: [PATCH 11/53] sorry, pep8 --- nova/tests/test_zones.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/nova/tests/test_zones.py b/nova/tests/test_zones.py index 79d766f2..48e1442c 100644 --- a/nova/tests/test_zones.py +++ b/nova/tests/test_zones.py @@ -88,28 +88,28 @@ class ZoneManagerTestCase(test.TestCase): zm.update_service_capabilities("svc1", "host1", dict(a=2, b=3)) caps = zm.get_zone_capabilities(self, None) self.assertEquals(caps, dict(svc1_a=(2, 2), svc1_b=(3, 3))) - + zm.update_service_capabilities("svc1", "host2", dict(a=20, b=30)) caps = zm.get_zone_capabilities(self, None) self.assertEquals(caps, dict(svc1_a=(2, 20), svc1_b=(3, 30))) - + zm.update_service_capabilities("svc10", "host1", dict(a=99, b=99)) caps = zm.get_zone_capabilities(self, None) self.assertEquals(caps, dict(svc1_a=(2, 20), svc1_b=(3, 30), svc10_a=(99, 99), svc10_b=(99, 99))) - + zm.update_service_capabilities("svc1", "host3", dict(c=5)) caps = zm.get_zone_capabilities(self, None) self.assertEquals(caps, dict(svc1_a=(2, 20), svc1_b=(3, 30), svc1_c=(5, 5), svc10_a=(99, 99), svc10_b=(99, 99))) - + caps = zm.get_zone_capabilities(self, 'svc1') self.assertEquals(caps, dict(svc1_a=(2, 20), svc1_b=(3, 30), svc1_c=(5, 5))) caps = zm.get_zone_capabilities(self, 'svc10') self.assertEquals(caps, dict(svc10_a=(99, 99), svc10_b=(99, 99))) - + def test_refresh_from_db_replace_existing(self): zm = zone_manager.ZoneManager() zone_state = zone_manager.ZoneState() From 12b601d3a864f8352e2678cbdb89a12590cb6456 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Fri, 11 Mar 2011 14:33:12 -0500 Subject: [PATCH 12/53] Add config for osapi_extensions_path. Update the ExtensionManager so that it loads extensions in the osapi_extensions_path. --- CA/openssl.cnf.tmpl | 2 +- nova/flags.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CA/openssl.cnf.tmpl b/CA/openssl.cnf.tmpl index dd81f1c2..cf8bac82 100644 --- a/CA/openssl.cnf.tmpl +++ b/CA/openssl.cnf.tmpl @@ -43,7 +43,7 @@ policy = policy_match [ policy_match ] countryName = match -stateOrProvinceName = match +stateOrProvinceName = optional organizationName = optional organizationalUnitName = optional commonName = supplied diff --git a/nova/flags.py b/nova/flags.py index 9123e9ac..63e4cb5b 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -298,6 +298,8 @@ DEFINE_string('ec2_dmz_host', '$my_ip', 'internal ip of api server') DEFINE_integer('ec2_port', 8773, 'cloud controller port') DEFINE_string('ec2_scheme', 'http', 'prefix for ec2') DEFINE_string('ec2_path', '/services/Cloud', 'suffix for ec2') +DEFINE_string('osapi_extensions_path', '/var/lib/nova/extensions', + 'default directory for nova extensions') DEFINE_string('osapi_host', '$my_ip', 'ip of api server') DEFINE_string('osapi_scheme', 'http', 'prefix for openstack') DEFINE_integer('osapi_port', 8774, 'OpenStack API port') From 9b490a1838d037f84e981ad025f0570bbe4b072a Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Tue, 15 Mar 2011 18:29:26 -0700 Subject: [PATCH 13/53] first pass openstack redirect working --- nova/scheduler/api.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 8f9806f7..c0e28a0a 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -73,13 +73,22 @@ class API(object): args=dict(service_name=service_name, host=host, capabilities=capabilities)) return rpc.fanout_cast(context, 'scheduler', kwargs) + + @classmethod + def get_instance_or_reroute(cls, context, instance_id): + instance = db.instance_get(context, instance_id) + zones = db.zone_get_all(context) + + LOG.debug("*** Firing ZoneRouteException") + # Throw a reroute Exception for the middleware to pick up. + raise exception.ZoneRouteException(zones) @classmethod - def get_queue_for_instance(cls, context, service, instance_id) + def get_queue_for_instance(cls, context, service, instance_id): instance = db.instance_get(context, instance_id) zone = db.get_zone(instance.zone.id) if cls._is_current_zone(zone): - return db.queue_get_for(context, service, instance['host']): + return db.queue_get_for(context, service, instance['host']) # Throw a reroute Exception for the middleware to pick up. raise exception.ZoneRouteException(zone) From 2f1676497fb397fefcd3b7b2b8b4300ef4e9a204 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Wed, 16 Mar 2011 06:47:27 -0700 Subject: [PATCH 14/53] Checks locally before routing --- nova/scheduler/api.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index c0e28a0a..48da5bcf 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -76,11 +76,16 @@ class API(object): @classmethod def get_instance_or_reroute(cls, context, instance_id): - instance = db.instance_get(context, instance_id) - zones = db.zone_get_all(context) + try: + instance = db.instance_get(context, instance_id) + return instance + except exception.InstanceNotFound, e: + LOG.debug(_("Instance %(instance_id)s not found locally: '%(e)s'" % + locals())) - LOG.debug("*** Firing ZoneRouteException") # Throw a reroute Exception for the middleware to pick up. + LOG.debug("Firing ZoneRouteException") + zones = db.zone_get_all(context) raise exception.ZoneRouteException(zones) @classmethod From 05a8fc547cb722af8c941f9767e26c8f763768b0 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Wed, 16 Mar 2011 14:22:29 -0400 Subject: [PATCH 15/53] Revert commit that modified CA/openssl.cnf.tmpl. --- CA/openssl.cnf.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CA/openssl.cnf.tmpl b/CA/openssl.cnf.tmpl index cf8bac82..dd81f1c2 100644 --- a/CA/openssl.cnf.tmpl +++ b/CA/openssl.cnf.tmpl @@ -43,7 +43,7 @@ policy = policy_match [ policy_match ] countryName = match -stateOrProvinceName = optional +stateOrProvinceName = match organizationName = optional organizationalUnitName = optional commonName = supplied From 08bfa2e34b359360e536f6f90fec038935be553b Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Wed, 16 Mar 2011 11:44:40 -0700 Subject: [PATCH 16/53] Refactored ZoneRedirect into ZoneChildHelper so ZoneManager can use this too. --- nova/scheduler/api.py | 40 +++++++++++++++++++++++++++------- nova/scheduler/zone_manager.py | 2 +- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 48da5bcf..073784f3 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -23,6 +23,10 @@ from nova import flags from nova import log as logging from nova import rpc +import novaclient.client as client + +from eventlet import greenpool + FLAGS = flags.FLAGS LOG = logging.getLogger('nova.scheduler.api') @@ -76,6 +80,8 @@ class API(object): @classmethod def get_instance_or_reroute(cls, context, instance_id): + """Return an instance from the db or throw a ZoneRouteException + if not found.""" try: instance = db.instance_get(context, instance_id) return instance @@ -88,12 +94,30 @@ class API(object): zones = db.zone_get_all(context) raise exception.ZoneRouteException(zones) - @classmethod - def get_queue_for_instance(cls, context, service, instance_id): - instance = db.instance_get(context, instance_id) - zone = db.get_zone(instance.zone.id) - if cls._is_current_zone(zone): - return db.queue_get_for(context, service, instance['host']) - # Throw a reroute Exception for the middleware to pick up. - raise exception.ZoneRouteException(zone) +def _wrap_method(function, self): + def _wrap(*args, **kwargs): + return function(self, *args, **kwargs) + return _wrap + + +def _process(self, zone): + nova = client.OpenStackClient(zone.username, zone.password, + zone.api_url) + nova.authenticate() + return self.process(nova, zone) + + +class ChildZoneHelper(object): + """Delegate a call to a set of Child Zones and wait for their + responses. Could be used for Zone Redirect or by the Scheduler + plug-ins to query the children.""" + + def start(self, zone_list): + self.green_pool = greenpool.GreenPool() + return [ result for result in self.green_pool.imap( + _wrap_method(_process, self), zone_list)] + + def process(self, client, zone): + """Derived class must override.""" + pass diff --git a/nova/scheduler/zone_manager.py b/nova/scheduler/zone_manager.py index c1a50dbc..d32cc2e8 100644 --- a/nova/scheduler/zone_manager.py +++ b/nova/scheduler/zone_manager.py @@ -104,7 +104,7 @@ class ZoneManager(object): """Keeps the zone states updated.""" def __init__(self): self.last_zone_db_check = datetime.min - self.zone_states = {} + self.zone_states = {} # { : ZoneState } self.service_states = {} # { : { : { cap k : v }}} self.green_pool = greenpool.GreenPool() From 94fa8dd933be088e907c0f5e836363f9abd03e37 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Wed, 16 Mar 2011 19:04:27 -0700 Subject: [PATCH 17/53] moved scheduler API check into db.api decorator --- nova/scheduler/api.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 073784f3..2da2dabf 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -78,30 +78,16 @@ class API(object): capabilities=capabilities)) return rpc.fanout_cast(context, 'scheduler', kwargs) - @classmethod - def get_instance_or_reroute(cls, context, instance_id): - """Return an instance from the db or throw a ZoneRouteException - if not found.""" - try: - instance = db.instance_get(context, instance_id) - return instance - except exception.InstanceNotFound, e: - LOG.debug(_("Instance %(instance_id)s not found locally: '%(e)s'" % - locals())) - - # Throw a reroute Exception for the middleware to pick up. - LOG.debug("Firing ZoneRouteException") - zones = db.zone_get_all(context) - raise exception.ZoneRouteException(zones) - def _wrap_method(function, self): + """Wrap method to supply 'self'.""" def _wrap(*args, **kwargs): return function(self, *args, **kwargs) return _wrap def _process(self, zone): + """Worker stub for green thread pool""" nova = client.OpenStackClient(zone.username, zone.password, zone.api_url) nova.authenticate() @@ -114,10 +100,13 @@ class ChildZoneHelper(object): plug-ins to query the children.""" def start(self, zone_list): + """Spawn a green thread for each child zone, calling the + derived classes process() method as the worker. Returns + a list of HTTP Responses. 1 per child.""" self.green_pool = greenpool.GreenPool() return [ result for result in self.green_pool.imap( _wrap_method(_process, self), zone_list)] def process(self, client, zone): - """Derived class must override.""" + """Worker Method. Derived class must override.""" pass From d5385f88893173db573d8584e7bc3ce75fc5ee57 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Wed, 16 Mar 2011 19:26:54 -0700 Subject: [PATCH 18/53] pep8 --- nova/scheduler/api.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 2da2dabf..f0b645c0 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -77,7 +77,7 @@ class API(object): args=dict(service_name=service_name, host=host, capabilities=capabilities)) return rpc.fanout_cast(context, 'scheduler', kwargs) - + def _wrap_method(function, self): """Wrap method to supply 'self'.""" @@ -92,7 +92,7 @@ def _process(self, zone): zone.api_url) nova.authenticate() return self.process(nova, zone) - + class ChildZoneHelper(object): """Delegate a call to a set of Child Zones and wait for their @@ -104,9 +104,9 @@ class ChildZoneHelper(object): derived classes process() method as the worker. Returns a list of HTTP Responses. 1 per child.""" self.green_pool = greenpool.GreenPool() - return [ result for result in self.green_pool.imap( + return [result for result in self.green_pool.imap( _wrap_method(_process, self), zone_list)] - + def process(self, client, zone): """Worker Method. Derived class must override.""" pass From 6ff4d1d7b5f79b5e003474aaedf228cac5d3ebf8 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Wed, 16 Mar 2011 23:31:06 -0300 Subject: [PATCH 19/53] removed dead method --- nova/scheduler/api.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 804dade6..71d211fe 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -49,10 +49,6 @@ def _call_scheduler(method, context, params=None): class API(object): """API for interacting with the scheduler.""" - @classmethod - def _is_current_zone(cls, zone): - return True - @classmethod def get_zone_list(cls, context): """Return a list of zones assoicated with this zone.""" From ac1f686186d2f67d7d3b68c8fe3790dd16cc7139 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 17 Mar 2011 07:30:22 -0700 Subject: [PATCH 20/53] Replaced capability flags with List --- nova/flags.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nova/flags.py b/nova/flags.py index c05cef37..3a8ec1a3 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -358,5 +358,6 @@ DEFINE_string('node_availability_zone', 'nova', 'availability zone of this node') DEFINE_string('zone_name', 'nova', 'name of this zone') -DEFINE_string('zone_capabilities', 'hypervisor:xenserver;os:linux', - 'Key/Value tags which represent capabilities of this zone') +DEFINE_list('zone_capabilities', + ['hypervisor=xenserver;kvm', 'os=linux;windows'], + 'Key/Multi-value list representng capabilities of this zone') From 90ccbdafe5b86d468d5ee48a839f23072a02db8a Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 17 Mar 2011 19:55:55 -0700 Subject: [PATCH 22/53] decorator more generic now --- nova/scheduler/api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 0e2c69f7..190eb363 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -99,6 +99,7 @@ def child_zone_helper(zone_list, func): def _issue_novaclient_command(nova, zone, method_name, instance_id): server = None try: + manager = getattr(nova, "servers") if isinstance(instance_id, int) or instance_id.isdigit(): server = manager.get(int(instance_id)) else: From 4e01d42dc1777bf1b980da8206e43d301d42f0c1 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Fri, 18 Mar 2011 06:38:02 -0700 Subject: [PATCH 23/53] enable_zone_routing flag --- nova/scheduler/api.py | 60 ++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 190eb363..90b92d7e 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -28,6 +28,10 @@ import novaclient.client as client from eventlet import greenpool FLAGS = flags.FLAGS +flags.DEFINE_bool('enable_zone_routing', + False, + 'When True, routing to child zones will occur.') + LOG = logging.getLogger('nova.scheduler.api') @@ -83,7 +87,8 @@ def _wrap_method(function, self): def _process(func, zone): - """Worker stub for green thread pool""" + """Worker stub for green thread pool. Give the worker + an authenticated nova client and zone info.""" nova = client.OpenStackClient(zone.username, zone.password, zone.api_url) nova.authenticate() @@ -91,36 +96,42 @@ def _process(func, zone): def child_zone_helper(zone_list, func): + """Fire off a command to each zone in the list.""" green_pool = greenpool.GreenPool() return [result for result in green_pool.imap( _wrap_method(_process, func), zone_list)] -def _issue_novaclient_command(nova, zone, method_name, instance_id): - server = None +def _issue_novaclient_command(nova, zone, collection, method_name, \ + item_id): + """Use novaclient to issue command to a single child zone. + One of these will be run in parallel for each child zone.""" + item = None try: - manager = getattr(nova, "servers") - if isinstance(instance_id, int) or instance_id.isdigit(): - server = manager.get(int(instance_id)) + manager = getattr(nova, collection) + if isinstance(item_id, int) or item_id.isdigit(): + item = manager.get(int(item_id)) else: - server = manager.find(name=instance_id) + item = manager.find(name=item_id) except novaclient.NotFound: url = zone.api_url - LOG.debug(_("Instance %(instance_id)s not found on '%(url)s'" % + LOG.debug(_("%(collection)s '%(item_id)s' not found on '%(url)s'" % locals())) return - return getattr(server, method_name)() + return getattr(item, method_name)() -def wrap_novaclient_function(f, method_name, instance_id): +def wrap_novaclient_function(f, collection, method_name, item_id): + """Appends collection, method_name and item_id to the incoming + (nova, zone) call from child_zone_helper.""" def inner(nova, zone): - return f(nova, zone, method_name, instance_id) + return f(nova, zone, collection, method_name, item_id) return inner -class reroute_if_not_found(object): +class reroute_compute(object): """Decorator used to indicate that the method should delegate the call the child zones if the db query can't find anything. @@ -130,19 +141,32 @@ class reroute_if_not_found(object): def __call__(self, f): def wrapped_f(*args, **kwargs): - LOG.debug("***REROUTE-3: %s / %s" % (args, kwargs)) - context = args[1] - instance_id = args[2] + collection, context, item_id = \ + self.get_collection_context_and_id() try: return f(*args, **kwargs) except exception.InstanceNotFound, e: - LOG.debug(_("Instance %(instance_id)s not found " + LOG.debug(_("Instance %(item_id)s not found " "locally: '%(e)s'" % locals())) + if not FLAGS.enable_zone_routing: + raise + zones = db.zone_get_all(context) + if not zones: + raise + result = child_zone_helper(zones, wrap_novaclient_function(_issue_novaclient_command, - self.method_name, instance_id)) + collection, self.method_name, item_id)) LOG.debug("***REROUTE: %s" % result) - return result + return self.unmarshall_result(result) return wrapped_f + + def get_collection_context_and_id(self, args): + """Returns a tuple of (novaclient collection name, security + context and resource id. Derived class should override this.""" + return ("servers", args[1], args[2]) + + def unmarshall_result(self, result): + return result From a19358e76f77cecc2a35324a2615be682df5b444 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Fri, 18 Mar 2011 07:47:23 -0700 Subject: [PATCH 24/53] whoopsy2 --- nova/scheduler/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 90b92d7e..b639ae78 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -142,7 +142,7 @@ class reroute_compute(object): def __call__(self, f): def wrapped_f(*args, **kwargs): collection, context, item_id = \ - self.get_collection_context_and_id() + self.get_collection_context_and_id(args) try: return f(*args, **kwargs) except exception.InstanceNotFound, e: From caaba7cb2cf404eca2017b474bdafc604f0a10c1 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Fri, 18 Mar 2011 09:02:36 -0700 Subject: [PATCH 25/53] fixed up novaclient usage to include managers --- nova/scheduler/api.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index b639ae78..7efc2807 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -17,14 +17,14 @@ Handles all requests relating to schedulers. """ +import novaclient + from nova import db from nova import exception from nova import flags from nova import log as logging from nova import rpc -import novaclient.client as client - from eventlet import greenpool FLAGS = flags.FLAGS @@ -80,7 +80,7 @@ class API(object): def _wrap_method(function, self): - """Wrap method to supply 'self'.""" + """Wrap method to supply self.""" def _wrap(*args, **kwargs): return function(self, *args, **kwargs) return _wrap @@ -89,8 +89,7 @@ def _wrap_method(function, self): def _process(func, zone): """Worker stub for green thread pool. Give the worker an authenticated nova client and zone info.""" - nova = client.OpenStackClient(zone.username, zone.password, - zone.api_url) + nova = novaclient.OpenStack(zone.username, zone.password, zone.api_url) nova.authenticate() return func(nova, zone) @@ -134,8 +133,7 @@ def wrap_novaclient_function(f, collection, method_name, item_id): class reroute_compute(object): """Decorator used to indicate that the method should delegate the call the child zones if the db query - can't find anything. - """ + can't find anything.""" def __init__(self, method_name): self.method_name = method_name From 4804a8fa4c7b22b3b88f425ab3f467e1e7e7b459 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Fri, 18 Mar 2011 09:21:08 -0700 Subject: [PATCH 26/53] results --- nova/scheduler/api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 7efc2807..6b0f804f 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -118,7 +118,10 @@ def _issue_novaclient_command(nova, zone, collection, method_name, \ locals())) return - return getattr(item, method_name)() + LOG.debug("***CALLING CHILD ZONE") + result = getattr(item, method_name)() + LOG.debug("***CHILD ZONE GAVE %s", result) + return result def wrap_novaclient_function(f, collection, method_name, item_id): From 9161928b5357edab5d8ad3528490c8cff5b6e2cc Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Fri, 18 Mar 2011 11:46:27 -0700 Subject: [PATCH 27/53] api decorator --- nova/scheduler/api.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 6b0f804f..f5df446b 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -105,22 +105,25 @@ def _issue_novaclient_command(nova, zone, collection, method_name, \ item_id): """Use novaclient to issue command to a single child zone. One of these will be run in parallel for each child zone.""" - item = None + result = None try: manager = getattr(nova, collection) if isinstance(item_id, int) or item_id.isdigit(): - item = manager.get(int(item_id)) + result = manager.get(int(item_id)) else: - item = manager.find(name=item_id) + result = manager.find(name=item_id) except novaclient.NotFound: url = zone.api_url LOG.debug(_("%(collection)s '%(item_id)s' not found on '%(url)s'" % locals())) return - LOG.debug("***CALLING CHILD ZONE") - result = getattr(item, method_name)() - LOG.debug("***CHILD ZONE GAVE %s", result) + if method_name.lower() not in ['get', 'find']: + LOG.debug("***CALLING CHILD ZONE") + m = getattr(item, method_name) + LOG.debug("***METHOD ATTR %s" % m) + result = getattr(item, method_name)() + LOG.debug("***CHILD ZONE GAVE %s", result) return result @@ -133,6 +136,14 @@ def wrap_novaclient_function(f, collection, method_name, item_id): return inner +class RedirectResult(exception.Error): + """Used to the HTTP API know that these results are pre-cooked + and they can be returned to the caller directly.""" + def __init__(self, results): + self.results = results + super(RedirectResult, self).__init__( + message=_("Uncaught Zone redirection exception")) + class reroute_compute(object): """Decorator used to indicate that the method should delegate the call the child zones if the db query @@ -161,7 +172,7 @@ class reroute_compute(object): wrap_novaclient_function(_issue_novaclient_command, collection, self.method_name, item_id)) LOG.debug("***REROUTE: %s" % result) - return self.unmarshall_result(result) + raise RedirectResult(self.unmarshall_result(result)) return wrapped_f def get_collection_context_and_id(self, args): @@ -170,4 +181,14 @@ class reroute_compute(object): return ("servers", args[1], args[2]) def unmarshall_result(self, result): - return result + return [server.__dict__ for server in result] + + +def redirect_handler(f): + def new_f(*args, **kwargs): + try: + return f(*args, **kwargs) + except RedirectResult, e: + LOG.debug("***CAUGHT REROUTE: %s" % e.results) + return e.results + return new_f From abc233036bb957378d081ea05c8a98cc5c34faa9 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Fri, 18 Mar 2011 12:00:35 -0700 Subject: [PATCH 28/53] works again. woo hoo --- nova/scheduler/api.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index f5df446b..8b8457e8 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -181,7 +181,13 @@ class reroute_compute(object): return ("servers", args[1], args[2]) def unmarshall_result(self, result): - return [server.__dict__ for server in result] + server = result[0].__dict__ + + for k in server.keys(): + if k[0] == '_' or k == 'manager': + del server[k] + + return dict(server=server) def redirect_handler(f): From d82458a3701aeac9c652c28eb12c5157e437d4c4 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 21 Mar 2011 07:49:58 -0700 Subject: [PATCH 29/53] remove scheduler.api.API. naming changes. --- nova/rpc.py | 2 +- nova/scheduler/api.py | 43 +++++++++++++++++++------------------------ 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/nova/rpc.py b/nova/rpc.py index 4918c0b9..2e3cd905 100644 --- a/nova/rpc.py +++ b/nova/rpc.py @@ -244,7 +244,7 @@ class FanoutPublisher(Publisher): self.exchange = "%s_fanout" % topic self.queue = "%s_fanout" % topic self.durable = False - LOG.info(_("Writing to '%(exchange)s' fanout exchange"), + LOG.info(_("Creating '%(exchange)s' fanout exchange"), dict(exchange=self.exchange)) super(FanoutPublisher, self).__init__(connection=connection) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index b6d27dac..e2cf3b6a 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -40,30 +40,25 @@ def _call_scheduler(method, context, params=None): return rpc.call(context, queue, kwargs) -class API(object): - """API for interacting with the scheduler.""" +def get_zone_list(context): + """Return a list of zones assoicated with this zone.""" + items = _call_scheduler('get_zone_list', context) + for item in items: + item['api_url'] = item['api_url'].replace('\\/', '/') + return items - @classmethod - def get_zone_list(cls, context): - """Return a list of zones assoicated with this zone.""" - items = _call_scheduler('get_zone_list', context) - for item in items: - item['api_url'] = item['api_url'].replace('\\/', '/') - return items - @classmethod - def get_zone_capabilities(cls, context, service=None): - """Returns a dict of key, value capabilities for this zone, - or for a particular class of services running in this zone.""" - return _call_scheduler('get_zone_capabilities', context=context, - params=dict(service=service)) +def get_zone_capabilities(context, service=None): + """Returns a dict of key, value capabilities for this zone, + or for a particular class of services running in this zone.""" + return _call_scheduler('get_zone_capabilities', context=context, + params=dict(service=service)) - @classmethod - def update_service_capabilities(cls, context, service_name, host, - capabilities): - """Send an update to all the scheduler services informing them - of the capabilities of this service.""" - kwargs = dict(method='update_service_capabilities', - args=dict(service_name=service_name, host=host, - capabilities=capabilities)) - return rpc.fanout_cast(context, 'scheduler', kwargs) + +def update_service_capabilities(context, service_name, host, capabilities): + """Send an update to all the scheduler services informing them + of the capabilities of this service.""" + kwargs = dict(method='update_service_capabilities', + args=dict(service_name=service_name, host=host, + capabilities=capabilities)) + return rpc.fanout_cast(context, 'scheduler', kwargs) From 7e298eea38c80b42a769b1408ca832ce55f8f665 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 21 Mar 2011 11:01:34 -0700 Subject: [PATCH 30/53] more robust extraction of arguments --- nova/scheduler/api.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index c7acd354..935e7b36 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -149,7 +149,7 @@ class reroute_compute(object): def __call__(self, f): def wrapped_f(*args, **kwargs): collection, context, item_id = \ - self.get_collection_context_and_id(args) + self.get_collection_context_and_id(args, kwargs) try: return f(*args, **kwargs) except exception.InstanceNotFound, e: @@ -170,10 +170,16 @@ class reroute_compute(object): raise RedirectResult(self.unmarshall_result(result)) return wrapped_f - def get_collection_context_and_id(self, args): + def get_collection_context_and_id(self, args, kwargs): """Returns a tuple of (novaclient collection name, security context and resource id. Derived class should override this.""" - return ("servers", args[1], args[2]) + context = kwargs.get('context', None) + instance_id = kwargs.get('instance_id', None) + if len(args) > 0 and not context: + context = args[1] + if len(args) > 1 and not instance_id: + context = args[2] + return ("servers", context, instance_id) def unmarshall_result(self, result): server = result[0].__dict__ From 87e36b0e98b7ecc410bee6bd8546ea07b21b4a89 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 21 Mar 2011 11:07:19 -0700 Subject: [PATCH 31/53] pep8 --- nova/scheduler/api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 935e7b36..aebfe177 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -127,7 +127,7 @@ def wrap_novaclient_function(f, collection, method_name, item_id): (nova, zone) call from child_zone_helper.""" def inner(nova, zone): return f(nova, zone, collection, method_name, item_id) - + return inner @@ -139,6 +139,7 @@ class RedirectResult(exception.Error): super(RedirectResult, self).__init__( message=_("Uncaught Zone redirection exception")) + class reroute_compute(object): """Decorator used to indicate that the method should delegate the call the child zones if the db query @@ -158,7 +159,7 @@ class reroute_compute(object): if not FLAGS.enable_zone_routing: raise - + zones = db.zone_get_all(context) if not zones: raise From 1e3e1f80ed362a50d59d2c7a2dacfb765d2aa311 Mon Sep 17 00:00:00 2001 From: Josh Kearney Date: Mon, 21 Mar 2011 13:21:26 -0500 Subject: [PATCH 32/53] Added XenAPI rescue unit tests --- nova/tests/test_xenapi.py | 12 ++++++++++++ nova/tests/xenapi/stubs.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index 66a973a7..e54ffe71 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -186,6 +186,7 @@ class XenAPIVMTestCase(test.TestCase): stubs.stubout_stream_disk(self.stubs) stubs.stubout_is_vdi_pv(self.stubs) self.stubs.Set(VMOps, 'reset_network', reset_network) + stubs.stub_out_vm_methods(self.stubs) glance_stubs.stubout_glance_client(self.stubs, glance_stubs.FakeGlance) self.conn = xenapi_conn.get_connection(False) @@ -369,6 +370,17 @@ class XenAPIVMTestCase(test.TestCase): self.assertEquals(vif_rec['qos_algorithm_params']['kbps'], str(4 * 1024)) + def test_rescue(self): + instance = self._create_instance() + conn = xenapi_conn.get_connection(False) + conn.rescue(instance, None) + + def test_unrescue(self): + instance = self._create_instance() + conn = xenapi_conn.get_connection(False) + # Ensure that it will not unrescue a non-rescued instance. + self.assertRaises(Exception, conn.unrescue, instance, None) + def tearDown(self): super(XenAPIVMTestCase, self).tearDown() self.manager.delete_project(self.project) diff --git a/nova/tests/xenapi/stubs.py b/nova/tests/xenapi/stubs.py index 70d46a1f..a0370a2e 100644 --- a/nova/tests/xenapi/stubs.py +++ b/nova/tests/xenapi/stubs.py @@ -185,6 +185,25 @@ class FakeSessionForVMTests(fake.SessionBase): pass +def stub_out_vm_methods(stubs): + def fake_shutdown(self, inst, vm, method="clean"): + pass + + def fake_acquire_bootlock(self, vm): + pass + + def fake_release_bootlock(self, vm): + pass + + def fake_spawn_rescue(self, inst): + pass + + stubs.Set(vmops.VMOps, "_shutdown", fake_shutdown) + stubs.Set(vmops.VMOps, "_acquire_bootlock", fake_acquire_bootlock) + stubs.Set(vmops.VMOps, "_release_bootlock", fake_release_bootlock) + stubs.Set(vmops.VMOps, "spawn_rescue", fake_spawn_rescue) + + class FakeSessionForVolumeTests(fake.SessionBase): """ Stubs out a XenAPISession for Volume tests """ def __init__(self, uri): From 8ec86affc84b0f4416e6b08b71d810b62c243f72 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 21 Mar 2011 18:16:35 -0700 Subject: [PATCH 33/53] better comments. First redirect test --- nova/scheduler/api.py | 45 +++++++++++++++++------ nova/tests/test_scheduler.py | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 10 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index aebfe177..ff7e2167 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -84,13 +84,18 @@ def _wrap_method(function, self): def _process(func, zone): """Worker stub for green thread pool. Give the worker an authenticated nova client and zone info.""" + LOG.debug("*** PROCESS %s/%s" % (func, zone)) nova = novaclient.OpenStack(zone.username, zone.password, zone.api_url) nova.authenticate() return func(nova, zone) def child_zone_helper(zone_list, func): - """Fire off a command to each zone in the list.""" + """Fire off a command to each zone in the list. + The return is [novaclient return objects] from each child zone. + For example, if you are calling server.pause(), the list will + be whatever the response from server.pause() is. One entry + per child zone called.""" green_pool = greenpool.GreenPool() return [result for result in green_pool.imap( _wrap_method(_process, func), zone_list)] @@ -103,6 +108,7 @@ def _issue_novaclient_command(nova, zone, collection, method_name, \ result = None try: manager = getattr(nova, collection) + LOG.debug("***MANAGER %s" % manager) if isinstance(item_id, int) or item_id.isdigit(): result = manager.get(int(item_id)) else: @@ -115,9 +121,9 @@ def _issue_novaclient_command(nova, zone, collection, method_name, \ if method_name.lower() not in ['get', 'find']: LOG.debug("***CALLING CHILD ZONE") - m = getattr(item, method_name) + m = getattr(result, method_name) LOG.debug("***METHOD ATTR %s" % m) - result = getattr(item, method_name)() + result = getattr(result, method_name)() LOG.debug("***CHILD ZONE GAVE %s", result) return result @@ -152,6 +158,7 @@ class reroute_compute(object): collection, context, item_id = \ self.get_collection_context_and_id(args, kwargs) try: + # Call the original function ... return f(*args, **kwargs) except exception.InstanceNotFound, e: LOG.debug(_("Instance %(item_id)s not found " @@ -164,32 +171,50 @@ class reroute_compute(object): if not zones: raise + # Ask the children to provide an answer ... result = child_zone_helper(zones, wrap_novaclient_function(_issue_novaclient_command, collection, self.method_name, item_id)) LOG.debug("***REROUTE: %s" % result) + # Scrub the results and raise another exception + # so the API layers can bail out gracefully ... raise RedirectResult(self.unmarshall_result(result)) return wrapped_f def get_collection_context_and_id(self, args, kwargs): """Returns a tuple of (novaclient collection name, security context and resource id. Derived class should override this.""" + LOG.debug("***COLLECT: %s/%s" % (args, kwargs)) context = kwargs.get('context', None) instance_id = kwargs.get('instance_id', None) if len(args) > 0 and not context: context = args[1] if len(args) > 1 and not instance_id: - context = args[2] + instance_id = args[2] return ("servers", context, instance_id) - def unmarshall_result(self, result): - server = result[0].__dict__ + def unmarshall_result(self, zone_responses): + """Result is a list of responses from each child zone. + Each decorator derivation is responsible to turning this + into a format expected by the calling method. For + example, this one is expected to return a single Server + dict {'server':{k:v}}. Others may return a list of them, like + {'servers':[{k,v}]}""" + reduced_response = [] + for zone_response in zone_responses: + if not zone_response: + continue - for k in server.keys(): - if k[0] == '_' or k == 'manager': - del server[k] + server = zone_response.__dict__ - return dict(server=server) + for k in server.keys(): + if k[0] == '_' or k == 'manager': + del server[k] + + reduced_response.append(dict(server=server)) + if reduced_response: + return reduced_response[0] # first for now. + return {} def redirect_handler(f): diff --git a/nova/tests/test_scheduler.py b/nova/tests/test_scheduler.py index 244e43bd..50e2429b 100644 --- a/nova/tests/test_scheduler.py +++ b/nova/tests/test_scheduler.py @@ -21,6 +21,8 @@ Tests For Scheduler import datetime import mox +import stubout +import webob from mox import IgnoreArg from nova import context @@ -32,6 +34,7 @@ from nova import test from nova import rpc from nova import utils from nova.auth import manager as auth_manager +from nova.scheduler import api from nova.scheduler import manager from nova.scheduler import driver from nova.compute import power_state @@ -937,3 +940,70 @@ class SimpleDriverTestCase(test.TestCase): db.instance_destroy(self.context, instance_id) db.service_destroy(self.context, s_ref['id']) db.service_destroy(self.context, s_ref2['id']) + + +class FakeZone(object): + def __init__(self, api_url, username, password): + self.api_url = api_url + self.username = username + self.password = password + +def zone_get_all(context): + return [ + FakeZone('http://example.com', 'bob', 'xxx'), + ] + + +def go_boom(self, context, instance): + raise exception.InstanceNotFound("boom message", instance) + + +def fake_openstack_init(self, username, password, api): + servers=[] + + +def fake_auth(self): + pass + +class FakeServer: + def foo(self): + pass + +class FakeManager: + def get(self, id): + return FakeServer() + +class FakeOpenStack: + + def __init__(self, username, api, auth): + self.servers = FakeManager() + + def authenticate(self): + pass + + +class ZoneRedirectTest(test.TestCase): + def setUp(self): + super(ZoneRedirectTest, self).setUp() + self.stubs = stubout.StubOutForTesting() + + self.stubs.Set(api.novaclient, 'OpenStack', FakeOpenStack) + self.stubs.Set(db, 'zone_get_all', zone_get_all) + + self.enable_zone_routing = FLAGS.enable_zone_routing + FLAGS.enable_zone_routing = True + + def tearDown(self): + self.stubs.UnsetAll() + FLAGS.enable_zone_routing = self.enable_zone_routing + super(ZoneRedirectTest, self).tearDown() + + def test_trap_found_locally(self): + decorator = api.reroute_compute("foo") + try: + wrapper = decorator(go_boom) + result = wrapper(None, None, 1) # self, context, id + except api.RedirectResult, e: + pass + + From c158e05b063248e68d5de413e2223857e96fe43b Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 21 Mar 2011 18:56:59 -0700 Subject: [PATCH 34/53] better comments. First redirect test --- nova/tests/test_scheduler.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/nova/tests/test_scheduler.py b/nova/tests/test_scheduler.py index 50e2429b..6d55cad0 100644 --- a/nova/tests/test_scheduler.py +++ b/nova/tests/test_scheduler.py @@ -958,23 +958,23 @@ def go_boom(self, context, instance): raise exception.InstanceNotFound("boom message", instance) -def fake_openstack_init(self, username, password, api): - servers=[] +class FakeServer(object): + def __init__(self): + self.name = 'myserver' + self.kvm = 'kvm' + self.manager = 100 + self._hidden = True - -def fake_auth(self): - pass - -class FakeServer: def foo(self): - pass + return None -class FakeManager: + +class FakeManager(object): def get(self, id): return FakeServer() -class FakeOpenStack: +class FakeOpenStack: def __init__(self, username, api, auth): self.servers = FakeManager() @@ -987,7 +987,6 @@ class ZoneRedirectTest(test.TestCase): super(ZoneRedirectTest, self).setUp() self.stubs = stubout.StubOutForTesting() - self.stubs.Set(api.novaclient, 'OpenStack', FakeOpenStack) self.stubs.Set(db, 'zone_get_all', zone_get_all) self.enable_zone_routing = FLAGS.enable_zone_routing @@ -999,11 +998,12 @@ class ZoneRedirectTest(test.TestCase): super(ZoneRedirectTest, self).tearDown() def test_trap_found_locally(self): + self.stubs.Set(api.novaclient, 'OpenStack', FakeOpenStack) decorator = api.reroute_compute("foo") try: wrapper = decorator(go_boom) result = wrapper(None, None, 1) # self, context, id except api.RedirectResult, e: - pass + self.assertTrue(e.results, {}) From a20fdf8be4a298e8f6135e4a5fab4018bdae7e15 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 21 Mar 2011 21:41:41 -0700 Subject: [PATCH 35/53] routing test coverage --- nova/scheduler/api.py | 18 ++--- nova/tests/test_scheduler.py | 123 ++++++++++++++++++++++++++++------- 2 files changed, 106 insertions(+), 35 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index ff7e2167..4f189fe3 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -84,7 +84,6 @@ def _wrap_method(function, self): def _process(func, zone): """Worker stub for green thread pool. Give the worker an authenticated nova client and zone info.""" - LOG.debug("*** PROCESS %s/%s" % (func, zone)) nova = novaclient.OpenStack(zone.username, zone.password, zone.api_url) nova.authenticate() return func(nova, zone) @@ -108,7 +107,6 @@ def _issue_novaclient_command(nova, zone, collection, method_name, \ result = None try: manager = getattr(nova, collection) - LOG.debug("***MANAGER %s" % manager) if isinstance(item_id, int) or item_id.isdigit(): result = manager.get(int(item_id)) else: @@ -117,14 +115,10 @@ def _issue_novaclient_command(nova, zone, collection, method_name, \ url = zone.api_url LOG.debug(_("%(collection)s '%(item_id)s' not found on '%(url)s'" % locals())) - return + return None if method_name.lower() not in ['get', 'find']: - LOG.debug("***CALLING CHILD ZONE") - m = getattr(result, method_name) - LOG.debug("***METHOD ATTR %s" % m) result = getattr(result, method_name)() - LOG.debug("***CHILD ZONE GAVE %s", result) return result @@ -172,19 +166,22 @@ class reroute_compute(object): raise # Ask the children to provide an answer ... - result = child_zone_helper(zones, + result = self._call_child_zones(zones, wrap_novaclient_function(_issue_novaclient_command, collection, self.method_name, item_id)) - LOG.debug("***REROUTE: %s" % result) # Scrub the results and raise another exception # so the API layers can bail out gracefully ... raise RedirectResult(self.unmarshall_result(result)) return wrapped_f + def _call_child_zones(self, zones, function): + """Ask the child zones to perform this operation. + Broken out for testing.""" + return child_zone_helper(zones, function) + def get_collection_context_and_id(self, args, kwargs): """Returns a tuple of (novaclient collection name, security context and resource id. Derived class should override this.""" - LOG.debug("***COLLECT: %s/%s" % (args, kwargs)) context = kwargs.get('context', None) instance_id = kwargs.get('instance_id', None) if len(args) > 0 and not context: @@ -222,6 +219,5 @@ def redirect_handler(f): try: return f(*args, **kwargs) except RedirectResult, e: - LOG.debug("***CAUGHT REROUTE: %s" % e.results) return e.results return new_f diff --git a/nova/tests/test_scheduler.py b/nova/tests/test_scheduler.py index 6d55cad0..0aebd038 100644 --- a/nova/tests/test_scheduler.py +++ b/nova/tests/test_scheduler.py @@ -21,6 +21,7 @@ Tests For Scheduler import datetime import mox +import novaclient.exceptions import stubout import webob @@ -954,34 +955,33 @@ def zone_get_all(context): ] +class FakeRerouteCompute(api.reroute_compute): + def _call_child_zones(self, zones, function): + return [ ] + + def get_collection_context_and_id(self, args, kwargs): + return ("servers", None, 1) + + def unmarshall_result(self, zone_responses): + return dict(magic="found me") + + def go_boom(self, context, instance): raise exception.InstanceNotFound("boom message", instance) -class FakeServer(object): - def __init__(self): - self.name = 'myserver' - self.kvm = 'kvm' - self.manager = 100 - self._hidden = True - - def foo(self): - return None +def found_instance(self, context, instance): + return dict(name='myserver') -class FakeManager(object): - def get(self, id): - return FakeServer() +class FakeResource(object): + def __init__(self, attribute_dict): + for k, v in attribute_dict.iteritems(): + setattr(self, k, v) - -class FakeOpenStack: - def __init__(self, username, api, auth): - self.servers = FakeManager() - - def authenticate(self): + def pause(self): pass - class ZoneRedirectTest(test.TestCase): def setUp(self): super(ZoneRedirectTest, self).setUp() @@ -998,12 +998,87 @@ class ZoneRedirectTest(test.TestCase): super(ZoneRedirectTest, self).tearDown() def test_trap_found_locally(self): - self.stubs.Set(api.novaclient, 'OpenStack', FakeOpenStack) - decorator = api.reroute_compute("foo") + decorator = FakeRerouteCompute("foo") try: - wrapper = decorator(go_boom) - result = wrapper(None, None, 1) # self, context, id + result = decorator(found_instance)(None, None, 1) except api.RedirectResult, e: - self.assertTrue(e.results, {}) + self.fail(_("Successful database hit should succeed")) + def test_trap_not_found_locally(self): + decorator = FakeRerouteCompute("foo") + try: + result = decorator(go_boom)(None, None, 1) + except api.RedirectResult, e: + self.assertEquals(e.results['magic'], 'found me') + def test_get_collection_context_and_id(self): + decorator = api.reroute_compute("foo") + self.assertEquals(decorator.get_collection_context_and_id( + (None, 10, 20), {}), ("servers", 10, 20)) + self.assertEquals(decorator.get_collection_context_and_id( + (None, 11,), dict(instance_id=21)), ("servers", 11, 21)) + self.assertEquals(decorator.get_collection_context_and_id( + (None,), dict(context=12, instance_id=22)), ("servers", 12, 22)) + + def test_unmarshal_single_server(self): + decorator = api.reroute_compute("foo") + self.assertEquals(decorator.unmarshall_result([]), {}) + self.assertEquals(decorator.unmarshall_result( + [FakeResource(dict(a=1, b=2)),]), + dict(server=dict(a=1, b=2))) + self.assertEquals(decorator.unmarshall_result( + [FakeResource(dict(a=1, _b=2)),]), + dict(server=dict(a=1,))) + self.assertEquals(decorator.unmarshall_result( + [FakeResource(dict(a=1, manager=2)),]), + dict(server=dict(a=1,))) + self.assertEquals(decorator.unmarshall_result( + [FakeResource(dict(_a=1, manager=2)),]), + dict(server={})) + +class FakeServerCollection(object): + def get(self, instance_id): + return FakeResource(dict(a=10, b=20)) + + def find(self, name): + return FakeResource(dict(a=11, b=22)) + +class FakeEmptyServerCollection(object): + def get(self, f): + raise novaclient.NotFound(1) + + def find(self, name): + raise novaclient.NotFound(2) + +class FakeNovaClient(object): + def __init__(self, collection): + self.servers = collection + +class DynamicNovaClientTest(test.TestCase): + def test_issue_novaclient_command_found(self): + zone = FakeZone('http://example.com', 'bob', 'xxx') + self.assertEquals(api._issue_novaclient_command( + FakeNovaClient(FakeServerCollection()), + zone, "servers", "get", 100).a, 10) + + self.assertEquals(api._issue_novaclient_command( + FakeNovaClient(FakeServerCollection()), + zone, "servers", "find", "name").b, 22) + + self.assertEquals(api._issue_novaclient_command( + FakeNovaClient(FakeServerCollection()), + zone, "servers", "pause", 100), None) + + def test_issue_novaclient_command_not_found(self): + zone = FakeZone('http://example.com', 'bob', 'xxx') + self.assertEquals(api._issue_novaclient_command( + FakeNovaClient(FakeEmptyServerCollection()), + zone, "servers", "get", 100), None) + + self.assertEquals(api._issue_novaclient_command( + FakeNovaClient(FakeEmptyServerCollection()), + zone, "servers", "find", "name"), None) + + self.assertEquals(api._issue_novaclient_command( + FakeNovaClient(FakeEmptyServerCollection()), + zone, "servers", "any", "name"), None) From 16dde389f5712b64ebcee355aa1b870b9504dd26 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 21 Mar 2011 21:47:58 -0700 Subject: [PATCH 36/53] routing test coverage --- nova/tests/test_scheduler.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nova/tests/test_scheduler.py b/nova/tests/test_scheduler.py index 0aebd038..8434f5a4 100644 --- a/nova/tests/test_scheduler.py +++ b/nova/tests/test_scheduler.py @@ -1008,9 +1008,18 @@ class ZoneRedirectTest(test.TestCase): decorator = FakeRerouteCompute("foo") try: result = decorator(go_boom)(None, None, 1) + self.assertFail(_("Should have rerouted.")) except api.RedirectResult, e: self.assertEquals(e.results['magic'], 'found me') + def test_routing_flags(self): + FLAGS.enable_zone_routing = False + decorator = FakeRerouteCompute("foo") + try: + result = decorator(go_boom)(None, None, 1) + except exception.InstanceNotFound, e: + self.assertEquals(e.message, 'boom message') + def test_get_collection_context_and_id(self): decorator = api.reroute_compute("foo") self.assertEquals(decorator.get_collection_context_and_id( From 026a499f59965d16074988e4d65e21e405cdb0e6 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 21 Mar 2011 21:51:14 -0700 Subject: [PATCH 37/53] added zone routing flag test --- nova/tests/test_scheduler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nova/tests/test_scheduler.py b/nova/tests/test_scheduler.py index 8434f5a4..277ffe36 100644 --- a/nova/tests/test_scheduler.py +++ b/nova/tests/test_scheduler.py @@ -1017,6 +1017,7 @@ class ZoneRedirectTest(test.TestCase): decorator = FakeRerouteCompute("foo") try: result = decorator(go_boom)(None, None, 1) + self.assertFail(_("Should have thrown exception.")) except exception.InstanceNotFound, e: self.assertEquals(e.message, 'boom message') From f8ddbadd4ef87c5cb0a19c51b15c801c30f4bde1 Mon Sep 17 00:00:00 2001 From: Josh Kleinpeter Date: Tue, 22 Mar 2011 12:33:34 -0500 Subject: [PATCH 38/53] Changed default for disabled on service_get_all to None. Changed calls to service_get_all so that the results should still be as they previously were. --- bin/nova-manage | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/nova-manage b/bin/nova-manage index 69cbf6f9..c5a4bea7 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -610,7 +610,7 @@ class ServiceCommands(object): args: [host] [service]""" ctxt = context.get_admin_context() now = datetime.datetime.utcnow() - services = db.service_get_all(ctxt) + db.service_get_all(ctxt, True) + services = db.service_get_all(ctxt) if host: services = [s for s in services if s['host'] == host] if service: From 26ab2a84143d514163dc490e7c6839cfa1c388e8 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Tue, 22 Mar 2011 10:37:56 -0700 Subject: [PATCH 39/53] Whoops --- nova/scheduler/api.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 4f189fe3..bd64f9b9 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -149,8 +149,10 @@ class reroute_compute(object): def __call__(self, f): def wrapped_f(*args, **kwargs): + LOG.debug(_("IN DECORATOR ...")) collection, context, item_id = \ self.get_collection_context_and_id(args, kwargs) + LOG.debug(_("IN DECORATOR 2...")) try: # Call the original function ... return f(*args, **kwargs) @@ -166,6 +168,7 @@ class reroute_compute(object): raise # Ask the children to provide an answer ... + LOG.debug(_("Asking child zones ...")) result = self._call_child_zones(zones, wrap_novaclient_function(_issue_novaclient_command, collection, self.method_name, item_id)) From b0e7ac29965446c3636b77df336c82a6bcd03a75 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Tue, 22 Mar 2011 20:36:49 -0700 Subject: [PATCH 40/53] pep8 and fixed up zone-list --- nova/scheduler/api.py | 24 ++++++++++++++++---- nova/scheduler/zone_manager.py | 5 +++-- nova/tests/test_scheduler.py | 40 +++++++++++++++++++--------------- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index bd64f9b9..c1417dfe 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -55,9 +55,27 @@ def get_zone_list(context): items = _call_scheduler('get_zone_list', context) for item in items: item['api_url'] = item['api_url'].replace('\\/', '/') + if not items: + items = db.zone_get_all(context) return items +def zone_get(context, zone_id): + return db.zone_get(context, zone_id) + + +def zone_delete(context, zone_id): + return db.zone_delete(context, zone_id) + + +def zone_create(context, data): + return db.zone_create(context, data) + + +def zone_update(context, zone_id, data): + return db.zone_update(context, zone_id, data) + + def get_zone_capabilities(context, service=None): """Returns a dict of key, value capabilities for this zone, or for a particular class of services running in this zone.""" @@ -149,10 +167,8 @@ class reroute_compute(object): def __call__(self, f): def wrapped_f(*args, **kwargs): - LOG.debug(_("IN DECORATOR ...")) collection, context, item_id = \ self.get_collection_context_and_id(args, kwargs) - LOG.debug(_("IN DECORATOR 2...")) try: # Call the original function ... return f(*args, **kwargs) @@ -181,7 +197,7 @@ class reroute_compute(object): """Ask the child zones to perform this operation. Broken out for testing.""" return child_zone_helper(zones, function) - + def get_collection_context_and_id(self, args, kwargs): """Returns a tuple of (novaclient collection name, security context and resource id. Derived class should override this.""" @@ -212,7 +228,7 @@ class reroute_compute(object): del server[k] reduced_response.append(dict(server=server)) - if reduced_response: + if reduced_response: return reduced_response[0] # first for now. return {} diff --git a/nova/scheduler/zone_manager.py b/nova/scheduler/zone_manager.py index d32cc2e8..198f9d4c 100644 --- a/nova/scheduler/zone_manager.py +++ b/nova/scheduler/zone_manager.py @@ -58,8 +58,9 @@ class ZoneState(object): child zone.""" self.last_seen = datetime.now() self.attempt = 0 - self.name = zone_metadata["name"] - self.capabilities = zone_metadata["capabilities"] + self.name = zone_metadata.get("name", "n/a") + self.capabilities = ", ".join(["%s=%s" % (k, v) + for k, v in zone_metadata.iteritems() if k != 'name']) self.is_active = True def to_dict(self): diff --git a/nova/tests/test_scheduler.py b/nova/tests/test_scheduler.py index 277ffe36..6df74dd6 100644 --- a/nova/tests/test_scheduler.py +++ b/nova/tests/test_scheduler.py @@ -949,6 +949,7 @@ class FakeZone(object): self.username = username self.password = password + def zone_get_all(context): return [ FakeZone('http://example.com', 'bob', 'xxx'), @@ -957,8 +958,8 @@ def zone_get_all(context): class FakeRerouteCompute(api.reroute_compute): def _call_child_zones(self, zones, function): - return [ ] - + return [] + def get_collection_context_and_id(self, args, kwargs): return ("servers", None, 1) @@ -982,6 +983,7 @@ class FakeResource(object): def pause(self): pass + class ZoneRedirectTest(test.TestCase): def setUp(self): super(ZoneRedirectTest, self).setUp() @@ -1024,27 +1026,28 @@ class ZoneRedirectTest(test.TestCase): def test_get_collection_context_and_id(self): decorator = api.reroute_compute("foo") self.assertEquals(decorator.get_collection_context_and_id( - (None, 10, 20), {}), ("servers", 10, 20)) + (None, 10, 20), {}), ("servers", 10, 20)) self.assertEquals(decorator.get_collection_context_and_id( - (None, 11,), dict(instance_id=21)), ("servers", 11, 21)) + (None, 11,), dict(instance_id=21)), ("servers", 11, 21)) self.assertEquals(decorator.get_collection_context_and_id( (None,), dict(context=12, instance_id=22)), ("servers", 12, 22)) def test_unmarshal_single_server(self): decorator = api.reroute_compute("foo") - self.assertEquals(decorator.unmarshall_result([]), {}) + self.assertEquals(decorator.unmarshall_result([]), {}) self.assertEquals(decorator.unmarshall_result( - [FakeResource(dict(a=1, b=2)),]), - dict(server=dict(a=1, b=2))) + [FakeResource(dict(a=1, b=2)), ]), + dict(server=dict(a=1, b=2))) self.assertEquals(decorator.unmarshall_result( - [FakeResource(dict(a=1, _b=2)),]), - dict(server=dict(a=1,))) + [FakeResource(dict(a=1, _b=2)), ]), + dict(server=dict(a=1,))) self.assertEquals(decorator.unmarshall_result( - [FakeResource(dict(a=1, manager=2)),]), - dict(server=dict(a=1,))) + [FakeResource(dict(a=1, manager=2)), ]), + dict(server=dict(a=1,))) self.assertEquals(decorator.unmarshall_result( - [FakeResource(dict(_a=1, manager=2)),]), - dict(server={})) + [FakeResource(dict(_a=1, manager=2)), ]), + dict(server={})) + class FakeServerCollection(object): def get(self, instance_id): @@ -1053,6 +1056,7 @@ class FakeServerCollection(object): def find(self, name): return FakeResource(dict(a=11, b=22)) + class FakeEmptyServerCollection(object): def get(self, f): raise novaclient.NotFound(1) @@ -1060,10 +1064,12 @@ class FakeEmptyServerCollection(object): def find(self, name): raise novaclient.NotFound(2) + class FakeNovaClient(object): def __init__(self, collection): self.servers = collection + class DynamicNovaClientTest(test.TestCase): def test_issue_novaclient_command_found(self): zone = FakeZone('http://example.com', 'bob', 'xxx') @@ -1078,17 +1084,17 @@ class DynamicNovaClientTest(test.TestCase): self.assertEquals(api._issue_novaclient_command( FakeNovaClient(FakeServerCollection()), zone, "servers", "pause", 100), None) - + def test_issue_novaclient_command_not_found(self): zone = FakeZone('http://example.com', 'bob', 'xxx') self.assertEquals(api._issue_novaclient_command( FakeNovaClient(FakeEmptyServerCollection()), - zone, "servers", "get", 100), None) + zone, "servers", "get", 100), None) self.assertEquals(api._issue_novaclient_command( FakeNovaClient(FakeEmptyServerCollection()), - zone, "servers", "find", "name"), None) + zone, "servers", "find", "name"), None) self.assertEquals(api._issue_novaclient_command( FakeNovaClient(FakeEmptyServerCollection()), - zone, "servers", "any", "name"), None) + zone, "servers", "any", "name"), None) From 8cf49562b201fb635af9ec6561204e014cefc731 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Wed, 23 Mar 2011 12:31:15 -0700 Subject: [PATCH 41/53] indenting cleanup --- nova/flags.py | 2 +- nova/rpc.py | 4 ++-- nova/scheduler/api.py | 2 +- nova/tests/test_zones.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/nova/flags.py b/nova/flags.py index 3a8ec1a3..bf83b8e0 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -360,4 +360,4 @@ DEFINE_string('node_availability_zone', 'nova', DEFINE_string('zone_name', 'nova', 'name of this zone') DEFINE_list('zone_capabilities', ['hypervisor=xenserver;kvm', 'os=linux;windows'], - 'Key/Multi-value list representng capabilities of this zone') + 'Key/Multi-value list representng capabilities of this zone') diff --git a/nova/rpc.py b/nova/rpc.py index 6ddaea09..388f78d6 100644 --- a/nova/rpc.py +++ b/nova/rpc.py @@ -219,8 +219,8 @@ class FanoutAdapterConsumer(AdapterConsumer): self.queue = "%s_fanout_%s" % (topic, unique) self.durable = False LOG.info(_("Created '%(exchange)s' fanout exchange " - "with '%(key)s' routing key"), - dict(exchange=self.exchange, key=self.routing_key)) + "with '%(key)s' routing key"), + dict(exchange=self.exchange, key=self.routing_key)) super(FanoutAdapterConsumer, self).__init__(connection=connection, topic=topic, proxy=proxy) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index e2cf3b6a..19a05b71 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -52,7 +52,7 @@ def get_zone_capabilities(context, service=None): """Returns a dict of key, value capabilities for this zone, or for a particular class of services running in this zone.""" return _call_scheduler('get_zone_capabilities', context=context, - params=dict(service=service)) + params=dict(service=service)) def update_service_capabilities(context, service_name, host, capabilities): diff --git a/nova/tests/test_zones.py b/nova/tests/test_zones.py index 48e1442c..688dc704 100644 --- a/nova/tests/test_zones.py +++ b/nova/tests/test_zones.py @@ -96,7 +96,7 @@ class ZoneManagerTestCase(test.TestCase): zm.update_service_capabilities("svc10", "host1", dict(a=99, b=99)) caps = zm.get_zone_capabilities(self, None) self.assertEquals(caps, dict(svc1_a=(2, 20), svc1_b=(3, 30), - svc10_a=(99, 99), svc10_b=(99, 99))) + svc10_a=(99, 99), svc10_b=(99, 99))) zm.update_service_capabilities("svc1", "host3", dict(c=5)) caps = zm.get_zone_capabilities(self, None) From 4c0f8e6e63806a077e25a9833b5636767939de66 Mon Sep 17 00:00:00 2001 From: termie Date: Wed, 23 Mar 2011 13:39:01 -0700 Subject: [PATCH 42/53] fix utils.execute retries for osx also some minor misc cleanups --- nova/tests/test_volume.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/tests/test_volume.py b/nova/tests/test_volume.py index 5d68ca2a..d71b75f3 100644 --- a/nova/tests/test_volume.py +++ b/nova/tests/test_volume.py @@ -356,8 +356,8 @@ class ISCSITestCase(DriverTestCase): tid = db.volume_get_iscsi_target_num(self.context, volume_id_list[0]) self.mox.StubOutWithMock(self.volume.driver, '_execute') self.volume.driver._execute("sudo", "ietadm", "--op", "show", - "--tid=%(tid)d" % locals() - ).AndRaise(exception.ProcessExecutionError()) + "--tid=%(tid)d" % locals()).AndRaise( + exception.ProcessExecutionError()) self.mox.ReplayAll() self.assertRaises(exception.ProcessExecutionError, From 148dc8f1ba81cb0be94112f4d74ac28611dda955 Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Thu, 24 Mar 2011 00:36:07 +0300 Subject: [PATCH 43/53] migration gateway_v6 to network_info --- nova/tests/test_virt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nova/tests/test_virt.py b/nova/tests/test_virt.py index b214f5ce..98bb1152 100644 --- a/nova/tests/test_virt.py +++ b/nova/tests/test_virt.py @@ -785,7 +785,8 @@ class NWFilterTestCase(test.TestCase): instance_ref = db.instance_create(self.context, {'user_id': 'fake', - 'project_id': 'fake'}) + 'project_id': 'fake', + 'mac_address': '00:A0:C9:14:C8:29'}) inst_id = instance_ref['id'] ip = '10.11.12.13' From b8d24e1c72d2c3c7a851b285cb1e83ce4f8d9721 Mon Sep 17 00:00:00 2001 From: Muneyuki Noguchi Date: Thu, 24 Mar 2011 16:21:50 +0900 Subject: [PATCH 44/53] Declare libvirt_type to avoid AttributeError in live_migration --- bin/nova-manage | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/nova-manage b/bin/nova-manage index 69cbf6f9..6712fbad 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -97,6 +97,7 @@ flags.DECLARE('vlan_start', 'nova.network.manager') flags.DECLARE('vpn_start', 'nova.network.manager') flags.DECLARE('fixed_range_v6', 'nova.network.manager') flags.DECLARE('images_path', 'nova.image.local') +flags.DECLARE('libvirt_type', 'nova.virt.libvirt_conn') flags.DEFINE_flag(flags.HelpFlag()) flags.DEFINE_flag(flags.HelpshortFlag()) flags.DEFINE_flag(flags.HelpXMLFlag()) From 8ce46be0941bf087af62a8e441a9a107d3e8429d Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 24 Mar 2011 05:02:54 -0700 Subject: [PATCH 45/53] fix based on sirp's comments --- nova/scheduler/api.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index c1417dfe..a4f304c6 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -118,16 +118,15 @@ def child_zone_helper(zone_list, func): _wrap_method(_process, func), zone_list)] -def _issue_novaclient_command(nova, zone, collection, method_name, \ - item_id): +def _issue_novaclient_command(nova, zone, collection, method_name, item_id): """Use novaclient to issue command to a single child zone. One of these will be run in parallel for each child zone.""" + manager = getattr(nova, collection) result = None try: - manager = getattr(nova, collection) - if isinstance(item_id, int) or item_id.isdigit(): + try: result = manager.get(int(item_id)) - else: + except ValueError, e: result = manager.find(name=item_id) except novaclient.NotFound: url = zone.api_url From 95e9dcaf9c6586c9d574d1e14e4784779ff94012 Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Thu, 24 Mar 2011 16:53:32 +0300 Subject: [PATCH 46/53] couple of bugs fixed --- nova/tests/test_virt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nova/tests/test_virt.py b/nova/tests/test_virt.py index 98bb1152..12f97383 100644 --- a/nova/tests/test_virt.py +++ b/nova/tests/test_virt.py @@ -803,7 +803,8 @@ class NWFilterTestCase(test.TestCase): 'instance_id': instance_ref['id']}) def _ensure_all_called(): - instance_filter = 'nova-instance-%s' % instance_ref['name'] + instance_filter = 'nova-instance-%s-%s' % (instance_ref['name'], + '00A0C914C829') secgroup_filter = 'nova-secgroup-%s' % self.security_group['id'] for required in [secgroup_filter, 'allow-dhcp-server', 'no-arp-spoofing', 'no-ip-spoofing', From 1d65862a8687ef0da9e88a56618a3722cae84ad0 Mon Sep 17 00:00:00 2001 From: termie Date: Thu, 24 Mar 2011 12:42:46 -0700 Subject: [PATCH 47/53] support volume and network in the direct api --- bin/nova-direct-api | 9 +++++++-- nova/tests/test_direct.py | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/bin/nova-direct-api b/bin/nova-direct-api index a2c9f155..1a78fb0c 100755 --- a/bin/nova-direct-api +++ b/bin/nova-direct-api @@ -34,12 +34,14 @@ if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): gettext.install('nova', unicode=1) +from nova import compute from nova import flags from nova import log as logging +from nova import network from nova import utils +from nova import volume from nova import wsgi from nova.api import direct -from nova.compute import api as compute_api FLAGS = flags.FLAGS @@ -50,12 +52,15 @@ flags.DEFINE_flag(flags.HelpshortFlag()) flags.DEFINE_flag(flags.HelpXMLFlag()) + if __name__ == '__main__': utils.default_flagfile() FLAGS(sys.argv) logging.setup() - direct.register_service('compute', compute_api.API()) + direct.register_service('compute', compute.API()) + direct.register_service('volume', volume.API()) + direct.register_service('network', network.API()) direct.register_service('reflect', direct.Reflection()) router = direct.Router() with_json = direct.JsonParamsMiddleware(router) diff --git a/nova/tests/test_direct.py b/nova/tests/test_direct.py index 80e4d2e1..001246fc 100644 --- a/nova/tests/test_direct.py +++ b/nova/tests/test_direct.py @@ -25,7 +25,9 @@ import webob from nova import compute from nova import context from nova import exception +from nova import network from nova import test +from nova import volume from nova import utils from nova.api import direct from nova.tests import test_cloud @@ -93,12 +95,20 @@ class DirectTestCase(test.TestCase): class DirectCloudTestCase(test_cloud.CloudTestCase): def setUp(self): super(DirectCloudTestCase, self).setUp() - compute_handle = compute.API(network_api=self.cloud.network_api, - volume_api=self.cloud.volume_api) + compute_handle = compute.API(image_service=self.cloud.image_service) + volume_handle = volume.API() + network_handle = network.API() direct.register_service('compute', compute_handle) + direct.register_service('volume', volume_handle) + direct.register_service('network', network_handle) + self.router = direct.JsonParamsMiddleware(direct.Router()) proxy = direct.Proxy(self.router) self.cloud.compute_api = proxy.compute + self.cloud.volume_api = proxy.volume + self.cloud.network_api = proxy.network + compute_handle.volume_api = proxy.volume + compute_handle.network_api = proxy.network def tearDown(self): super(DirectCloudTestCase, self).tearDown() From efb7b812dd1a326b3f47fe69d0b6ec2b2950c961 Mon Sep 17 00:00:00 2001 From: termie Date: Thu, 24 Mar 2011 12:42:46 -0700 Subject: [PATCH 48/53] improve the formatting of the stack tool --- bin/stack | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/bin/stack b/bin/stack index 25caca06..d84a82e2 100755 --- a/bin/stack +++ b/bin/stack @@ -59,11 +59,21 @@ USAGE = """usage: stack [options] [arg1=value arg2=value] def format_help(d): """Format help text, keys are labels and values are descriptions.""" + MAX_INDENT = 30 indent = max([len(k) for k in d]) + if indent > MAX_INDENT: + indent = MAX_INDENT - 6 + out = [] for k, v in d.iteritems(): - t = textwrap.TextWrapper(initial_indent=' %s ' % k.ljust(indent), - subsequent_indent=' ' * (indent + 6)) + if (len(k) + 6) > MAX_INDENT: + out.extend([' %s' % k]) + initial_indent = ' ' * (indent + 6) + else: + initial_indent = ' %s ' % k.ljust(indent) + subsequent_indent = ' ' * (indent + 6) + t = textwrap.TextWrapper(initial_indent=initial_indent, + subsequent_indent=subsequent_indent) out.extend(t.wrap(v)) return out From 6b1cf513a086de6d3b23a8096b787b965888bf89 Mon Sep 17 00:00:00 2001 From: termie Date: Thu, 24 Mar 2011 12:42:46 -0700 Subject: [PATCH 49/53] add Limited, an API limiting/versioning wrapper --- bin/nova-direct-api | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bin/nova-direct-api b/bin/nova-direct-api index 1a78fb0c..ac0b5b51 100755 --- a/bin/nova-direct-api +++ b/bin/nova-direct-api @@ -53,12 +53,19 @@ flags.DEFINE_flag(flags.HelpXMLFlag()) +class ReadOnlyCompute(direct.Limited): + """Read-only Compute API.""" + + _allowed = ['get', 'get_all', 'get_console_output'] + + if __name__ == '__main__': utils.default_flagfile() FLAGS(sys.argv) logging.setup() direct.register_service('compute', compute.API()) + direct.register_service('compute-readonly', ReadOnlyCompute(compute.API())) direct.register_service('volume', volume.API()) direct.register_service('network', network.API()) direct.register_service('reflect', direct.Reflection()) From 4a459a80e3130521d4dcecbe2d69d55abe54da2a Mon Sep 17 00:00:00 2001 From: termie Date: Thu, 24 Mar 2011 12:42:47 -0700 Subject: [PATCH 50/53] add an example of a versioned api --- bin/nova-direct-api | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bin/nova-direct-api b/bin/nova-direct-api index ac0b5b51..0f758987 100755 --- a/bin/nova-direct-api +++ b/bin/nova-direct-api @@ -53,11 +53,19 @@ flags.DEFINE_flag(flags.HelpXMLFlag()) +# An example of an API that only exposes read-only methods. class ReadOnlyCompute(direct.Limited): """Read-only Compute API.""" _allowed = ['get', 'get_all', 'get_console_output'] +# An example of an API that provides a backwards compatibility layer. +class VolumeVersionOne(direct.Limited): + _allowed = ['create', 'delete', 'update', 'get'] + + def create(self, context, size, name): + self.proxy.create(context, size, name, description=None) + if __name__ == '__main__': utils.default_flagfile() @@ -65,10 +73,11 @@ if __name__ == '__main__': logging.setup() direct.register_service('compute', compute.API()) - direct.register_service('compute-readonly', ReadOnlyCompute(compute.API())) direct.register_service('volume', volume.API()) direct.register_service('network', network.API()) direct.register_service('reflect', direct.Reflection()) + direct.register_service('compute-readonly', ReadOnlyCompute(compute.API())) + direct.register_service('volume-v1', VolumeVersionOne(volume.API())) router = direct.Router() with_json = direct.JsonParamsMiddleware(router) with_req = direct.PostParamsMiddleware(with_json) From c2f169a542e62301ab4770d716b3ca9ca64dc10e Mon Sep 17 00:00:00 2001 From: termie Date: Thu, 24 Mar 2011 12:42:47 -0700 Subject: [PATCH 51/53] add some more docs and make it more obvious which parts are examples --- bin/nova-direct-api | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/bin/nova-direct-api b/bin/nova-direct-api index 0f758987..bb3aa8ae 100755 --- a/bin/nova-direct-api +++ b/bin/nova-direct-api @@ -54,12 +54,19 @@ flags.DEFINE_flag(flags.HelpXMLFlag()) # An example of an API that only exposes read-only methods. +# In this case we're just limiting which methods are exposed. class ReadOnlyCompute(direct.Limited): """Read-only Compute API.""" _allowed = ['get', 'get_all', 'get_console_output'] + # An example of an API that provides a backwards compatibility layer. +# In this case we're overwriting the implementation to ensure +# compatibility with an older version. In reality we would want the +# "description=None" to be part of the actual API so that code +# like this isn't even necessary, but this example shows what one can +# do if that isn't the situation. class VolumeVersionOne(direct.Limited): _allowed = ['create', 'delete', 'update', 'get'] @@ -76,8 +83,12 @@ if __name__ == '__main__': direct.register_service('volume', volume.API()) direct.register_service('network', network.API()) direct.register_service('reflect', direct.Reflection()) - direct.register_service('compute-readonly', ReadOnlyCompute(compute.API())) - direct.register_service('volume-v1', VolumeVersionOne(volume.API())) + + # Here is how we could expose the code in the examples above. + #direct.register_service('compute-readonly', + # ReadOnlyCompute(compute.API())) + #direct.register_service('volume-v1', VolumeVersionOne(volume.API())) + router = direct.Router() with_json = direct.JsonParamsMiddleware(router) with_req = direct.PostParamsMiddleware(with_json) From f0d50a981d90a7c09e0b2d9286a7e6775a171ea1 Mon Sep 17 00:00:00 2001 From: termie Date: Thu, 24 Mar 2011 12:42:47 -0700 Subject: [PATCH 52/53] better error handling and serialization --- nova/tests/test_direct.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/nova/tests/test_direct.py b/nova/tests/test_direct.py index 001246fc..38384023 100644 --- a/nova/tests/test_direct.py +++ b/nova/tests/test_direct.py @@ -33,6 +33,9 @@ from nova.api import direct from nova.tests import test_cloud +class ArbitraryObject(object): + pass + class FakeService(object): def echo(self, context, data): return {'data': data} @@ -41,6 +44,9 @@ class FakeService(object): return {'user': context.user_id, 'project': context.project_id} + def invalid_return(self, context): + return ArbitraryObject() + class DirectTestCase(test.TestCase): def setUp(self): @@ -86,6 +92,12 @@ class DirectTestCase(test.TestCase): resp_parsed = json.loads(resp.body) self.assertEqual(resp_parsed['data'], 'foo') + def test_invalid(self): + req = webob.Request.blank('/fake/invalid_return') + req.environ['openstack.context'] = self.context + req.method = 'POST' + self.assertRaises(exception.Error, req.get_response, self.router) + def test_proxy(self): proxy = direct.Proxy(self.router) rv = proxy.fake.echo(self.context, data='baz') From ffb04ff86f768ec515eb1571ea8574081203ad90 Mon Sep 17 00:00:00 2001 From: termie Date: Thu, 24 Mar 2011 13:20:15 -0700 Subject: [PATCH 53/53] pep8 cleanups --- bin/nova-direct-api | 1 - nova/tests/test_direct.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/nova-direct-api b/bin/nova-direct-api index bb3aa8ae..83ec7272 100755 --- a/bin/nova-direct-api +++ b/bin/nova-direct-api @@ -52,7 +52,6 @@ flags.DEFINE_flag(flags.HelpshortFlag()) flags.DEFINE_flag(flags.HelpXMLFlag()) - # An example of an API that only exposes read-only methods. # In this case we're just limiting which methods are exposed. class ReadOnlyCompute(direct.Limited): diff --git a/nova/tests/test_direct.py b/nova/tests/test_direct.py index 38384023..588a24b3 100644 --- a/nova/tests/test_direct.py +++ b/nova/tests/test_direct.py @@ -36,6 +36,7 @@ from nova.tests import test_cloud class ArbitraryObject(object): pass + class FakeService(object): def echo(self, context, data): return {'data': data}