1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """
23 worker commands
24 """
25
26 import os
27 import time
28
29 from gettext import gettext as _
30
31 from twisted.python import failure
32
33 from flumotion.configure import configure
34 from flumotion.common import errors, log, i18n, messages
35 from flumotion.monitor.nagios import util
36
37 from flumotion.admin.command import common
38
39 __version__ = "$Rev: 6562 $"
40
41
42 -class Invoke(common.AdminCommand):
94
95 d.addCallback(cb)
96 d.addErrback(eb)
97
98 return d
99
100
101 -class List(common.AdminCommand):
114
115
116 -class Run(common.AdminCommand):
117 usage = "[module-name] [method-name] [arguments]"
118 summary = "run a method on a worker"
119 description = """Run a method on a worker
120 %s
121 Any method that the worker can import can be run.
122 This is useful for testing worker checks.
123
124 Examples:
125
126 flumotion.worker.checks.video checkTVCard s /dev/video0
127
128 flumotion.worker.checks.audio checkMixerTracks ssi alsasrc hw:0 2
129 """ % common.ARGUMENTS_DESCRIPTION
130
132 try:
133 moduleName = args[0]
134 except IndexError:
135 common.errorRaise('Please specify a module name to invoke from.')
136 try:
137 methodName = args[1]
138 except IndexError:
139 common.errorRaise('Please specify a method name.')
140
141 if len(args) > 2:
142 args = common.parseTypedArgs(args[2], args[3:])
143 if args is None:
144 common.errorRaise('Could not parse arguments.')
145 else:
146 args = []
147
148 p = self.parentCommand
149 workerName = p.options.name
150 d = self.getRootCommand().medium.callRemote(
151 'workerCallRemote', workerName, 'runFunction',
152 moduleName, methodName, *args)
153
154 def cb(result):
155 i18n.installGettext()
156
157 self.stdout.write("Invoking '%s' on '%s' returned:\n" %
158 (methodName, workerName))
159 import pprint
160 self.stdout.write("%s\n" % pprint.pformat(result))
161
162 if isinstance(result, messages.Result):
163 _headings = {
164 messages.ERROR: _('Error'),
165 messages.WARNING: _('Warning'),
166 messages.INFO: _('Note'),
167 }
168
169 for m in result.messages:
170 translator = i18n.Translator()
171 localedir = os.path.join(configure.localedatadir, 'locale')
172
173 translator.addLocaleDir(configure.PACKAGE, localedir)
174 self.stdout.write('%s:\n' % _headings[m.level])
175 self.stdout.write(translator.translate(m) + '\n')
176 if hasattr(m, 'timestamp'):
177 self.stdout.write(_("\nPosted on %s.\n") %
178 time.strftime("%c", time.localtime(m.timestamp)))
179 if m.debug:
180 self.stdout.write("DEBUG:\n%s\n" % m.debug)
181
182 if result.failed:
183 self.stdout.write('Result failed.\n')
184 else:
185 self.stdout.write('Result successful:\n%s\n' %
186 pprint.pformat(result.value))
187
188 def eb(failure):
189 if failure.check(errors.NoMethodError):
190 common.errorRaise("No method '%s' on worker '%s'." % (
191 methodName, workerName))
192 elif failure.check(errors.SleepingComponentError):
193 common.errorRaise("Component '%s' is sleeping." %
194 p.componentId)
195 elif failure.check(errors.RemoteRunError):
196 common.errorRaise(log.getFailureMessage(failure))
197 else:
198 common.errorRaise(log.getFailureMessage(failure))
199
200 d.addCallback(cb)
201 d.addErrback(eb)
202
203 return d
204
205
206 -class Worker(util.LogCommand):
207 """
208 @param workerHeavenState: the planet state; set when logged in to manager.
209 @type workerHeavenState: L{flumotion.common.state.WorkerHeavenState}
210 """
211 description = "Act on workers."
212
213 subCommandClasses = [Invoke, List, Run]
214
216 self.parser.add_option('-n', '--name',
217 action="store", dest="name",
218 help="name of the component")
219
223
229 d.addCallback(gotWorkerHeavenStateCb)
230 return d
231