~adam-collard/landscape-charm/install-sources-keys

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from charmhelpers.core import hookenv
from charmhelpers.core.hookenv import ERROR

from lib.error import CharmError


class Hook(object):
    """Juju hook Abstraction, providing dependency injection for testing."""

    def __init__(self, hookenv=hookenv):
        """
        @param hookenv: The charm-helpers C{hookenv} module, will be replaced
            by tests.
        """
        self._hookenv = hookenv

    def __call__(self):
        """Invoke the hook.

        @return: An integer with the exit code for the hook.
        """
        self._hookenv.log("Invoke handler for %s" % self._hookenv.hook_name())
        try:
            self._run()
        except CharmError as error:
            self._hookenv.log(str(error), ERROR)
            return 1
        return 0

    def _run(self):
        """Do the job."""
        raise NotImplementedError("Must be implemented by sub-classes")