| 1 |
# |
| 2 |
# Copyright (C) 2015 FreeIPA Contributors see COPYING for license |
| 3 |
# |
| 4 |
|
| 5 |
""" |
| 6 |
Common stuff. |
| 7 |
""" |
| 8 |
|
| 9 |
import logging |
| 10 |
|
| 11 |
from . import core |
| 12 |
from .util import from_ |
| 13 |
|
| 14 |
__all__ = ['step', 'Installable', 'Interactive', 'installer', |
| 15 |
'uninstaller'] |
| 16 |
|
| 17 |
logger = logging.getLogger(__name__) |
| 18 |
|
| 19 |
|
| 20 |
def step(): |
| 21 |
def decorator(func): |
| 22 |
cls = core.Component(Step) |
| 23 |
cls._installer = staticmethod(func) |
| 24 |
return cls |
| 25 |
|
| 26 |
return decorator |
| 27 |
|
| 28 |
|
| 29 |
class Installable(core.Configurable): |
| 30 |
""" |
| 31 |
Configurable which does install or uninstall. |
| 32 |
""" |
| 33 |
|
| 34 |
uninstalling = core.Property(False) |
| 35 |
|
| 36 |
def _get_components(self): |
| 37 |
components = super(Installable, self)._get_components() |
| 38 |
if self.uninstalling: # pylint: disable=using-constant-test |
| 39 |
components = reversed(list(components)) |
| 40 |
return components |
| 41 |
|
| 42 |
def _configure(self): |
| 43 |
if self.uninstalling: # pylint: disable=using-constant-test |
| 44 |
return self._uninstall() |
| 45 |
else: |
| 46 |
return self._install() |
| 47 |
|
| 48 |
def _install(self): |
| 49 |
assert not hasattr(super(Installable, self), '_install') |
| 50 |
|
| 51 |
return super(Installable, self)._configure() |
| 52 |
|
| 53 |
def _uninstall(self): |
| 54 |
assert not hasattr(super(Installable, self), '_uninstall') |
| 55 |
|
| 56 |
return super(Installable, self)._configure() |
| 57 |
|
| 58 |
|
| 59 |
class Step(Installable): |
| 60 |
@property |
| 61 |
def parent(self): |
| 62 |
raise AttributeError('parent') |
| 63 |
|
| 64 |
def _install(self): |
| 65 |
for unused in self._installer(self.parent): |
| 66 |
yield from_(super(Step, self)._install()) |
| 67 |
|
| 68 |
@staticmethod |
| 69 |
def _installer(obj): |
| 70 |
yield |
| 71 |
|
| 72 |
def _uninstall(self): |
| 73 |
for unused in self._uninstaller(self.parent): |
| 74 |
yield from_(super(Step, self)._uninstall()) |
| 75 |
|
| 76 |
@staticmethod |
| 77 |
def _uninstaller(obj): |
| 78 |
yield |
| 79 |
|
| 80 |
@classmethod |
| 81 |
def uninstaller(cls, func): |
| 82 |
cls._uninstaller = staticmethod(func) |
| 83 |
return cls |
| 84 |
|
| 85 |
|
| 86 |
class Interactive(core.Configurable): |
| 87 |
interactive = core.Property(False) |
| 88 |
|
| 89 |
|
| 90 |
def installer(cls): |
| 91 |
class Installer(cls, Installable): |
| 92 |
def __init__(self, **kwargs): |
| 93 |
super(Installer, self).__init__(uninstalling=False, |
| 94 |
**kwargs) |
| 95 |
Installer.__name__ = 'installer({0})'.format(cls.__name__) |
| 96 |
|
| 97 |
return Installer |
| 98 |
|
| 99 |
|
| 100 |
def uninstaller(cls): |
| 101 |
class Uninstaller(cls, Installable): |
| 102 |
def __init__(self, **kwargs): |
| 103 |
super(Uninstaller, self).__init__(uninstalling=True, |
| 104 |
**kwargs) |
| 105 |
Uninstaller.__name__ = 'uninstaller({0})'.format(cls.__name__) |
| 106 |
|
| 107 |
return Uninstaller |