2
Support code for the C{landscape-message} utility, which sends a text
3
message to the Landscape web UI via the landscape-client's dbus
4
messaging service (see L{landscape.plugins.dbus_message}).
8
from optparse import OptionParser
10
from twisted.python import log
11
from twisted.python.failure import Failure
12
from twisted.internet.defer import fail
14
from landscape.lib.dbus_util import get_bus
16
from landscape import VERSION
17
from landscape.broker.broker import BUS_NAME
18
from landscape.broker.remote import (
19
RemoteBroker, SecurityError, ServiceUnknownError, NoReplyError)
22
class AcceptedTypeError(Exception):
24
Raised when a message is sent without 'text-message' being an
29
class EmptyMessageError(Exception):
30
"""Raised when an empty message is provied."""
33
def send_message(text, broker):
34
"""Add a message to the queue via a remote broker.
36
The message is of type C{text-message}.
38
@param broker: The L{landscape.broker.remote.RemoteBroker}
39
object to use to send the message.
40
@return: A L{Deferred} which will fire with the result of the send.
41
@raise ServiceUnknownError: (Deferred) if an
42
org.freedesktop.DBus.Error.ServiceUnknown is raised from DBUS.
43
@raise SecurityError: (Deferred) if a security policy prevents
46
message = {"type": "text-message", "message": text}
47
response = broker.send_message(message, True)
51
def got_result(result):
52
print u"Message sent."
55
def got_error(failure):
57
An error occurred. Attempt to write a meaningful message if it's
58
an error we know about, otherwise just print the exception value.
60
print u"Failure sending message."
61
if failure.check(ServiceUnknownError):
62
print "Couldn't find the %s service." % BUS_NAME
63
print "Is the Landscape Client running?"
64
elif failure.check(SecurityError, NoReplyError):
65
print "Couldn't access the %s service." % BUS_NAME
66
print "You may need to run landscape-message as root."
67
elif failure.check(AcceptedTypeError):
68
print ("Server not accepting text messages. "
69
"Is Landscape Client registered with the server?")
71
print "Unknown error:", failure.type, failure.getErrorMessage()
74
def get_message(args):
76
print ("Please enter your message, and send EOF (Control + D after "
77
"newline) when done.")
78
message = sys.stdin.read().decode(sys.stdin.encoding)
80
message = u" ".join([x.decode(sys.stdin.encoding) for x in args[1:]])
82
raise EmptyMessageError("Text messages may not be empty.")
86
def got_accepted_types(accepted_types, broker, args):
87
from twisted.internet import reactor
89
if not "text-message" in accepted_types:
90
raise AcceptedTypeError("Text messages may not be created. Is "
91
"Landscape Client registered with the server?")
92
message = get_message(args)
93
d = send_message(message, broker)
94
d.addCallback(got_result)
98
def run(args=sys.argv):
99
"""Send a message to Landscape.
101
This function runs a Twisted reactor, prints various status
102
messages, and exits the process.
105
from twisted.internet.glib2reactor import install
107
from twisted.internet import reactor
109
parser = OptionParser(version=VERSION)
110
parser.add_option("-b", "--bus", default="system",
111
help="The DBUS bus to use to send the message.")
112
options, args = parser.parse_args(args)
115
broker = RemoteBroker(get_bus(options.bus))
120
result = broker.get_accepted_message_types()
121
result.addCallback(got_accepted_types, broker, args)
122
result.addErrback(got_error)
123
result.addBoth(lambda x: reactor.callWhenRunning(reactor.stop))