~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

« back to all changes in this revision

Viewing changes to landscape/manager/manager.py

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys
 
2
 
 
3
from logging import exception
 
4
 
 
5
from landscape.log import format_object
 
6
from landscape.plugin import Plugin, PluginRegistry, BrokerPlugin
 
7
from landscape.lib.dbus_util import method
 
8
 
 
9
# Protocol messages! Same constants are defined in the server.
 
10
FAILED = 5
 
11
SUCCEEDED = 6
 
12
 
 
13
SERVICE = "com.canonical.landscape.manager"
 
14
 
 
15
 
 
16
BUS_NAME = "com.canonical.landscape.Manager"
 
17
OBJECT_PATH = "/com/canonical/landscape/Manager"
 
18
IFACE_NAME = BUS_NAME
 
19
 
 
20
 
 
21
class ManagerDBusObject(BrokerPlugin):
 
22
    """A DBUS object which provides an interface to the Landscape Manager."""
 
23
 
 
24
    bus_name = BUS_NAME
 
25
    object_path = OBJECT_PATH
 
26
 
 
27
    ping = method(IFACE_NAME)(BrokerPlugin.ping)
 
28
    exit = method(IFACE_NAME)(BrokerPlugin.exit)
 
29
    message = method(IFACE_NAME)(BrokerPlugin.message)
 
30
 
 
31
 
 
32
class ManagerPluginRegistry(PluginRegistry):
 
33
    """Central point of integration for the Landscape Manager."""
 
34
 
 
35
    def __init__(self, reactor, broker, config, bus=None):
 
36
        super(ManagerPluginRegistry, self).__init__()
 
37
        self.reactor = reactor
 
38
        self.broker = broker
 
39
        self.config = config
 
40
        self.bus = bus
 
41
 
 
42
 
 
43
class ManagerPlugin(Plugin):
 
44
 
 
45
    def call_with_operation_result(self, message, callable, *args, **kwargs):
 
46
        """Send an operation-result message after calling C{callable}.
 
47
 
 
48
        If the function returns normally, an operation-result
 
49
        indicating success will be sent.  If the function raises an
 
50
        exception, an operation-result indicating failure will be
 
51
        sent.
 
52
 
 
53
        @param message: The original message.
 
54
        @param callable: The function to call to handle the message.
 
55
            C{args} and C{kwargs} are passed to it.
 
56
        """
 
57
        try:
 
58
            text = callable(*args, **kwargs)
 
59
        except:
 
60
            status = FAILED
 
61
            cls, obj = sys.exc_info()[:2]
 
62
            text = "%s: %s" % (cls.__name__, obj)
 
63
            exception("Error occured running message handler %s "
 
64
                      "with args %r %r.",
 
65
                      format_object(callable), args, kwargs)
 
66
        else:
 
67
            status = SUCCEEDED
 
68
        operation_result = {"type": "operation-result", "status": status,
 
69
                            "operation-id": message["operation-id"]}
 
70
        if text:
 
71
            operation_result["result-text"] = text
 
72
        return self.registry.broker.send_message(operation_result, urgent=True)