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