Move db access out into a seperate file.

This is in preparation for using the openstack.common Service classes.

Change-Id: I5ac78202199217387b91685e9d0409ae08cc18e0
This commit is contained in:
Angus Salkeld 2012-11-01 13:27:56 +11:00
parent 5aa03745eb
commit b631dc3a7e
5 changed files with 52 additions and 8 deletions

View File

@ -19,6 +19,7 @@
from nova import manager
from ceilometer import extension_manager
from ceilometer.compute import resources
from ceilometer.openstack.common import cfg
from ceilometer.openstack.common import log
from ceilometer import publish
@ -38,6 +39,9 @@ PLUGIN_NAMESPACE = 'ceilometer.poll.central'
class AgentManager(manager.Manager):
def __init__(self, host=None):
super(AgentManager, self).__init__(host=host)
self.resources = resources.Resources()
def init_host(self):
self.ext_manager = extension_manager.ActivatedExtensionManager(

View File

@ -21,6 +21,7 @@ from nova import manager
from ceilometer import extension_manager
from ceilometer.openstack.common import cfg
from ceilometer.openstack.common import log
from ceilometer.compute import resources
from ceilometer import publish
OPTS = [
@ -40,6 +41,10 @@ PLUGIN_NAMESPACE = 'ceilometer.poll.compute'
class AgentManager(manager.Manager):
def __init__(self, host=None):
super(AgentManager, self).__init__(host=host)
self.resources = resources.Resources()
def init_host(self):
self.ext_manager = extension_manager.ActivatedExtensionManager(
namespace=PLUGIN_NAMESPACE,
@ -75,8 +80,6 @@ class AgentManager(manager.Manager):
def periodic_tasks(self, context, raise_on_error=False):
"""Tasks to be run at a periodic interval."""
# FIXME(dhellmann): How do we get a list of instances without
# talking directly to the database?
for instance in self.db.instance_get_all_by_host(context, self.host):
for instance in self.resources.instance_get_all_by_host(context):
if instance['vm_state'] != 'error':
self.poll_instance(context, instance)

View File

@ -0,0 +1,37 @@
#
# 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.
from ceilometer.openstack.common import cfg
from nova.openstack.common import importutils
db_driver_opt = cfg.StrOpt('db_driver',
default='nova.db',
help='driver to use for database access')
cfg.CONF.register_opt(db_driver_opt)
# FIXME(dhellmann): How do we get a list of instances without
# talking directly to the database?
class Resources(object):
def __init__(self):
self.db = importutils.import_module(cfg.CONF.db_driver)
def instance_get_all_by_host(self, context):
return self.db.instance_get_all_by_host(context, cfg.CONF.host)
def floating_ip_get_all(self, context):
return self.db.floating_ip_get_all(context)

View File

@ -31,7 +31,7 @@ class FloatingIPPollster(plugin.CentralPollster):
def get_counters(self, manager, context):
try:
ips = manager.db.floating_ip_get_all(context)
ips = manager.resources.floating_ip_get_all(context)
except exception.FloatingIpNotFoundForHost:
pass
else:

View File

@ -84,10 +84,10 @@ class TestRunTasks(base.TestCase):
stillborn_instance = mock.MagicMock()
stillborn_instance.name = 'stillborn'
stillborn_instance.vm_state = 'error'
self.mox.StubOutWithMock(self.mgr.db, 'instance_get_all_by_host')
self.mgr.db.instance_get_all_by_host(
None,
self.mgr.host,
self.mox.StubOutWithMock(self.mgr.resources,
'instance_get_all_by_host')
self.mgr.resources.instance_get_all_by_host(
None
).AndReturn([self.instance, stillborn_instance])
self.mox.ReplayAll()