2008-07-27 23:34:25 -05:00
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2008-07-27 23:34:25 -05:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-07-27 23:34:25 -05:00
|
|
|
|
|
|
|
"""
|
2008-08-14 15:32:35 -05:00
|
|
|
Plugin framework.
|
2008-08-14 13:59:12 -05:00
|
|
|
|
|
|
|
The classes in this module make heavy use of Python container emulation. If
|
|
|
|
you are unfamiliar with this Python feature, see
|
|
|
|
http://docs.python.org/ref/sequence-types.html
|
2008-07-27 23:34:25 -05:00
|
|
|
"""
|
|
|
|
|
2008-08-05 01:33:09 -05:00
|
|
|
import re
|
2008-10-31 19:17:08 -05:00
|
|
|
import sys
|
2008-07-27 23:34:25 -05:00
|
|
|
import inspect
|
2008-10-30 02:11:33 -05:00
|
|
|
import threading
|
2008-10-31 14:27:42 -05:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from os import path
|
2008-11-06 12:57:21 -06:00
|
|
|
import subprocess
|
2009-01-27 17:28:50 -06:00
|
|
|
import optparse
|
2009-04-23 07:51:59 -05:00
|
|
|
import errors
|
2008-12-22 16:41:24 -06:00
|
|
|
from config import Env
|
2008-10-27 01:23:43 -05:00
|
|
|
import util
|
2010-02-08 06:03:28 -06:00
|
|
|
import text
|
2009-01-02 01:46:45 -06:00
|
|
|
from base import ReadOnly, NameSpace, lock, islocked, check_name
|
2009-01-22 10:58:35 -06:00
|
|
|
from constants import DEFAULT_CONFIG, FORMAT_STDERR, FORMAT_FILE
|
2008-08-14 13:50:21 -05:00
|
|
|
|
2010-02-08 06:03:28 -06:00
|
|
|
# FIXME: Updated constants.TYPE_ERROR to use this clearer format from wehjit:
|
|
|
|
TYPE_ERROR = '%s: need a %r; got a %r: %r'
|
|
|
|
|
2011-02-09 16:02:10 -06:00
|
|
|
def is_production_mode(obj):
|
|
|
|
"""
|
|
|
|
If the object has self.env.mode defined and that mode is
|
|
|
|
production return True, otherwise return False.
|
|
|
|
"""
|
|
|
|
if getattr(obj, 'env', None) is None:
|
|
|
|
return False
|
|
|
|
if getattr(obj.env, 'mode', None) is None:
|
|
|
|
return False
|
|
|
|
return obj.env.mode == 'production'
|
|
|
|
|
2008-08-14 13:50:21 -05:00
|
|
|
|
2008-08-14 20:04:19 -05:00
|
|
|
class SetProxy(ReadOnly):
|
2008-08-14 20:24:51 -05:00
|
|
|
"""
|
2008-08-14 22:24:37 -05:00
|
|
|
A read-only container with set/sequence behaviour.
|
2008-08-14 20:24:51 -05:00
|
|
|
|
2008-08-14 22:24:37 -05:00
|
|
|
This container acts as a proxy to an actual set-like object (a set,
|
|
|
|
frozenset, or dict) that is passed to the constructor. To the extent
|
|
|
|
possible in Python, this underlying set-like object cannot be modified
|
|
|
|
through the SetProxy... which just means you wont do it accidentally.
|
2008-08-14 20:24:51 -05:00
|
|
|
"""
|
2008-08-14 20:04:19 -05:00
|
|
|
def __init__(self, s):
|
2008-08-14 22:24:37 -05:00
|
|
|
"""
|
|
|
|
:param s: The target set-like object (a set, frozenset, or dict)
|
|
|
|
"""
|
2008-08-14 20:04:19 -05:00
|
|
|
allowed = (set, frozenset, dict)
|
|
|
|
if type(s) not in allowed:
|
|
|
|
raise TypeError('%r not in %r' % (type(s), allowed))
|
|
|
|
self.__s = s
|
|
|
|
lock(self)
|
|
|
|
|
|
|
|
def __len__(self):
|
2008-08-14 22:24:37 -05:00
|
|
|
"""
|
2008-10-18 02:02:31 -05:00
|
|
|
Return the number of items in this container.
|
2008-08-14 22:24:37 -05:00
|
|
|
"""
|
2008-08-14 20:04:19 -05:00
|
|
|
return len(self.__s)
|
|
|
|
|
|
|
|
def __iter__(self):
|
2008-08-14 22:24:37 -05:00
|
|
|
"""
|
2008-10-18 02:02:31 -05:00
|
|
|
Iterate (in ascending order) through keys.
|
2008-08-14 22:24:37 -05:00
|
|
|
"""
|
2008-08-14 20:04:19 -05:00
|
|
|
for key in sorted(self.__s):
|
|
|
|
yield key
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
2008-08-14 22:24:37 -05:00
|
|
|
"""
|
2008-10-18 02:02:31 -05:00
|
|
|
Return True if this container contains ``key``.
|
2008-08-14 22:24:37 -05:00
|
|
|
|
2008-10-18 02:02:31 -05:00
|
|
|
:param key: The key to test for membership.
|
2008-08-14 22:24:37 -05:00
|
|
|
"""
|
2008-08-14 20:04:19 -05:00
|
|
|
return key in self.__s
|
|
|
|
|
|
|
|
|
|
|
|
class DictProxy(SetProxy):
|
2008-08-14 20:24:51 -05:00
|
|
|
"""
|
2008-08-14 22:24:37 -05:00
|
|
|
A read-only container with mapping behaviour.
|
|
|
|
|
|
|
|
This container acts as a proxy to an actual mapping object (a dict) that
|
|
|
|
is passed to the constructor. To the extent possible in Python, this
|
|
|
|
underlying mapping object cannot be modified through the DictProxy...
|
|
|
|
which just means you wont do it accidentally.
|
|
|
|
|
|
|
|
Also see `SetProxy`.
|
2008-08-14 20:24:51 -05:00
|
|
|
"""
|
2008-08-14 20:04:19 -05:00
|
|
|
def __init__(self, d):
|
2008-08-14 22:24:37 -05:00
|
|
|
"""
|
|
|
|
:param d: The target mapping object (a dict)
|
|
|
|
"""
|
2008-08-14 20:04:19 -05:00
|
|
|
if type(d) is not dict:
|
|
|
|
raise TypeError('%r is not %r' % (type(d), dict))
|
|
|
|
self.__d = d
|
|
|
|
super(DictProxy, self).__init__(d)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
"""
|
2008-10-18 02:02:31 -05:00
|
|
|
Return the value corresponding to ``key``.
|
2008-08-14 22:24:37 -05:00
|
|
|
|
|
|
|
:param key: The key of the value you wish to retrieve.
|
2008-08-14 20:04:19 -05:00
|
|
|
"""
|
|
|
|
return self.__d[key]
|
|
|
|
|
2008-08-15 00:19:02 -05:00
|
|
|
def __call__(self):
|
|
|
|
"""
|
2008-10-18 02:02:31 -05:00
|
|
|
Iterate (in ascending order by key) through values.
|
2008-08-15 00:19:02 -05:00
|
|
|
"""
|
|
|
|
for key in self:
|
|
|
|
yield self.__d[key]
|
|
|
|
|
2008-08-14 20:04:19 -05:00
|
|
|
|
2008-08-14 20:24:51 -05:00
|
|
|
class MagicDict(DictProxy):
|
|
|
|
"""
|
2008-10-17 21:50:34 -05:00
|
|
|
A mapping container whose values can be accessed as attributes.
|
2008-08-14 22:24:37 -05:00
|
|
|
|
2008-10-17 21:50:34 -05:00
|
|
|
For example:
|
2008-08-14 20:24:51 -05:00
|
|
|
|
2008-10-17 21:50:34 -05:00
|
|
|
>>> magic = MagicDict({'the_key': 'the value'})
|
|
|
|
>>> magic['the_key']
|
|
|
|
'the value'
|
|
|
|
>>> magic.the_key
|
|
|
|
'the value'
|
2008-08-14 20:24:51 -05:00
|
|
|
|
2008-08-14 22:24:37 -05:00
|
|
|
This container acts as a proxy to an actual mapping object (a dict) that
|
|
|
|
is passed to the constructor. To the extent possible in Python, this
|
|
|
|
underlying mapping object cannot be modified through the MagicDict...
|
|
|
|
which just means you wont do it accidentally.
|
|
|
|
|
|
|
|
Also see `DictProxy` and `SetProxy`.
|
2008-08-14 20:24:51 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
"""
|
2008-10-18 02:02:31 -05:00
|
|
|
Return the value corresponding to ``name``.
|
2008-08-14 22:24:37 -05:00
|
|
|
|
|
|
|
:param name: The name of the attribute you wish to retrieve.
|
2008-08-14 20:24:51 -05:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self[name]
|
|
|
|
except KeyError:
|
2008-08-14 22:24:37 -05:00
|
|
|
raise AttributeError('no magic attribute %r' % name)
|
2008-08-14 20:24:51 -05:00
|
|
|
|
|
|
|
|
2008-08-14 02:10:07 -05:00
|
|
|
class Plugin(ReadOnly):
|
|
|
|
"""
|
|
|
|
Base class for all plugins.
|
|
|
|
"""
|
|
|
|
|
2010-02-08 06:03:28 -06:00
|
|
|
label = None
|
|
|
|
|
2008-11-24 11:09:30 -06:00
|
|
|
def __init__(self):
|
2010-01-27 06:59:09 -06:00
|
|
|
self.__api = None
|
2008-12-17 22:47:43 -06:00
|
|
|
cls = self.__class__
|
|
|
|
self.name = cls.__name__
|
|
|
|
self.module = cls.__module__
|
|
|
|
self.fullname = '%s.%s' % (self.module, self.name)
|
2009-10-14 16:08:30 -05:00
|
|
|
self.bases = tuple(
|
|
|
|
'%s.%s' % (b.__module__, b.__name__) for b in cls.__bases__
|
|
|
|
)
|
2008-12-18 00:08:52 -06:00
|
|
|
self.doc = inspect.getdoc(cls)
|
|
|
|
if self.doc is None:
|
|
|
|
self.summary = '<%s>' % self.fullname
|
|
|
|
else:
|
|
|
|
self.summary = self.doc.split('\n\n', 1)[0]
|
2008-12-21 20:34:32 -06:00
|
|
|
log = logging.getLogger(self.fullname)
|
|
|
|
for name in ('debug', 'info', 'warning', 'error', 'critical', 'exception'):
|
|
|
|
if hasattr(self, name):
|
|
|
|
raise StandardError(
|
|
|
|
'%s.%s attribute (%r) conflicts with Plugin logger' % (
|
|
|
|
self.name, name, getattr(self, name))
|
|
|
|
)
|
2008-11-24 11:09:30 -06:00
|
|
|
setattr(self, name, getattr(log, name))
|
2010-02-08 06:03:28 -06:00
|
|
|
if self.label is None:
|
|
|
|
self.label = text.FixMe(self.name + '.label')
|
|
|
|
if not isinstance(self.label, text.LazyText):
|
|
|
|
raise TypeError(
|
|
|
|
TYPE_ERROR % (
|
|
|
|
self.fullname + '.label',
|
|
|
|
text.LazyText,
|
|
|
|
type(self.label),
|
|
|
|
self.label
|
|
|
|
)
|
|
|
|
)
|
2008-11-24 11:09:30 -06:00
|
|
|
|
2008-08-14 02:10:07 -05:00
|
|
|
def __get_api(self):
|
|
|
|
"""
|
2008-10-18 02:02:31 -05:00
|
|
|
Return `API` instance passed to `finalize()`.
|
|
|
|
|
|
|
|
If `finalize()` has not yet been called, None is returned.
|
2008-08-14 02:10:07 -05:00
|
|
|
"""
|
|
|
|
return self.__api
|
|
|
|
api = property(__get_api)
|
|
|
|
|
2008-09-21 16:50:56 -05:00
|
|
|
def finalize(self):
|
2008-08-14 02:10:07 -05:00
|
|
|
"""
|
|
|
|
"""
|
2011-02-09 16:02:10 -06:00
|
|
|
if not is_production_mode(self):
|
|
|
|
lock(self)
|
2008-08-14 02:10:07 -05:00
|
|
|
|
2008-09-21 16:30:19 -05:00
|
|
|
def set_api(self, api):
|
|
|
|
"""
|
|
|
|
Set reference to `API` instance.
|
|
|
|
"""
|
|
|
|
assert self.__api is None, 'set_api() can only be called once'
|
|
|
|
assert api is not None, 'set_api() argument cannot be None'
|
|
|
|
self.__api = api
|
2008-10-30 15:11:24 -05:00
|
|
|
if not isinstance(api, API):
|
|
|
|
return
|
|
|
|
for name in api:
|
|
|
|
assert not hasattr(self, name)
|
|
|
|
setattr(self, name, api[name])
|
2008-12-21 20:58:48 -06:00
|
|
|
# FIXME: the 'log' attribute is depreciated. See Plugin.__init__()
|
2008-10-30 15:11:24 -05:00
|
|
|
for name in ('env', 'context', 'log'):
|
|
|
|
if hasattr(api, name):
|
|
|
|
assert not hasattr(self, name)
|
|
|
|
setattr(self, name, getattr(api, name))
|
2008-09-21 16:30:19 -05:00
|
|
|
|
2008-12-21 18:12:00 -06:00
|
|
|
def call(self, executable, *args):
|
2008-11-06 12:57:21 -06:00
|
|
|
"""
|
2008-12-21 18:12:00 -06:00
|
|
|
Call ``executable`` with ``args`` using subprocess.call().
|
2008-11-06 12:57:21 -06:00
|
|
|
|
2008-12-21 18:12:00 -06:00
|
|
|
If the call exits with a non-zero exit status,
|
2009-04-23 07:51:59 -05:00
|
|
|
`ipalib.errors.SubprocessError` is raised, from which you can retrieve
|
2008-12-21 18:12:00 -06:00
|
|
|
the exit code by checking the SubprocessError.returncode attribute.
|
|
|
|
|
|
|
|
This method does *not* return what ``executable`` sent to stdout... for
|
|
|
|
that, use `Plugin.callread()`.
|
2008-11-06 12:57:21 -06:00
|
|
|
"""
|
2008-12-21 18:12:00 -06:00
|
|
|
argv = (executable,) + args
|
|
|
|
self.debug('Calling %r', argv)
|
2009-01-03 19:02:58 -06:00
|
|
|
code = subprocess.call(argv)
|
|
|
|
if code != 0:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.SubprocessError(returncode=code, argv=argv)
|
2008-11-06 12:57:21 -06:00
|
|
|
|
2008-08-14 02:10:07 -05:00
|
|
|
def __repr__(self):
|
|
|
|
"""
|
2008-10-18 02:02:31 -05:00
|
|
|
Return 'module_name.class_name()' representation.
|
|
|
|
|
|
|
|
This representation could be used to instantiate this Plugin
|
|
|
|
instance given the appropriate environment.
|
2008-08-14 02:10:07 -05:00
|
|
|
"""
|
2008-09-08 16:51:05 -05:00
|
|
|
return '%s.%s()' % (
|
2008-08-14 02:10:07 -05:00
|
|
|
self.__class__.__module__,
|
|
|
|
self.__class__.__name__
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2008-08-15 00:07:17 -05:00
|
|
|
class Registrar(DictProxy):
|
2008-08-14 13:50:21 -05:00
|
|
|
"""
|
|
|
|
Collects plugin classes as they are registered.
|
|
|
|
|
|
|
|
The Registrar does not instantiate plugins... it only implements the
|
|
|
|
override logic and stores the plugins in a namespace per allowed base
|
|
|
|
class.
|
|
|
|
|
|
|
|
The plugins are instantiated when `API.finalize()` is called.
|
|
|
|
"""
|
2008-08-08 12:11:29 -05:00
|
|
|
def __init__(self, *allowed):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-14 04:01:02 -05:00
|
|
|
:param allowed: Base classes from which plugins accepted by this
|
2008-08-14 03:28:48 -05:00
|
|
|
Registrar must subclass.
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-15 00:07:17 -05:00
|
|
|
self.__allowed = dict((base, {}) for base in allowed)
|
2008-08-08 16:40:03 -05:00
|
|
|
self.__registered = set()
|
2008-08-15 00:07:17 -05:00
|
|
|
super(Registrar, self).__init__(
|
|
|
|
dict(self.__base_iter())
|
|
|
|
)
|
|
|
|
|
|
|
|
def __base_iter(self):
|
|
|
|
for (base, sub_d) in self.__allowed.iteritems():
|
|
|
|
assert inspect.isclass(base)
|
|
|
|
name = base.__name__
|
|
|
|
assert not hasattr(self, name)
|
|
|
|
setattr(self, name, MagicDict(sub_d))
|
|
|
|
yield (name, base)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-14 04:38:28 -05:00
|
|
|
def __findbases(self, klass):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-14 04:38:28 -05:00
|
|
|
Iterates through allowed bases that ``klass`` is a subclass of.
|
|
|
|
|
2009-04-23 07:51:59 -05:00
|
|
|
Raises `errors.PluginSubclassError` if ``klass`` is not a subclass of
|
2009-01-03 19:08:39 -06:00
|
|
|
any allowed base.
|
2008-08-14 04:38:28 -05:00
|
|
|
|
2009-01-03 19:08:39 -06:00
|
|
|
:param klass: The plugin class to find bases for.
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-14 04:38:28 -05:00
|
|
|
assert inspect.isclass(klass)
|
2008-08-08 16:40:03 -05:00
|
|
|
found = False
|
2008-08-15 00:07:17 -05:00
|
|
|
for (base, sub_d) in self.__allowed.iteritems():
|
2008-08-14 04:38:28 -05:00
|
|
|
if issubclass(klass, base):
|
2008-08-08 16:40:03 -05:00
|
|
|
found = True
|
2008-08-15 00:07:17 -05:00
|
|
|
yield (base, sub_d)
|
2008-08-08 16:40:03 -05:00
|
|
|
if not found:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.PluginSubclassError(
|
2009-01-03 18:27:53 -06:00
|
|
|
plugin=klass, bases=self.__allowed.keys()
|
|
|
|
)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-14 04:38:28 -05:00
|
|
|
def __call__(self, klass, override=False):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-14 04:38:28 -05:00
|
|
|
Register the plugin ``klass``.
|
|
|
|
|
|
|
|
:param klass: A subclass of `Plugin` to attempt to register.
|
|
|
|
:param override: If true, override an already registered plugin.
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-14 04:38:28 -05:00
|
|
|
if not inspect.isclass(klass):
|
2009-01-03 18:27:53 -06:00
|
|
|
raise TypeError('plugin must be a class; got %r' % klass)
|
2008-08-08 16:40:03 -05:00
|
|
|
|
|
|
|
# Raise DuplicateError if this exact class was already registered:
|
2008-08-14 04:38:28 -05:00
|
|
|
if klass in self.__registered:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.PluginDuplicateError(plugin=klass)
|
2008-08-08 16:40:03 -05:00
|
|
|
|
|
|
|
# Find the base class or raise SubclassError:
|
2008-08-15 00:07:17 -05:00
|
|
|
for (base, sub_d) in self.__findbases(klass):
|
2008-08-08 16:40:03 -05:00
|
|
|
# Check override:
|
2008-08-14 04:38:28 -05:00
|
|
|
if klass.__name__ in sub_d:
|
2008-08-08 16:40:03 -05:00
|
|
|
if not override:
|
2008-08-15 00:07:17 -05:00
|
|
|
# Must use override=True to override:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.PluginOverrideError(
|
2009-01-03 18:27:53 -06:00
|
|
|
base=base.__name__,
|
|
|
|
name=klass.__name__,
|
|
|
|
plugin=klass,
|
|
|
|
)
|
2008-08-08 16:40:03 -05:00
|
|
|
else:
|
|
|
|
if override:
|
2008-08-15 00:07:17 -05:00
|
|
|
# There was nothing already registered to override:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.PluginMissingOverrideError(
|
2009-01-03 18:27:53 -06:00
|
|
|
base=base.__name__,
|
|
|
|
name=klass.__name__,
|
|
|
|
plugin=klass,
|
|
|
|
)
|
2008-08-08 16:40:03 -05:00
|
|
|
|
|
|
|
# The plugin is okay, add to sub_d:
|
2008-08-14 04:38:28 -05:00
|
|
|
sub_d[klass.__name__] = klass
|
2008-08-08 16:40:03 -05:00
|
|
|
|
|
|
|
# The plugin is okay, add to __registered:
|
2008-08-14 04:38:28 -05:00
|
|
|
self.__registered.add(klass)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-01 16:25:46 -05:00
|
|
|
|
2008-08-14 20:32:20 -05:00
|
|
|
class API(DictProxy):
|
2008-08-14 16:40:37 -05:00
|
|
|
"""
|
|
|
|
Dynamic API object through which `Plugin` instances are accessed.
|
|
|
|
"""
|
2008-08-12 18:40:36 -05:00
|
|
|
|
2008-10-02 13:24:05 -05:00
|
|
|
def __init__(self, *allowed):
|
2008-08-14 17:13:42 -05:00
|
|
|
self.__d = dict()
|
2008-10-27 00:28:06 -05:00
|
|
|
self.__done = set()
|
2008-08-08 16:28:56 -05:00
|
|
|
self.register = Registrar(*allowed)
|
2008-10-27 16:19:49 -05:00
|
|
|
self.env = Env()
|
2008-08-14 20:32:20 -05:00
|
|
|
super(API, self).__init__(self.__d)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
def __doing(self, name):
|
|
|
|
if name in self.__done:
|
|
|
|
raise StandardError(
|
|
|
|
'%s.%s() already called' % (self.__class__.__name__, name)
|
|
|
|
)
|
|
|
|
self.__done.add(name)
|
|
|
|
|
|
|
|
def __do_if_not_done(self, name):
|
|
|
|
if name not in self.__done:
|
|
|
|
getattr(self, name)()
|
|
|
|
|
|
|
|
def isdone(self, name):
|
|
|
|
return name in self.__done
|
|
|
|
|
|
|
|
def bootstrap(self, **overrides):
|
|
|
|
"""
|
2008-10-31 14:27:42 -05:00
|
|
|
Initialize environment variables and logging.
|
2008-10-27 00:28:06 -05:00
|
|
|
"""
|
|
|
|
self.__doing('bootstrap')
|
2008-10-27 16:19:49 -05:00
|
|
|
self.env._bootstrap(**overrides)
|
2008-10-31 14:27:42 -05:00
|
|
|
self.env._finalize_core(**dict(DEFAULT_CONFIG))
|
2009-01-22 10:58:35 -06:00
|
|
|
log = logging.getLogger()
|
2008-10-31 14:27:42 -05:00
|
|
|
object.__setattr__(self, 'log', log)
|
2010-02-09 05:57:23 -06:00
|
|
|
|
|
|
|
# If logging has already been configured somewhere else (like in the
|
|
|
|
# installer), don't add handlers or change levels:
|
2011-01-14 15:45:37 -06:00
|
|
|
if len(log.handlers) > 0 or self.env.validate_api:
|
2010-02-09 05:57:23 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
if self.env.debug:
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
else:
|
|
|
|
log.setLevel(logging.INFO)
|
2008-10-31 14:27:42 -05:00
|
|
|
|
|
|
|
# Add stderr handler:
|
|
|
|
stderr = logging.StreamHandler()
|
|
|
|
if self.env.debug:
|
2008-10-31 19:55:32 -05:00
|
|
|
stderr.setLevel(logging.DEBUG)
|
2010-06-03 16:07:24 -05:00
|
|
|
elif self.env.verbose > 0:
|
2008-10-31 19:55:32 -05:00
|
|
|
stderr.setLevel(logging.INFO)
|
2008-10-31 14:27:42 -05:00
|
|
|
else:
|
2008-10-31 19:55:32 -05:00
|
|
|
stderr.setLevel(logging.WARNING)
|
2009-01-22 10:58:35 -06:00
|
|
|
stderr.setFormatter(util.LogFormatter(FORMAT_STDERR))
|
2008-10-31 14:27:42 -05:00
|
|
|
log.addHandler(stderr)
|
|
|
|
|
|
|
|
# Add file handler:
|
2008-11-07 03:26:38 -06:00
|
|
|
if self.env.mode in ('dummy', 'unit_test'):
|
2009-01-22 10:58:35 -06:00
|
|
|
return # But not if in unit-test mode
|
2009-01-30 21:53:32 -06:00
|
|
|
if self.env.log is None:
|
|
|
|
return
|
2008-10-31 14:27:42 -05:00
|
|
|
log_dir = path.dirname(self.env.log)
|
|
|
|
if not path.isdir(log_dir):
|
|
|
|
try:
|
|
|
|
os.makedirs(log_dir)
|
|
|
|
except OSError:
|
2009-02-05 20:08:33 -06:00
|
|
|
log.error('Could not create log_dir %r', log_dir)
|
2008-10-31 14:27:42 -05:00
|
|
|
return
|
2009-02-05 20:08:33 -06:00
|
|
|
try:
|
|
|
|
handler = logging.FileHandler(self.env.log)
|
|
|
|
except IOError, e:
|
|
|
|
log.error('Cannot open log file %r: %s', self.env.log, e.strerror)
|
|
|
|
return
|
2009-01-22 10:58:35 -06:00
|
|
|
handler.setFormatter(util.LogFormatter(FORMAT_FILE))
|
2008-10-31 14:27:42 -05:00
|
|
|
if self.env.debug:
|
2008-10-31 19:55:32 -05:00
|
|
|
handler.setLevel(logging.DEBUG)
|
2008-10-31 14:27:42 -05:00
|
|
|
else:
|
2008-10-31 19:55:32 -05:00
|
|
|
handler.setLevel(logging.INFO)
|
2008-10-31 14:27:42 -05:00
|
|
|
log.addHandler(handler)
|
|
|
|
|
2009-01-28 14:05:26 -06:00
|
|
|
def build_global_parser(self, parser=None, context=None):
|
2009-01-27 17:28:50 -06:00
|
|
|
"""
|
|
|
|
Add global options to an optparse.OptionParser instance.
|
|
|
|
"""
|
|
|
|
if parser is None:
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.disable_interspersed_args()
|
|
|
|
parser.add_option('-e', dest='env', metavar='KEY=VAL', action='append',
|
|
|
|
help='Set environment variable KEY to VAL',
|
|
|
|
)
|
|
|
|
parser.add_option('-c', dest='conf', metavar='FILE',
|
|
|
|
help='Load configuration from FILE',
|
|
|
|
)
|
|
|
|
parser.add_option('-d', '--debug', action='store_true',
|
|
|
|
help='Produce full debuging output',
|
|
|
|
)
|
2010-06-03 16:07:24 -05:00
|
|
|
parser.add_option('-v', '--verbose', action='count',
|
|
|
|
help='Produce more verbose output. A second -v displays the XML-RPC request',
|
2009-01-27 17:28:50 -06:00
|
|
|
)
|
|
|
|
if context == 'cli':
|
|
|
|
parser.add_option('-a', '--prompt-all', action='store_true',
|
2009-01-28 14:05:26 -06:00
|
|
|
help='Prompt for ALL values (even if optional)'
|
2008-10-31 19:17:08 -05:00
|
|
|
)
|
2009-01-27 17:28:50 -06:00
|
|
|
parser.add_option('-n', '--no-prompt', action='store_false',
|
2009-01-28 14:05:26 -06:00
|
|
|
dest='interactive',
|
|
|
|
help='Prompt for NO values (even if required)'
|
2009-01-27 17:28:50 -06:00
|
|
|
)
|
2010-07-26 16:54:38 -05:00
|
|
|
parser.add_option('-f', '--no-fallback', action='store_false',
|
|
|
|
dest='fallback',
|
|
|
|
help='Only use the server configured in /etc/ipa/default.conf'
|
|
|
|
)
|
2009-11-18 15:50:16 -06:00
|
|
|
topics = optparse.OptionGroup(parser, "Available help topics",
|
|
|
|
"ipa help topics")
|
|
|
|
cmds = optparse.OptionGroup(parser, "Available commands",
|
|
|
|
"ipa help commands")
|
|
|
|
parser.add_option_group(topics)
|
|
|
|
parser.add_option_group(cmds)
|
|
|
|
|
2009-01-27 17:28:50 -06:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def bootstrap_with_global_options(self, parser=None, context=None):
|
2009-01-28 14:05:26 -06:00
|
|
|
parser = self.build_global_parser(parser, context)
|
2009-01-27 17:28:50 -06:00
|
|
|
(options, args) = parser.parse_args()
|
2008-10-31 19:17:08 -05:00
|
|
|
overrides = {}
|
|
|
|
if options.env is not None:
|
|
|
|
assert type(options.env) is list
|
|
|
|
for item in options.env:
|
|
|
|
try:
|
|
|
|
(key, value) = item.split('=', 1)
|
|
|
|
except ValueError:
|
|
|
|
# FIXME: this should raise an IPA exception with an
|
|
|
|
# error code.
|
|
|
|
# --Jason, 2008-10-31
|
|
|
|
pass
|
|
|
|
overrides[str(key.strip())] = value.strip()
|
2010-07-26 16:54:38 -05:00
|
|
|
for key in ('conf', 'debug', 'verbose', 'prompt_all', 'interactive',
|
|
|
|
'fallback'):
|
2008-10-31 19:17:08 -05:00
|
|
|
value = getattr(options, key, None)
|
|
|
|
if value is not None:
|
|
|
|
overrides[key] = value
|
2010-01-26 07:39:00 -06:00
|
|
|
if hasattr(options, 'prod'):
|
|
|
|
overrides['webui_prod'] = options.prod
|
2008-10-31 19:17:08 -05:00
|
|
|
if context is not None:
|
|
|
|
overrides['context'] = context
|
|
|
|
self.bootstrap(**overrides)
|
2009-10-13 12:28:00 -05:00
|
|
|
return (options, args)
|
2008-10-27 00:28:06 -05:00
|
|
|
|
2008-10-28 00:39:43 -05:00
|
|
|
def load_plugins(self):
|
2008-10-27 00:53:44 -05:00
|
|
|
"""
|
|
|
|
Load plugins from all standard locations.
|
|
|
|
|
|
|
|
`API.bootstrap` will automatically be called if it hasn't been
|
|
|
|
already.
|
|
|
|
"""
|
|
|
|
self.__doing('load_plugins')
|
|
|
|
self.__do_if_not_done('bootstrap')
|
2008-11-07 03:26:38 -06:00
|
|
|
if self.env.mode in ('dummy', 'unit_test'):
|
2008-10-27 01:23:43 -05:00
|
|
|
return
|
2009-02-12 03:10:12 -06:00
|
|
|
self.import_plugins('ipalib')
|
2008-10-28 00:39:43 -05:00
|
|
|
if self.env.in_server:
|
2009-02-12 03:10:12 -06:00
|
|
|
self.import_plugins('ipaserver')
|
|
|
|
|
|
|
|
# FIXME: This method has no unit test
|
|
|
|
def import_plugins(self, package):
|
|
|
|
"""
|
|
|
|
Import modules in ``plugins`` sub-package of ``package``.
|
|
|
|
"""
|
|
|
|
subpackage = '%s.plugins' % package
|
|
|
|
try:
|
|
|
|
parent = __import__(package)
|
|
|
|
plugins = __import__(subpackage).plugins
|
|
|
|
except ImportError, e:
|
|
|
|
self.log.error(
|
|
|
|
'cannot import plugins sub-package %s: %s', subpackage, e
|
|
|
|
)
|
|
|
|
raise e
|
|
|
|
parent_dir = path.dirname(path.abspath(parent.__file__))
|
|
|
|
plugins_dir = path.dirname(path.abspath(plugins.__file__))
|
|
|
|
if parent_dir == plugins_dir:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.PluginsPackageError(
|
2009-02-12 03:10:12 -06:00
|
|
|
name=subpackage, file=plugins.__file__
|
|
|
|
)
|
|
|
|
self.log.debug('importing all plugin modules in %r...', plugins_dir)
|
|
|
|
for (name, pyfile) in util.find_modules_in_dir(plugins_dir):
|
|
|
|
fullname = '%s.%s' % (subpackage, name)
|
|
|
|
self.log.debug('importing plugin module %r', pyfile)
|
|
|
|
try:
|
|
|
|
__import__(fullname)
|
2009-04-23 07:51:59 -05:00
|
|
|
except errors.SkipPluginModule, e:
|
2009-02-12 03:10:12 -06:00
|
|
|
self.log.info(
|
|
|
|
'skipping plugin module %s: %s', fullname, e.reason
|
|
|
|
)
|
|
|
|
except StandardError, e:
|
2010-06-25 12:37:27 -05:00
|
|
|
if self.env.startup_traceback:
|
|
|
|
import traceback
|
|
|
|
self.log.error('could not load plugin module %r\n%s', pyfile, traceback.format_exc())
|
2009-02-12 03:10:12 -06:00
|
|
|
raise e
|
2008-10-27 00:53:44 -05:00
|
|
|
|
2008-08-12 17:52:37 -05:00
|
|
|
def finalize(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
|
|
|
Finalize the registration, instantiate the plugins.
|
2008-10-27 00:53:44 -05:00
|
|
|
|
|
|
|
`API.bootstrap` will automatically be called if it hasn't been
|
|
|
|
already.
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-10-27 00:28:06 -05:00
|
|
|
self.__doing('finalize')
|
2008-10-31 13:29:59 -05:00
|
|
|
self.__do_if_not_done('load_plugins')
|
2008-08-14 17:13:42 -05:00
|
|
|
|
2008-09-23 23:44:52 -05:00
|
|
|
class PluginInstance(object):
|
|
|
|
"""
|
|
|
|
Represents a plugin instance.
|
|
|
|
"""
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
def __init__(self, klass):
|
|
|
|
self.created = self.next()
|
|
|
|
self.klass = klass
|
|
|
|
self.instance = klass()
|
|
|
|
self.bases = []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def next(cls):
|
|
|
|
cls.i += 1
|
|
|
|
return cls.i
|
|
|
|
|
|
|
|
class PluginInfo(ReadOnly):
|
|
|
|
def __init__(self, p):
|
|
|
|
assert isinstance(p, PluginInstance)
|
|
|
|
self.created = p.created
|
|
|
|
self.name = p.klass.__name__
|
|
|
|
self.module = str(p.klass.__module__)
|
|
|
|
self.plugin = '%s.%s' % (self.module, self.name)
|
|
|
|
self.bases = tuple(b.__name__ for b in p.bases)
|
|
|
|
lock(self)
|
|
|
|
|
|
|
|
plugins = {}
|
|
|
|
def plugin_iter(base, subclasses):
|
|
|
|
for klass in subclasses:
|
|
|
|
assert issubclass(klass, base)
|
|
|
|
if klass not in plugins:
|
|
|
|
plugins[klass] = PluginInstance(klass)
|
|
|
|
p = plugins[klass]
|
|
|
|
assert base not in p.bases
|
|
|
|
p.bases.append(base)
|
2009-08-04 03:41:11 -05:00
|
|
|
yield p.instance
|
2008-08-08 16:40:03 -05:00
|
|
|
|
2011-02-09 16:02:10 -06:00
|
|
|
production_mode = is_production_mode(self)
|
2008-08-15 00:07:17 -05:00
|
|
|
for name in self.register:
|
|
|
|
base = self.register[name]
|
|
|
|
magic = getattr(self.register, name)
|
|
|
|
namespace = NameSpace(
|
|
|
|
plugin_iter(base, (magic[k] for k in magic))
|
|
|
|
)
|
2011-02-09 16:02:10 -06:00
|
|
|
if not production_mode:
|
2011-01-19 10:24:31 -06:00
|
|
|
assert not (
|
|
|
|
name in self.__d or hasattr(self, name)
|
|
|
|
)
|
2008-08-14 17:13:42 -05:00
|
|
|
self.__d[name] = namespace
|
|
|
|
object.__setattr__(self, name, namespace)
|
|
|
|
|
2008-09-23 23:44:52 -05:00
|
|
|
for p in plugins.itervalues():
|
|
|
|
p.instance.set_api(self)
|
2011-02-09 16:02:10 -06:00
|
|
|
if not production_mode:
|
2011-01-19 10:24:31 -06:00
|
|
|
assert p.instance.api is self
|
2008-09-21 16:50:56 -05:00
|
|
|
|
2008-09-23 23:44:52 -05:00
|
|
|
for p in plugins.itervalues():
|
|
|
|
p.instance.finalize()
|
2011-02-09 16:02:10 -06:00
|
|
|
if not production_mode:
|
2011-01-19 10:24:31 -06:00
|
|
|
assert islocked(p.instance) is True
|
2008-08-12 18:40:36 -05:00
|
|
|
object.__setattr__(self, '_API__finalized', True)
|
2008-09-23 23:44:52 -05:00
|
|
|
tuple(PluginInfo(p) for p in plugins.itervalues())
|
|
|
|
object.__setattr__(self, 'plugins',
|
|
|
|
tuple(PluginInfo(p) for p in plugins.itervalues())
|
|
|
|
)
|