From: Angus Salkeld Date: Fri, 30 Mar 2012 10:28:37 +0000 (+1100) Subject: Move simpledb to db/anydbm just so we are using the new API. X-Git-Tag: 2014.1~2123 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=ba17e5286b78ce6e0189b96352327ce1dab934a9;p=openstack-build%2Fheat-build.git Move simpledb to db/anydbm just so we are using the new API. Signed-off-by: Angus Salkeld --- diff --git a/bin/heat-engine b/bin/heat-engine index df0176b5..cb9bb8ab 100755 --- a/bin/heat-engine +++ b/bin/heat-engine @@ -40,7 +40,7 @@ from heat.common import wsgi if __name__ == '__main__': try: - conf = config.HeatConfigOpts() + conf = config.HeatEngineConfigOpts() conf() app = config.load_paste_app(conf) diff --git a/heat/common/config.py b/heat/common/config.py index 5f446637..0928b4ba 100644 --- a/heat/common/config.py +++ b/heat/common/config.py @@ -47,13 +47,17 @@ class HeatConfigOpts(cfg.CommonConfigOpts): **kwargs) -class HeatCacheConfigOpts(HeatConfigOpts): +class HeatEngineConfigOpts(HeatConfigOpts): + + db_opts = [ + cfg.StrOpt('db_backend', default='heat.db.anydbm.api', help='The backend to use for db'), + ] def __init__(self, **kwargs): config_files = cfg.find_config_files(project='heat', - prog='heat-cache') - super(HeatCacheConfigOpts, self).__init__(config_files, **kwargs) - + prog='heat-engine') + super(HeatEngineConfigOpts, self).__init__(config_files, **kwargs) + self.register_cli_opts(self.db_opts) def setup_logging(conf): """ diff --git a/heat/common/utils.py b/heat/common/utils.py index 4bd3973f..04344264 100644 --- a/heat/common/utils.py +++ b/heat/common/utils.py @@ -25,6 +25,55 @@ import uuid from heat.common import exception +def import_class(import_str): + """Returns a class from a string including module and class.""" + mod_str, _sep, class_str = import_str.rpartition('.') + try: + __import__(mod_str) + return getattr(sys.modules[mod_str], class_str) + except (ImportError, ValueError, AttributeError), exc: + #LOG.debug(_('Inner Exception: %s'), exc) + raise exception.ClassNotFound(class_name=class_str, exception=exc) + + +def import_object(import_str): + """Returns an object including a module or module and class.""" + try: + __import__(import_str) + return sys.modules[import_str] + except ImportError: + cls = import_class(import_str) + return cls() + +class LazyPluggable(object): + """A pluggable backend loaded lazily based on some value.""" + + def __init__(self, pivot, **backends): + self.__backends = backends + self.__pivot = pivot + self.__backend = None + + def __get_backend(self): + if not self.__backend: + backend_name = FLAGS[self.__pivot] + if backend_name not in self.__backends: + raise exception.Error(_('Invalid backend: %s') % backend_name) + + backend = self.__backends[backend_name] + if isinstance(backend, tuple): + name = backend[0] + fromlist = backend[1] + else: + name = backend + fromlist = backend + + self.__backend = __import__(name, None, None, fromlist) + #LOG.debug(_('backend %s'), self.__backend) + return self.__backend + + def __getattr__(self, key): + backend = self.__get_backend() + return getattr(backend, key) def chunkreadable(iter, chunk_size=65536): """ diff --git a/heat/db/anydbm/__init__.py b/heat/db/anydbm/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/heat/engine/simpledb.py b/heat/db/anydbm/api.py similarity index 68% rename from heat/engine/simpledb.py rename to heat/db/anydbm/api.py index a3907991..5f1be480 100644 --- a/heat/engine/simpledb.py +++ b/heat/db/anydbm/api.py @@ -16,25 +16,43 @@ import anydbm import json -def event_append(event): - ''' - EventId The unique ID of this event. - Timestamp Time the status was updated. - ''' - name = event['StackName'] - d = anydbm.open('/var/lib/heat/%s.events.db' % name, 'c') - if d.has_key('lastid'): - newid = int(d['lastid']) + 1 - else: - newid = 1 - event['EventId'] = '%d' % newid - d['lastid'] = event['EventId'] - d[event['EventId']] = json.dumps(event) +def raw_template_get(context, template_id): + return 'test return value' - d.close() +def raw_template_get_all(context): + pass + +def raw_template_create(context, values): + pass + + +def parsed_template_get(context, template_id): + pass + +def parsed_template_get_all(context): + pass + +def parsed_template_create(context, values): + pass + + +def state_get(context, state_id): + pass +def state_get_all(context): + pass -def events_get(stack_id): +def state_create(context, values): + pass + + +def event_get(context, event_id): + pass + +def event_get_all(context): + pass + +def event_get_all_by_stack(context, stack_id): events = {'events': []} try: d = anydbm.open('/var/lib/heat/%s.events.db' % stack_id, 'r') @@ -48,3 +66,20 @@ def events_get(stack_id): d.close() return events +def event_create(context, event): + ''' + EventId The unique ID of this event. + Timestamp Time the status was updated. + ''' + name = event['StackName'] + d = anydbm.open('/var/lib/heat/%s.events.db' % name, 'c') + if d.has_key('lastid'): + newid = int(d['lastid']) + 1 + else: + newid = 1 + event['EventId'] = '%d' % newid + d['lastid'] = event['EventId'] + d[event['EventId']] = json.dumps(event) + + d.close() + diff --git a/heat/db/api.py b/heat/db/api.py index 639fae2f..ad3d7b56 100644 --- a/heat/db/api.py +++ b/heat/db/api.py @@ -26,20 +26,12 @@ The underlying driver is loaded as a :class:`LazyPluggable`. SQLAlchemy is currently the only supported backend. ''' -from nova import flags -from nova.openstack.common import cfg -from nova import utils - - -db_opts = [ - cfg.StrOpt('db_backend', default='db', help='The backend to use for db'), - ] - -FLAGS = flags.FLAGS -FLAGS.register_opts(db_opts) - -IMPL = utils.LazyPluggable('db_backend', db='heat.db.sqlalchemy.api') +from heat.openstack.common import cfg +from heat.common import utils +def configure(conf): + global IMPL + IMPL = utils.import_object(conf.db_backend) def raw_template_get(context, template_id): return IMPL.raw_template_get(context, template_id) diff --git a/heat/engine/api/v1/events.py b/heat/engine/api/v1/events.py index 941fa744..7fc676a4 100644 --- a/heat/engine/api/v1/events.py +++ b/heat/engine/api/v1/events.py @@ -28,7 +28,7 @@ from heat.common import exception from heat.common import wsgi from heat.engine import parser -from heat.engine import simpledb +from heat.db import api as db_api logger = logging.getLogger('heat.engine.api.v1.events') @@ -43,7 +43,7 @@ class EventsController(object): self.conf = conf def index(self, req, stack_id): - return simpledb.events_get(stack_id) + return db_api.event_get_all_by_stack(None, stack_id) def create_resource(conf): """Events resource factory method.""" diff --git a/heat/engine/api/v1/stacks.py b/heat/engine/api/v1/stacks.py index 99f82da6..684baefa 100644 --- a/heat/engine/api/v1/stacks.py +++ b/heat/engine/api/v1/stacks.py @@ -28,6 +28,7 @@ from heat.common import exception from heat.common import wsgi from heat.engine import parser +from heat.db import api as db_api logger = logging.getLogger('heat.engine.api.v1.stacks') @@ -41,6 +42,7 @@ class StacksController(object): def __init__(self, conf): self.conf = conf + db_api.configure(conf) def index(self, req, format='json'): logger.info('format is %s' % format) diff --git a/heat/engine/resources.py b/heat/engine/resources.py index 7abc491a..1d3f0d28 100644 --- a/heat/engine/resources.py +++ b/heat/engine/resources.py @@ -18,7 +18,7 @@ import os import time from novaclient.v1_1 import client -from heat.engine import simpledb +from heat.db import api as db_api logger = logging.getLogger('heat.engine.resources') @@ -72,7 +72,7 @@ class Resource(object): ev['ResourceType'] = self.t['Type'] ev['ResourceProperties'] = self.t['Properties'] - simpledb.event_append(ev) + db_api.event_create(None, ev) self.state = new_state def stop(self):