-
Notifications
You must be signed in to change notification settings - Fork 5
/
flumotion2stomp.py
393 lines (343 loc) · 14.5 KB
/
flumotion2stomp.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/python
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
# (C) Copyright 2007 Zaheer Abbas Merali <zaheerabbas at merali dot org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import sys
import copy
import os
root = os.path.join('/usr/lib', 'flumotion', 'python')
sys.path.insert(0, root)
from stompservice import StompClientFactory
from orbited import json
from flumotion.component import feed
from twisted.internet import reactor, defer
from twisted.internet.task import LoopingCall
from flumotion.twisted import pb, flavors
from flumotion.common import log, errors
from flumotion.common.i18n import Translator
from flumotion.common.planet import moods
from flumotion.admin import connections
from flumotion.admin.command import utils
from flumotion.admin.admin import AdminModel
from flumotion.monitor.nagios import util
from zope.interface import implements
import optparse
# register the unjellyable
from flumotion.common import componentui
class FluToStomp:
implements(flavors.IStateListener)
def __init__(self, args):
self._components = []
self.uistates = {}
self.uistates_by_name = {}
self._translator = Translator()
log.init()
parser = optparse.OptionParser()
parser.add_option('-d', '--debug',
action="store", type="string", dest="debug",
help="set debug levels")
parser.add_option('-u', '--usage',
action="store_true", dest="usage",
help="show a usage message")
parser.add_option('-m', '--manager',
action="store", type="string", dest="manager",
help="the manager to connect to, e.g. localhost:7531")
parser.add_option('', '--no-ssl',
action="store_true", dest="no_ssl",
help="disable encryption when connecting to the manager")
parser.add_option('-s', '--stomp-port', action="store", type="string",
dest="stomp")
options, args = parser.parse_args(args)
if options.debug:
log.setFluDebug(options.debug)
if options.usage:
self.usage(args)
if not options.manager or not options.stomp:
self.usage(args)
print "need to connect to stomp port %s" % (options.stomp,)
self.options = options
connection = connections.parsePBConnectionInfo(options.manager,
not options.no_ssl)
self.model = model = AdminModel()
self.stomp_client = StompClient()
reactor.connectTCP("localhost", int(self.options.stomp), self.stomp_client)
self.model.connect('connected', self._connected)
self.model.connect('disconnected', self._disconnected)
self.model.connect('update', self._update)
d = model.connectToManager(connection)
def failed(failure):
if failure.check(errors.ConnectionRefusedError):
print "Manager refused connection. Check your user and password."
elif failure.check(errors.ConnectionFailedError):
message = "".join(failure.value.args)
print "Connection to manager failed: %s" % message
else:
print ("Exception while connecting to manager: %s"
% log.getFailureMessage(failure))
return failure
d.addErrback(failed)
d.addErrback(lambda x: reactor.stop())
#d.addCallback(self.manager_connected)
@defer.inlineCallbacks
def _connected(self, admin):
d = self.manager_connected(admin)
yield d
self.stomp_client.send_changes({"action": "connected"})
def _disconnected(self, admin):
flows = self.planet.get('flows')
flow = flows[0]
for f in flows:
if f.get('name') == 'default':
flow = f
break
flow.removeListener(self)
self.planet = None
for uistate in self.uistates:
uistate.removeListener(self)
self.uistates = {}
self.uistates_by_name = {}
for c in self._components:
c.removeListener(self)
self._components = []
self.stomp_client.stop_statuses()
self.stomp_client.send_changes({"action": "disconnected"})
def _update(self, admin):
self.stomp_client.send_changes({"action": "update"})
@defer.inlineCallbacks
def manager_connected(self, model):
try:
psd = model.callRemote('getPlanetState')
yield psd
planet = psd.result
self.planet = planet
flows = planet.get('flows')
if flows:
flow = flows[0]
for f in flows:
if f.get('name') == 'default':
self.default_flow = flow = f
break
self._components = flow.get('components')
flow.addListener(self, append=self.flow_state_append, remove=self.flow_state_remove)
for c in self._components:
self.new_component(c)
self.planet.addListener(self, append=self.planet_state_append, remove=self.planet_state_remove)
self.stomp_client.restart_statuses()
except Exception, e:
print log.getExceptionMessage(e)
def usage(self, args, exitval=0):
print "usage: %s [OPTIONS] -m MANAGER " \
"-s STOMPPORT" % args[0]
print ''
print 'See %s -h for help on the available options.' % args[0]
sys.exit(exitval)
def components(self):
l = []
for c in self._components:
l.append(self.parse_component(c))
return l
def parse_component(self, state):
component = {}
for k in state.keys():
if k == 'parent':
continue
elif k == 'messages':
messages = []
for m in state.get('messages'):
messages.append({"mid": getattr(m, "id"),
"text":self._translator.translate(m),
"description":m.getDescription(),
"timestamp":m.getTimeStamp(),
"debug":m.debug,
"level":m.level,
"priority":m.priority})
component['messages'] = messages
else:
component[k] = state.get(k)
return component
def component_state_set(self, state, key, value):
component = self.parse_component(state)
print "Component changed %r to %r" % (key, value)
if key == 'mood' and value != 0:
self.stop_listening_for_uistate_on_component(state.get('name'))
self.stomp_client.send_changes({ "action": "change", "component": component })
def planet_state_append(self, state, key, value):
if key == 'flows':
if value.get('name') == 'default':
self._components = value.get('components')
for c in self._components:
component = self.parse_component(c)
self.new_component(c)
self.stomp_client.send_changes({ "action": "add", "component": component })
value.addListener(self, append=self.flow_state_append, remove=self.flow_state_remove)
self.default_flow = value
def planet_state_remove(self, state, key, value):
if key == 'flows':
if value.get('name') == 'default':
for c in self._components:
self.stop_listening_for_uistate_on_component(c)
self.stomp_client.send_changes({ "action": "remove", "component": c })
self._components = []
def flow_state_append(self, state, key, value):
if key == 'components':
component = self.parse_component(value)
self.new_component(value)
self.stomp_client.send_changes({ "action": "add", "component": component })
def flow_state_remove(self, state, key, value):
if key == 'components':
component = value.get('name')
self.stop_listening_for_uistate_on_component(component)
self.stomp_client.send_changes({ "action": "remove", "component": component })
def stop_listening_for_uistate_on_component(self, component):
print "trying to stop listening on uistate for %r" % (component,)
if component in self.uistates_by_name:
uistate = self.uistates_by_name[component]
uistate.removeListener(self)
del self.uistates_by_name[component]
del self.uistates[uistate]
def new_component(self, state):
state.addListener(self, set_= self.component_state_set)
def run_command(self, message):
command = message["command"]
component = message.get("component", None)
params = message.get("params", [])
method = message.get("method", None)
# allow only specific commands
if command not in ["componentCallRemote", "invokeOnComponents", "componentStart", "componentStop"]:
return False
if not component:
return False
state = None
if command == "invokeOnComponents":
state = component
else:
for c in self._components:
if c.get('name') == component:
state = c
break
if command in ["componentStart", "componentStop"]:
need_method = False
else:
need_method = True
if need_method and not method:
print "need method but none specified"
return False
if not state:
print "component %s not found" % (component,)
return False
try:
print "about to run %s on %r" % (command, state)
if need_method:
d = self.model.callRemote(command, state, method, *params)
else:
d = self.model.callRemote(command, state, *params)
return d
except Exception, e:
print "Got exception %r running %s" % (e, command)
return False
@defer.inlineCallbacks
def poll_uistate(self, component):
state = None
if component in self.uistates_by_name:
parsed = self.parse_uistate(self.uistates_by_name[component])
for key in parsed:
self.stomp_client.send_uistate(component, key, parsed[key])
print "No need to add listener, already listening for uistate of %r" % (component,)
defer.returnValue(False)
for c in self._components:
if c.get('name') == component:
state = c
break
if not state:
defer.returnValue(False)
d = self.model.componentCallRemote(state, "getUIState")
try:
yield d
uistate = d.result
self.uistates[uistate] = state.get('name')
self.uistates_by_name[state.get('name')] = uistate
parsed = self.parse_uistate(uistate)
for key in parsed:
self.stomp_client.send_uistate(state.get('name'), key, parsed[key])
uistate.addListener(self, set_ = self.uistate_set, append=self.uistate_set)
except Exception, e:
print "got error %r getting uistate from component %r" % (e,state)
def uistate_set(self, state, key, value):
component = self.uistates[state]
data = value
if hasattr(value, "append"):
data = []
for item in value:
data.append(self.parse_uistate(item))
self.stomp_client.send_uistate(component, key, data)
def parse_uistate(self, state):
component = {}
if not hasattr(state, 'keys'):
return state
for k in state.keys():
if k == 'parent':
continue
elif k == 'feeders' or k == 'eaters' or k == 'clients':
component[k] = []
for e in state.get(k):
component[k].append(self.parse_uistate(e))
else:
component[k] = state.get(k)
return component
class StompClient(StompClientFactory):
status = {}
def recv_connected(self, msg):
print "Connected with stomp"
self.timer = None
def stop_statuses(self):
self.timer.stop()
self.timer = None
def restart_statuses(self):
self.subscribe("/flumotion/poll")
self.subscribe("/flumotion/command")
self.timer = LoopingCall(self.send_status)
self.timer.start(5)
def recv_message(self, msg):
print "Message received %r" % (msg,)
if msg["headers"]["destination"] == "/flumotion/poll":
try:
component = msg["body"]
global main
main.poll_uistate(component)
except Exception, e:
print "Broken Total Request %r" % (e,)
elif msg["headers"]["destination"] == "/flumotion/command":
try:
message = json.decode(msg["body"])
global main
main.run_command(message)
except Exception, e:
print "Broken request %r" % (e,)
def send_status(self):
global main
data = json.encode(main.components())
self.send("/flumotion/components/initial", data)
def send_changes(self, changes):
self.send("/flumotion/components/changes", json.encode(changes))
def send_uistate(self, component, key, value):
try:
encoded = json.encode(value)
self.send("/flumotion/components/uistate/%s/%s" % (component, key), encoded)
except Exception, e:
print "Error %r with uistate %r" % (e, value)
main = FluToStomp(sys.argv)
reactor.run()