summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/models.py
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-07-30 10:24:12 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-07-30 10:24:12 -0400
commit8b438fda3ae2d9516dbfb6014c280b68036c17e1 (patch)
treeb8acb22b313e4b57797a227b42f69b95b54bd976 /src/lib/Bcfg2/Server/models.py
parent7a008a0b1b4d3b819da5a6544ac15faab3cbb28a (diff)
downloadbcfg2-8b438fda3ae2d9516dbfb6014c280b68036c17e1.tar.gz
bcfg2-8b438fda3ae2d9516dbfb6014c280b68036c17e1.tar.bz2
bcfg2-8b438fda3ae2d9516dbfb6014c280b68036c17e1.zip
Metadata and other improvements:
* Added support for Client tag in groups.xml * Added support for nested Group tags in groups.xml * Added support for negated groups in groups.xml * Added DatabaseBacked plugin mixin to easily allow plugins to connect to a database specified in global database settings in bcfg2.conf * Added DBMetadata plugin that uses relational DB to store client records instead of writing to clients.xml
Diffstat (limited to 'src/lib/Bcfg2/Server/models.py')
-rw-r--r--src/lib/Bcfg2/Server/models.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/lib/Bcfg2/Server/models.py b/src/lib/Bcfg2/Server/models.py
new file mode 100644
index 000000000..ba9ea761c
--- /dev/null
+++ b/src/lib/Bcfg2/Server/models.py
@@ -0,0 +1,62 @@
+import sys
+import logging
+import Bcfg2.Options
+import Bcfg2.Server.Plugins
+from django.db import models
+from Bcfg2.Bcfg2Py3k import ConfigParser
+
+logger = logging.getLogger('Bcfg2.Server.models')
+
+MODELS = []
+
+def load_models(plugins=None, cfile='/etc/bcfg2.conf', quiet=True):
+ global MODELS
+
+ if plugins is None:
+ # we want to provide a different default plugin list --
+ # namely, _all_ plugins, so that the database is guaranteed to
+ # work, even if /etc/bcfg2.conf isn't set up properly
+ plugin_opt = Bcfg2.Options.SERVER_PLUGINS
+ plugin_opt.default = Bcfg2.Server.Plugins.__all__
+
+ setup = Bcfg2.Options.OptionParser(dict(plugins=plugin_opt,
+ configfile=Bcfg2.Options.CFILE),
+ quiet=quiet)
+ setup.parse([Bcfg2.Options.CFILE.cmd, cfile])
+ plugins = setup['plugins']
+
+ if MODELS:
+ # load_models() has been called once, so first unload all of
+ # the models; otherwise we might call load_models() with no
+ # arguments, end up with _all_ models loaded, and then in a
+ # subsequent call only load a subset of models
+ for model in MODELS:
+ delattr(sys.modules[__name__], model)
+ MODELS = []
+
+ for plugin in plugins:
+ try:
+ mod = getattr(__import__("Bcfg2.Server.Plugins.%s" %
+ plugin).Server.Plugins, plugin)
+ except ImportError:
+ try:
+ mod = __import__(plugin)
+ except:
+ if plugins != Bcfg2.Server.Plugins.__all__:
+ # only produce errors if the default plugin list
+ # was not used -- i.e., if the config file was set
+ # up. don't produce errors when trying to load
+ # all plugins, IOW
+ err = sys.exc_info()[1]
+ logger.error("Failed to load plugin %s: %s" % (plugin, err))
+ continue
+ for sym in dir(mod):
+ obj = getattr(mod, sym)
+ if hasattr(obj, "__bases__") and models.Model in obj.__bases__:
+ print("Adding %s to models" % sym)
+ setattr(sys.modules[__name__], sym, obj)
+ MODELS.append(sym)
+
+# basic invocation to ensure that a default set of models is loaded,
+# and thus that this module will always work.
+load_models(quiet=True)