Skip to content

Commit

Permalink
Allows non-admin users to use simple scheduler
Browse files Browse the repository at this point in the history
Fixes bug 885955

Change-Id: I88be04cbc55e272162328a33656fc5d6e1831c32
  • Loading branch information
vishvananda committed Nov 4, 2011
1 parent 7c5fa14 commit 36f6da4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
16 changes: 9 additions & 7 deletions nova/scheduler/simple.py
Expand Up @@ -40,6 +40,7 @@ class SimpleScheduler(chance.ChanceScheduler):

def _schedule_instance(self, context, instance_opts, *_args, **_kwargs):
"""Picks a host that is up and has the fewest running instances."""
elevated = context.elevated()

availability_zone = instance_opts.get('availability_zone')

Expand All @@ -48,13 +49,12 @@ def _schedule_instance(self, context, instance_opts, *_args, **_kwargs):
zone, _x, host = availability_zone.partition(':')

if host and context.is_admin:
service = db.service_get_by_args(context.elevated(), host,
'nova-compute')
service = db.service_get_by_args(elevated, host, 'nova-compute')
if not self.service_is_up(service):
raise driver.WillNotSchedule(_("Host %s is not alive") % host)
return host

results = db.service_get_all_compute_sorted(context)
results = db.service_get_all_compute_sorted(elevated)
if zone:
results = [(service, cores) for (service, cores) in results
if service['availability_zone'] == zone]
Expand Down Expand Up @@ -90,22 +90,23 @@ def schedule_start_instance(self, context, instance_id, *_args, **_kwargs):

def schedule_create_volume(self, context, volume_id, *_args, **_kwargs):
"""Picks a host that is up and has the fewest volumes."""
elevated = context.elevated()

volume_ref = db.volume_get(context, volume_id)
availability_zone = volume_ref.get('availability_zone')

zone, host = None, None
if availability_zone:
zone, _x, host = availability_zone.partition(':')
if host and context.is_admin:
service = db.service_get_by_args(context.elevated(), host,
'nova-volume')
service = db.service_get_by_args(elevated, host, 'nova-volume')
if not self.service_is_up(service):
raise driver.WillNotSchedule(_("Host %s not available") % host)
driver.cast_to_volume_host(context, host, 'create_volume',
volume_id=volume_id, **_kwargs)
return None

results = db.service_get_all_volume_sorted(context)
results = db.service_get_all_volume_sorted(elevated)
if zone:
results = [(service, gigs) for (service, gigs) in results
if service['availability_zone'] == zone]
Expand All @@ -124,8 +125,9 @@ def schedule_create_volume(self, context, volume_id, *_args, **_kwargs):

def schedule_set_network_host(self, context, *_args, **_kwargs):
"""Picks a host that is up and has the fewest networks."""
elevated = context.elevated()

results = db.service_get_all_network_sorted(context)
results = db.service_get_all_network_sorted(elevated)
for result in results:
(service, instance_count) = result
if instance_count >= FLAGS.max_networks:
Expand Down
19 changes: 19 additions & 0 deletions nova/tests/scheduler/test_scheduler.py
Expand Up @@ -286,6 +286,25 @@ def _create_compute_service(self, **kwargs):
db.compute_node_create(self.context, dic)
return db.service_get(self.context, s_ref['id'])

def test_regular_user_can_schedule(self):
"""Ensures a non-admin can run an instance"""
compute1 = service.Service('host1',
'nova-compute',
'compute',
FLAGS.compute_manager)
compute1.start()
instance_id = _create_instance()['id']
ctxt = context.RequestContext('fake', 'fake', False)
global instance_ids
instance_ids = []
self.stubs.Set(SimpleScheduler,
'create_instance_db_entry', _fake_create_instance_db_entry)
self.stubs.Set(driver,
'cast_to_compute_host', _fake_cast_to_compute_host)
request_spec = _create_request_spec()
self.scheduler.driver.schedule_run_instance(ctxt, request_spec)
compute1.kill()

def test_doesnt_report_disabled_hosts_as_up_no_queue(self):
"""Ensures driver doesn't find hosts before they are enabled"""
# NOTE(vish): constructing service without create method
Expand Down

0 comments on commit 36f6da4

Please sign in to comment.