~free.ekanayaka/landscape-client/lucid-1.5.4-0ubuntu0.10.04.0

« back to all changes in this revision

Viewing changes to scripts/landscape-dbus-proxy

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-06-28 18:07:18 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100628180718-vytyqgbtkiirv5sb
Tags: 1.5.2.1-0ubuntu0.10.04.0
Filter duplicate network interfaces in get_active_interfaces (LP: #597000)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import os
 
4
import dbus
 
5
import dbus.service
 
6
import dbus.glib # This as side effects, don't remove it!
 
7
 
 
8
from dbus.service import Object, BusName, method
 
9
 
 
10
from twisted.internet import glib2reactor
 
11
glib2reactor.install()
 
12
from twisted.internet import reactor
 
13
 
 
14
from landscape.lib.bpickle import loads
 
15
from landscape.lib.lock import lock_path, LockError
 
16
from landscape.reactor import TwistedReactor
 
17
from landscape.deployment import Configuration
 
18
from landscape.broker.amp import RemoteBrokerConnector
 
19
 
 
20
 
 
21
BUS_NAME = "com.canonical.landscape.Broker"
 
22
OBJECT_PATH = "/com/canonical/landscape/Broker"
 
23
 
 
24
 
 
25
def array_to_string(array):
 
26
    """Convert an L{Array} of L{Byte}s (or integers) to a Python str."""
 
27
    result = []
 
28
    for item in array:
 
29
        if item < 0:
 
30
            item = item + 256
 
31
        result.append(chr(item))
 
32
    return "".join(result)
 
33
 
 
34
 
 
35
class BrokerDBusObject(Object):
 
36
    """A DBus-published object proxing L{RemoteBroker.send_message}.
 
37
 
 
38
    It is used when upgrading from a DBus-based version of the Landscape client
 
39
    to the newer AMP-based one, for letting the old package-changer process
 
40
    performing the upgrade communicate with the new version of the client.
 
41
    """
 
42
 
 
43
    bus_name = BUS_NAME
 
44
    object_path = OBJECT_PATH
 
45
 
 
46
    def __init__(self, config):
 
47
        super(BrokerDBusObject, self).__init__(BusName(
 
48
            self.bus_name, dbus.SystemBus()), object_path=self.object_path)
 
49
        self.config = config
 
50
 
 
51
    @method(BUS_NAME)
 
52
    def send_message(self, message, urgent=True):
 
53
        """Queue the given message in the message exchange."""
 
54
        message = loads(array_to_string(message))
 
55
 
 
56
        def cb_connected(broker):
 
57
            result = broker.send_message(message, urgent=True)
 
58
            return result.addCallback(cb_done)
 
59
 
 
60
        def cb_done(ignored):
 
61
            return reactor.stop()
 
62
 
 
63
        twisted_reactor = TwistedReactor()
 
64
        connector = RemoteBrokerConnector(twisted_reactor, self.config)
 
65
        connected = connector.connect()
 
66
        connected.addCallback(cb_connected)
 
67
 
 
68
 
 
69
config = Configuration()
 
70
lock_filename = os.path.join(config.data_path, "package", "changer.lock")
 
71
try:
 
72
    lock_path(lock_filename)
 
73
except LockError:
 
74
    # The package-changer is running, this means that we're upgrading from
 
75
    # a non-AMP version and that the upgrade is Landscape driven, so let's
 
76
    # expose the DBus broker proxy to give a chance to the package-changer
 
77
    # to send its result message.
 
78
    remote = BrokerDBusObject(config)
 
79
    reactor.run()