From 06fd2363578b21726636cd189c7ab1ce0db632ba Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Thu, 5 Aug 2010 10:30:06 -0500 Subject: [PATCH 01/87] added bin/nova-listinstances, which is mostly just a duplication of euca-describe-instances but doesn't go through the API. --- bin/nova-listinstances | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100755 bin/nova-listinstances diff --git a/bin/nova-listinstances b/bin/nova-listinstances new file mode 100755 index 00000000..2f8ff28f --- /dev/null +++ b/bin/nova-listinstances @@ -0,0 +1,19 @@ +#!/usr/bin/python + +# +# Duplicates the functionality of euca-describe-instances, but doesn't require +# going through the API. Does a direct query to the datastore. This is +# mostly a test program written for the scheduler +# + +from nova.compute import model + +data_needed = ['image_id', 'memory_kb', 'local_gb', 'node_name', 'vcpus'] + +instances = model.InstanceDirectory().all + +for instance in instances: + print 'Instance: %s' % instance['instance_id'] + for x in data_needed: + print ' %s: %s' % (x, instance[x]) + From 6beb3aa2bea06e3be0f53083cf687d09932e2200 Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Thu, 5 Aug 2010 16:11:59 -0500 Subject: [PATCH 02/87] almost there on random scheduler. not pushing to correct compute node topic, yet, apparently... --- bin/nova-scheduler | 32 ++++++++++++++ nova/endpoint/cloud.py | 2 +- nova/flags.py | 1 + nova/scheduler/__init__.py | 33 +++++++++++++++ nova/scheduler/service.py | 87 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100755 bin/nova-scheduler create mode 100644 nova/scheduler/__init__.py create mode 100644 nova/scheduler/service.py diff --git a/bin/nova-scheduler b/bin/nova-scheduler new file mode 100755 index 00000000..1ad41bbd --- /dev/null +++ b/bin/nova-scheduler @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. + +""" + Twistd daemon for the nova scheduler nodes. +""" + +from nova import twistd +from nova.scheduler import service + + +if __name__ == '__main__': + twistd.serve(__file__) + +if __name__ == '__builtin__': + application = service.SchedulerService.create() diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 0ee278f8..a808e54c 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -576,7 +576,7 @@ class CloudController(object): inst['private_dns_name'] = str(address) # TODO: allocate expresses on the router node inst.save() - rpc.cast(FLAGS.compute_topic, + rpc.cast(FLAGS.scheduler_topic, {"method": "run_instance", "args": {"instance_id" : inst.instance_id}}) logging.debug("Casting to node for %s's instance with IP of %s" % diff --git a/nova/flags.py b/nova/flags.py index f35f5fa1..7f92e3f7 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -41,6 +41,7 @@ DEFINE_integer('s3_port', 3333, 's3 port') DEFINE_string('s3_host', '127.0.0.1', 's3 host') #DEFINE_string('cloud_topic', 'cloud', 'the topic clouds listen on') DEFINE_string('compute_topic', 'compute', 'the topic compute nodes listen on') +DEFINE_string('scheduler_topic', 'scheduler', 'the topic scheduler nodes listen on') DEFINE_string('volume_topic', 'volume', 'the topic volume nodes listen on') DEFINE_string('network_topic', 'network', 'the topic network nodes listen on') diff --git a/nova/scheduler/__init__.py b/nova/scheduler/__init__.py new file mode 100644 index 00000000..516ea61b --- /dev/null +++ b/nova/scheduler/__init__.py @@ -0,0 +1,33 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. + +""" +:mod:`nova.scheduler` -- Scheduler Nodes +===================================================== + +.. automodule:: nova.scheduler + :platform: Unix + :synopsis: Daemon that picks a host for a VM instance. +.. moduleauthor:: Jesse Andrews +.. moduleauthor:: Devin Carlen +.. moduleauthor:: Vishvananda Ishaya +.. moduleauthor:: Joshua McKenty +.. moduleauthor:: Manish Singh +.. moduleauthor:: Andy Smith +.. moduleauthor:: Chris Behrens +""" diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py new file mode 100644 index 00000000..aca5b5db --- /dev/null +++ b/nova/scheduler/service.py @@ -0,0 +1,87 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. + +""" +Scheduler Service +""" + +import logging +import random +import sys +from twisted.internet import defer +from twisted.internet import task + +from nova import exception +from nova import flags +from nova import process +from nova import rpc +from nova import service +from nova import utils +from nova.compute import model +from nova.datastore import Redis + +FLAGS = flags.FLAGS + + +class SchedulerService(service.Service): + """ + Manages the running instances. + """ + def __init__(self): + super(SchedulerService, self).__init__() + self.instdir = model.InstanceDirectory() + + def noop(self): + """ simple test of an AMQP message call """ + return defer.succeed('PONG') + + @defer.inlineCallbacks + def report_state(self, nodename, daemon): + # TODO(termie): make this pattern be more elegant. -todd + try: + record = model.Daemon(nodename, daemon) + record.heartbeat() + if getattr(self, "model_disconnected", False): + self.model_disconnected = False + logging.error("Recovered model server connection!") + + except model.ConnectionError, ex: + if not getattr(self, "model_disconnected", False): + self.model_disconnected = True + logging.exception("model server went away") + yield + + @property + def compute_identifiers(self): + return [identifier for identifier in Redis.instance().smembers("daemons") if (identifier.split(':')[1] == "nova-compute")] + + def pick_node(self, instance_id, **_kwargs): + identifiers = self.compute_identifiers + return identifiers[int(random.random() * len(identifiers))].split(':')[0] + + @exception.wrap_exception + def run_instance(self, instance_id, **_kwargs): + node = self.pick_node(instance_id, **_kwargs) + + rpc.cast('%s:%s' % (FLAGS.compute_topic, node), + {"method": "run_instance", + "args": {"instance_id" : instance_id}}) + logging.debug("Casting to node %s for instance %s" % + (node, instance_id)) + + From 04b31cca07b9e34001ae83fb039868238334c990 Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Thu, 5 Aug 2010 16:19:21 -0500 Subject: [PATCH 03/87] compute topic for a node is compute.node not compute:node! --- nova/scheduler/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index aca5b5db..2875dd55 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -78,7 +78,7 @@ class SchedulerService(service.Service): def run_instance(self, instance_id, **_kwargs): node = self.pick_node(instance_id, **_kwargs) - rpc.cast('%s:%s' % (FLAGS.compute_topic, node), + rpc.cast('%s.%s' % (FLAGS.compute_topic, node), {"method": "run_instance", "args": {"instance_id" : instance_id}}) logging.debug("Casting to node %s for instance %s" % From 5574f3923a73d417e6134a3063d61cb8ec0ce406 Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Thu, 5 Aug 2010 15:10:56 -0700 Subject: [PATCH 04/87] fixed doc string --- nova/scheduler/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index 2875dd55..46e541a4 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -40,7 +40,7 @@ FLAGS = flags.FLAGS class SchedulerService(service.Service): """ - Manages the running instances. + Picks nodes for instances to run. """ def __init__(self): super(SchedulerService, self).__init__() From 08ded6406630a6de0ed43016d6c0f187e3bc1bec Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Fri, 6 Aug 2010 14:40:24 -0500 Subject: [PATCH 05/87] Start breaking out scheduler classes... --- nova/scheduler/service.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index 46e541a4..3a226322 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -23,6 +23,7 @@ Scheduler Service import logging import random import sys +import time from twisted.internet import defer from twisted.internet import task @@ -36,11 +37,12 @@ from nova.compute import model from nova.datastore import Redis FLAGS = flags.FLAGS - +flags.DEFINE_integer('node_down_time', 60, + 'seconds without heartbeat that determines a compute node to be down') class SchedulerService(service.Service): """ - Picks nodes for instances to run. + Manages the running instances. """ def __init__(self): super(SchedulerService, self).__init__() @@ -67,12 +69,20 @@ class SchedulerService(service.Service): yield @property - def compute_identifiers(self): - return [identifier for identifier in Redis.instance().smembers("daemons") if (identifier.split(':')[1] == "nova-compute")] + def compute_nodes(self): + return [identifier.split(':')[0] for identifier in Redis.instance().smembers("daemons") if (identifier.split(':')[1] == "nova-compute")] + + def compute_node_is_up(self, node): + time_str = Redis.instance().hget('%s:%s:%s' % ('daemon', node, 'nova-compute'), 'updated_at') + return(time_str and + (time.time() - (int(time.mktime(time.strptime(time_str.replace('Z', 'UTC'), '%Y-%m-%dT%H:%M:%S%Z'))) - time.timezone) < FLAGS.node_down_time)) + + def compute_nodes_up(self): + return [node for node in self.compute_nodes if self.compute_node_is_up(node)] def pick_node(self, instance_id, **_kwargs): - identifiers = self.compute_identifiers - return identifiers[int(random.random() * len(identifiers))].split(':')[0] + """You DEFINITELY want to define this in your subclass""" + raise NotImplementedError("Your subclass should define pick_node") @exception.wrap_exception def run_instance(self, instance_id, **_kwargs): @@ -84,4 +94,16 @@ class SchedulerService(service.Service): logging.debug("Casting to node %s for instance %s" % (node, instance_id)) +class RandomService(SchedulerService): + """ + Implements SchedulerService as a random node selector + """ + + def __init__(self): + super(RandomService, self).__init__() + + def pick_node(self, instance_id, **_kwargs): + nodes = self.compute_nodes_up() + return nodes[int(random.random() * len(nodes))] + From 05a643a960d4cdfa266edc15b879d86df6ab483b Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Fri, 6 Aug 2010 17:40:10 -0500 Subject: [PATCH 06/87] Moved Scheduler classes into scheduler.py. Created a way to specify scheduler class that the SchedulerService uses... --- nova/scheduler/scheduler.py | 82 +++++++++++++++++++++++++++++++++++++ nova/scheduler/service.py | 50 ++++++---------------- 2 files changed, 95 insertions(+), 37 deletions(-) create mode 100644 nova/scheduler/scheduler.py diff --git a/nova/scheduler/scheduler.py b/nova/scheduler/scheduler.py new file mode 100644 index 00000000..0da7b95c --- /dev/null +++ b/nova/scheduler/scheduler.py @@ -0,0 +1,82 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. + +""" +Scheduler Classes +""" + +import logging +import random +import sys +import time + +from nova import exception +from nova import flags +from nova.datastore import Redis + +FLAGS = flags.FLAGS +flags.DEFINE_integer('node_down_time', + 60, + 'seconds without heartbeat that determines a compute node to be down') + + +class Scheduler(object): + """ + The base class that all Scheduler clases should inherit from + """ + + @property + def compute_nodes(self): + return [identifier.split(':')[0] for identifier in Redis.instance().smembers("daemons") if (identifier.split(':')[1] == "nova-compute")] + + def compute_node_is_up(self, node): + time_str = Redis.instance().hget('%s:%s:%s' % ('daemon', node, 'nova-compute'), 'updated_at') + # Would be a lot easier if we stored heartbeat time in epoch :) + return(time_str and + (time.time() - (int(time.mktime(time.strptime(time_str.replace('Z', 'UTC'), '%Y-%m-%dT%H:%M:%S%Z'))) - time.timezone) < FLAGS.node_down_time)) + + def compute_nodes_up(self): + return [node for node in self.compute_nodes if self.compute_node_is_up(node)] + + def pick_node(self, instance_id, **_kwargs): + """You DEFINITELY want to define this in your subclass""" + raise NotImplementedError("Your subclass should define pick_node") + +class RandomScheduler(Scheduler): + """ + Implements Scheduler as a random node selector + """ + + def __init__(self): + super(RandomScheduler, self).__init__() + + def pick_node(self, instance_id, **_kwargs): + nodes = self.compute_nodes_up() + return nodes[int(random.random() * len(nodes))] + +class BestFitScheduler(Scheduler): + """ + Implements Scheduler as a best-fit node selector + """ + + def __init__(self): + super(BestFitScheduler, self).__init__() + + def pick_node(self, instance_id, **_kwargs): + raise NotImplementedError("BestFitScheduler is not done yet") + diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index 3a226322..3a86cefb 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -21,32 +21,34 @@ Scheduler Service """ import logging -import random -import sys -import time from twisted.internet import defer -from twisted.internet import task from nova import exception from nova import flags -from nova import process from nova import rpc from nova import service -from nova import utils from nova.compute import model -from nova.datastore import Redis +from nova.scheduler import scheduler FLAGS = flags.FLAGS -flags.DEFINE_integer('node_down_time', 60, - 'seconds without heartbeat that determines a compute node to be down') +flags.DEFINE_string('scheduler_type', + 'random', + 'the scheduler to use') +scheduler_classes = { + 'random': scheduler.RandomScheduler, + 'bestfit': scheduler.BestFitScheduler + } + class SchedulerService(service.Service): """ Manages the running instances. """ def __init__(self): super(SchedulerService, self).__init__() - self.instdir = model.InstanceDirectory() + if (FLAGS.scheduler_type not in scheduler_classes): + raise exception.Error("Scheduler '%s' does not exist" % FLAGS.scheduler_type) + self._scheduler_class = scheduler_classes[FLAGS.scheduler_type] def noop(self): """ simple test of an AMQP message call """ @@ -68,21 +70,8 @@ class SchedulerService(service.Service): logging.exception("model server went away") yield - @property - def compute_nodes(self): - return [identifier.split(':')[0] for identifier in Redis.instance().smembers("daemons") if (identifier.split(':')[1] == "nova-compute")] - - def compute_node_is_up(self, node): - time_str = Redis.instance().hget('%s:%s:%s' % ('daemon', node, 'nova-compute'), 'updated_at') - return(time_str and - (time.time() - (int(time.mktime(time.strptime(time_str.replace('Z', 'UTC'), '%Y-%m-%dT%H:%M:%S%Z'))) - time.timezone) < FLAGS.node_down_time)) - - def compute_nodes_up(self): - return [node for node in self.compute_nodes if self.compute_node_is_up(node)] - def pick_node(self, instance_id, **_kwargs): - """You DEFINITELY want to define this in your subclass""" - raise NotImplementedError("Your subclass should define pick_node") + return self._scheduler_class().pick_node(instance_id, **_kwargs) @exception.wrap_exception def run_instance(self, instance_id, **_kwargs): @@ -94,16 +83,3 @@ class SchedulerService(service.Service): logging.debug("Casting to node %s for instance %s" % (node, instance_id)) -class RandomService(SchedulerService): - """ - Implements SchedulerService as a random node selector - """ - - def __init__(self): - super(RandomService, self).__init__() - - def pick_node(self, instance_id, **_kwargs): - nodes = self.compute_nodes_up() - return nodes[int(random.random() * len(nodes))] - - From 56a6ed754d23681f9d120d1282f270660b41fdd2 Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Fri, 6 Aug 2010 18:10:41 -0500 Subject: [PATCH 07/87] fix copyrights for new files, etc --- bin/nova-listinstances | 21 ++++++++++++++++++++- nova/scheduler/__init__.py | 12 ++---------- nova/scheduler/scheduler.py | 4 +--- nova/scheduler/service.py | 4 +--- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/bin/nova-listinstances b/bin/nova-listinstances index 2f8ff28f..386283d2 100755 --- a/bin/nova-listinstances +++ b/bin/nova-listinstances @@ -1,4 +1,19 @@ -#!/usr/bin/python +#!/usr/bin/env python +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright (c) 2010 Openstack, LLC. +# +# 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. # # Duplicates the functionality of euca-describe-instances, but doesn't require @@ -6,6 +21,10 @@ # mostly a test program written for the scheduler # +""" +List instances by doing a direct query to the datastore +""" + from nova.compute import model data_needed = ['image_id', 'memory_kb', 'local_gb', 'node_name', 'vcpus'] diff --git a/nova/scheduler/__init__.py b/nova/scheduler/__init__.py index 516ea61b..8359a7ae 100644 --- a/nova/scheduler/__init__.py +++ b/nova/scheduler/__init__.py @@ -1,8 +1,6 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# All Rights Reserved. +# Copyright (c) 2010 Openstack, LLC. # # 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 @@ -22,12 +20,6 @@ .. automodule:: nova.scheduler :platform: Unix - :synopsis: Daemon that picks a host for a VM instance. -.. moduleauthor:: Jesse Andrews -.. moduleauthor:: Devin Carlen -.. moduleauthor:: Vishvananda Ishaya -.. moduleauthor:: Joshua McKenty -.. moduleauthor:: Manish Singh -.. moduleauthor:: Andy Smith + :synopsis: Module that picks a compute node to run a VM instance. .. moduleauthor:: Chris Behrens """ diff --git a/nova/scheduler/scheduler.py b/nova/scheduler/scheduler.py index 0da7b95c..79ed9dc0 100644 --- a/nova/scheduler/scheduler.py +++ b/nova/scheduler/scheduler.py @@ -1,8 +1,6 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# All Rights Reserved. +# Copyright (c) 2010 Openstack, LLC. # # 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 diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index 3a86cefb..39bfd6e0 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -1,8 +1,6 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# All Rights Reserved. +# Copyright (c) 2010 Openstack, LLC. # # 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 From 738630605af009ddde6c9595802270c298210f33 Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Fri, 6 Aug 2010 18:54:45 -0500 Subject: [PATCH 08/87] pep8 and pylint cleanups --- nova/scheduler/scheduler.py | 29 +++++++++++++++++++---------- nova/scheduler/service.py | 18 +++++++++--------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/nova/scheduler/scheduler.py b/nova/scheduler/scheduler.py index 79ed9dc0..49ef40f0 100644 --- a/nova/scheduler/scheduler.py +++ b/nova/scheduler/scheduler.py @@ -18,19 +18,17 @@ Scheduler Classes """ -import logging import random -import sys import time -from nova import exception from nova import flags from nova.datastore import Redis FLAGS = flags.FLAGS flags.DEFINE_integer('node_down_time', 60, - 'seconds without heartbeat that determines a compute node to be down') + 'seconds without heartbeat that determines a ' + 'compute node to be down') class Scheduler(object): @@ -40,21 +38,32 @@ class Scheduler(object): @property def compute_nodes(self): - return [identifier.split(':')[0] for identifier in Redis.instance().smembers("daemons") if (identifier.split(':')[1] == "nova-compute")] + return [identifier.split(':')[0] + for identifier in Redis.instance().smembers("daemons") + if (identifier.split(':')[1] == "nova-compute")] def compute_node_is_up(self, node): - time_str = Redis.instance().hget('%s:%s:%s' % ('daemon', node, 'nova-compute'), 'updated_at') + time_str = Redis.instance().hget('%s:%s:%s' % + ('daemon', node, 'nova-compute'), + 'updated_at') + if not time_str: + return False + # Would be a lot easier if we stored heartbeat time in epoch :) - return(time_str and - (time.time() - (int(time.mktime(time.strptime(time_str.replace('Z', 'UTC'), '%Y-%m-%dT%H:%M:%S%Z'))) - time.timezone) < FLAGS.node_down_time)) + time_str = time_str.replace('Z', 'UTC') + time_split = time.strptime(time_str, '%Y-%m-%dT%H:%M:%S%Z') + epoch_time = int(time.mktime(time_split)) - time.timezone + return (time.time() - epoch_time) < FLAGS.node_down_time def compute_nodes_up(self): - return [node for node in self.compute_nodes if self.compute_node_is_up(node)] + return [node for node in self.compute_nodes + if self.compute_node_is_up(node)] def pick_node(self, instance_id, **_kwargs): """You DEFINITELY want to define this in your subclass""" raise NotImplementedError("Your subclass should define pick_node") + class RandomScheduler(Scheduler): """ Implements Scheduler as a random node selector @@ -67,6 +76,7 @@ class RandomScheduler(Scheduler): nodes = self.compute_nodes_up() return nodes[int(random.random() * len(nodes))] + class BestFitScheduler(Scheduler): """ Implements Scheduler as a best-fit node selector @@ -77,4 +87,3 @@ class BestFitScheduler(Scheduler): def pick_node(self, instance_id, **_kwargs): raise NotImplementedError("BestFitScheduler is not done yet") - diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index 39bfd6e0..1246b6e7 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -33,19 +33,20 @@ flags.DEFINE_string('scheduler_type', 'random', 'the scheduler to use') -scheduler_classes = { - 'random': scheduler.RandomScheduler, - 'bestfit': scheduler.BestFitScheduler - } - +scheduler_classes = {'random': scheduler.RandomScheduler, + 'bestfit': scheduler.BestFitScheduler} + + class SchedulerService(service.Service): """ Manages the running instances. """ + def __init__(self): super(SchedulerService, self).__init__() if (FLAGS.scheduler_type not in scheduler_classes): - raise exception.Error("Scheduler '%s' does not exist" % FLAGS.scheduler_type) + raise exception.Error("Scheduler '%s' does not exist" % + FLAGS.scheduler_type) self._scheduler_class = scheduler_classes[FLAGS.scheduler_type] def noop(self): @@ -77,7 +78,6 @@ class SchedulerService(service.Service): rpc.cast('%s.%s' % (FLAGS.compute_topic, node), {"method": "run_instance", - "args": {"instance_id" : instance_id}}) - logging.debug("Casting to node %s for instance %s" % + "args": {"instance_id": instance_id}}) + logging.debug("Casting to node %s for running instance %s" % (node, instance_id)) - From 139b7e9a1781357bd9eedeefbd6fb459b00d50f1 Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Mon, 9 Aug 2010 09:37:50 -0500 Subject: [PATCH 09/87] remove duplicated report_state that exists in the base class more pylint fixes --- nova/scheduler/service.py | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index 1246b6e7..9d2d35f1 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -25,7 +25,6 @@ from nova import exception from nova import flags from nova import rpc from nova import service -from nova.compute import model from nova.scheduler import scheduler FLAGS = flags.FLAGS @@ -33,7 +32,7 @@ flags.DEFINE_string('scheduler_type', 'random', 'the scheduler to use') -scheduler_classes = {'random': scheduler.RandomScheduler, +SCHEDULER_CLASSES = {'random': scheduler.RandomScheduler, 'bestfit': scheduler.BestFitScheduler} @@ -44,40 +43,33 @@ class SchedulerService(service.Service): def __init__(self): super(SchedulerService, self).__init__() - if (FLAGS.scheduler_type not in scheduler_classes): + if (FLAGS.scheduler_type not in SCHEDULER_CLASSES): raise exception.Error("Scheduler '%s' does not exist" % FLAGS.scheduler_type) - self._scheduler_class = scheduler_classes[FLAGS.scheduler_type] + self._scheduler_class = SCHEDULER_CLASSES[FLAGS.scheduler_type] def noop(self): """ simple test of an AMQP message call """ return defer.succeed('PONG') - @defer.inlineCallbacks - def report_state(self, nodename, daemon): - # TODO(termie): make this pattern be more elegant. -todd - try: - record = model.Daemon(nodename, daemon) - record.heartbeat() - if getattr(self, "model_disconnected", False): - self.model_disconnected = False - logging.error("Recovered model server connection!") - - except model.ConnectionError, ex: - if not getattr(self, "model_disconnected", False): - self.model_disconnected = True - logging.exception("model server went away") - yield - def pick_node(self, instance_id, **_kwargs): + """ + Return a node to use based on the selected Scheduler + """ + return self._scheduler_class().pick_node(instance_id, **_kwargs) @exception.wrap_exception def run_instance(self, instance_id, **_kwargs): + """ + Picks a node for a running VM and casts the run_instance request + """ + node = self.pick_node(instance_id, **_kwargs) rpc.cast('%s.%s' % (FLAGS.compute_topic, node), {"method": "run_instance", "args": {"instance_id": instance_id}}) - logging.debug("Casting to node %s for running instance %s" % - (node, instance_id)) + logging.debug("Casting to node %s for running instance %s", + node, instance_id) + From e58966c18384c2b5893d9ce4b2276c3b98e15e1a Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Mon, 9 Aug 2010 12:56:32 -0500 Subject: [PATCH 10/87] separated scheduler types into own modules --- nova/scheduler/{scheduler.py => base.py} | 32 +++--------------------- nova/scheduler/bestfit.py | 30 ++++++++++++++++++++++ nova/scheduler/service.py | 10 ++++---- 3 files changed, 39 insertions(+), 33 deletions(-) rename nova/scheduler/{scheduler.py => base.py} (75%) create mode 100644 nova/scheduler/bestfit.py diff --git a/nova/scheduler/scheduler.py b/nova/scheduler/base.py similarity index 75% rename from nova/scheduler/scheduler.py rename to nova/scheduler/base.py index 49ef40f0..5c359943 100644 --- a/nova/scheduler/scheduler.py +++ b/nova/scheduler/base.py @@ -15,10 +15,9 @@ # under the License. """ -Scheduler Classes +Scheduler base class that all Schedulers should inherit from """ -import random import time from nova import flags @@ -50,7 +49,9 @@ class Scheduler(object): return False # Would be a lot easier if we stored heartbeat time in epoch :) - time_str = time_str.replace('Z', 'UTC') + + # The 'str()' here is to get rid of a pylint error + time_str = str(time_str).replace('Z', 'UTC') time_split = time.strptime(time_str, '%Y-%m-%dT%H:%M:%S%Z') epoch_time = int(time.mktime(time_split)) - time.timezone return (time.time() - epoch_time) < FLAGS.node_down_time @@ -62,28 +63,3 @@ class Scheduler(object): def pick_node(self, instance_id, **_kwargs): """You DEFINITELY want to define this in your subclass""" raise NotImplementedError("Your subclass should define pick_node") - - -class RandomScheduler(Scheduler): - """ - Implements Scheduler as a random node selector - """ - - def __init__(self): - super(RandomScheduler, self).__init__() - - def pick_node(self, instance_id, **_kwargs): - nodes = self.compute_nodes_up() - return nodes[int(random.random() * len(nodes))] - - -class BestFitScheduler(Scheduler): - """ - Implements Scheduler as a best-fit node selector - """ - - def __init__(self): - super(BestFitScheduler, self).__init__() - - def pick_node(self, instance_id, **_kwargs): - raise NotImplementedError("BestFitScheduler is not done yet") diff --git a/nova/scheduler/bestfit.py b/nova/scheduler/bestfit.py new file mode 100644 index 00000000..1bd24456 --- /dev/null +++ b/nova/scheduler/bestfit.py @@ -0,0 +1,30 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright (c) 2010 Openstack, LLC. +# +# 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. + +""" +Best Fit Scheduler +""" + +from nova.scheduler.base import Scheduler + + +class BestFitScheduler(Scheduler): + """ + Implements Scheduler as a best-fit node selector + """ + + def pick_node(self, instance_id, **_kwargs): + raise NotImplementedError("BestFitScheduler is not done yet") diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index 9d2d35f1..44b30ecb 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -25,15 +25,16 @@ from nova import exception from nova import flags from nova import rpc from nova import service -from nova.scheduler import scheduler +from nova.scheduler import chance +from nova.scheduler import bestfit FLAGS = flags.FLAGS flags.DEFINE_string('scheduler_type', - 'random', + 'chance', 'the scheduler to use') -SCHEDULER_CLASSES = {'random': scheduler.RandomScheduler, - 'bestfit': scheduler.BestFitScheduler} +SCHEDULER_CLASSES = {'chance': chance.ChanceScheduler, + 'bestfit': bestfit.BestFitScheduler} class SchedulerService(service.Service): @@ -72,4 +73,3 @@ class SchedulerService(service.Service): "args": {"instance_id": instance_id}}) logging.debug("Casting to node %s for running instance %s", node, instance_id) - From d53ad0a002ab5f4a23f0e1c256bbd880d702acf6 Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Tue, 10 Aug 2010 18:48:33 -0500 Subject: [PATCH 11/87] more pylint fixes --- nova/scheduler/base.py | 23 +++++++++++++++++++---- nova/scheduler/bestfit.py | 4 ++++ nova/scheduler/service.py | 3 ++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/nova/scheduler/base.py b/nova/scheduler/base.py index 5c359943..2872ae6f 100644 --- a/nova/scheduler/base.py +++ b/nova/scheduler/base.py @@ -35,13 +35,23 @@ class Scheduler(object): The base class that all Scheduler clases should inherit from """ - @property - def compute_nodes(self): + @staticmethod + def compute_nodes(): + """ + Return a list of compute nodes + """ + return [identifier.split(':')[0] for identifier in Redis.instance().smembers("daemons") if (identifier.split(':')[1] == "nova-compute")] - def compute_node_is_up(self, node): + @staticmethod + def compute_node_is_up(node): + """ + Given a node name, return whether the node is considered 'up' by + if it's sent a heartbeat recently + """ + time_str = Redis.instance().hget('%s:%s:%s' % ('daemon', node, 'nova-compute'), 'updated_at') @@ -57,9 +67,14 @@ class Scheduler(object): return (time.time() - epoch_time) < FLAGS.node_down_time def compute_nodes_up(self): - return [node for node in self.compute_nodes + """ + Return the list of compute nodes that are considered 'up' + """ + + return [node for node in self.compute_nodes() if self.compute_node_is_up(node)] def pick_node(self, instance_id, **_kwargs): """You DEFINITELY want to define this in your subclass""" + raise NotImplementedError("Your subclass should define pick_node") diff --git a/nova/scheduler/bestfit.py b/nova/scheduler/bestfit.py index 1bd24456..bdd4fcbd 100644 --- a/nova/scheduler/bestfit.py +++ b/nova/scheduler/bestfit.py @@ -27,4 +27,8 @@ class BestFitScheduler(Scheduler): """ def pick_node(self, instance_id, **_kwargs): + """ + Picks a node that is up and is a best fit for the new instance + """ + raise NotImplementedError("BestFitScheduler is not done yet") diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py index 44b30ecb..136f262c 100644 --- a/nova/scheduler/service.py +++ b/nova/scheduler/service.py @@ -49,7 +49,8 @@ class SchedulerService(service.Service): FLAGS.scheduler_type) self._scheduler_class = SCHEDULER_CLASSES[FLAGS.scheduler_type] - def noop(self): + @staticmethod + def noop(): """ simple test of an AMQP message call """ return defer.succeed('PONG') From de665142a7ae364033f04c3f7662b10b3cff52dc Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Fri, 20 Aug 2010 13:26:24 +0100 Subject: [PATCH 12/87] Better error message on subprocess spawn fail, and it's a ProcessExecutionException irrespective of how the process is run. --- nova/process.py | 65 +++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/nova/process.py b/nova/process.py index 425d9f16..259e3f92 100644 --- a/nova/process.py +++ b/nova/process.py @@ -29,28 +29,12 @@ from twisted.internet import protocol from twisted.internet import reactor from nova import flags +from nova.utils import ProcessExecutionError FLAGS = flags.FLAGS flags.DEFINE_integer('process_pool_size', 4, 'Number of processes to use in the process pool') - -# NOTE(termie): this is copied from twisted.internet.utils but since -# they don't export it I've copied and modified -class UnexpectedErrorOutput(IOError): - """ - Standard error data was received where it was not expected. This is a - subclass of L{IOError} to preserve backward compatibility with the previous - error behavior of L{getProcessOutput}. - - @ivar processEnded: A L{Deferred} which will fire when the process which - produced the data on stderr has ended (exited and all file descriptors - closed). - """ - def __init__(self, stdout=None, stderr=None): - IOError.__init__(self, "got stdout: %r\nstderr: %r" % (stdout, stderr)) - - # This is based on _BackRelay from twister.internal.utils, but modified to # capture both stdout and stderr, without odd stderr handling, and also to # handle stdin @@ -62,22 +46,23 @@ class BackRelayWithInput(protocol.ProcessProtocol): @ivar deferred: A L{Deferred} which will be called back with all of stdout and all of stderr as well (as a tuple). C{terminate_on_stderr} is true and any bytes are received over stderr, this will fire with an - L{_UnexpectedErrorOutput} instance and the attribute will be set to + L{_ProcessExecutionError} instance and the attribute will be set to C{None}. @ivar onProcessEnded: If C{terminate_on_stderr} is false and bytes are received over stderr, this attribute will refer to a L{Deferred} which will be called back when the process ends. This C{Deferred} is also - associated with the L{_UnexpectedErrorOutput} which C{deferred} fires + associated with the L{_ProcessExecutionError} which C{deferred} fires with earlier in this case so that users can determine when the process has actually ended, in addition to knowing when bytes have been received via stderr. """ - def __init__(self, deferred, started_deferred=None, + def __init__(self, deferred, cmd, started_deferred=None, terminate_on_stderr=False, check_exit_code=True, process_input=None): self.deferred = deferred + self.cmd = cmd self.stdout = StringIO.StringIO() self.stderr = StringIO.StringIO() self.started_deferred = started_deferred @@ -85,14 +70,18 @@ class BackRelayWithInput(protocol.ProcessProtocol): self.check_exit_code = check_exit_code self.process_input = process_input self.on_process_ended = None - + + def _build_execution_error(self, exit_code=None): + return ProcessExecutionError( cmd=self.cmd, + exit_code=exit_code, + stdout=self.stdout.getvalue(), + stderr=self.stderr.getvalue()) + def errReceived(self, text): self.stderr.write(text) if self.terminate_on_stderr and (self.deferred is not None): self.on_process_ended = defer.Deferred() - self.deferred.errback(UnexpectedErrorOutput( - stdout=self.stdout.getvalue(), - stderr=self.stderr.getvalue())) + self.deferred.errback(self._build_execution_error()) self.deferred = None self.transport.loseConnection() @@ -102,15 +91,19 @@ class BackRelayWithInput(protocol.ProcessProtocol): def processEnded(self, reason): if self.deferred is not None: stdout, stderr = self.stdout.getvalue(), self.stderr.getvalue() - try: - if self.check_exit_code: - reason.trap(error.ProcessDone) - self.deferred.callback((stdout, stderr)) - except: - # NOTE(justinsb): This logic is a little suspicious to me... - # If the callback throws an exception, then errback will be - # called also. However, this is what the unit tests test for... - self.deferred.errback(UnexpectedErrorOutput(stdout, stderr)) + exit_code = reason.value.exitCode + if self.check_exit_code and exit_code <> 0: + self.deferred.errback(self._build_execution_error(exit_code)) + else: + try: + if self.check_exit_code: + reason.trap(error.ProcessDone) + self.deferred.callback((stdout, stderr)) + except: + # NOTE(justinsb): This logic is a little suspicious to me... + # If the callback throws an exception, then errback will be + # called also. However, this is what the unit tests test for... + self.deferred.errback(_build_execution_error(exit-code)) elif self.on_process_ended is not None: self.on_process_ended.errback(reason) @@ -131,8 +124,12 @@ def get_process_output(executable, args=None, env=None, path=None, args = args and args or () env = env and env and {} deferred = defer.Deferred() + cmd = executable + if args: + cmd = cmd + " " + ' '.join(args) process_handler = BackRelayWithInput( - deferred, + deferred, + cmd, started_deferred=started_deferred, check_exit_code=check_exit_code, process_input=process_input, From d41be94d1da77824e202804eac7e81cbf3370e1e Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Fri, 20 Aug 2010 14:50:43 +0100 Subject: [PATCH 13/87] Fixed typo --- nova/process.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/process.py b/nova/process.py index 259e3f92..81262a50 100644 --- a/nova/process.py +++ b/nova/process.py @@ -103,7 +103,7 @@ class BackRelayWithInput(protocol.ProcessProtocol): # NOTE(justinsb): This logic is a little suspicious to me... # If the callback throws an exception, then errback will be # called also. However, this is what the unit tests test for... - self.deferred.errback(_build_execution_error(exit-code)) + self.deferred.errback(_build_execution_error(exit_code)) elif self.on_process_ended is not None: self.on_process_ended.errback(reason) From 26d315cb4a113e4640963208024fe2dcf2111534 Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Sat, 21 Aug 2010 11:54:03 +0100 Subject: [PATCH 14/87] Added missing "self." --- nova/process.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/process.py b/nova/process.py index 81262a50..06931080 100644 --- a/nova/process.py +++ b/nova/process.py @@ -103,7 +103,7 @@ class BackRelayWithInput(protocol.ProcessProtocol): # NOTE(justinsb): This logic is a little suspicious to me... # If the callback throws an exception, then errback will be # called also. However, this is what the unit tests test for... - self.deferred.errback(_build_execution_error(exit_code)) + self.deferred.errback(self._build_execution_error(exit_code)) elif self.on_process_ended is not None: self.on_process_ended.errback(reason) From 063af4dfcd54bbe2aca0ace2fa73d069a5562fce Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 2 Sep 2010 11:32:37 -0700 Subject: [PATCH 15/87] scheduler + unittests --- bin/nova-scheduler | 4 +- nova/endpoint/cloud.py | 6 +- nova/flags.py | 2 + nova/scheduler/base.py | 80 ------------------------ nova/scheduler/bestfit.py | 34 ---------- nova/scheduler/service.py | 76 ----------------------- nova/scheduler/simple.py | 81 ++++++++++++++++++++++++ nova/tests/compute_unittest.py | 2 +- nova/tests/scheduler_unittest.py | 103 +++++++++++++++++++++++++++++++ run_tests.py | 1 + 10 files changed, 193 insertions(+), 196 deletions(-) delete mode 100644 nova/scheduler/base.py delete mode 100644 nova/scheduler/bestfit.py delete mode 100644 nova/scheduler/service.py create mode 100644 nova/scheduler/simple.py create mode 100644 nova/tests/scheduler_unittest.py diff --git a/bin/nova-scheduler b/bin/nova-scheduler index 1ad41bbd..97f98b17 100755 --- a/bin/nova-scheduler +++ b/bin/nova-scheduler @@ -21,12 +21,12 @@ Twistd daemon for the nova scheduler nodes. """ +from nova import service from nova import twistd -from nova.scheduler import service if __name__ == '__main__': twistd.serve(__file__) if __name__ == '__builtin__': - application = service.SchedulerService.create() + application = service.Service.create() diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 4e86145d..2c88ef40 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -488,9 +488,9 @@ class CloudController(object): host = db.network_get_host(context, network_ref['id']) if not host: host = yield rpc.call(FLAGS.network_topic, - {"method": "set_network_host", - "args": {"context": None, - "project_id": context.project.id}}) + {"method": "set_network_host", + "args": {"context": None, + "project_id": context.project.id}}) defer.returnValue(db.queue_get_for(context, FLAGS.network_topic, host)) @rbac.allow('projectmanager', 'sysadmin') diff --git a/nova/flags.py b/nova/flags.py index aa964884..40ce9c73 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -220,5 +220,7 @@ DEFINE_string('network_manager', 'nova.network.manager.VlanManager', 'Manager for network') DEFINE_string('volume_manager', 'nova.volume.manager.AOEManager', 'Manager for volume') +DEFINE_string('scheduler_manager', 'nova.scheduler.manager.SchedulerManager', + 'Manager for scheduler') diff --git a/nova/scheduler/base.py b/nova/scheduler/base.py deleted file mode 100644 index 2872ae6f..00000000 --- a/nova/scheduler/base.py +++ /dev/null @@ -1,80 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright (c) 2010 Openstack, LLC. -# -# 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. - -""" -Scheduler base class that all Schedulers should inherit from -""" - -import time - -from nova import flags -from nova.datastore import Redis - -FLAGS = flags.FLAGS -flags.DEFINE_integer('node_down_time', - 60, - 'seconds without heartbeat that determines a ' - 'compute node to be down') - - -class Scheduler(object): - """ - The base class that all Scheduler clases should inherit from - """ - - @staticmethod - def compute_nodes(): - """ - Return a list of compute nodes - """ - - return [identifier.split(':')[0] - for identifier in Redis.instance().smembers("daemons") - if (identifier.split(':')[1] == "nova-compute")] - - @staticmethod - def compute_node_is_up(node): - """ - Given a node name, return whether the node is considered 'up' by - if it's sent a heartbeat recently - """ - - time_str = Redis.instance().hget('%s:%s:%s' % - ('daemon', node, 'nova-compute'), - 'updated_at') - if not time_str: - return False - - # Would be a lot easier if we stored heartbeat time in epoch :) - - # The 'str()' here is to get rid of a pylint error - time_str = str(time_str).replace('Z', 'UTC') - time_split = time.strptime(time_str, '%Y-%m-%dT%H:%M:%S%Z') - epoch_time = int(time.mktime(time_split)) - time.timezone - return (time.time() - epoch_time) < FLAGS.node_down_time - - def compute_nodes_up(self): - """ - Return the list of compute nodes that are considered 'up' - """ - - return [node for node in self.compute_nodes() - if self.compute_node_is_up(node)] - - def pick_node(self, instance_id, **_kwargs): - """You DEFINITELY want to define this in your subclass""" - - raise NotImplementedError("Your subclass should define pick_node") diff --git a/nova/scheduler/bestfit.py b/nova/scheduler/bestfit.py deleted file mode 100644 index bdd4fcbd..00000000 --- a/nova/scheduler/bestfit.py +++ /dev/null @@ -1,34 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright (c) 2010 Openstack, LLC. -# -# 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. - -""" -Best Fit Scheduler -""" - -from nova.scheduler.base import Scheduler - - -class BestFitScheduler(Scheduler): - """ - Implements Scheduler as a best-fit node selector - """ - - def pick_node(self, instance_id, **_kwargs): - """ - Picks a node that is up and is a best fit for the new instance - """ - - raise NotImplementedError("BestFitScheduler is not done yet") diff --git a/nova/scheduler/service.py b/nova/scheduler/service.py deleted file mode 100644 index 136f262c..00000000 --- a/nova/scheduler/service.py +++ /dev/null @@ -1,76 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright (c) 2010 Openstack, LLC. -# -# 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. - -""" -Scheduler Service -""" - -import logging -from twisted.internet import defer - -from nova import exception -from nova import flags -from nova import rpc -from nova import service -from nova.scheduler import chance -from nova.scheduler import bestfit - -FLAGS = flags.FLAGS -flags.DEFINE_string('scheduler_type', - 'chance', - 'the scheduler to use') - -SCHEDULER_CLASSES = {'chance': chance.ChanceScheduler, - 'bestfit': bestfit.BestFitScheduler} - - -class SchedulerService(service.Service): - """ - Manages the running instances. - """ - - def __init__(self): - super(SchedulerService, self).__init__() - if (FLAGS.scheduler_type not in SCHEDULER_CLASSES): - raise exception.Error("Scheduler '%s' does not exist" % - FLAGS.scheduler_type) - self._scheduler_class = SCHEDULER_CLASSES[FLAGS.scheduler_type] - - @staticmethod - def noop(): - """ simple test of an AMQP message call """ - return defer.succeed('PONG') - - def pick_node(self, instance_id, **_kwargs): - """ - Return a node to use based on the selected Scheduler - """ - - return self._scheduler_class().pick_node(instance_id, **_kwargs) - - @exception.wrap_exception - def run_instance(self, instance_id, **_kwargs): - """ - Picks a node for a running VM and casts the run_instance request - """ - - node = self.pick_node(instance_id, **_kwargs) - - rpc.cast('%s.%s' % (FLAGS.compute_topic, node), - {"method": "run_instance", - "args": {"instance_id": instance_id}}) - logging.debug("Casting to node %s for running instance %s", - node, instance_id) diff --git a/nova/scheduler/simple.py b/nova/scheduler/simple.py new file mode 100644 index 00000000..6c76fd32 --- /dev/null +++ b/nova/scheduler/simple.py @@ -0,0 +1,81 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright (c) 2010 Openstack, LLC. +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. + +""" +Simple Scheduler +""" + +from nova import db +from nova import flags +from nova.scheduler import driver + +FLAGS = flags.FLAGS +flags.DEFINE_integer("max_instances", 16, + "maximum number of instances to allow per host") +flags.DEFINE_integer("max_volumes", 100, + "maximum number of volumes to allow per host") +flags.DEFINE_integer("max_networks", 1000, + "maximum number of networks to allow per host") + +class SimpleScheduler(driver.Scheduler): + """ + Implements Naive Scheduler that tries to find least loaded host + """ + + def pick_compute_host(self, context, instance_id, **_kwargs): + """ + Picks a host that is up and has the fewest running instances + """ + + results = db.daemon_get_all_compute_sorted(context) + for result in results: + (daemon, instance_count) = result + if instance_count >= FLAGS.max_instances: + raise driver.NoValidHost("All hosts have too many instances") + if self.daemon_is_up(daemon): + return daemon['host'] + raise driver.NoValidHost("No hosts found") + + def pick_volume_host(self, context, volume_id, **_kwargs): + """ + Picks a host that is up and has the fewest volumes + """ + + results = db.daemon_get_all_volume_sorted(context) + for result in results: + (daemon, instance_count) = result + if instance_count >= FLAGS.max_volumes: + raise driver.NoValidHost("All hosts have too many volumes") + if self.daemon_is_up(daemon): + return daemon['host'] + raise driver.NoValidHost("No hosts found") + + def pick_network_host(self, context, network_id, **_kwargs): + """ + Picks a host that is up and has the fewest networks + """ + + results = db.daemon_get_all_network_sorted(context) + for result in results: + (daemon, instance_count) = result + if instance_count >= FLAGS.max_networks: + raise driver.NoValidHost("All hosts have too many networks") + if self.daemon_is_up(daemon): + return daemon['host'] + raise driver.NoValidHost("No hosts found") diff --git a/nova/tests/compute_unittest.py b/nova/tests/compute_unittest.py index 867b572f..23013e4c 100644 --- a/nova/tests/compute_unittest.py +++ b/nova/tests/compute_unittest.py @@ -61,7 +61,7 @@ class ComputeTestCase(test.TrialTestCase): inst['instance_type'] = 'm1.tiny' inst['mac_address'] = utils.generate_mac() inst['ami_launch_index'] = 0 - return db.instance_create(None, inst) + return db.instance_create(self.context, inst) @defer.inlineCallbacks def test_run_terminate(self): diff --git a/nova/tests/scheduler_unittest.py b/nova/tests/scheduler_unittest.py new file mode 100644 index 00000000..d3616dd6 --- /dev/null +++ b/nova/tests/scheduler_unittest.py @@ -0,0 +1,103 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. +""" +Tests For Scheduler +""" +import logging + +from twisted.internet import defer + +from nova import db +from nova import flags +from nova import service +from nova import test +from nova import utils +from nova.auth import manager as auth_manager +from nova.scheduler import manager + + +FLAGS = flags.FLAGS + + +class SchedulerTestCase(test.TrialTestCase): + """Test case for scheduler""" + def setUp(self): # pylint: disable-msg=C0103 + super(SchedulerTestCase, self).setUp() + self.flags(connection_type='fake', + scheduler_driver='nova.scheduler.simple.SimpleScheduler') + self.scheduler = manager.SchedulerManager() + self.context = None + self.manager = auth_manager.AuthManager() + self.user = self.manager.create_user('fake', 'fake', 'fake') + self.project = self.manager.create_project('fake', 'fake', 'fake') + self.context = None + + def tearDown(self): # pylint: disable-msg=C0103 + self.manager.delete_user(self.user) + self.manager.delete_project(self.project) + + def _create_instance(self): + """Create a test instance""" + inst = {} + inst['image_id'] = 'ami-test' + inst['reservation_id'] = 'r-fakeres' + inst['launch_time'] = '10' + inst['user_id'] = self.user.id + inst['project_id'] = self.project.id + inst['instance_type'] = 'm1.tiny' + inst['mac_address'] = utils.generate_mac() + inst['ami_launch_index'] = 0 + return db.instance_create(self.context, inst) + + def test_hosts_are_up(self): + # NOTE(vish): constructing service without create method + # because we are going to use it without queue + service1 = service.Service('host1', + 'nova-compute', + 'compute', + FLAGS.compute_manager) + service2 = service.Service('host2', + 'nova-compute', + 'compute', + FLAGS.compute_manager) + service1.report_state() + service2.report_state() + hosts = self.scheduler.driver.hosts_up(self.context, 'compute') + self.assertEqual(len(hosts), 2) + + def test_least_busy_host_gets_instance(self): + # NOTE(vish): constructing service without create method + # because we are going to use it without queue + service1 = service.Service('host1', + 'nova-compute', + 'compute', + FLAGS.compute_manager) + service2 = service.Service('host2', + 'nova-compute', + 'compute', + FLAGS.compute_manager) + service1.report_state() + service2.report_state() + instance_id = self._create_instance() + FLAGS.host = 'host1' + service1.run_instance(self.context, + instance_id) + print type(self.scheduler.driver) + host = self.scheduler.driver.pick_compute_host(self.context, + instance_id) + self.assertEqual(host, 'host2') diff --git a/run_tests.py b/run_tests.py index c47cbe2e..5d76a74c 100644 --- a/run_tests.py +++ b/run_tests.py @@ -60,6 +60,7 @@ from nova.tests.network_unittest import * from nova.tests.objectstore_unittest import * from nova.tests.process_unittest import * from nova.tests.rpc_unittest import * +from nova.tests.scheduler_unittest import * from nova.tests.service_unittest import * from nova.tests.validator_unittest import * from nova.tests.volume_unittest import * From ab776939c1adf5c66713b3c1fae5312f455551ab Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 2 Sep 2010 15:15:39 -0700 Subject: [PATCH 16/87] test for too many instances work --- nova/scheduler/simple.py | 25 ++++++------- nova/tests/scheduler_unittest.py | 63 +++++++++++++------------------- 2 files changed, 37 insertions(+), 51 deletions(-) diff --git a/nova/scheduler/simple.py b/nova/scheduler/simple.py index 294dc111..83241720 100644 --- a/nova/scheduler/simple.py +++ b/nova/scheduler/simple.py @@ -43,14 +43,13 @@ class SimpleScheduler(driver.Scheduler): Picks a host that is up and has the fewest running instances """ - results = db.daemon_get_all_compute_sorted(context) + results = db.service_get_all_compute_sorted(context) for result in results: - (daemon, instance_count) = result - print daemon.host, instance_count + (service, instance_count) = result if instance_count >= FLAGS.max_instances: raise driver.NoValidHost("All hosts have too many instances") - if self.daemon_is_up(daemon): - return daemon['host'] + if self.service_is_up(service): + return service['host'] raise driver.NoValidHost("No hosts found") def pick_volume_host(self, context, volume_id, **_kwargs): @@ -58,13 +57,13 @@ class SimpleScheduler(driver.Scheduler): Picks a host that is up and has the fewest volumes """ - results = db.daemon_get_all_volume_sorted(context) + results = db.service_get_all_volume_sorted(context) for result in results: - (daemon, instance_count) = result + (service, instance_count) = result if instance_count >= FLAGS.max_volumes: raise driver.NoValidHost("All hosts have too many volumes") - if self.daemon_is_up(daemon): - return daemon['host'] + if self.service_is_up(service): + return service['host'] raise driver.NoValidHost("No hosts found") def pick_network_host(self, context, network_id, **_kwargs): @@ -72,11 +71,11 @@ class SimpleScheduler(driver.Scheduler): Picks a host that is up and has the fewest networks """ - results = db.daemon_get_all_network_sorted(context) + results = db.service_get_all_network_sorted(context) for result in results: - (daemon, instance_count) = result + (service, instance_count) = result if instance_count >= FLAGS.max_networks: raise driver.NoValidHost("All hosts have too many networks") - if self.daemon_is_up(daemon): - return daemon['host'] + if self.service_is_up(service): + return service['host'] raise driver.NoValidHost("No hosts found") diff --git a/nova/tests/scheduler_unittest.py b/nova/tests/scheduler_unittest.py index 45ffac43..bdd77713 100644 --- a/nova/tests/scheduler_unittest.py +++ b/nova/tests/scheduler_unittest.py @@ -18,9 +18,6 @@ """ Tests For Scheduler """ -import logging - -from twisted.internet import defer from nova import db from nova import flags @@ -36,10 +33,10 @@ FLAGS = flags.FLAGS flags.DECLARE('max_instances', 'nova.scheduler.simple') -class SchedulerTestCase(test.TrialTestCase): +class SimpleSchedulerTestCase(test.TrialTestCase): """Test case for scheduler""" def setUp(self): # pylint: disable-msg=C0103 - super(SchedulerTestCase, self).setUp() + super(SimpleSchedulerTestCase, self).setUp() self.flags(connection_type='fake', max_instances=4, scheduler_driver='nova.scheduler.simple.SimpleScheduler') @@ -49,10 +46,20 @@ class SchedulerTestCase(test.TrialTestCase): self.user = self.manager.create_user('fake', 'fake', 'fake') self.project = self.manager.create_project('fake', 'fake', 'fake') self.context = None + self.service1 = service.Service('host1', + 'nova-compute', + 'compute', + FLAGS.compute_manager) + self.service2 = service.Service('host2', + 'nova-compute', + 'compute', + FLAGS.compute_manager) def tearDown(self): # pylint: disable-msg=C0103 self.manager.delete_user(self.user) self.manager.delete_project(self.project) + self.service1.kill() + self.service2.kill() def _create_instance(self): """Create a test instance""" @@ -70,53 +77,33 @@ class SchedulerTestCase(test.TrialTestCase): def test_hosts_are_up(self): # NOTE(vish): constructing service without create method # because we are going to use it without queue - service1 = service.Service('host1', - 'nova-compute', - 'compute', - FLAGS.compute_manager) - service2 = service.Service('host2', - 'nova-compute', - 'compute', - FLAGS.compute_manager) - hosts = self.scheduler.driver.hosts_up(self.context, 'compute') - self.assertEqual(len(hosts), 0) - service1.report_state() - service2.report_state() hosts = self.scheduler.driver.hosts_up(self.context, 'compute') self.assertEqual(len(hosts), 2) def test_least_busy_host_gets_instance(self): - service1 = service.Service('host1', - 'nova-compute', - 'compute', - FLAGS.compute_manager) - service2 = service.Service('host2', - 'nova-compute', - 'compute', - FLAGS.compute_manager) - service1.report_state() - service2.report_state() instance_id = self._create_instance() - service1.run_instance(self.context, instance_id) + self.service1.run_instance(self.context, instance_id) host = self.scheduler.driver.pick_compute_host(self.context, instance_id) self.assertEqual(host, 'host2') - service1.terminate_instance(self.context, instance_id) + self.service1.terminate_instance(self.context, instance_id) def test_too_many_instances(self): - service1 = service.Service('host', - 'nova-compute', - 'compute', - FLAGS.compute_manager) - instance_ids = [] + instance_ids1 = [] + instance_ids2 = [] for index in xrange(FLAGS.max_instances): instance_id = self._create_instance() - service1.run_instance(self.context, instance_id) - instance_ids.append(instance_id) + self.service1.run_instance(self.context, instance_id) + instance_ids1.append(instance_id) + instance_id = self._create_instance() + self.service2.run_instance(self.context, instance_id) + instance_ids2.append(instance_id) instance_id = self._create_instance() self.assertRaises(driver.NoValidHost, self.scheduler.driver.pick_compute_host, self.context, instance_id) - for instance_id in instance_ids: - service1.terminate_instance(self.context, instance_id) + for instance_id in instance_ids1: + self.service1.terminate_instance(self.context, instance_id) + for instance_id in instance_ids2: + self.service2.terminate_instance(self.context, instance_id) From bf1f12e13194489808753b7ae35e6a2101eea99c Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 2 Sep 2010 17:06:01 -0700 Subject: [PATCH 17/87] send ultimate topic in to scheduler --- nova/endpoint/cloud.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 2c88ef40..1ff22042 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -567,6 +567,7 @@ class CloudController(object): rpc.cast(FLAGS.scheduler_topic, {"method": "run_instance", "args": {"context": None, + "topic": FLAGS.compute_topic, "instance_id": inst_id}}) logging.debug("Casting to scheduler for %s/%s's instance %s" % (context.project.name, context.user.name, inst_id)) From 088b765655b2e8667791288ecfe73e16eed16d2e Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 3 Sep 2010 00:53:41 -0700 Subject: [PATCH 18/87] removed extra file and updated sql note --- bin/nova-listinstances | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100755 bin/nova-listinstances diff --git a/bin/nova-listinstances b/bin/nova-listinstances deleted file mode 100755 index 386283d2..00000000 --- a/bin/nova-listinstances +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright (c) 2010 Openstack, LLC. -# -# 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. - -# -# Duplicates the functionality of euca-describe-instances, but doesn't require -# going through the API. Does a direct query to the datastore. This is -# mostly a test program written for the scheduler -# - -""" -List instances by doing a direct query to the datastore -""" - -from nova.compute import model - -data_needed = ['image_id', 'memory_kb', 'local_gb', 'node_name', 'vcpus'] - -instances = model.InstanceDirectory().all - -for instance in instances: - print 'Instance: %s' % instance['instance_id'] - for x in data_needed: - print ' %s: %s' % (x, instance[x]) - From 676769f786dac746dbe1d187cf03b701f918850b Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Sun, 5 Sep 2010 05:56:53 +0100 Subject: [PATCH 19/87] Bug #630640: Duplicated power state constants Remove power state constants that have ended up duplicated following a bad merge. They were moved from nova.compute.node.Instance into nova.compute.power_state at the same time that Instance was moved into nova.compute.service. We've ended up with these constants in both places. Remove the ones from service, in favour of the ones in power_state. --- nova/tests/cloud_unittest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nova/tests/cloud_unittest.py b/nova/tests/cloud_unittest.py index 900ff5a9..19aa23b9 100644 --- a/nova/tests/cloud_unittest.py +++ b/nova/tests/cloud_unittest.py @@ -28,6 +28,7 @@ from nova import flags from nova import rpc from nova import test from nova.auth import manager +from nova.compute import power_state from nova.compute import service from nova.endpoint import api from nova.endpoint import cloud @@ -95,7 +96,7 @@ class CloudTestCase(test.BaseTestCase): rv = yield defer.succeed(time.sleep(1)) info = self.cloud._get_instance(instance['instance_id']) logging.debug(info['state']) - if info['state'] == node.Instance.RUNNING: + if info['state'] == power_state.RUNNING: break self.assert_(rv) From 871d08de05cd88017a5b75fccb136bd6cef091fc Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Tue, 7 Sep 2010 13:01:21 -0700 Subject: [PATCH 20/87] fix docstrings and formatting --- nova/scheduler/simple.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/nova/scheduler/simple.py b/nova/scheduler/simple.py index d10ddaba..ea4eef98 100644 --- a/nova/scheduler/simple.py +++ b/nova/scheduler/simple.py @@ -35,14 +35,10 @@ flags.DEFINE_integer("max_networks", 1000, "maximum number of networks to allow per host") class SimpleScheduler(chance.ChanceScheduler): - """ - Implements Naive Scheduler that tries to find least loaded host - """ + """Implements Naive Scheduler that tries to find least loaded host.""" def schedule_run_instance(self, context, _instance_id, *_args, **_kwargs): - """ - Picks a host that is up and has the fewest running instances - """ + """Picks a host that is up and has the fewest running instances.""" results = db.service_get_all_compute_sorted(context) for result in results: @@ -54,9 +50,7 @@ class SimpleScheduler(chance.ChanceScheduler): raise driver.NoValidHost("No hosts found") def schedule_create_volume(self, context, _volume_id, *_args, **_kwargs): - """ - Picks a host that is up and has the fewest volumes - """ + """Picks a host that is up and has the fewest volumes.""" results = db.service_get_all_volume_sorted(context) for result in results: @@ -67,10 +61,9 @@ class SimpleScheduler(chance.ChanceScheduler): return service['host'] raise driver.NoValidHost("No hosts found") - def schedule_set_network_host(self, context, _network_id, *_args, **_kwargs): - """ - Picks a host that is up and has the fewest networks - """ + def schedule_set_network_host(self, context, _network_id, + *_args, **_kwargs): + """Picks a host that is up and has the fewest networks.""" results = db.service_get_all_network_sorted(context) for result in results: From 9b6b5b9465e65736191ccd315361c6052b385ef9 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Tue, 7 Sep 2010 18:32:08 -0700 Subject: [PATCH 21/87] whitespace fixes --- nova/process.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/nova/process.py b/nova/process.py index 06931080..259e6235 100644 --- a/nova/process.py +++ b/nova/process.py @@ -35,8 +35,8 @@ FLAGS = flags.FLAGS flags.DEFINE_integer('process_pool_size', 4, 'Number of processes to use in the process pool') -# This is based on _BackRelay from twister.internal.utils, but modified to -# capture both stdout and stderr, without odd stderr handling, and also to +# This is based on _BackRelay from twister.internal.utils, but modified to +# capture both stdout and stderr, without odd stderr handling, and also to # handle stdin class BackRelayWithInput(protocol.ProcessProtocol): """ @@ -46,21 +46,21 @@ class BackRelayWithInput(protocol.ProcessProtocol): @ivar deferred: A L{Deferred} which will be called back with all of stdout and all of stderr as well (as a tuple). C{terminate_on_stderr} is true and any bytes are received over stderr, this will fire with an - L{_ProcessExecutionError} instance and the attribute will be set to + L{_ProcessExecutionError} instance and the attribute will be set to C{None}. - @ivar onProcessEnded: If C{terminate_on_stderr} is false and bytes are - received over stderr, this attribute will refer to a L{Deferred} which - will be called back when the process ends. This C{Deferred} is also - associated with the L{_ProcessExecutionError} which C{deferred} fires - with earlier in this case so that users can determine when the process + @ivar onProcessEnded: If C{terminate_on_stderr} is false and bytes are + received over stderr, this attribute will refer to a L{Deferred} which + will be called back when the process ends. This C{Deferred} is also + associated with the L{_ProcessExecutionError} which C{deferred} fires + with earlier in this case so that users can determine when the process has actually ended, in addition to knowing when bytes have been received via stderr. """ - def __init__(self, deferred, cmd, started_deferred=None, - terminate_on_stderr=False, check_exit_code=True, - process_input=None): + def __init__(self, deferred, cmd, started_deferred=None, + terminate_on_stderr=False, check_exit_code=True, + process_input=None): self.deferred = deferred self.cmd = cmd self.stdout = StringIO.StringIO() @@ -70,12 +70,12 @@ class BackRelayWithInput(protocol.ProcessProtocol): self.check_exit_code = check_exit_code self.process_input = process_input self.on_process_ended = None - + def _build_execution_error(self, exit_code=None): - return ProcessExecutionError( cmd=self.cmd, - exit_code=exit_code, - stdout=self.stdout.getvalue(), - stderr=self.stderr.getvalue()) + return ProcessExecutionError(cmd=self.cmd, + exit_code=exit_code, + stdout=self.stdout.getvalue(), + stderr=self.stderr.getvalue()) def errReceived(self, text): self.stderr.write(text) @@ -101,7 +101,7 @@ class BackRelayWithInput(protocol.ProcessProtocol): self.deferred.callback((stdout, stderr)) except: # NOTE(justinsb): This logic is a little suspicious to me... - # If the callback throws an exception, then errback will be + # If the callback throws an exception, then errback will be # called also. However, this is what the unit tests test for... self.deferred.errback(self._build_execution_error(exit_code)) elif self.on_process_ended is not None: @@ -115,8 +115,8 @@ class BackRelayWithInput(protocol.ProcessProtocol): self.transport.write(self.process_input) self.transport.closeStdin() -def get_process_output(executable, args=None, env=None, path=None, - process_reactor=None, check_exit_code=True, +def get_process_output(executable, args=None, env=None, path=None, + process_reactor=None, check_exit_code=True, process_input=None, started_deferred=None, terminate_on_stderr=False): if process_reactor is None: @@ -130,8 +130,8 @@ def get_process_output(executable, args=None, env=None, path=None, process_handler = BackRelayWithInput( deferred, cmd, - started_deferred=started_deferred, - check_exit_code=check_exit_code, + started_deferred=started_deferred, + check_exit_code=check_exit_code, process_input=process_input, terminate_on_stderr=terminate_on_stderr) # NOTE(vish): commands come in as unicode, but self.executes needs @@ -139,7 +139,7 @@ def get_process_output(executable, args=None, env=None, path=None, executable = str(executable) if not args is None: args = [str(x) for x in args] - process_reactor.spawnProcess( process_handler, executable, + process_reactor.spawnProcess( process_handler, executable, (executable,)+tuple(args), env, path) return deferred From f45f576d487c74eceb86df3f58718431dcf5b81a Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Tue, 7 Sep 2010 18:32:31 -0700 Subject: [PATCH 22/87] one more whitespace fix --- nova/process.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/process.py b/nova/process.py index 259e6235..c3b077dc 100644 --- a/nova/process.py +++ b/nova/process.py @@ -139,8 +139,8 @@ def get_process_output(executable, args=None, env=None, path=None, executable = str(executable) if not args is None: args = [str(x) for x in args] - process_reactor.spawnProcess( process_handler, executable, - (executable,)+tuple(args), env, path) + process_reactor.spawnProcess(process_handler, executable, + (executable,)+tuple(args), env, path) return deferred From 0a7f6ad0ad20836a92a9e0b0c4811b3cbd1e5f01 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Tue, 7 Sep 2010 21:53:40 -0700 Subject: [PATCH 23/87] a few formatting fixes and moved exception --- nova/process.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/process.py b/nova/process.py index c3b077dc..5a5d8cbd 100644 --- a/nova/process.py +++ b/nova/process.py @@ -29,7 +29,7 @@ from twisted.internet import protocol from twisted.internet import reactor from nova import flags -from nova.utils import ProcessExecutionError +from nova.exception import ProcessExecutionError FLAGS = flags.FLAGS flags.DEFINE_integer('process_pool_size', 4, @@ -126,7 +126,7 @@ def get_process_output(executable, args=None, env=None, path=None, deferred = defer.Deferred() cmd = executable if args: - cmd = cmd + " " + ' '.join(args) + cmd = " ".join([cmd] + args) process_handler = BackRelayWithInput( deferred, cmd, From 1f671ecde14c010fbb267a61eba0a094452df1cf Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Wed, 8 Sep 2010 01:53:07 -0700 Subject: [PATCH 25/87] make timestamps for instances and volumes, includes additions to get deleted objects from db using deleted flag. --- nova/tests/compute_unittest.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/nova/tests/compute_unittest.py b/nova/tests/compute_unittest.py index 746c035d..e5da6b05 100644 --- a/nova/tests/compute_unittest.py +++ b/nova/tests/compute_unittest.py @@ -18,6 +18,8 @@ """ Tests For Compute """ + +import datetime import logging from twisted.internet import defer @@ -79,6 +81,24 @@ class ComputeTestCase(test.TrialTestCase): logging.info("After terminating instances: %s", instances) self.assertEqual(len(instances), 0) + @defer.inlineCallbacks + def test_run_terminate_timestamps(self): + """Make sure it is possible to run and terminate instance""" + instance_id = self._create_instance() + instance_ref = db.instance_get(self.context, instance_id) + self.assertEqual(instance_ref['launched_at'], None) + self.assertEqual(instance_ref['terminated_at'], None) + launch = datetime.datetime.now() + yield self.compute.run_instance(self.context, instance_id) + instance_ref = db.instance_get(self.context, instance_id) + self.assert_(instance_ref['launched_at'] > launch) + self.assertEqual(instance_ref['terminated_at'], None) + terminate = datetime.datetime.now() + yield self.compute.terminate_instance(self.context, instance_id) + instance_ref = db.instance_get({'deleted': True}, instance_id) + self.assert_(instance_ref['launched_at'] < terminate) + self.assert_(instance_ref['terminated_at'] > terminate) + @defer.inlineCallbacks def test_reboot(self): """Ensure instance can be rebooted""" From 5832292b8127c5b047e36ef5603ad14b1a16562d Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 02:30:07 -0700 Subject: [PATCH 26/87] put soren's fancy path code in scheduler bin as well --- bin/nova-scheduler | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bin/nova-scheduler b/bin/nova-scheduler index 97f98b17..38a8f213 100755 --- a/bin/nova-scheduler +++ b/bin/nova-scheduler @@ -21,6 +21,17 @@ Twistd daemon for the nova scheduler nodes. """ +import os +import sys + +# If ../nova/__init__.py exists, add ../ to Python search path, so that +# it will override what happens to be installed in /usr/(local/)lib/python... +possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), + os.pardir, + os.pardir)) +if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): + sys.path.insert(0, possible_topdir) + from nova import service from nova import twistd From 759fc8b287ff3c98c2d635273c2485fbf4dcdf38 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 06:06:29 -0700 Subject: [PATCH 27/87] switch to using utcnow --- nova/tests/compute_unittest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/tests/compute_unittest.py b/nova/tests/compute_unittest.py index e5da6b05..8a7f7b64 100644 --- a/nova/tests/compute_unittest.py +++ b/nova/tests/compute_unittest.py @@ -88,12 +88,12 @@ class ComputeTestCase(test.TrialTestCase): instance_ref = db.instance_get(self.context, instance_id) self.assertEqual(instance_ref['launched_at'], None) self.assertEqual(instance_ref['terminated_at'], None) - launch = datetime.datetime.now() + launch = datetime.datetime.utcnow() yield self.compute.run_instance(self.context, instance_id) instance_ref = db.instance_get(self.context, instance_id) self.assert_(instance_ref['launched_at'] > launch) self.assertEqual(instance_ref['terminated_at'], None) - terminate = datetime.datetime.now() + terminate = datetime.datetime.utcnow() yield self.compute.terminate_instance(self.context, instance_id) instance_ref = db.instance_get({'deleted': True}, instance_id) self.assert_(instance_ref['launched_at'] < terminate) From 305e3b52e94df333e2d832afd8e0bdbdc6910188 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 06:55:54 -0700 Subject: [PATCH 28/87] speed up describe by loading fixed and floating ips --- nova/endpoint/cloud.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 709c967b..6958eacf 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -379,11 +379,11 @@ class CloudController(object): 'code': instance['state'], 'name': instance['state_description'] } - floating_addr = db.instance_get_floating_address(context, - instance['id']) + floating_addr = None + if instance['fixed_ip']['floating_ips']: + floating_addr = instance['fixed_ip']['floating_ips'][0]['str_id'] i['publicDnsName'] = floating_addr - fixed_addr = db.instance_get_fixed_address(context, - instance['id']) + fixed_addr = instance['fixed_ip']['str_id'] i['privateDnsName'] = fixed_addr if not i['publicDnsName']: i['publicDnsName'] = i['privateDnsName'] From cc957e2398a4859ae07ac83018fd811023cacb72 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 07:47:30 -0700 Subject: [PATCH 29/87] floating ip commands --- bin/nova-manage | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/bin/nova-manage b/bin/nova-manage index ecef5d55..408a2d9c 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -26,6 +26,8 @@ import os import sys import time +import IPy + # If ../nova/__init__.py exists, add ../ to Python search path, so that # it will override what happens to be installed in /usr/(local/)lib/python... possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), @@ -218,12 +220,41 @@ class ProjectCommands(object): with open(filename, 'w') as f: f.write(zip_file) +class FloatingIpCommands(object): + """Class for managing floating ip.""" + + def create(self, host, range): + """Creates floating ips for host by range + arguments: host ip_range""" + for address in IPy.IP(range): + db.floating_ip_create(None, {'address': str(address), + 'host': host}) + + def delete(self, ip_range): + """Deletes floating ips by range + arguments: range""" + for address in IPy.IP(ip_range): + db.floating_ip_destroy(None, str(address)) + + + def list(self, host=None): + """Lists all floating ips (optionally by host) + arguments: [host]""" + if host == None: + floating_ips = db.floating_ip_get_all(None) + else: + floating_ips = db.floating_ip_get_all_by_host(None, host) + for floating_ip in floating_ips: + print "%s\t%s\ti-%s" % (floating_ip['host'], + floating_ip['address'], + floating_ip['instance_id']) CATEGORIES = [ ('user', UserCommands), ('project', ProjectCommands), ('role', RoleCommands), ('vpn', VpnCommands), + ('floating', FloatingIpCommands) ] From 444b02f4e7ce1d79b04416766a99228dcd22199d Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 07:53:57 -0700 Subject: [PATCH 30/87] list command for floating ips --- bin/nova-manage | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/nova-manage b/bin/nova-manage index 408a2d9c..56191252 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -245,9 +245,12 @@ class FloatingIpCommands(object): else: floating_ips = db.floating_ip_get_all_by_host(None, host) for floating_ip in floating_ips: - print "%s\t%s\ti-%s" % (floating_ip['host'], - floating_ip['address'], - floating_ip['instance_id']) + instance = None + if floating_ip['fixed_ip']: + instance = floating_ip['fixed_ip']['instance']['str_id'] + print "%s\t%s\t%s" % (floating_ip['host'], + floating_ip['address'], + instance) CATEGORIES = [ ('user', UserCommands), From fd037cc02799b4f484fe739d08e00e8b2aed56fd Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 09:59:55 -0700 Subject: [PATCH 31/87] fix volume delete issue and volume hostname display --- nova/endpoint/cloud.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 709c967b..c7355ccd 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -260,7 +260,7 @@ class CloudController(object): v['status'] = '%s (%s, %s, %s, %s)' % ( volume['status'], volume['user_id'], - 'host', + volume['host'], volume['instance_id'], volume['mountpoint']) if volume['attach_status'] == 'attached': @@ -635,7 +635,7 @@ class CloudController(object): # TODO: return error if not authorized volume_ref = db.volume_get_by_str(context, volume_id) host = db.volume_get_host(context, volume_ref['id']) - rpc.cast(db.queue_get_for(context, FLAGS.compute_topic, host), + rpc.cast(db.queue_get_for(context, FLAGS.volume_topic, host), {"method": "delete_volume", "args": {"context": None, "volume_id": volume_id}}) From f4616bfad8a74f70c764f1bdd3955633b7fe22ae Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 10:38:32 -0700 Subject: [PATCH 32/87] pass volume['id'] instead of string id to delete volume --- nova/endpoint/cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index c7355ccd..cb625bfa 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -638,7 +638,7 @@ class CloudController(object): rpc.cast(db.queue_get_for(context, FLAGS.volume_topic, host), {"method": "delete_volume", "args": {"context": None, - "volume_id": volume_id}}) + "volume_id": volume_ref['id']}}) return defer.succeed(True) @rbac.allow('all') From b97fb42e3f0c390eead4c508bdd3c8b4af889c25 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 10:43:19 -0700 Subject: [PATCH 33/87] remove extraneous get_host calls that were requiring an extra db trip --- nova/endpoint/cloud.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index cb625bfa..6cda7940 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -299,7 +299,7 @@ class CloudController(object): if volume_ref['attach_status'] == "attached": raise exception.ApiError("Volume is already attached") instance_ref = db.instance_get_by_str(context, instance_id) - host = db.instance_get_host(context, instance_ref['id']) + host = instance_ref['host'] rpc.cast(db.queue_get_for(context, FLAGS.compute_topic, host), {"method": "attach_volume", "args": {"context": None, @@ -323,7 +323,7 @@ class CloudController(object): if volume_ref['status'] == "available": raise exception.Error("Volume is already detached") try: - host = db.instance_get_host(context, instance_ref['id']) + host = instance_ref['host'] rpc.cast(db.queue_get_for(context, FLAGS.compute_topic, host), {"method": "detach_volume", "args": {"context": None, @@ -483,7 +483,7 @@ class CloudController(object): def _get_network_topic(self, context): """Retrieves the network host for a project""" network_ref = db.project_get_network(context, context.project.id) - host = db.network_get_host(context, network_ref['id']) + host = network_ref['host'] if not host: host = yield rpc.call(FLAGS.network_topic, {"method": "set_network_host", @@ -608,7 +608,7 @@ class CloudController(object): # we will need to cast here. db.fixed_ip_deallocate(context, address) - host = db.instance_get_host(context, instance_ref['id']) + host = instance_ref['host'] if host: rpc.cast(db.queue_get_for(context, FLAGS.compute_topic, host), {"method": "terminate_instance", @@ -623,7 +623,7 @@ class CloudController(object): """instance_id is a list of instance ids""" for id_str in instance_id: instance_ref = db.instance_get_by_str(context, id_str) - host = db.instance_get_host(context, instance_ref['id']) + host = instance_ref['host'] rpc.cast(db.queue_get_for(context, FLAGS.compute_topic, host), {"method": "reboot_instance", "args": {"context": None, @@ -634,7 +634,7 @@ class CloudController(object): def delete_volume(self, context, volume_id, **kwargs): # TODO: return error if not authorized volume_ref = db.volume_get_by_str(context, volume_id) - host = db.volume_get_host(context, volume_ref['id']) + host = volume_ref['host'] rpc.cast(db.queue_get_for(context, FLAGS.volume_topic, host), {"method": "delete_volume", "args": {"context": None, From a0cefa81004f87a58702d8b300bf65616b670371 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 11:02:37 -0700 Subject: [PATCH 34/87] fix describe addresses --- nova/endpoint/cloud.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 6cda7940..9a09454a 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -420,10 +420,12 @@ class CloudController(object): iterator = db.floating_ip_get_by_project(context, context.project.id) for floating_ip_ref in iterator: - address = floating_ip_ref['id_str'] - instance_ref = db.floating_ip_get_instance(address) + address = floating_ip_ref['str_id'] + instance_id = None + if floating_ip_ref['instance']: + instance_id = floating_ip_ref['instance']['str_id'] address_rv = {'public_ip': address, - 'instance_id': instance_ref['id_str']} + 'instance_id': instance_id} if context.user.is_admin(): details = "%s (%s)" % (address_rv['instance_id'], floating_ip_ref['project_id']) From 8988d72b329e110737e482ef607f4fda74b13242 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 11:07:10 -0700 Subject: [PATCH 35/87] solution that works with this version --- nova/endpoint/cloud.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 9a09454a..f84360c9 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -421,9 +421,8 @@ class CloudController(object): context.project.id) for floating_ip_ref in iterator: address = floating_ip_ref['str_id'] - instance_id = None - if floating_ip_ref['instance']: - instance_id = floating_ip_ref['instance']['str_id'] + instance_ref = db.floating_ip_get_instance(context, address) + instance_id = instance_ref['str_id'] address_rv = {'public_ip': address, 'instance_id': instance_id} if context.user.is_admin(): From bfe560870f94c04ef37c73b34bf75ac2dac36841 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 11:17:14 -0700 Subject: [PATCH 36/87] faster describe_addresses --- nova/endpoint/cloud.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 2866474e..26bae065 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -421,8 +421,10 @@ class CloudController(object): context.project.id) for floating_ip_ref in iterator: address = floating_ip_ref['str_id'] - instance_ref = db.floating_ip_get_instance(context, address) - instance_id = instance_ref['str_id'] + instance_id = None + if (floating_ip_ref['fixed_ip'] + and floating_ip_ref['fixed_ip']['instance']): + instance_id = floating_ip_ref['fixed_ip']['instance']['str_id'] address_rv = {'public_ip': address, 'instance_id': instance_id} if context.user.is_admin(): From b7befd5d121eaad561382ff0f52eccc3a7f7a133 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 12:38:33 -0700 Subject: [PATCH 37/87] floating_address is the name for the cast --- nova/endpoint/cloud.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 2866474e..bb24d1f0 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -449,9 +449,9 @@ class CloudController(object): floating_ip_ref = db.floating_ip_get_by_address(context, public_ip) network_topic = yield self._get_network_topic(context) rpc.cast(network_topic, - {"method": "deallocate_floating_ip", - "args": {"context": None, - "floating_ip": floating_ip_ref['str_id']}}) + {"method": "deallocate_floating_ip", + "args": {"context": None, + "floating_address": floating_ip_ref['str_id']}}) defer.returnValue({'releaseResponse': ["Address released."]}) @rbac.allow('netadmin') @@ -462,11 +462,11 @@ class CloudController(object): floating_ip_ref = db.floating_ip_get_by_address(context, public_ip) network_topic = yield self._get_network_topic(context) rpc.cast(network_topic, - {"method": "associate_floating_ip", - "args": {"context": None, - "floating_ip": floating_ip_ref['str_id'], - "fixed_ip": fixed_ip_ref['str_id'], - "instance_id": instance_ref['id']}}) + {"method": "associate_floating_ip", + "args": {"context": None, + "floating_address": floating_ip_ref['str_id'], + "fixed_address": fixed_ip_ref['str_id'], + "instance_id": instance_ref['id']}}) defer.returnValue({'associateResponse': ["Address associated."]}) @rbac.allow('netadmin') @@ -475,9 +475,9 @@ class CloudController(object): floating_ip_ref = db.floating_ip_get_by_address(context, public_ip) network_topic = yield self._get_network_topic(context) rpc.cast(network_topic, - {"method": "disassociate_floating_ip", - "args": {"context": None, - "floating_ip": floating_ip_ref['str_id']}}) + {"method": "disassociate_floating_ip", + "args": {"context": None, + "floating_address": floating_ip_ref['str_id']}}) defer.returnValue({'disassociateResponse': ["Address disassociated."]}) @defer.inlineCallbacks @@ -487,9 +487,9 @@ class CloudController(object): host = network_ref['host'] if not host: host = yield rpc.call(FLAGS.network_topic, - {"method": "set_network_host", - "args": {"context": None, - "project_id": context.project.id}}) + {"method": "set_network_host", + "args": {"context": None, + "project_id": context.project.id}}) defer.returnValue(db.queue_get_for(context, FLAGS.network_topic, host)) @rbac.allow('projectmanager', 'sysadmin') From fcb450d316beebdd610516fed6094df00c9c1d7a Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 12:45:51 -0700 Subject: [PATCH 38/87] don't need to pass instance_id to network on associate --- nova/endpoint/cloud.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index bb24d1f0..397c9c55 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -465,8 +465,7 @@ class CloudController(object): {"method": "associate_floating_ip", "args": {"context": None, "floating_address": floating_ip_ref['str_id'], - "fixed_address": fixed_ip_ref['str_id'], - "instance_id": instance_ref['id']}}) + "fixed_address": fixed_ip_ref['str_id']}}) defer.returnValue({'associateResponse': ["Address associated."]}) @rbac.allow('netadmin') From 0b434ac2678c854e7822736fb9567fdf707f304c Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 20:10:31 -0700 Subject: [PATCH 39/87] database support for quotas --- nova/endpoint/cloud.py | 53 ++++++++++++++++++++++++++++++---- nova/tests/compute_unittest.py | 1 + run_tests.py | 1 + 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 2866474e..b8a00075 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -32,6 +32,7 @@ from twisted.internet import defer from nova import db from nova import exception from nova import flags +from nova import quota from nova import rpc from nova import utils from nova.auth import rbac @@ -44,6 +45,11 @@ FLAGS = flags.FLAGS flags.DECLARE('storage_availability_zone', 'nova.volume.manager') +class QuotaError(exception.ApiError): + """Quota Exceeeded""" + pass + + def _gen_key(user_id, key_name): """ Tuck this into AuthManager """ try: @@ -276,6 +282,14 @@ class CloudController(object): @rbac.allow('projectmanager', 'sysadmin') def create_volume(self, context, size, **kwargs): + # check quota + size = int(size) + if quota.allowed_volumes(context, 1, size) < 1: + logging.warn("Quota exceeeded for %s, tried to create %sG volume", + context.project.id, size) + raise QuotaError("Volume quota exceeded. You cannot " + "create a volume of size %s" % + size) vol = {} vol['size'] = size vol['user_id'] = context.user.id @@ -435,6 +449,12 @@ class CloudController(object): @rbac.allow('netadmin') @defer.inlineCallbacks def allocate_address(self, context, **kwargs): + # check quota + if quota.allowed_floating_ips(context, 1) < 1: + logging.warn("Quota exceeeded for %s, tried to allocate address", + context.project.id) + raise QuotaError("Address quota exceeded. You cannot " + "allocate any more addresses") network_topic = yield self._get_network_topic(context) public_ip = yield rpc.call(network_topic, {"method": "allocate_floating_ip", @@ -487,14 +507,30 @@ class CloudController(object): host = network_ref['host'] if not host: host = yield rpc.call(FLAGS.network_topic, - {"method": "set_network_host", - "args": {"context": None, - "project_id": context.project.id}}) + {"method": "set_network_host", + "args": {"context": None, + "project_id": context.project.id}}) defer.returnValue(db.queue_get_for(context, FLAGS.network_topic, host)) @rbac.allow('projectmanager', 'sysadmin') @defer.inlineCallbacks def run_instances(self, context, **kwargs): + instance_type = kwargs.get('instance_type', 'm1.small') + if instance_type not in INSTANCE_TYPES: + raise exception.ApiError("Unknown instance type: %s", + instance_type) + # check quota + max_instances = int(kwargs.get('max_count', 1)) + min_instances = int(kwargs.get('min_count', max_instances)) + num_instances = quota.allowed_instances(context, + max_instances, + instance_type) + if num_instances < min_instances: + logging.warn("Quota exceeeded for %s, tried to run %s instances", + context.project.id, min_instances) + raise QuotaError("Instance quota exceeded. You can only " + "run %s more instances of this type." % + num_instances) # make sure user can access the image # vpn image is private so it doesn't show up on lists vpn = kwargs['image_id'] == FLAGS.vpn_image_id @@ -516,7 +552,7 @@ class CloudController(object): images.get(context, kernel_id) images.get(context, ramdisk_id) - logging.debug("Going to run instances...") + logging.debug("Going to run %s instances...", num_instances) launch_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()) key_data = None if kwargs.has_key('key_name'): @@ -540,10 +576,15 @@ class CloudController(object): base_options['user_id'] = context.user.id base_options['project_id'] = context.project.id base_options['user_data'] = kwargs.get('user_data', '') - base_options['instance_type'] = kwargs.get('instance_type', 'm1.small') base_options['security_group'] = security_group + base_options['instance_type'] = instance_type - for num in range(int(kwargs['max_count'])): + type_data = INSTANCE_TYPES['instance_type'] + base_options['memory_mb'] = type_data['memory_mb'] + base_options['vcpus'] = type_data['vcpus'] + base_options['local_gb'] = type_data['local_gb'] + + for num in range(): inst_id = db.instance_create(context, base_options) inst = {} diff --git a/nova/tests/compute_unittest.py b/nova/tests/compute_unittest.py index 8a7f7b64..b45367eb 100644 --- a/nova/tests/compute_unittest.py +++ b/nova/tests/compute_unittest.py @@ -50,6 +50,7 @@ class ComputeTestCase(test.TrialTestCase): def tearDown(self): # pylint: disable-msg=C0103 self.manager.delete_user(self.user) self.manager.delete_project(self.project) + super(ComputeTestCase, self).tearDown() def _create_instance(self): """Create a test instance""" diff --git a/run_tests.py b/run_tests.py index d5dc5f93..73bf57f9 100644 --- a/run_tests.py +++ b/run_tests.py @@ -58,6 +58,7 @@ from nova.tests.flags_unittest import * from nova.tests.network_unittest import * from nova.tests.objectstore_unittest import * from nova.tests.process_unittest import * +from nova.tests.quota_unittest import * from nova.tests.rpc_unittest import * from nova.tests.service_unittest import * from nova.tests.validator_unittest import * From 7f99bf70d7b92e319b0c0b236a3e56a561918550 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 21:29:00 -0700 Subject: [PATCH 40/87] kwargs don't work if you prepend an underscore --- nova/scheduler/simple.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/nova/scheduler/simple.py b/nova/scheduler/simple.py index ea4eef98..e53e9fa7 100644 --- a/nova/scheduler/simple.py +++ b/nova/scheduler/simple.py @@ -37,7 +37,7 @@ flags.DEFINE_integer("max_networks", 1000, class SimpleScheduler(chance.ChanceScheduler): """Implements Naive Scheduler that tries to find least loaded host.""" - def schedule_run_instance(self, context, _instance_id, *_args, **_kwargs): + def schedule_run_instance(self, context, *_args, **_kwargs): """Picks a host that is up and has the fewest running instances.""" results = db.service_get_all_compute_sorted(context) @@ -49,7 +49,7 @@ class SimpleScheduler(chance.ChanceScheduler): return service['host'] raise driver.NoValidHost("No hosts found") - def schedule_create_volume(self, context, _volume_id, *_args, **_kwargs): + def schedule_create_volume(self, context, *_args, **_kwargs): """Picks a host that is up and has the fewest volumes.""" results = db.service_get_all_volume_sorted(context) @@ -61,8 +61,7 @@ class SimpleScheduler(chance.ChanceScheduler): return service['host'] raise driver.NoValidHost("No hosts found") - def schedule_set_network_host(self, context, _network_id, - *_args, **_kwargs): + def schedule_set_network_host(self, context, *_args, **_kwargs): """Picks a host that is up and has the fewest networks.""" results = db.service_get_all_network_sorted(context) From ab67a0c86a473c5d30da2c127d661b2f91483d22 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 21:42:18 -0700 Subject: [PATCH 41/87] add missing files for quota --- nova/tests/quota_unittest.py | 127 +++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 nova/tests/quota_unittest.py diff --git a/nova/tests/quota_unittest.py b/nova/tests/quota_unittest.py new file mode 100644 index 00000000..bf3506c7 --- /dev/null +++ b/nova/tests/quota_unittest.py @@ -0,0 +1,127 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. + +import logging + +from nova import db +from nova import flags +from nova import quota +from nova import test +from nova import utils +from nova.auth import manager +from nova.endpoint import cloud +from nova.endpoint import api + + +FLAGS = flags.FLAGS + + +class QuotaTestCase(test.TrialTestCase): + def setUp(self): # pylint: disable-msg=C0103 + logging.getLogger().setLevel(logging.DEBUG) + super(QuotaTestCase, self).setUp() + self.flags(connection_type='fake', + quota_instances=2, + quota_cores=4, + quota_volumes=2, + quota_gigabytes=20, + quota_floating_ips=2) + + self.cloud = cloud.CloudController() + self.manager = manager.AuthManager() + self.user = self.manager.create_user('admin', 'admin', 'admin', True) + self.project = self.manager.create_project('admin', 'admin', 'admin') + self.context = api.APIRequestContext(handler=None, + project=self.project, + user=self.user) + + def tearDown(self): # pylint: disable-msg=C0103 + manager.AuthManager().delete_project(self.project) + manager.AuthManager().delete_user(self.user) + super(QuotaTestCase, self).tearDown() + + def _create_instance(self, cores=2): + """Create a test instance""" + inst = {} + inst['image_id'] = 'ami-test' + inst['reservation_id'] = 'r-fakeres' + inst['user_id'] = self.user.id + inst['project_id'] = self.project.id + inst['instance_type'] = 'm1.large' + inst['vcpus'] = cores + inst['mac_address'] = utils.generate_mac() + return db.instance_create(self.context, inst) + + def _create_volume(self, size=10): + """Create a test volume""" + vol = {} + vol['user_id'] = self.user.id + vol['project_id'] = self.project.id + vol['size'] = size + return db.volume_create(self.context, vol)['id'] + + def test_quota_overrides(self): + """Make sure overriding a projects quotas works""" + num_instances = quota.allowed_instances(self.context, 100, 'm1.small') + self.assertEqual(num_instances, 2) + db.quota_create(self.context, {'project_id': self.project.id, + 'instances': 10}) + num_instances = quota.allowed_instances(self.context, 100, 'm1.small') + self.assertEqual(num_instances, 4) + db.quota_update(self.context, self.project.id, {'cores': 100}) + num_instances = quota.allowed_instances(self.context, 100, 'm1.small') + self.assertEqual(num_instances, 10) + db.quota_destroy(self.context, self.project.id) + + def test_too_many_instances(self): + instance_ids = [] + for i in range(FLAGS.quota_instances): + instance_id = self._create_instance() + instance_ids.append(instance_id) + self.assertFailure(self.cloud.run_instances(self.context, + min_count=1, + max_count=1, + instance_type='m1.small'), + cloud.QuotaError) + for instance_id in instance_ids: + db.instance_destroy(self.context, instance_id) + + def test_too_many_cores(self): + instance_ids = [] + instance_id = self._create_instance(cores=4) + instance_ids.append(instance_id) + self.assertFailure(self.cloud.run_instances(self.context, + min_count=1, + max_count=1, + instance_type='m1.small'), + cloud.QuotaError) + for instance_id in instance_ids: + db.instance_destroy(self.context, instance_id) + + def test_too_many_volumes(self): + volume_ids = [] + for i in range(FLAGS.quota_volumes): + volume_id = self._create_volume() + volume_ids.append(volume_id) + self.assertRaises(cloud.QuotaError, + self.cloud.create_volume, + self.context, + size=10) + for volume_id in volume_ids: + db.volume_destroy(self.context, volume_id) + From d54cb543a3925dbd177baa732245367b1b4e3bc0 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 21:52:06 -0700 Subject: [PATCH 42/87] make the db creates return refs instead of ids --- nova/endpoint/cloud.py | 2 +- nova/tests/compute_unittest.py | 2 +- nova/tests/network_unittest.py | 8 ++++---- nova/tests/service_unittest.py | 4 ++-- nova/tests/volume_unittest.py | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 397c9c55..7f4a901c 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -543,7 +543,7 @@ class CloudController(object): base_options['security_group'] = security_group for num in range(int(kwargs['max_count'])): - inst_id = db.instance_create(context, base_options) + inst_id = db.instance_create(context, base_options)['id'] inst = {} inst['mac_address'] = utils.generate_mac() diff --git a/nova/tests/compute_unittest.py b/nova/tests/compute_unittest.py index 8a7f7b64..de2bf3d3 100644 --- a/nova/tests/compute_unittest.py +++ b/nova/tests/compute_unittest.py @@ -62,7 +62,7 @@ class ComputeTestCase(test.TrialTestCase): inst['instance_type'] = 'm1.tiny' inst['mac_address'] = utils.generate_mac() inst['ami_launch_index'] = 0 - return db.instance_create(self.context, inst) + return db.instance_create(self.context, inst)['id'] @defer.inlineCallbacks def test_run_terminate(self): diff --git a/nova/tests/network_unittest.py b/nova/tests/network_unittest.py index a89f1d62..9958600e 100644 --- a/nova/tests/network_unittest.py +++ b/nova/tests/network_unittest.py @@ -56,12 +56,12 @@ class NetworkTestCase(test.TrialTestCase): name)) # create the necessary network data for the project self.network.set_network_host(self.context, self.projects[i].id) - instance_id = db.instance_create(None, + instance_ref = db.instance_create(None, {'mac_address': utils.generate_mac()}) - self.instance_id = instance_id - instance_id = db.instance_create(None, + self.instance_id = instance_ref['id'] + instance_ref = db.instance_create(None, {'mac_address': utils.generate_mac()}) - self.instance2_id = instance_id + self.instance2_id = instance_ref['id'] def tearDown(self): # pylint: disable-msg=C0103 super(NetworkTestCase, self).tearDown() diff --git a/nova/tests/service_unittest.py b/nova/tests/service_unittest.py index 097a045e..01da0eb8 100644 --- a/nova/tests/service_unittest.py +++ b/nova/tests/service_unittest.py @@ -87,7 +87,7 @@ class ServiceTestCase(test.BaseTestCase): host, binary).AndRaise(exception.NotFound()) service.db.service_create(None, - service_create).AndReturn(service_ref['id']) + service_create).AndReturn(service_ref) self.mox.ReplayAll() app = service.Service.create(host=host, binary=binary) @@ -131,7 +131,7 @@ class ServiceTestCase(test.BaseTestCase): host, binary).AndRaise(exception.NotFound()) service.db.service_create(None, - service_create).AndReturn(service_ref['id']) + service_create).AndReturn(service_ref) service.db.service_get(None, service_ref['id']).AndReturn(service_ref) service.db.service_update(None, service_ref['id'], mox.ContainsKeyValue('report_count', 1)) diff --git a/nova/tests/volume_unittest.py b/nova/tests/volume_unittest.py index 9e35d2a1..1d665b50 100644 --- a/nova/tests/volume_unittest.py +++ b/nova/tests/volume_unittest.py @@ -108,7 +108,7 @@ class VolumeTestCase(test.TrialTestCase): inst['instance_type'] = 'm1.tiny' inst['mac_address'] = utils.generate_mac() inst['ami_launch_index'] = 0 - instance_id = db.instance_create(self.context, inst) + instance_id = db.instance_create(self.context, inst)['id'] mountpoint = "/dev/sdf" volume_id = self._create_volume() yield self.volume.create_volume(self.context, volume_id) From 3ecd942ee8c126cf852c50fc188ed2bfedf50c57 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 21:56:46 -0700 Subject: [PATCH 43/87] fix rare condition where describe is called before instance has an ip --- nova/endpoint/cloud.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 281c4535..5ff69edf 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -362,12 +362,14 @@ class CloudController(object): def _format_instances(self, context, reservation_id=None): reservations = {} if reservation_id: - instances = db.instance_get_by_reservation(context, reservation_id) + instances = db.instance_get_by_reservation(context, + reservation_id) else: if not context.user.is_admin(): instances = db.instance_get_all(context) else: - instances = db.instance_get_by_project(context, context.project.id) + instances = db.instance_get_by_project(context, + context.project.id) for instance in instances: if not context.user.is_admin(): if instance['image_id'] == FLAGS.vpn_image_id: @@ -379,12 +381,15 @@ class CloudController(object): 'code': instance['state'], 'name': instance['state_description'] } + fixed_addr = None floating_addr = None - if instance['fixed_ip']['floating_ips']: - floating_addr = instance['fixed_ip']['floating_ips'][0]['str_id'] - i['publicDnsName'] = floating_addr - fixed_addr = instance['fixed_ip']['str_id'] + if instance['fixed_ip']: + fixed_addr = instance['fixed_ip']['str_id'] + if instance['fixed_ip']['floating_ips']: + fixed = instance['fixed_ip'] + floating_addr = fixed['floating_ips'][0]['str_id'] i['privateDnsName'] = fixed_addr + i['publicDnsName'] = floating_addr if not i['publicDnsName']: i['publicDnsName'] = i['privateDnsName'] i['dnsName'] = None From c0f3fbc02bd34812f70d80c20065b2d70b6c32a2 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 21:59:09 -0700 Subject: [PATCH 44/87] fix unittest --- nova/tests/scheduler_unittest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/tests/scheduler_unittest.py b/nova/tests/scheduler_unittest.py index 51b9aeaa..09e45ea6 100644 --- a/nova/tests/scheduler_unittest.py +++ b/nova/tests/scheduler_unittest.py @@ -109,7 +109,7 @@ class SimpleDriverTestCase(test.TrialTestCase): inst['instance_type'] = 'm1.tiny' inst['mac_address'] = utils.generate_mac() inst['ami_launch_index'] = 0 - return db.instance_create(self.context, inst) + return db.instance_create(self.context, inst)['id'] def test_hosts_are_up(self): # NOTE(vish): constructing service without create method From 374ceeeb7bd7b8585367998bac0d7747d2811a5f Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 22:13:38 -0700 Subject: [PATCH 45/87] quota tests --- nova/endpoint/cloud.py | 1 + nova/tests/quota_unittest.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 5209ec90..b5ac5be4 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -450,6 +450,7 @@ class CloudController(object): @defer.inlineCallbacks def allocate_address(self, context, **kwargs): # check quota + print quota.allowed_floating_ips(context, 1) if quota.allowed_floating_ips(context, 1) < 1: logging.warn("Quota exceeeded for %s, tried to allocate address", context.project.id) diff --git a/nova/tests/quota_unittest.py b/nova/tests/quota_unittest.py index e3f23b84..d7c07bfa 100644 --- a/nova/tests/quota_unittest.py +++ b/nova/tests/quota_unittest.py @@ -19,6 +19,7 @@ import logging from nova import db +from nova import exception from nova import flags from nova import quota from nova import test @@ -46,6 +47,7 @@ class QuotaTestCase(test.TrialTestCase): self.manager = manager.AuthManager() self.user = self.manager.create_user('admin', 'admin', 'admin', True) self.project = self.manager.create_project('admin', 'admin', 'admin') + self.network = utils.import_object(FLAGS.network_manager) self.context = api.APIRequestContext(handler=None, project=self.project, user=self.user) @@ -125,3 +127,26 @@ class QuotaTestCase(test.TrialTestCase): for volume_id in volume_ids: db.volume_destroy(self.context, volume_id) + def test_too_many_gigabytes(self): + volume_ids = [] + volume_id = self._create_volume(size=20) + volume_ids.append(volume_id) + self.assertRaises(cloud.QuotaError, + self.cloud.create_volume, + self.context, + size=10) + for volume_id in volume_ids: + db.volume_destroy(self.context, volume_id) + + def test_too_many_addresses(self): + address = '192.168.0.100' + try: + db.floating_ip_get_by_address(None, address) + except exception.NotFound: + db.floating_ip_create(None, {'address': address, + 'host': FLAGS.host}) + #float_addr = self.network.allocate_floating_ip(self.context, + # self.project.id) + self.assertFailure(self.cloud.allocate_address(self.context), + cloud.QuotaError) + From c7d98b5ca37998781c84694e5acdf355356114d3 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 22:53:31 -0700 Subject: [PATCH 46/87] address test almost works --- nova/tests/quota_unittest.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/nova/tests/quota_unittest.py b/nova/tests/quota_unittest.py index d7c07bfa..9d697ccd 100644 --- a/nova/tests/quota_unittest.py +++ b/nova/tests/quota_unittest.py @@ -41,7 +41,7 @@ class QuotaTestCase(test.TrialTestCase): quota_cores=4, quota_volumes=2, quota_gigabytes=20, - quota_floating_ips=2) + quota_floating_ips=1) self.cloud = cloud.CloudController() self.manager = manager.AuthManager() @@ -145,8 +145,18 @@ class QuotaTestCase(test.TrialTestCase): except exception.NotFound: db.floating_ip_create(None, {'address': address, 'host': FLAGS.host}) - #float_addr = self.network.allocate_floating_ip(self.context, - # self.project.id) + float_addr = self.network.allocate_floating_ip(self.context, + self.project.id) + # NOTE(vish): This assert doesn't work. When cloud attempts to + # make an rpc.call, the test just finishes with OK. It + # appears to be something in the magic inline callbacks + # that is breaking. self.assertFailure(self.cloud.allocate_address(self.context), cloud.QuotaError) + try: + yield self.cloud.allocate_address(self.context) + self.fail('Should have raised QuotaError') + except cloud.QuotaError: + pass + From 19d7b97bb2ec99c93b826e5e4496abccb19a7abb Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 23:04:30 -0700 Subject: [PATCH 47/87] quotas working and tests passing --- nova/endpoint/cloud.py | 1 - nova/tests/quota_unittest.py | 9 +-------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index b5ac5be4..5209ec90 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -450,7 +450,6 @@ class CloudController(object): @defer.inlineCallbacks def allocate_address(self, context, **kwargs): # check quota - print quota.allowed_floating_ips(context, 1) if quota.allowed_floating_ips(context, 1) < 1: logging.warn("Quota exceeeded for %s, tried to allocate address", context.project.id) diff --git a/nova/tests/quota_unittest.py b/nova/tests/quota_unittest.py index 9d697ccd..cab9f663 100644 --- a/nova/tests/quota_unittest.py +++ b/nova/tests/quota_unittest.py @@ -147,16 +147,9 @@ class QuotaTestCase(test.TrialTestCase): 'host': FLAGS.host}) float_addr = self.network.allocate_floating_ip(self.context, self.project.id) - # NOTE(vish): This assert doesn't work. When cloud attempts to + # NOTE(vish): This assert never fails. When cloud attempts to # make an rpc.call, the test just finishes with OK. It # appears to be something in the magic inline callbacks # that is breaking. self.assertFailure(self.cloud.allocate_address(self.context), cloud.QuotaError) - try: - yield self.cloud.allocate_address(self.context) - self.fail('Should have raised QuotaError') - except cloud.QuotaError: - pass - - From ab76ae87b228c000780db743f7200a0b074132fd Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 23:32:43 -0700 Subject: [PATCH 48/87] don't pass topic into schedule_run_instance --- nova/tests/scheduler_unittest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nova/tests/scheduler_unittest.py b/nova/tests/scheduler_unittest.py index 09e45ea6..27e100fa 100644 --- a/nova/tests/scheduler_unittest.py +++ b/nova/tests/scheduler_unittest.py @@ -121,7 +121,6 @@ class SimpleDriverTestCase(test.TrialTestCase): instance_id = self._create_instance() self.service1.run_instance(self.context, instance_id) host = self.scheduler.driver.schedule_run_instance(self.context, - 'compute', instance_id) self.assertEqual(host, 'host2') self.service1.terminate_instance(self.context, instance_id) From 52b0042e9bf111a1efde7a2e30d851f6c10111db Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 9 Sep 2010 23:37:08 -0700 Subject: [PATCH 49/87] removed extra quotes around instance_type --- nova/endpoint/cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 5209ec90..ad5db666 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -578,7 +578,7 @@ class CloudController(object): base_options['security_group'] = security_group base_options['instance_type'] = instance_type - type_data = INSTANCE_TYPES['instance_type'] + type_data = INSTANCE_TYPES[instance_type] base_options['memory_mb'] = type_data['memory_mb'] base_options['vcpus'] = type_data['vcpus'] base_options['local_gb'] = type_data['local_gb'] From b9609f0873cfd50a1b2184930921721bad66601c Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 01:13:11 -0700 Subject: [PATCH 50/87] set host when item is scheduled --- nova/scheduler/simple.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/nova/scheduler/simple.py b/nova/scheduler/simple.py index e53e9fa7..48be4c1a 100644 --- a/nova/scheduler/simple.py +++ b/nova/scheduler/simple.py @@ -37,7 +37,7 @@ flags.DEFINE_integer("max_networks", 1000, class SimpleScheduler(chance.ChanceScheduler): """Implements Naive Scheduler that tries to find least loaded host.""" - def schedule_run_instance(self, context, *_args, **_kwargs): + def schedule_run_instance(self, context, instance_id, *_args, **_kwargs): """Picks a host that is up and has the fewest running instances.""" results = db.service_get_all_compute_sorted(context) @@ -46,10 +46,13 @@ class SimpleScheduler(chance.ChanceScheduler): if instance_count >= FLAGS.max_instances: raise driver.NoValidHost("All hosts have too many instances") if self.service_is_up(service): + db.instance_update(context, + instance_id, + {'host': service['host']}) return service['host'] raise driver.NoValidHost("No hosts found") - def schedule_create_volume(self, context, *_args, **_kwargs): + def schedule_create_volume(self, context, volume_id, *_args, **_kwargs): """Picks a host that is up and has the fewest volumes.""" results = db.service_get_all_volume_sorted(context) @@ -58,6 +61,9 @@ class SimpleScheduler(chance.ChanceScheduler): if instance_count >= FLAGS.max_volumes: raise driver.NoValidHost("All hosts have too many volumes") if self.service_is_up(service): + db.instance_update(context, + volume_id, + {'host': service['host']}) return service['host'] raise driver.NoValidHost("No hosts found") From 12707061180bd2bc27aae2b10621ccabca8a46bb Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 04:52:48 -0700 Subject: [PATCH 51/87] added modify project command to allow project manager and description to be updated --- nova/auth/fakeldap.py | 5 ++++- nova/auth/ldapdriver.py | 18 ++++++++++++++++++ nova/auth/manager.py | 20 ++++++++++++++++++++ nova/tests/auth_unittest.py | 6 ++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/nova/auth/fakeldap.py b/nova/auth/fakeldap.py index bfc3433c..2791dfde 100644 --- a/nova/auth/fakeldap.py +++ b/nova/auth/fakeldap.py @@ -33,6 +33,7 @@ SCOPE_ONELEVEL = 1 # not implemented SCOPE_SUBTREE = 2 MOD_ADD = 0 MOD_DELETE = 1 +MOD_REPLACE = 2 class NO_SUCH_OBJECT(Exception): # pylint: disable-msg=C0103 @@ -175,7 +176,7 @@ class FakeLDAP(object): Args: dn -- a dn attrs -- a list of tuples in the following form: - ([MOD_ADD | MOD_DELETE], attribute, value) + ([MOD_ADD | MOD_DELETE | MOD_REPACE], attribute, value) """ redis = datastore.Redis.instance() @@ -185,6 +186,8 @@ class FakeLDAP(object): values = _from_json(redis.hget(key, k)) if cmd == MOD_ADD: values.append(v) + elif cmd == MOD_REPLACE: + values = [v] else: values.remove(v) values = redis.hset(key, k, _to_json(values)) diff --git a/nova/auth/ldapdriver.py b/nova/auth/ldapdriver.py index 74ba011b..cc8e2caa 100644 --- a/nova/auth/ldapdriver.py +++ b/nova/auth/ldapdriver.py @@ -202,6 +202,24 @@ class LdapDriver(object): self.conn.add_s('cn=%s,%s' % (name, FLAGS.ldap_project_subtree), attr) return self.__to_project(dict(attr)) + def modify_project(self, project_id, manager_uid=None, description=None): + """Modify an existing project""" + if not manager_uid and not description: + return + attr = [] + if manager_uid: + if not self.__user_exists(manager_uid): + raise exception.NotFound("Project can't be modified because " + "manager %s doesn't exist" % + manager_uid) + manager_dn = self.__uid_to_dn(manager_uid) + attr.append((self.ldap.MOD_REPLACE, 'projectManager', manager_dn)) + if description: + attr.append((self.ldap.MOD_REPLACE, 'description', description)) + self.conn.modify_s('cn=%s,%s' % (project_id, + FLAGS.ldap_project_subtree), + attr) + def add_to_project(self, uid, project_id): """Add user to project""" dn = 'cn=%s,%s' % (project_id, FLAGS.ldap_project_subtree) diff --git a/nova/auth/manager.py b/nova/auth/manager.py index 284b2950..d094bb7e 100644 --- a/nova/auth/manager.py +++ b/nova/auth/manager.py @@ -525,6 +525,26 @@ class AuthManager(object): if project_dict: return Project(**project_dict) + def modify_project(self, project, manager_user=None, description=None): + """Modify a project + + @type name: Project or project_id + @param project: The project to modify. + + @type manager_user: User or uid + @param manager_user: This user will be the new project manager. + + @type description: str + @param project: This will be the new description of the project. + + """ + if manager_user: + manager_user = User.safe_id(manager_user) + with self.driver() as drv: + drv.modify_project(Project.safe_id(project), + manager_user, + description) + def add_to_project(self, user, project): """Add user to project""" with self.driver() as drv: diff --git a/nova/tests/auth_unittest.py b/nova/tests/auth_unittest.py index 0b404bfd..2fc78064 100644 --- a/nova/tests/auth_unittest.py +++ b/nova/tests/auth_unittest.py @@ -206,6 +206,12 @@ class AuthTestCase(test.BaseTestCase): self.assert_(len(self.manager.get_projects()) > 1) self.assertEqual(len(self.manager.get_projects('test2')), 1) + def test_220_can_modify_project(self): + self.manager.modify_project('testproj', 'test2', 'new description') + project = self.manager.get_project('testproj') + self.assertEqual(project.project_manager_id, 'test2') + self.assertEqual(project.description, 'new description') + def test_299_can_delete_project(self): self.manager.delete_project('testproj') self.assertFalse(filter(lambda p: p.name == 'testproj', self.manager.get_projects())) From 9a11274175519daae0f23442d5ff3788ce279cb9 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 05:25:57 -0700 Subject: [PATCH 52/87] fixed messed up call in metadata --- nova/endpoint/cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 5ff69edf..6f8cf94f 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -84,7 +84,7 @@ class CloudController(object): def _get_mpi_data(self, project_id): result = {} - for instance in db.instance_get_by_project(project_id): + for instance in db.instance_get_by_project(None, project_id): line = '%s slots=%d' % (instance.fixed_ip['str_id'], INSTANCE_TYPES[instance['instance_type']]['vcpus']) if instance['key_name'] in result: From 307a8085479eb9307cddd7c07783ee9660eaae8a Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 05:38:59 -0700 Subject: [PATCH 53/87] typo in metadata call --- nova/endpoint/cloud.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 6f8cf94f..bf2f07ad 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -108,8 +108,8 @@ class CloudController(object): else: keys = '' hostname = instance_ref['hostname'] - floating_ip = db.instance_get_floating_ip_address(None, - instance_ref['id']) + floating_ip = db.instance_get_floating_address(None, + instance_ref['id']) data = { 'user-data': base64.b64decode(instance_ref['user_data']), 'meta-data': { From 7aa05c5af3681fec90038942127ebf0b470f85d4 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 05:49:36 -0700 Subject: [PATCH 54/87] couple more errors in metadata --- nova/endpoint/cloud.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index bf2f07ad..c85383ef 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -114,7 +114,7 @@ class CloudController(object): 'user-data': base64.b64decode(instance_ref['user_data']), 'meta-data': { 'ami-id': instance_ref['image_id'], - 'ami-launch-index': instance_ref['ami_launch_index'], + 'ami-launch-index': instance_ref['launch_index'], 'ami-manifest-path': 'FIXME', 'block-device-mapping': { # TODO(vish): replace with real data 'ami': 'sda1', @@ -130,7 +130,7 @@ class CloudController(object): 'local-ipv4': address, 'kernel-id': instance_ref['kernel_id'], 'placement': { - 'availaibility-zone': instance_ref['availability_zone'], + 'availability-zone': 'nova' # TODO(vish): real zone }, 'public-hostname': hostname, 'public-ipv4': floating_ip or '', From 229fd1881ddee117367efc1dd45facd89131a0af Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 12:25:45 -0700 Subject: [PATCH 55/87] dhcpbridge needed host instead of node name --- bin/nova-dhcpbridge | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/nova-dhcpbridge b/bin/nova-dhcpbridge index c4795cca..42eaf4bc 100755 --- a/bin/nova-dhcpbridge +++ b/bin/nova-dhcpbridge @@ -53,7 +53,7 @@ def add_lease(_mac, ip_address, _hostname, _interface): network_manager = utils.import_object(FLAGS.network_manager) network_manager.lease_fixed_ip(None, ip_address) else: - rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.node_name), + rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.host), {"method": "lease_fixed_ip", "args": {"context": None, "address": ip_address}}) @@ -71,7 +71,7 @@ def del_lease(_mac, ip_address, _hostname, _interface): network_manager = utils.import_object(FLAGS.network_manager) network_manager.release_fixed_ip(None, ip_address) else: - rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.node_name), + rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.host), {"method": "release_fixed_ip", "args": {"context": None, "address": ip_address}}) From aaf9d445cd485223e38179b0a6aaa22118e97634 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 12:34:45 -0700 Subject: [PATCH 56/87] hostname should be string id --- nova/endpoint/cloud.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index c85383ef..2406e820 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -550,12 +550,13 @@ class CloudController(object): base_options['security_group'] = security_group for num in range(int(kwargs['max_count'])): - inst_id = db.instance_create(context, base_options)['id'] + instance_ref = db.instance_create(context, base_options) + inst_id = instance_ref['id'] inst = {} inst['mac_address'] = utils.generate_mac() inst['launch_index'] = num - inst['hostname'] = inst_id + inst['hostname'] = instance_ref['str_id'] db.instance_update(context, inst_id, inst) address = self.network_manager.allocate_fixed_ip(context, inst_id, From 96bfc8d8d97f7182d1d7dee0bd59c21e08434f00 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 14:16:14 -0700 Subject: [PATCH 57/87] fix mpi 500 on fixed ip --- nova/endpoint/cloud.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 2406e820..925d14e1 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -85,12 +85,13 @@ class CloudController(object): def _get_mpi_data(self, project_id): result = {} for instance in db.instance_get_by_project(None, project_id): - line = '%s slots=%d' % (instance.fixed_ip['str_id'], - INSTANCE_TYPES[instance['instance_type']]['vcpus']) - if instance['key_name'] in result: - result[instance['key_name']].append(line) - else: - result[instance['key_name']] = [line] + if instance['fixed_ip']: + line = '%s slots=%d' % (instance['fixed_ip']['str_id'], + INSTANCE_TYPES[instance['instance_type']]['vcpus']) + if instance['key_name'] in result: + result[instance['key_name']].append(line) + else: + result[instance['key_name']] = [line] return result def get_metadata(self, address): From e5a90a6de65b1d1bc1562df67d0699ea5215473a Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 15:04:52 -0700 Subject: [PATCH 58/87] set dnsName on describe --- nova/endpoint/cloud.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 925d14e1..6ca6855c 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -391,9 +391,7 @@ class CloudController(object): floating_addr = fixed['floating_ips'][0]['str_id'] i['privateDnsName'] = fixed_addr i['publicDnsName'] = floating_addr - if not i['publicDnsName']: - i['publicDnsName'] = i['privateDnsName'] - i['dnsName'] = None + i['dnsName'] = i['publicDnsName'] or i['privateDnsName'] i['keyName'] = instance['key_name'] if context.user.is_admin(): i['keyName'] = '%s (%s, %s)' % (i['keyName'], From 5955be2f2753f3b6a3564594e4b727c10fcebcac Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 16:40:49 -0700 Subject: [PATCH 59/87] make api error messages more readable --- nova/endpoint/api.py | 5 ++++- nova/endpoint/cloud.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/nova/endpoint/api.py b/nova/endpoint/api.py index 40be00bb..12eedfe6 100755 --- a/nova/endpoint/api.py +++ b/nova/endpoint/api.py @@ -304,7 +304,10 @@ class APIRequestHandler(tornado.web.RequestHandler): try: failure.raiseException() except exception.ApiError as ex: - self._error(type(ex).__name__ + "." + ex.code, ex.message) + if ex.code: + self._error(ex.code, ex.message) + else: + self._error(type(ex).__name__, ex.message) # TODO(vish): do something more useful with unknown exceptions except Exception as ex: self._error(type(ex).__name__, str(ex)) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index ad5db666..adb63351 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -529,7 +529,7 @@ class CloudController(object): context.project.id, min_instances) raise QuotaError("Instance quota exceeded. You can only " "run %s more instances of this type." % - num_instances) + num_instances, "InstanceLimitExceeded") # make sure user can access the image # vpn image is private so it doesn't show up on lists vpn = kwargs['image_id'] == FLAGS.vpn_image_id From a4082e45dd2b4d969d36985cb19b9329af2765df Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 17:12:49 -0700 Subject: [PATCH 60/87] multi-region flag for describe regions --- nova/endpoint/cloud.py | 15 ++++++++++++--- nova/flags.py | 15 +++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 8e2beb1e..180af054 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -174,9 +174,18 @@ class CloudController(object): @rbac.allow('all') def describe_regions(self, context, region_name=None, **kwargs): - # TODO(vish): region_name is an array. Support filtering - return {'regionInfo': [{'regionName': 'nova', - 'regionUrl': FLAGS.ec2_url}]} + if FLAGS.region_list: + regions = [] + for region in FLAGS.region_list: + name, _sep, url = region.partition(',') + regions.append({'regionName': name, + 'regionUrl': url}) + else: + regions = [{'regionName': 'nova', + 'regionUrl': FLAGS.ec2_url}] + if region_name: + regions = [r for r in regions if r['regionName'] in region_name] + return {'regionInfo': regions } @rbac.allow('all') def describe_snapshots(self, diff --git a/nova/flags.py b/nova/flags.py index 2bca36f7..19dcb96b 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -34,7 +34,7 @@ class FlagValues(gflags.FlagValues): Unknown flags will be ignored when parsing the command line, but the command line will be kept so that it can be replayed if new flags are defined after the initial parsing. - + """ def __init__(self): @@ -50,7 +50,7 @@ class FlagValues(gflags.FlagValues): # leftover args at the end sneaky_unparsed_args = {"value": None} original_argv = list(argv) - + if self.IsGnuGetOpt(): orig_getopt = getattr(getopt, 'gnu_getopt') orig_name = 'gnu_getopt' @@ -81,7 +81,7 @@ class FlagValues(gflags.FlagValues): args = argv[:1] finally: setattr(getopt, orig_name, orig_getopt) - + # Store the arguments for later, we'll need them for new flags # added at runtime self.__dict__['__stored_argv'] = original_argv @@ -92,7 +92,7 @@ class FlagValues(gflags.FlagValues): def SetDirty(self, name): """Mark a flag as dirty so that accessing it will case a reparse.""" self.__dict__['__dirty'].append(name) - + def IsDirty(self, name): return name in self.__dict__['__dirty'] @@ -113,12 +113,12 @@ class FlagValues(gflags.FlagValues): for k in self.__dict__['__dirty']: setattr(self, k, getattr(new_flags, k)) self.ClearDirty() - + def __setitem__(self, name, flag): gflags.FlagValues.__setitem__(self, name, flag) if self.WasAlreadyParsed(): self.SetDirty(name) - + def __getitem__(self, name): if self.IsDirty(name): self.ParseNewFlags() @@ -166,6 +166,9 @@ def DECLARE(name, module_string, flag_values=FLAGS): # Define any app-specific flags in their own files, docs at: # http://code.google.com/p/python-gflags/source/browse/trunk/gflags.py#39 +DEFINE_list('region_list', + [], + 'list of region,url pairs') DEFINE_string('connection_type', 'libvirt', 'libvirt, xenapi or fake') DEFINE_integer('s3_port', 3333, 's3 port') DEFINE_string('s3_host', '127.0.0.1', 's3 host') From fb8b9e8e726fef3910c9d10ae4d2797fafd0a395 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 18:51:22 -0700 Subject: [PATCH 61/87] moved keypairs to db using the same interface --- nova/auth/manager.py | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/nova/auth/manager.py b/nova/auth/manager.py index d5fbec7c..4cb23bea 100644 --- a/nova/auth/manager.py +++ b/nova/auth/manager.py @@ -668,42 +668,36 @@ class AuthManager(object): with self.driver() as drv: if not drv.get_user(uid): raise exception.NotFound("User %s doesn't exist" % user) - if drv.get_key_pair(uid, key_name): - raise exception.Duplicate("The keypair %s already exists" - % key_name) + try: + db.keypair_get(None, uid, key_name) + raise exception.Duplicate("The keypair %s already exists" + % key_name) + except exception.NotFound: + pass private_key, public_key, fingerprint = crypto.generate_key_pair() self.create_key_pair(uid, key_name, public_key, fingerprint) return private_key, fingerprint def create_key_pair(self, user, key_name, public_key, fingerprint): """Creates a key pair for user""" - with self.driver() as drv: - kp_dict = drv.create_key_pair(User.safe_id(user), - key_name, - public_key, - fingerprint) - if kp_dict: - return KeyPair(**kp_dict) + key = {} + key['user_id'] = User.safe_id(user) + key['name'] = key_name + key['public_key'] = public_key + key['fingerprint'] = fingerprint + return db.keypair_create(None, key) def get_key_pair(self, user, key_name): """Retrieves a key pair for user""" - with self.driver() as drv: - kp_dict = drv.get_key_pair(User.safe_id(user), key_name) - if kp_dict: - return KeyPair(**kp_dict) + return db.keypair_get(None, User.safe_id(user), key_name) def get_key_pairs(self, user): """Retrieves all key pairs for user""" - with self.driver() as drv: - kp_list = drv.get_key_pairs(User.safe_id(user)) - if not kp_list: - return [] - return [KeyPair(**kp_dict) for kp_dict in kp_list] + return db.keypair_get_all_by_user(None, User.safe_id(user)) def delete_key_pair(self, user, key_name): """Deletes a key pair for user""" - with self.driver() as drv: - drv.delete_key_pair(User.safe_id(user), key_name) + return db.keypair_destroy(None, User.safe_id(user), key_name) def get_credentials(self, user, project=None): """Get credential zip for user in project""" From 5286197935e3f31e9f76bf4e1fa7f7af8330d670 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 18:55:11 -0700 Subject: [PATCH 62/87] remove keypair from driver --- nova/auth/ldapdriver.py | 60 ----------------------------------------- nova/auth/manager.py | 23 ---------------- 2 files changed, 83 deletions(-) diff --git a/nova/auth/ldapdriver.py b/nova/auth/ldapdriver.py index 74ba011b..4e9afc85 100644 --- a/nova/auth/ldapdriver.py +++ b/nova/auth/ldapdriver.py @@ -99,13 +99,6 @@ class LdapDriver(object): dn = FLAGS.ldap_user_subtree return self.__to_user(self.__find_object(dn, query)) - def get_key_pair(self, uid, key_name): - """Retrieve key pair by uid and key name""" - dn = 'cn=%s,%s' % (key_name, - self.__uid_to_dn(uid)) - attr = self.__find_object(dn, '(objectclass=novaKeyPair)') - return self.__to_key_pair(uid, attr) - def get_project(self, pid): """Retrieve project by id""" dn = 'cn=%s,%s' % (pid, @@ -119,12 +112,6 @@ class LdapDriver(object): '(objectclass=novaUser)') return [self.__to_user(attr) for attr in attrs] - def get_key_pairs(self, uid): - """Retrieve list of key pairs""" - attrs = self.__find_objects(self.__uid_to_dn(uid), - '(objectclass=novaKeyPair)') - return [self.__to_key_pair(uid, attr) for attr in attrs] - def get_projects(self, uid=None): """Retrieve list of projects""" pattern = '(objectclass=novaProject)' @@ -154,21 +141,6 @@ class LdapDriver(object): self.conn.add_s(self.__uid_to_dn(name), attr) return self.__to_user(dict(attr)) - def create_key_pair(self, uid, key_name, public_key, fingerprint): - """Create a key pair""" - # TODO(vish): possibly refactor this to store keys in their own ou - # and put dn reference in the user object - attr = [ - ('objectclass', ['novaKeyPair']), - ('cn', [key_name]), - ('sshPublicKey', [public_key]), - ('keyFingerprint', [fingerprint]), - ] - self.conn.add_s('cn=%s,%s' % (key_name, - self.__uid_to_dn(uid)), - attr) - return self.__to_key_pair(uid, dict(attr)) - def create_project(self, name, manager_uid, description=None, member_uids=None): """Create a project""" @@ -265,19 +237,10 @@ class LdapDriver(object): """Delete a user""" if not self.__user_exists(uid): raise exception.NotFound("User %s doesn't exist" % uid) - self.__delete_key_pairs(uid) self.__remove_from_all(uid) self.conn.delete_s('uid=%s,%s' % (uid, FLAGS.ldap_user_subtree)) - def delete_key_pair(self, uid, key_name): - """Delete a key pair""" - if not self.__key_pair_exists(uid, key_name): - raise exception.NotFound("Key Pair %s doesn't exist for user %s" % - (key_name, uid)) - self.conn.delete_s('cn=%s,uid=%s,%s' % (key_name, uid, - FLAGS.ldap_user_subtree)) - def delete_project(self, project_id): """Delete a project""" project_dn = 'cn=%s,%s' % (project_id, FLAGS.ldap_project_subtree) @@ -288,10 +251,6 @@ class LdapDriver(object): """Check if user exists""" return self.get_user(uid) != None - def __key_pair_exists(self, uid, key_name): - """Check if key pair exists""" - return self.get_key_pair(uid, key_name) != None - def __project_exists(self, project_id): """Check if project exists""" return self.get_project(project_id) != None @@ -341,13 +300,6 @@ class LdapDriver(object): """Check if group exists""" return self.__find_object(dn, '(objectclass=groupOfNames)') != None - def __delete_key_pairs(self, uid): - """Delete all key pairs for user""" - keys = self.get_key_pairs(uid) - if keys != None: - for key in keys: - self.delete_key_pair(uid, key['name']) - @staticmethod def __role_to_dn(role, project_id=None): """Convert role to corresponding dn""" @@ -472,18 +424,6 @@ class LdapDriver(object): 'secret': attr['secretKey'][0], 'admin': (attr['isAdmin'][0] == 'TRUE')} - @staticmethod - def __to_key_pair(owner, attr): - """Convert ldap attributes to KeyPair object""" - if attr == None: - return None - return { - 'id': attr['cn'][0], - 'name': attr['cn'][0], - 'owner_id': owner, - 'public_key': attr['sshPublicKey'][0], - 'fingerprint': attr['keyFingerprint'][0]} - def __to_project(self, attr): """Convert ldap attributes to Project object""" if attr == None: diff --git a/nova/auth/manager.py b/nova/auth/manager.py index 4cb23bea..ef6a5a48 100644 --- a/nova/auth/manager.py +++ b/nova/auth/manager.py @@ -154,29 +154,6 @@ class User(AuthBase): self.admin) -class KeyPair(AuthBase): - """Represents an ssh key returned from the datastore - - Even though this object is named KeyPair, only the public key and - fingerprint is stored. The user's private key is not saved. - """ - - def __init__(self, id, name, owner_id, public_key, fingerprint): - AuthBase.__init__(self) - self.id = id - self.name = name - self.owner_id = owner_id - self.public_key = public_key - self.fingerprint = fingerprint - - def __repr__(self): - return "KeyPair('%s', '%s', '%s', '%s', '%s')" % (self.id, - self.name, - self.owner_id, - self.public_key, - self.fingerprint) - - class Project(AuthBase): """Represents a Project returned from the datastore""" From c88569728e3028ac19aeabe40c39ee01bd982843 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 19:03:35 -0700 Subject: [PATCH 63/87] delete keypairs when a user is deleted --- nova/auth/manager.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nova/auth/manager.py b/nova/auth/manager.py index ef6a5a48..e2bb748b 100644 --- a/nova/auth/manager.py +++ b/nova/auth/manager.py @@ -620,9 +620,13 @@ class AuthManager(object): return User(**user_dict) def delete_user(self, user): - """Deletes a user""" + """Deletes a user + + Additionally deletes all users keypairs""" + uid = User.safe_id(user) + db.keypair_destroy_all_by_user(None, uid) with self.driver() as drv: - drv.delete_user(User.safe_id(user)) + drv.delete_user(uid) def generate_key_pair(self, user, key_name): """Generates a key pair for a user From ad0de7c9b29309c7282d5fc7cdfc0b5fb0697baf Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 22:13:36 -0700 Subject: [PATCH 64/87] move keypair generation out of auth and fix tests --- nova/auth/manager.py | 70 ------------------------------------ nova/endpoint/cloud.py | 48 +++++++++++++++++-------- nova/tests/api_unittest.py | 7 ++-- nova/tests/auth_unittest.py | 31 ---------------- nova/tests/cloud_unittest.py | 53 ++++++++++++++++++++++----- 5 files changed, 83 insertions(+), 126 deletions(-) diff --git a/nova/auth/manager.py b/nova/auth/manager.py index e2bb748b..fb87847d 100644 --- a/nova/auth/manager.py +++ b/nova/auth/manager.py @@ -128,24 +128,6 @@ class User(AuthBase): def is_project_manager(self, project): return AuthManager().is_project_manager(self, project) - def generate_key_pair(self, name): - return AuthManager().generate_key_pair(self.id, name) - - def create_key_pair(self, name, public_key, fingerprint): - return AuthManager().create_key_pair(self.id, - name, - public_key, - fingerprint) - - def get_key_pair(self, name): - return AuthManager().get_key_pair(self.id, name) - - def delete_key_pair(self, name): - return AuthManager().delete_key_pair(self.id, name) - - def get_key_pairs(self): - return AuthManager().get_key_pairs(self.id) - def __repr__(self): return "User('%s', '%s', '%s', '%s', %s)" % (self.id, self.name, @@ -628,58 +610,6 @@ class AuthManager(object): with self.driver() as drv: drv.delete_user(uid) - def generate_key_pair(self, user, key_name): - """Generates a key pair for a user - - Generates a public and private key, stores the public key using the - key_name, and returns the private key and fingerprint. - - @type user: User or uid - @param user: User for which to create key pair. - - @type key_name: str - @param key_name: Name to use for the generated KeyPair. - - @rtype: tuple (private_key, fingerprint) - @return: A tuple containing the private_key and fingerprint. - """ - # NOTE(vish): generating key pair is slow so check for legal - # creation before creating keypair - uid = User.safe_id(user) - with self.driver() as drv: - if not drv.get_user(uid): - raise exception.NotFound("User %s doesn't exist" % user) - try: - db.keypair_get(None, uid, key_name) - raise exception.Duplicate("The keypair %s already exists" - % key_name) - except exception.NotFound: - pass - private_key, public_key, fingerprint = crypto.generate_key_pair() - self.create_key_pair(uid, key_name, public_key, fingerprint) - return private_key, fingerprint - - def create_key_pair(self, user, key_name, public_key, fingerprint): - """Creates a key pair for user""" - key = {} - key['user_id'] = User.safe_id(user) - key['name'] = key_name - key['public_key'] = public_key - key['fingerprint'] = fingerprint - return db.keypair_create(None, key) - - def get_key_pair(self, user, key_name): - """Retrieves a key pair for user""" - return db.keypair_get(None, User.safe_id(user), key_name) - - def get_key_pairs(self, user): - """Retrieves all key pairs for user""" - return db.keypair_get_all_by_user(None, User.safe_id(user)) - - def delete_key_pair(self, user, key_name): - """Deletes a key pair for user""" - return db.keypair_destroy(None, User.safe_id(user), key_name) - def get_credentials(self, user, project=None): """Get credential zip for user in project""" if not isinstance(user, User): diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 6ca6855c..172c65d7 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -29,13 +29,13 @@ import time from twisted.internet import defer +from nova import crypto from nova import db from nova import exception from nova import flags from nova import rpc from nova import utils from nova.auth import rbac -from nova.auth import manager from nova.compute.instance_types import INSTANCE_TYPES from nova.endpoint import images @@ -44,14 +44,30 @@ FLAGS = flags.FLAGS flags.DECLARE('storage_availability_zone', 'nova.volume.manager') -def _gen_key(user_id, key_name): - """ Tuck this into AuthManager """ +def _gen_key(context, user_id, key_name): + """Generate a key + + This is a module level method because it is slow and we need to defer + it into a process pool.""" try: - mgr = manager.AuthManager() - private_key, fingerprint = mgr.generate_key_pair(user_id, key_name) + # NOTE(vish): generating key pair is slow so check for legal + # creation before creating keypair + try: + db.keypair_get(context, user_id, key_name) + raise exception.Duplicate("The keypair %s already exists" + % key_name) + except exception.NotFound: + pass + private_key, public_key, fingerprint = crypto.generate_key_pair() + key = {} + key['user_id'] = user_id + key['name'] = key_name + key['public_key'] = public_key + key['fingerprint'] = fingerprint + db.keypair_create(context, key) + return {'private_key': private_key, 'fingerprint': fingerprint} except Exception as ex: return {'exception': ex} - return {'private_key': private_key, 'fingerprint': fingerprint} class CloudController(object): @@ -177,18 +193,18 @@ class CloudController(object): @rbac.allow('all') def describe_key_pairs(self, context, key_name=None, **kwargs): - key_pairs = context.user.get_key_pairs() + key_pairs = db.keypair_get_all_by_user(context, context.user.id) if not key_name is None: - key_pairs = [x for x in key_pairs if x.name in key_name] + key_pairs = [x for x in key_pairs if x['name'] in key_name] result = [] for key_pair in key_pairs: # filter out the vpn keys suffix = FLAGS.vpn_key_suffix - if context.user.is_admin() or not key_pair.name.endswith(suffix): + if context.user.is_admin() or not key_pair['name'].endswith(suffix): result.append({ - 'keyName': key_pair.name, - 'keyFingerprint': key_pair.fingerprint, + 'keyName': key_pair['name'], + 'keyFingerprint': key_pair['fingerprint'], }) return {'keypairsSet': result} @@ -204,14 +220,18 @@ class CloudController(object): dcall.callback({'keyName': key_name, 'keyFingerprint': kwargs['fingerprint'], 'keyMaterial': kwargs['private_key']}) - pool.apply_async(_gen_key, [context.user.id, key_name], + # TODO(vish): when context is no longer an object, pass it here + pool.apply_async(_gen_key, [None, context.user.id, key_name], callback=_complete) return dcall @rbac.allow('all') def delete_key_pair(self, context, key_name, **kwargs): - context.user.delete_key_pair(key_name) - # aws returns true even if the key doens't exist + try: + db.keypair_destroy(context, context.user.id, key_name) + except exception.NotFound: + # aws returns true even if the key doesn't exist + pass return True @rbac.allow('all') diff --git a/nova/tests/api_unittest.py b/nova/tests/api_unittest.py index 462d1b29..fdb9e21d 100644 --- a/nova/tests/api_unittest.py +++ b/nova/tests/api_unittest.py @@ -41,8 +41,8 @@ FLAGS = flags.FLAGS # it's pretty damn circuitous so apologies if you have to fix # a bug in it # NOTE(jaypipes) The pylint disables here are for R0913 (too many args) which -# isn't controllable since boto's HTTPRequest needs that many -# args, and for the version-differentiated import of tornado's +# isn't controllable since boto's HTTPRequest needs that many +# args, and for the version-differentiated import of tornado's # httputil. # NOTE(jaypipes): The disable-msg=E1101 and E1103 below is because pylint is # unable to introspect the deferred's return value properly @@ -224,7 +224,8 @@ class ApiEc2TestCase(test.BaseTestCase): for x in range(random.randint(4, 8))) user = self.manager.create_user('fake', 'fake', 'fake') project = self.manager.create_project('fake', 'fake', 'fake') - self.manager.generate_key_pair(user.id, keyname) + # NOTE(vish): create depends on pool, so call helper directly + cloud._gen_key(None, user.id, keyname) rv = self.ec2.get_all_key_pairs() results = [k for k in rv if k.name == keyname] diff --git a/nova/tests/auth_unittest.py b/nova/tests/auth_unittest.py index b54e6827..1b4e1267 100644 --- a/nova/tests/auth_unittest.py +++ b/nova/tests/auth_unittest.py @@ -17,8 +17,6 @@ # under the License. import logging -from M2Crypto import BIO -from M2Crypto import RSA from M2Crypto import X509 import unittest @@ -65,35 +63,6 @@ class AuthTestCase(test.BaseTestCase): 'export S3_URL="http://127.0.0.1:3333/"\n' + 'export EC2_USER_ID="test1"\n') - def test_006_test_key_storage(self): - user = self.manager.get_user('test1') - user.create_key_pair('public', 'key', 'fingerprint') - key = user.get_key_pair('public') - self.assertEqual('key', key.public_key) - self.assertEqual('fingerprint', key.fingerprint) - - def test_007_test_key_generation(self): - user = self.manager.get_user('test1') - private_key, fingerprint = user.generate_key_pair('public2') - key = RSA.load_key_string(private_key, callback=lambda: None) - bio = BIO.MemoryBuffer() - public_key = user.get_key_pair('public2').public_key - key.save_pub_key_bio(bio) - converted = crypto.ssl_pub_to_ssh_pub(bio.read()) - # assert key fields are equal - self.assertEqual(public_key.split(" ")[1].strip(), - converted.split(" ")[1].strip()) - - def test_008_can_list_key_pairs(self): - keys = self.manager.get_user('test1').get_key_pairs() - self.assertTrue(filter(lambda k: k.name == 'public', keys)) - self.assertTrue(filter(lambda k: k.name == 'public2', keys)) - - def test_009_can_delete_key_pair(self): - self.manager.get_user('test1').delete_key_pair('public') - keys = self.manager.get_user('test1').get_key_pairs() - self.assertFalse(filter(lambda k: k.name == 'public', keys)) - def test_010_can_list_users(self): users = self.manager.get_users() logging.warn(users) diff --git a/nova/tests/cloud_unittest.py b/nova/tests/cloud_unittest.py index 29947e03..4bad25c2 100644 --- a/nova/tests/cloud_unittest.py +++ b/nova/tests/cloud_unittest.py @@ -17,13 +17,18 @@ # under the License. import logging +from M2Crypto import BIO +from M2Crypto import RSA import StringIO import time + from tornado import ioloop from twisted.internet import defer import unittest from xml.etree import ElementTree +from nova import crypto +from nova import db from nova import flags from nova import rpc from nova import test @@ -54,16 +59,21 @@ class CloudTestCase(test.BaseTestCase): proxy=self.compute) self.injected.append(self.compute_consumer.attach_to_tornado(self.ioloop)) - try: - manager.AuthManager().create_user('admin', 'admin', 'admin') - except: pass - admin = manager.AuthManager().get_user('admin') - project = manager.AuthManager().create_project('proj', 'admin', 'proj') - self.context = api.APIRequestContext(handler=None,project=project,user=admin) + self.manager = manager.AuthManager() + self.user = self.manager.create_user('admin', 'admin', 'admin', True) + self.project = self.manager.create_project('proj', 'admin', 'proj') + self.context = api.APIRequestContext(handler=None, + user=self.user, + project=self.project) def tearDown(self): - manager.AuthManager().delete_project('proj') - manager.AuthManager().delete_user('admin') + self.manager.delete_project(self.project) + self.manager.delete_user(self.user) + super(CloudTestCase, self).setUp() + + def _create_key(self, name): + # NOTE(vish): create depends on pool, so just call helper directly + return cloud._gen_key(self.context, self.context.user.id, name) def test_console_output(self): if FLAGS.connection_type == 'fake': @@ -76,6 +86,33 @@ class CloudTestCase(test.BaseTestCase): self.assert_(output) rv = yield self.compute.terminate_instance(instance_id) + + def test_key_generation(self): + result = self._create_key('test') + private_key = result['private_key'] + key = RSA.load_key_string(private_key, callback=lambda: None) + bio = BIO.MemoryBuffer() + public_key = db.keypair_get(self.context, + self.context.user.id, + 'test')['public_key'] + key.save_pub_key_bio(bio) + converted = crypto.ssl_pub_to_ssh_pub(bio.read()) + # assert key fields are equal + self.assertEqual(public_key.split(" ")[1].strip(), + converted.split(" ")[1].strip()) + + def test_describe_key_pairs(self): + self._create_key('test1') + self._create_key('test2') + result = self.cloud.describe_key_pairs(self.context) + keys = result["keypairsSet"] + self.assertTrue(filter(lambda k: k['keyName'] == 'test1', keys)) + self.assertTrue(filter(lambda k: k['keyName'] == 'test2', keys)) + + def test_delete_key_pair(self): + self._create_key('test') + self.cloud.delete_key_pair(self.context, 'test') + def test_run_instances(self): if FLAGS.connection_type == 'fake': logging.debug("Can't test instances without a real virtual env.") From 35b77ae276286a99af63de529f0787a69f33f43c Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Sep 2010 22:42:51 -0700 Subject: [PATCH 65/87] it is called regionEndpoint, and use pipe as a separator --- nova/endpoint/cloud.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 180af054..02eb50b1 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -177,12 +177,12 @@ class CloudController(object): if FLAGS.region_list: regions = [] for region in FLAGS.region_list: - name, _sep, url = region.partition(',') + name, _sep, url = region.partition('|') regions.append({'regionName': name, - 'regionUrl': url}) + 'regionEndpoint': url}) else: regions = [{'regionName': 'nova', - 'regionUrl': FLAGS.ec2_url}] + 'regionEndpoint': FLAGS.ec2_url}] if region_name: regions = [r for r in regions if r['regionName'] in region_name] return {'regionInfo': regions } From 961b20a77acd0cdc69bd57b5c66297a0d4421225 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 00:16:12 -0700 Subject: [PATCH 66/87] improved network error case handling for fixed ips --- bin/nova-dhcpbridge | 10 +++++---- nova/tests/network_unittest.py | 41 ++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/bin/nova-dhcpbridge b/bin/nova-dhcpbridge index 42eaf4bc..2f75bf43 100755 --- a/bin/nova-dhcpbridge +++ b/bin/nova-dhcpbridge @@ -46,16 +46,17 @@ flags.DECLARE('network_size', 'nova.network.manager') flags.DECLARE('num_networks', 'nova.network.manager') -def add_lease(_mac, ip_address, _hostname, _interface): +def add_lease(mac, ip_address, _hostname, _interface): """Set the IP that was assigned by the DHCP server.""" if FLAGS.fake_rabbit: logging.debug("leasing ip") network_manager = utils.import_object(FLAGS.network_manager) - network_manager.lease_fixed_ip(None, ip_address) + network_manager.lease_fixed_ip(None, mac, ip_address) else: rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.host), {"method": "lease_fixed_ip", "args": {"context": None, + "mac": mac, "address": ip_address}}) @@ -64,16 +65,17 @@ def old_lease(_mac, _ip_address, _hostname, _interface): logging.debug("Adopted old lease or got a change of mac/hostname") -def del_lease(_mac, ip_address, _hostname, _interface): +def del_lease(mac, ip_address, _hostname, _interface): """Called when a lease expires.""" if FLAGS.fake_rabbit: logging.debug("releasing ip") network_manager = utils.import_object(FLAGS.network_manager) - network_manager.release_fixed_ip(None, ip_address) + network_manager.release_fixed_ip(None, mac, ip_address) else: rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.host), {"method": "release_fixed_ip", "args": {"context": None, + "mac": mac, "address": ip_address}}) diff --git a/nova/tests/network_unittest.py b/nova/tests/network_unittest.py index 9958600e..d8d4ec0c 100644 --- a/nova/tests/network_unittest.py +++ b/nova/tests/network_unittest.py @@ -147,10 +147,23 @@ class NetworkTestCase(test.TrialTestCase): """Makes sure that private ips don't overlap""" first = self._create_address(0) lease_ip(first) + instance_ids = [] for i in range(1, 5): - address = self._create_address(i) - address2 = self._create_address(i) - address3 = self._create_address(i) + mac = utils.generate_mac() + instance_ref = db.instance_create(None, + {'mac_address': mac}) + instance_ids.append(instance_ref['id']) + address = self._create_address(i, instance_ref['id']) + mac = utils.generate_mac() + instance_ref = db.instance_create(None, + {'mac_address': mac}) + instance_ids.append(instance_ref['id']) + address2 = self._create_address(i, instance_ref['id']) + mac = utils.generate_mac() + instance_ref = db.instance_create(None, + {'mac_address': mac}) + instance_ids.append(instance_ref['id']) + address3 = self._create_address(i, instance_ref['id']) lease_ip(address) lease_ip(address2) lease_ip(address3) @@ -166,6 +179,8 @@ class NetworkTestCase(test.TrialTestCase): release_ip(address) release_ip(address2) release_ip(address3) + for instance_id in instance_ids: + db.instance_destroy(None, instance_id) release_ip(first) db.fixed_ip_deallocate(None, first) @@ -226,8 +241,13 @@ class NetworkTestCase(test.TrialTestCase): num_available_ips = db.network_count_available_ips(None, network['id']) addresses = [] + instance_ids = [] for i in range(num_available_ips): - address = self._create_address(0) + mac = utils.generate_mac() + instance_ref = db.instance_create(None, + {'mac_address': mac}) + instance_ids.append(instance_ref['id']) + address = self._create_address(0, instance_ref['id']) addresses.append(address) lease_ip(address) @@ -238,9 +258,10 @@ class NetworkTestCase(test.TrialTestCase): None, network['id']) - for i in range(len(addresses)): + for i in range(num_available_ips): db.fixed_ip_deallocate(None, addresses[i]) release_ip(addresses[i]) + db.instance_destroy(None, instance_ids[i]) self.assertEqual(db.network_count_available_ips(None, network['id']), num_available_ips) @@ -263,7 +284,10 @@ def binpath(script): def lease_ip(private_ip): """Run add command on dhcpbridge""" network_ref = db.fixed_ip_get_network(None, private_ip) - cmd = "%s add fake %s fake" % (binpath('nova-dhcpbridge'), private_ip) + instance_ref = db.fixed_ip_get_instance(None, private_ip) + cmd = "%s add %s %s fake" % (binpath('nova-dhcpbridge'), + instance_ref['mac_address'], + private_ip) env = {'DNSMASQ_INTERFACE': network_ref['bridge'], 'TESTING': '1', 'FLAGFILE': FLAGS.dhcpbridge_flagfile} @@ -274,7 +298,10 @@ def lease_ip(private_ip): def release_ip(private_ip): """Run del command on dhcpbridge""" network_ref = db.fixed_ip_get_network(None, private_ip) - cmd = "%s del fake %s fake" % (binpath('nova-dhcpbridge'), private_ip) + instance_ref = db.fixed_ip_get_instance(None, private_ip) + cmd = "%s del %s %s fake" % (binpath('nova-dhcpbridge'), + instance_ref['mac_address'], + private_ip) env = {'DNSMASQ_INTERFACE': network_ref['bridge'], 'TESTING': '1', 'FLAGFILE': FLAGS.dhcpbridge_flagfile} From c90d504306bfca045f1a0cfd1a182b722a67ac34 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 04:01:44 -0700 Subject: [PATCH 67/87] fixed tests, added a flag for updating dhcp on disassociate --- nova/endpoint/cloud.py | 2 +- nova/tests/network_unittest.py | 37 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 6ca6855c..622b4e2a 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -613,7 +613,7 @@ class CloudController(object): # NOTE(vish): Currently, nothing needs to be done on the # network node until release. If this changes, # we will need to cast here. - db.fixed_ip_deallocate(context, address) + self.network.deallocate_fixed_ip(context, address) host = instance_ref['host'] if host: diff --git a/nova/tests/network_unittest.py b/nova/tests/network_unittest.py index d8d4ec0c..dc5277f0 100644 --- a/nova/tests/network_unittest.py +++ b/nova/tests/network_unittest.py @@ -28,6 +28,7 @@ from nova import flags from nova import test from nova import utils from nova.auth import manager +from nova.endpoint import api FLAGS = flags.FLAGS @@ -48,7 +49,7 @@ class NetworkTestCase(test.TrialTestCase): self.user = self.manager.create_user('netuser', 'netuser', 'netuser') self.projects = [] self.network = utils.import_object(FLAGS.network_manager) - self.context = None + self.context = api.APIRequestContext(None, project=None, user=self.user) for i in range(5): name = 'project%s' % i self.projects.append(self.manager.create_project(name, @@ -75,12 +76,10 @@ class NetworkTestCase(test.TrialTestCase): def _create_address(self, project_num, instance_id=None): """Create an address in given project num""" - net = db.project_get_network(None, self.projects[project_num].id) - address = db.fixed_ip_allocate(None, net['id']) if instance_id is None: instance_id = self.instance_id - db.fixed_ip_instance_associate(None, address, instance_id) - return address + self.context.project = self.projects[project_num] + return self.network.allocate_fixed_ip(self.context, instance_id) def test_public_network_association(self): """Makes sure that we can allocaate a public ip""" @@ -103,14 +102,14 @@ class NetworkTestCase(test.TrialTestCase): address = db.instance_get_floating_address(None, self.instance_id) self.assertEqual(address, None) self.network.deallocate_floating_ip(self.context, float_addr) - db.fixed_ip_deallocate(None, fix_addr) + self.network.deallocate_fixed_ip(self.context, fix_addr) def test_allocate_deallocate_fixed_ip(self): """Makes sure that we can allocate and deallocate a fixed ip""" address = self._create_address(0) self.assertTrue(is_allocated_in_project(address, self.projects[0].id)) lease_ip(address) - db.fixed_ip_deallocate(None, address) + self.network.deallocate_fixed_ip(self.context, address) # Doesn't go away until it's dhcp released self.assertTrue(is_allocated_in_project(address, self.projects[0].id)) @@ -131,14 +130,14 @@ class NetworkTestCase(test.TrialTestCase): lease_ip(address) lease_ip(address2) - db.fixed_ip_deallocate(None, address) + self.network.deallocate_fixed_ip(self.context, address) release_ip(address) self.assertFalse(is_allocated_in_project(address, self.projects[0].id)) # First address release shouldn't affect the second self.assertTrue(is_allocated_in_project(address2, self.projects[1].id)) - db.fixed_ip_deallocate(None, address2) + self.network.deallocate_fixed_ip(self.context, address2) release_ip(address2) self.assertFalse(is_allocated_in_project(address2, self.projects[1].id)) @@ -173,16 +172,16 @@ class NetworkTestCase(test.TrialTestCase): self.projects[0].id)) self.assertFalse(is_allocated_in_project(address3, self.projects[0].id)) - db.fixed_ip_deallocate(None, address) - db.fixed_ip_deallocate(None, address2) - db.fixed_ip_deallocate(None, address3) + self.network.deallocate_fixed_ip(self.context, address) + self.network.deallocate_fixed_ip(self.context, address2) + self.network.deallocate_fixed_ip(self.context, address3) release_ip(address) release_ip(address2) release_ip(address3) for instance_id in instance_ids: db.instance_destroy(None, instance_id) release_ip(first) - db.fixed_ip_deallocate(None, first) + self.network.deallocate_fixed_ip(self.context, first) def test_vpn_ip_and_port_looks_valid(self): """Ensure the vpn ip and port are reasonable""" @@ -209,12 +208,12 @@ class NetworkTestCase(test.TrialTestCase): """Makes sure that ip addresses that are deallocated get reused""" address = self._create_address(0) lease_ip(address) - db.fixed_ip_deallocate(None, address) + self.network.deallocate_fixed_ip(self.context, address) release_ip(address) address2 = self._create_address(0) self.assertEqual(address, address2) - db.fixed_ip_deallocate(None, address2) + self.network.deallocate_fixed_ip(self.context, address2) def test_available_ips(self): """Make sure the number of available ips for the network is correct @@ -254,12 +253,12 @@ class NetworkTestCase(test.TrialTestCase): self.assertEqual(db.network_count_available_ips(None, network['id']), 0) self.assertRaises(db.NoMoreAddresses, - db.fixed_ip_allocate, - None, - network['id']) + self.network.allocate_fixed_ip, + self.context, + 'foo') for i in range(num_available_ips): - db.fixed_ip_deallocate(None, addresses[i]) + self.network.deallocate_fixed_ip(self.context, addresses[i]) release_ip(addresses[i]) db.instance_destroy(None, instance_ids[i]) self.assertEqual(db.network_count_available_ips(None, From 24c846f1f1d99adb0a105ef32027f5e3203b3b16 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 04:06:22 -0700 Subject: [PATCH 68/87] typo fixes, add flag to nova-dhcpbridge --- bin/nova-dhcpbridge | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/nova-dhcpbridge b/bin/nova-dhcpbridge index 2f75bf43..a127ed03 100755 --- a/bin/nova-dhcpbridge +++ b/bin/nova-dhcpbridge @@ -44,6 +44,7 @@ flags.DECLARE('auth_driver', 'nova.auth.manager') flags.DECLARE('redis_db', 'nova.datastore') flags.DECLARE('network_size', 'nova.network.manager') flags.DECLARE('num_networks', 'nova.network.manager') +flags.DECLARE('update_dhcp_on_disassociate', 'nova.network.manager') def add_lease(mac, ip_address, _hostname, _interface): From f7a859c22f4f053d4b11544f09ea042317b4a1c5 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 04:18:30 -0700 Subject: [PATCH 69/87] fixed old key reference and made keypair name constistent -> key_pair --- nova/auth/manager.py | 4 ++-- nova/endpoint/cloud.py | 21 ++++++++++----------- nova/tests/cloud_unittest.py | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/nova/auth/manager.py b/nova/auth/manager.py index fb87847d..4e321c1b 100644 --- a/nova/auth/manager.py +++ b/nova/auth/manager.py @@ -604,9 +604,9 @@ class AuthManager(object): def delete_user(self, user): """Deletes a user - Additionally deletes all users keypairs""" + Additionally deletes all users key_pairs""" uid = User.safe_id(user) - db.keypair_destroy_all_by_user(None, uid) + db.key_pair_destroy_all_by_user(None, uid) with self.driver() as drv: drv.delete_user(uid) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 172c65d7..f30565ac 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -51,10 +51,10 @@ def _gen_key(context, user_id, key_name): it into a process pool.""" try: # NOTE(vish): generating key pair is slow so check for legal - # creation before creating keypair + # creation before creating key_pair try: - db.keypair_get(context, user_id, key_name) - raise exception.Duplicate("The keypair %s already exists" + db.key_pair_get(context, user_id, key_name) + raise exception.Duplicate("The key_pair %s already exists" % key_name) except exception.NotFound: pass @@ -64,7 +64,7 @@ def _gen_key(context, user_id, key_name): key['name'] = key_name key['public_key'] = public_key key['fingerprint'] = fingerprint - db.keypair_create(context, key) + db.key_pair_create(context, key) return {'private_key': private_key, 'fingerprint': fingerprint} except Exception as ex: return {'exception': ex} @@ -193,7 +193,7 @@ class CloudController(object): @rbac.allow('all') def describe_key_pairs(self, context, key_name=None, **kwargs): - key_pairs = db.keypair_get_all_by_user(context, context.user.id) + key_pairs = db.key_pair_get_all_by_user(context, context.user.id) if not key_name is None: key_pairs = [x for x in key_pairs if x['name'] in key_name] @@ -228,7 +228,7 @@ class CloudController(object): @rbac.allow('all') def delete_key_pair(self, context, key_name, **kwargs): try: - db.keypair_destroy(context, context.user.id, key_name) + db.key_pair_destroy(context, context.user.id, key_name) except exception.NotFound: # aws returns true even if the key doesn't exist pass @@ -545,11 +545,10 @@ class CloudController(object): launch_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()) key_data = None if kwargs.has_key('key_name'): - key_pair = context.user.get_key_pair(kwargs['key_name']) - if not key_pair: - raise exception.ApiError('Key Pair %s not found' % - kwargs['key_name']) - key_data = key_pair.public_key + key_pair_ref = db.key_pair_get(context, + context.user.id, + kwargs['key_name']) + key_data = key_pair_ref['public_key'] # TODO: Get the real security group of launch in here security_group = "default" diff --git a/nova/tests/cloud_unittest.py b/nova/tests/cloud_unittest.py index 4bad25c2..e56ea6ac 100644 --- a/nova/tests/cloud_unittest.py +++ b/nova/tests/cloud_unittest.py @@ -92,7 +92,7 @@ class CloudTestCase(test.BaseTestCase): private_key = result['private_key'] key = RSA.load_key_string(private_key, callback=lambda: None) bio = BIO.MemoryBuffer() - public_key = db.keypair_get(self.context, + public_key = db.key_pair_get(self.context, self.context.user.id, 'test')['public_key'] key.save_pub_key_bio(bio) From e7ae11c9fcfeb5f66235e743427f9838c52ef9ae Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 04:48:37 -0700 Subject: [PATCH 70/87] fixed typo network => network_manager in cloud.py --- nova/endpoint/cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 622b4e2a..0eedd9fe 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -613,7 +613,7 @@ class CloudController(object): # NOTE(vish): Currently, nothing needs to be done on the # network node until release. If this changes, # we will need to cast here. - self.network.deallocate_fixed_ip(context, address) + self.network_manager.deallocate_fixed_ip(context, address) host = instance_ref['host'] if host: From 17bc5e0866f3bfcbd56d075eab8b3d318bfb1afe Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 06:29:13 -0700 Subject: [PATCH 71/87] fixed reversed admin logic on describe instances --- nova/endpoint/cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 0eedd9fe..a25598dc 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -366,7 +366,7 @@ class CloudController(object): instances = db.instance_get_by_reservation(context, reservation_id) else: - if not context.user.is_admin(): + if context.user.is_admin(): instances = db.instance_get_all(context) else: instances = db.instance_get_by_project(context, From 7289ca245328fb83fc47f8d8a878ed9d64a1734a Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 08:43:48 -0700 Subject: [PATCH 72/87] added terminated_at to volume and moved setting of terminated_at into cloud --- nova/endpoint/cloud.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 4b82e6d4..faa646b5 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -23,6 +23,7 @@ datastore. """ import base64 +import datetime import logging import os import time @@ -594,6 +595,10 @@ class CloudController(object): % id_str) continue + now = datetime.datetime.utcnow() + self.db.instance_update(context, + instance_ref['id'], + {'terminated_at': now}) # FIXME(ja): where should network deallocate occur? address = db.instance_get_floating_address(context, instance_ref['id']) @@ -643,6 +648,10 @@ class CloudController(object): def delete_volume(self, context, volume_id, **kwargs): # TODO: return error if not authorized volume_ref = db.volume_get_by_str(context, volume_id) + now = datetime.datetime.utcnow() + self.db.volume_update(context, + volume_ref['id'], + {'terminated_at': now}) host = volume_ref['host'] rpc.cast(db.queue_get_for(context, FLAGS.volume_topic, host), {"method": "delete_volume", From 8a4efea1b9f5d3e70061cd8cc845a8c0df4f2bfd Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 17:12:43 -0700 Subject: [PATCH 73/87] db not self.db --- nova/endpoint/cloud.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index faa646b5..9d8e45f3 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -596,9 +596,9 @@ class CloudController(object): continue now = datetime.datetime.utcnow() - self.db.instance_update(context, - instance_ref['id'], - {'terminated_at': now}) + db.instance_update(context, + instance_ref['id'], + {'terminated_at': now}) # FIXME(ja): where should network deallocate occur? address = db.instance_get_floating_address(context, instance_ref['id']) @@ -649,9 +649,7 @@ class CloudController(object): # TODO: return error if not authorized volume_ref = db.volume_get_by_str(context, volume_id) now = datetime.datetime.utcnow() - self.db.volume_update(context, - volume_ref['id'], - {'terminated_at': now}) + db.volume_update(context, volume_ref['id'], {'terminated_at': now}) host = volume_ref['host'] rpc.cast(db.queue_get_for(context, FLAGS.volume_topic, host), {"method": "delete_volume", From 64380eb810abe935f05441f415c7389ab77979e1 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 18:16:10 -0700 Subject: [PATCH 74/87] use a string version of key name when constructing mpi dict because None doesn't work well in lookup --- nova/endpoint/cloud.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 622b4e2a..45291ca3 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -88,10 +88,11 @@ class CloudController(object): if instance['fixed_ip']: line = '%s slots=%d' % (instance['fixed_ip']['str_id'], INSTANCE_TYPES[instance['instance_type']]['vcpus']) - if instance['key_name'] in result: - result[instance['key_name']].append(line) + key = str(instance['key_name']) + if key in result: + result[key].append(line) else: - result[instance['key_name']] = [line] + result[key] = [line] return result def get_metadata(self, address): From 926fe27703543e204eba8af6a15861d295270821 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 18:45:15 -0700 Subject: [PATCH 75/87] use gigabytes and cores --- nova/scheduler/simple.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/nova/scheduler/simple.py b/nova/scheduler/simple.py index 48be4c1a..6e77debf 100644 --- a/nova/scheduler/simple.py +++ b/nova/scheduler/simple.py @@ -27,10 +27,10 @@ from nova.scheduler import driver from nova.scheduler import chance FLAGS = flags.FLAGS -flags.DEFINE_integer("max_instances", 16, - "maximum number of instances to allow per host") -flags.DEFINE_integer("max_volumes", 100, - "maximum number of volumes to allow per host") +flags.DEFINE_integer("max_cores", 16, + "maximum number of instance cores to allow per host") +flags.DEFINE_integer("max_gigabytes", 10000, + "maximum number of volume gigabytes to allow per host") flags.DEFINE_integer("max_networks", 1000, "maximum number of networks to allow per host") @@ -42,9 +42,9 @@ class SimpleScheduler(chance.ChanceScheduler): results = db.service_get_all_compute_sorted(context) for result in results: - (service, instance_count) = result - if instance_count >= FLAGS.max_instances: - raise driver.NoValidHost("All hosts have too many instances") + (service, instance_cores) = result + if instance_cores >= FLAGS.max_cores: + raise driver.NoValidHost("All hosts have too many cores") if self.service_is_up(service): db.instance_update(context, instance_id, @@ -57,13 +57,13 @@ class SimpleScheduler(chance.ChanceScheduler): results = db.service_get_all_volume_sorted(context) for result in results: - (service, instance_count) = result - if instance_count >= FLAGS.max_volumes: - raise driver.NoValidHost("All hosts have too many volumes") + (service, volume_gigabytes) = result + if volume_gigabytes >= FLAGS.max_gigabytes: + raise driver.NoValidHost("All hosts have too many gigabytes") if self.service_is_up(service): - db.instance_update(context, - volume_id, - {'host': service['host']}) + db.volume_update(context, + volume_id, + {'host': service['host']}) return service['host'] raise driver.NoValidHost("No hosts found") From 9cbed574e8a1368e896c6f9becc154c0d790dddc Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 18:57:15 -0700 Subject: [PATCH 76/87] update query and test --- nova/scheduler/simple.py | 1 + nova/tests/scheduler_unittest.py | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/nova/scheduler/simple.py b/nova/scheduler/simple.py index 6e77debf..3feeca84 100644 --- a/nova/scheduler/simple.py +++ b/nova/scheduler/simple.py @@ -43,6 +43,7 @@ class SimpleScheduler(chance.ChanceScheduler): results = db.service_get_all_compute_sorted(context) for result in results: (service, instance_cores) = result + print service, instance_cores if instance_cores >= FLAGS.max_cores: raise driver.NoValidHost("All hosts have too many cores") if self.service_is_up(service): diff --git a/nova/tests/scheduler_unittest.py b/nova/tests/scheduler_unittest.py index 27e100fa..b9371e86 100644 --- a/nova/tests/scheduler_unittest.py +++ b/nova/tests/scheduler_unittest.py @@ -33,7 +33,7 @@ from nova.scheduler import driver FLAGS = flags.FLAGS -flags.DECLARE('max_instances', 'nova.scheduler.simple') +flags.DECLARE('max_cores', 'nova.scheduler.simple') class TestDriver(driver.Scheduler): """Scheduler Driver for Tests""" @@ -75,7 +75,7 @@ class SimpleDriverTestCase(test.TrialTestCase): def setUp(self): # pylint: disable-msg=C0103 super(SimpleDriverTestCase, self).setUp() self.flags(connection_type='fake', - max_instances=4, + max_cores=4, scheduler_driver='nova.scheduler.simple.SimpleScheduler') self.scheduler = manager.SchedulerManager() self.context = None @@ -109,6 +109,7 @@ class SimpleDriverTestCase(test.TrialTestCase): inst['instance_type'] = 'm1.tiny' inst['mac_address'] = utils.generate_mac() inst['ami_launch_index'] = 0 + inst['vcpus'] = 1 return db.instance_create(self.context, inst)['id'] def test_hosts_are_up(self): @@ -125,10 +126,10 @@ class SimpleDriverTestCase(test.TrialTestCase): self.assertEqual(host, 'host2') self.service1.terminate_instance(self.context, instance_id) - def test_too_many_instances(self): + def test_too_many_cores(self): instance_ids1 = [] instance_ids2 = [] - for index in xrange(FLAGS.max_instances): + for index in xrange(FLAGS.max_cores): instance_id = self._create_instance() self.service1.run_instance(self.context, instance_id) instance_ids1.append(instance_id) @@ -139,7 +140,6 @@ class SimpleDriverTestCase(test.TrialTestCase): self.assertRaises(driver.NoValidHost, self.scheduler.driver.schedule_run_instance, self.context, - 'compute', instance_id) for instance_id in instance_ids1: self.service1.terminate_instance(self.context, instance_id) From 8950c9b656a26183719847bd3ce78da2f0c5cc92 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 19:40:38 -0700 Subject: [PATCH 77/87] tests for volumes work --- nova/scheduler/simple.py | 9 +-- nova/tests/scheduler_unittest.py | 126 +++++++++++++++++++++++++------ 2 files changed, 109 insertions(+), 26 deletions(-) diff --git a/nova/scheduler/simple.py b/nova/scheduler/simple.py index 3feeca84..c4ba17ca 100644 --- a/nova/scheduler/simple.py +++ b/nova/scheduler/simple.py @@ -39,12 +39,11 @@ class SimpleScheduler(chance.ChanceScheduler): def schedule_run_instance(self, context, instance_id, *_args, **_kwargs): """Picks a host that is up and has the fewest running instances.""" - + instance_ref = db.instance_get(context, instance_id) results = db.service_get_all_compute_sorted(context) for result in results: (service, instance_cores) = result - print service, instance_cores - if instance_cores >= FLAGS.max_cores: + if instance_cores + instance_ref['vcpus'] > FLAGS.max_cores: raise driver.NoValidHost("All hosts have too many cores") if self.service_is_up(service): db.instance_update(context, @@ -55,11 +54,11 @@ class SimpleScheduler(chance.ChanceScheduler): def schedule_create_volume(self, context, volume_id, *_args, **_kwargs): """Picks a host that is up and has the fewest volumes.""" - + volume_ref = db.volume_get(context, volume_id) results = db.service_get_all_volume_sorted(context) for result in results: (service, volume_gigabytes) = result - if volume_gigabytes >= FLAGS.max_gigabytes: + if volume_gigabytes + volume_ref['size'] > FLAGS.max_gigabytes: raise driver.NoValidHost("All hosts have too many gigabytes") if self.service_is_up(service): db.volume_update(context, diff --git a/nova/tests/scheduler_unittest.py b/nova/tests/scheduler_unittest.py index b9371e86..fde30f81 100644 --- a/nova/tests/scheduler_unittest.py +++ b/nova/tests/scheduler_unittest.py @@ -19,8 +19,6 @@ Tests For Scheduler """ -import mox - from nova import db from nova import flags from nova import service @@ -76,6 +74,8 @@ class SimpleDriverTestCase(test.TrialTestCase): super(SimpleDriverTestCase, self).setUp() self.flags(connection_type='fake', max_cores=4, + max_gigabytes=4, + volume_driver='nova.volume.driver.FakeAOEDriver', scheduler_driver='nova.scheduler.simple.SimpleScheduler') self.scheduler = manager.SchedulerManager() self.context = None @@ -83,27 +83,16 @@ class SimpleDriverTestCase(test.TrialTestCase): self.user = self.manager.create_user('fake', 'fake', 'fake') self.project = self.manager.create_project('fake', 'fake', 'fake') self.context = None - self.service1 = service.Service('host1', - 'nova-compute', - 'compute', - FLAGS.compute_manager) - self.service2 = service.Service('host2', - 'nova-compute', - 'compute', - FLAGS.compute_manager) def tearDown(self): # pylint: disable-msg=C0103 self.manager.delete_user(self.user) self.manager.delete_project(self.project) - self.service1.kill() - self.service2.kill() def _create_instance(self): """Create a test instance""" inst = {} inst['image_id'] = 'ami-test' inst['reservation_id'] = 'r-fakeres' - inst['launch_time'] = '10' inst['user_id'] = self.user.id inst['project_id'] = self.project.id inst['instance_type'] = 'm1.tiny' @@ -112,29 +101,70 @@ class SimpleDriverTestCase(test.TrialTestCase): inst['vcpus'] = 1 return db.instance_create(self.context, inst)['id'] + def _create_volume(self): + """Create a test volume""" + vol = {} + vol['image_id'] = 'ami-test' + vol['reservation_id'] = 'r-fakeres' + vol['size'] = 1 + return db.volume_create(self.context, vol)['id'] + def test_hosts_are_up(self): + """Ensures driver can find the hosts that are up""" # NOTE(vish): constructing service without create method # because we are going to use it without queue + compute1 = service.Service('host1', + 'nova-compute', + 'compute', + FLAGS.compute_manager) + compute2 = service.Service('host2', + 'nova-compute', + 'compute', + FLAGS.compute_manager) hosts = self.scheduler.driver.hosts_up(self.context, 'compute') self.assertEqual(len(hosts), 2) + compute1.kill() + compute2.kill() def test_least_busy_host_gets_instance(self): - instance_id = self._create_instance() - self.service1.run_instance(self.context, instance_id) + """Ensures the host with less cores gets the next one""" + compute1 = service.Service('host1', + 'nova-compute', + 'compute', + FLAGS.compute_manager) + compute2 = service.Service('host2', + 'nova-compute', + 'compute', + FLAGS.compute_manager) + instance_id1 = self._create_instance() + compute1.run_instance(self.context, instance_id1) + instance_id2 = self._create_instance() host = self.scheduler.driver.schedule_run_instance(self.context, - instance_id) + instance_id2) self.assertEqual(host, 'host2') - self.service1.terminate_instance(self.context, instance_id) + compute1.terminate_instance(self.context, instance_id1) + db.instance_destroy(self.context, instance_id2) + compute1.kill() + compute2.kill() def test_too_many_cores(self): + """Ensures we don't go over max cores""" + compute1 = service.Service('host1', + 'nova-compute', + 'compute', + FLAGS.compute_manager) + compute2 = service.Service('host2', + 'nova-compute', + 'compute', + FLAGS.compute_manager) instance_ids1 = [] instance_ids2 = [] for index in xrange(FLAGS.max_cores): instance_id = self._create_instance() - self.service1.run_instance(self.context, instance_id) + compute1.run_instance(self.context, instance_id) instance_ids1.append(instance_id) instance_id = self._create_instance() - self.service2.run_instance(self.context, instance_id) + compute2.run_instance(self.context, instance_id) instance_ids2.append(instance_id) instance_id = self._create_instance() self.assertRaises(driver.NoValidHost, @@ -142,6 +172,60 @@ class SimpleDriverTestCase(test.TrialTestCase): self.context, instance_id) for instance_id in instance_ids1: - self.service1.terminate_instance(self.context, instance_id) + compute1.terminate_instance(self.context, instance_id) for instance_id in instance_ids2: - self.service2.terminate_instance(self.context, instance_id) + compute2.terminate_instance(self.context, instance_id) + compute1.kill() + compute2.kill() + + def test_least_busy_host_gets_volume(self): + """Ensures the host with less gigabytes gets the next one""" + volume1 = service.Service('host1', + 'nova-volume', + 'volume', + FLAGS.volume_manager) + volume2 = service.Service('host2', + 'nova-volume', + 'volume', + FLAGS.volume_manager) + volume_id1 = self._create_volume() + volume1.create_volume(self.context, volume_id1) + volume_id2 = self._create_volume() + host = self.scheduler.driver.schedule_create_volume(self.context, + volume_id2) + self.assertEqual(host, 'host2') + volume1.delete_volume(self.context, volume_id1) + db.volume_destroy(self.context, volume_id2) + volume1.kill() + volume2.kill() + + def test_too_many_gigabytes(self): + """Ensures we don't go over max gigabytes""" + volume1 = service.Service('host1', + 'nova-volume', + 'volume', + FLAGS.volume_manager) + volume2 = service.Service('host2', + 'nova-volume', + 'volume', + FLAGS.volume_manager) + volume_ids1 = [] + volume_ids2 = [] + for index in xrange(FLAGS.max_gigabytes): + volume_id = self._create_volume() + volume1.create_volume(self.context, volume_id) + volume_ids1.append(volume_id) + volume_id = self._create_volume() + volume2.create_volume(self.context, volume_id) + volume_ids2.append(volume_id) + volume_id = self._create_volume() + self.assertRaises(driver.NoValidHost, + self.scheduler.driver.schedule_create_volume, + self.context, + volume_id) + for volume_id in volume_ids1: + volume1.delete_volume(self.context, volume_id) + for volume_id in volume_ids2: + volume2.delete_volume(self.context, volume_id) + volume1.kill() + volume2.kill() From 5aaa5ddda91457f43edeec5e7816631353914dac Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 19:43:02 -0700 Subject: [PATCH 78/87] move volume to the scheduler --- nova/endpoint/cloud.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 4fda484e..584c9c64 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -300,9 +300,11 @@ class CloudController(object): vol['attach_status'] = "detached" volume_ref = db.volume_create(context, vol) - rpc.cast(FLAGS.volume_topic, {"method": "create_volume", - "args": {"context": None, - "volume_id": volume_ref['id']}}) + rpc.cast(FLAGS.scheduler_topic, + {"method": "create_volume", + "args": {"context": None, + "topic": FLAGS.volume_topic, + "volume_id": volume_ref['id']}}) return {'volumeSet': [self._format_volume(context, volume_ref)]} From 318d6e7f6bb494b893328ab229b5015255c26d29 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 20:00:02 -0700 Subject: [PATCH 79/87] fix instance time --- nova/tests/compute_unittest.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nova/tests/compute_unittest.py b/nova/tests/compute_unittest.py index de2bf3d3..c983e05c 100644 --- a/nova/tests/compute_unittest.py +++ b/nova/tests/compute_unittest.py @@ -83,21 +83,21 @@ class ComputeTestCase(test.TrialTestCase): @defer.inlineCallbacks def test_run_terminate_timestamps(self): - """Make sure it is possible to run and terminate instance""" + """Make sure timestamps are set for launched and destroyed""" instance_id = self._create_instance() instance_ref = db.instance_get(self.context, instance_id) self.assertEqual(instance_ref['launched_at'], None) - self.assertEqual(instance_ref['terminated_at'], None) + self.assertEqual(instance_ref['deleted_at'], None) launch = datetime.datetime.utcnow() yield self.compute.run_instance(self.context, instance_id) instance_ref = db.instance_get(self.context, instance_id) self.assert_(instance_ref['launched_at'] > launch) - self.assertEqual(instance_ref['terminated_at'], None) + self.assertEqual(instance_ref['deleted_at'], None) terminate = datetime.datetime.utcnow() yield self.compute.terminate_instance(self.context, instance_id) instance_ref = db.instance_get({'deleted': True}, instance_id) self.assert_(instance_ref['launched_at'] < terminate) - self.assert_(instance_ref['terminated_at'] > terminate) + self.assert_(instance_ref['deleted_at'] > terminate) @defer.inlineCallbacks def test_reboot(self): From 65c5e450459ade71a010e00cc9cf15c3a3ae21ae Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Sat, 11 Sep 2010 22:48:59 -0700 Subject: [PATCH 80/87] don't allow deletion or attachment of volume unless it is available --- nova/endpoint/cloud.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 94a04fb1..6e2fedd6 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -314,6 +314,8 @@ class CloudController(object): def attach_volume(self, context, volume_id, instance_id, device, **kwargs): volume_ref = db.volume_get_by_str(context, volume_id) # TODO(vish): abstract status checking? + if volume_ref['status'] != "available": + raise exception.ApiError("Volume status must be available") if volume_ref['attach_status'] == "attached": raise exception.ApiError("Volume is already attached") instance_ref = db.instance_get_by_str(context, instance_id) @@ -336,10 +338,10 @@ class CloudController(object): volume_ref = db.volume_get_by_str(context, volume_id) instance_ref = db.volume_get_instance(context, volume_ref['id']) if not instance_ref: - raise exception.Error("Volume isn't attached to anything!") + raise exception.ApiError("Volume isn't attached to anything!") # TODO(vish): abstract status checking? if volume_ref['status'] == "available": - raise exception.Error("Volume is already detached") + raise exception.ApiError("Volume is already detached") try: host = instance_ref['host'] rpc.cast(db.queue_get_for(context, FLAGS.compute_topic, host), @@ -691,6 +693,8 @@ class CloudController(object): def delete_volume(self, context, volume_id, **kwargs): # TODO: return error if not authorized volume_ref = db.volume_get_by_str(context, volume_id) + if volume_ref['status'] != "available": + raise exception.ApiError("Volume status must be available") now = datetime.datetime.utcnow() db.volume_update(context, volume_ref['id'], {'terminated_at': now}) host = volume_ref['host'] From d039d7a30bd6ac90c566f1890b288a7010ce79f8 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Mon, 13 Sep 2010 09:03:14 +0200 Subject: [PATCH 81/87] Move vol.destroy() call out of the _check method in test_multiple_volume_race_condition test and into a callback of the DeferredList. This should fix the intermittent failure of that test. I /think/ test_too_many_volumes's failure was caused by test_multiple_volume_race_condition failure, since I have not been able to reproduce its failure after fixing this one. --- nova/tests/volume_unittest.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/nova/tests/volume_unittest.py b/nova/tests/volume_unittest.py index 2a07afe6..540b7158 100644 --- a/nova/tests/volume_unittest.py +++ b/nova/tests/volume_unittest.py @@ -128,7 +128,6 @@ class VolumeTestCase(test.TrialTestCase): volume_service.get_volume, volume_id) - @defer.inlineCallbacks def test_multiple_volume_race_condition(self): vol_size = "5" user_id = "fake" @@ -137,17 +136,28 @@ class VolumeTestCase(test.TrialTestCase): def _check(volume_id): vol = volume_service.get_volume(volume_id) shelf_blade = '%s.%s' % (vol['shelf_id'], vol['blade_id']) - self.assert_(shelf_blade not in shelf_blades) + self.assertTrue(shelf_blade not in shelf_blades, + "Same shelf/blade tuple came back twice") shelf_blades.append(shelf_blade) logging.debug("got %s" % shelf_blade) - vol.destroy() + return vol deferreds = [] for i in range(5): d = self.volume.create_volume(vol_size, user_id, project_id) d.addCallback(_check) d.addErrback(self.fail) deferreds.append(d) - yield defer.DeferredList(deferreds) + def destroy_volumes(retvals): + overall_succes = True + for success, volume in retvals: + if not success: + overall_succes = False + else: + volume.destroy() + self.assertTrue(overall_succes) + d = defer.DeferredList(deferreds) + d.addCallback(destroy_volumes) + return d def test_multi_node(self): # TODO(termie): Figure out how to test with two nodes, From 48bacdd68accf7f4dfeca413c6ecc9fb499efd5b Mon Sep 17 00:00:00 2001 From: Jesse Andrews Date: Mon, 13 Sep 2010 00:06:32 -0700 Subject: [PATCH 82/87] add a shell to nova-manage, which respects flags (taken from django) --- bin/nova-manage | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/bin/nova-manage b/bin/nova-manage index d2fd49d8..6e526676 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -17,6 +17,37 @@ # License for the specific language governing permissions and limitations # under the License. +# Interactive shell based on Django: +# +# Copyright (c) 2005, the Lawrence Journal-World +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of Django nor the names of its contributors may be used +# to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + """ CLI interface for nova management. Connects to the running ADMIN api in the api daemon. @@ -103,6 +134,29 @@ class VpnCommands(object): self.pipe.launch_vpn_instance(project_id) +class ShellCommands(object): + def run(self): + "Runs a Python interactive interpreter. Tries to use IPython, if it's available." + try: + import IPython + # Explicitly pass an empty list as arguments, because otherwise IPython + # would use sys.argv from this script. + shell = IPython.Shell.IPShell(argv=[]) + shell.mainloop() + except ImportError: + import code + try: # Try activating rlcompleter, because it's handy. + import readline + except ImportError: + pass + else: + # We don't have to wrap the following import in a 'try', because + # we already know 'readline' was imported successfully. + import rlcompleter + readline.parse_and_bind("tab:complete") + code.interact() + + class RoleCommands(object): """Class for managing roles.""" @@ -225,6 +279,7 @@ CATEGORIES = [ ('user', UserCommands), ('project', ProjectCommands), ('role', RoleCommands), + ('shell', ShellCommands), ('vpn', VpnCommands), ] From 68c0aaae457e5f5f69feaba7f77f6c1c8dce52e3 Mon Sep 17 00:00:00 2001 From: Michael Gundlach Date: Wed, 15 Sep 2010 17:17:20 -0400 Subject: [PATCH 83/87] Support querying version list --- run_tests.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/run_tests.py b/run_tests.py index 77aa9088..cf37b820 100644 --- a/run_tests.py +++ b/run_tests.py @@ -50,8 +50,10 @@ from nova import flags from nova import twistd from nova.tests.access_unittest import * -from nova.tests.auth_unittest import * from nova.tests.api_unittest import * +from nova.tests.api import * +from nova.tests.api.rackspace import * +from nova.tests.auth_unittest import * from nova.tests.cloud_unittest import * from nova.tests.compute_unittest import * from nova.tests.flags_unittest import * From 1542f6a32719a7c27730edc24020f5d2f9607954 Mon Sep 17 00:00:00 2001 From: Jesse Andrews Date: Sun, 19 Sep 2010 18:23:41 -0700 Subject: [PATCH 84/87] updated docstring --- nova/flags.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/flags.py b/nova/flags.py index efa14f9d..64dd9d45 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -169,7 +169,7 @@ def DECLARE(name, module_string, flag_values=FLAGS): DEFINE_list('region_list', [], - 'list of region,url pairs') + 'list of region|url pairs separated by commas') DEFINE_string('connection_type', 'libvirt', 'libvirt, xenapi or fake') DEFINE_integer('s3_port', 3333, 's3 port') DEFINE_string('s3_host', '127.0.0.1', 's3 host') From 6a20679377993db7b41be91ca86cf4a505ad5437 Mon Sep 17 00:00:00 2001 From: Michael Gundlach Date: Mon, 20 Sep 2010 17:02:32 -0400 Subject: [PATCH 86/87] Undo run_tests.py modification in the hopes of making this merge --- run_tests.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/run_tests.py b/run_tests.py index cf37b820..77aa9088 100644 --- a/run_tests.py +++ b/run_tests.py @@ -50,10 +50,8 @@ from nova import flags from nova import twistd from nova.tests.access_unittest import * -from nova.tests.api_unittest import * -from nova.tests.api import * -from nova.tests.api.rackspace import * from nova.tests.auth_unittest import * +from nova.tests.api_unittest import * from nova.tests.cloud_unittest import * from nova.tests.compute_unittest import * from nova.tests.flags_unittest import * From 4b45ebe4a53a1b4d124e64c5c6e2b8cf80f5445e Mon Sep 17 00:00:00 2001 From: Todd Willey Date: Mon, 20 Sep 2010 23:56:17 -0400 Subject: [PATCH 87/87] Don't use something the shell will escape as a separator. | is now =. --- nova/endpoint/cloud.py | 2 +- nova/flags.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index b28bb0dc..2b67af96 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -158,7 +158,7 @@ class CloudController(object): if FLAGS.region_list: regions = [] for region in FLAGS.region_list: - name, _sep, url = region.partition('|') + name, _sep, url = region.partition('=') regions.append({'regionName': name, 'regionEndpoint': url}) else: diff --git a/nova/flags.py b/nova/flags.py index 64dd9d45..c5dee285 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -169,7 +169,7 @@ def DECLARE(name, module_string, flag_values=FLAGS): DEFINE_list('region_list', [], - 'list of region|url pairs separated by commas') + 'list of region=url pairs separated by commas') DEFINE_string('connection_type', 'libvirt', 'libvirt, xenapi or fake') DEFINE_integer('s3_port', 3333, 's3 port') DEFINE_string('s3_host', '127.0.0.1', 's3 host')