Package flumotion :: Package admin :: Package command :: Module component
[hide private]

Source Code for Module flumotion.admin.command.component

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """ 
 23  component commands 
 24  """ 
 25   
 26  from twisted.python import failure 
 27   
 28  from flumotion.common import errors, planet, log 
 29  from flumotion.common import common as fccommon 
 30  from flumotion.monitor.nagios import util 
 31  from flumotion.common.planet import moods 
 32   
 33  from flumotion.admin.command import common 
 34   
 35  __version__ = "$Rev: 6562 $" 
 36   
 37   
38 -class Delete(common.AdminCommand):
39 description = "Delete a component." 40
41 - def doCallback(self, args):
42 if not self.parentCommand.componentId: 43 common.errorRaise("Please specify a component id " 44 "with 'component -i [component-id]'") 45 46 d = self.getRootCommand().medium.callRemote('deleteComponent', 47 self.parentCommand.componentState) 48 49 def cb(result): 50 self.stdout.write("Deleted component.\n")
51 52 def eb(failure): 53 if failure.trap(errors.ComponentMoodError): 54 common.errorRaise("Component '%s' is in the wrong mood." % 55 self.parentCommand.componentId) 56 else: 57 common.errorRaise(log.getFailureMessage(failure))
58 59 d.addCallback(cb) 60 d.addErrback(eb) 61 62 return d 63 64
65 -class Invoke(common.AdminCommand):
66 usage = "[method-name] [arguments]" 67 summary = "invoke a method on a component" 68 description = """Invoke a method on a component. 69 %s 70 For a list of methods that can be invoked, see the component's medium class 71 and its remote_* methods. 72 73 Examples: getConfig, setFluDebug""" % common.ARGUMENTS_DESCRIPTION 74
75 - def doCallback(self, args):
76 if not self.parentCommand.componentId: 77 common.errorRaise("Please specify a component id " 78 "with 'component -i [component-id]'") 79 80 try: 81 methodName = args[0] 82 except IndexError: 83 common.errorRaise('Please specify a method name to invoke.') 84 if len(args) > 1: 85 args = common.parseTypedArgs(args[1], args[2:]) 86 if args is None: 87 common.errorRaise('Could not parse arguments.') 88 else: 89 args = [] 90 91 p = self.parentCommand 92 d = self.getRootCommand().medium.componentCallRemote( 93 self.parentCommand.componentState, methodName, *args) 94 95 def cb(result): 96 import pprint 97 self.stdout.write("Invoking '%s' on '%s' returned:\n%s\n" % ( 98 methodName, p.componentId, pprint.pformat(result)))
99 100 def eb(failure): 101 if failure.check(errors.NoMethodError): 102 common.errorRaise("No method '%s' on component '%s'." % ( 103 methodName, p.componentId)) 104 elif failure.check(errors.SleepingComponentError): 105 common.errorRaise( 106 "Component '%s' is sleeping." % p.componentId) 107 else: 108 common.errorRaise(log.getFailureMessage(failure))
109 110 d.addCallback(cb) 111 d.addErrback(eb) 112 113 return d 114 115
116 -class List(common.AdminCommand):
117 description = "List components." 118
119 - def doCallback(self, args):
120 p = self.parentCommand 121 a = p.planetState.get('atmosphere') 122 if a.get('components'): 123 self.stdout.write('atmosphere:\n') 124 for c in a.get('components'): 125 self.stdout.write(' ' + c.get('name') + '\n') 126 127 for f in p.planetState.get('flows'): 128 if f.get('components'): 129 self.stdout.write('%s flow:\n' % f.get('name')) 130 for c in f.get('components'): 131 self.stdout.write(' ' + c.get('name') + '\n')
132 133
134 -class Mood(common.AdminCommand):
135 description = "Check the mood of a component." 136
137 - def doCallback(self, args):
138 if not self.parentCommand.componentId: 139 common.errorRaise("Please specify a component id " 140 "with 'component -i [component-id]'") 141 142 p = self.parentCommand 143 moodValue = p.componentState.get('mood') 144 moodName = planet.moods.get(moodValue).name 145 self.stdout.write("Component '%s' is %s.\n" % (p.componentId, 146 moodName))
147 148
149 -class PropertyGet(common.AdminCommand):
150 description = "Get a property of a component." 151 name = 'get' 152
153 - def do(self, args):
154 if not args: 155 return common.errorReturn('Please specify a property to get.') 156 157 self._propertyName = args[0] 158 159 return common.AdminCommand.do(self, args)
160
161 - def doCallback(self, args):
162 u = self.parentCommand.uiState 163 name = self._propertyName 164 165 if not u.hasKey(name): 166 common.errorRaise("Component '%s' does not have property '%s'." % ( 167 self.parentCommand.parentCommand.componentId, name)) 168 169 self.stdout.write("Property '%s' is '%r'.\n" % ( 170 name, u.get(name)))
171 172
173 -class PropertyList(common.AdminCommand):
174 description = "List properties of a component." 175 name = 'list' 176
177 - def doCallback(self, args):
178 l = self.parentCommand.uiState.keys() 179 l.sort() 180 self.stdout.write('Properties:\n') 181 for p in l: 182 self.stdout.write('- %s\n' % p)
183 184 # FIXME: why is this called property when it really is about ui state ? 185 186
187 -class Property(util.LogCommand):
188 """ 189 @param uiState: the ui state of the component; set after logging in. 190 """ 191 192 description = "Act on properties of a component." 193 194 subCommandClasses = [PropertyGet, PropertyList] 195
196 - def handleOptions(self, options):
197 if not self.parentCommand.componentId: 198 common.errorRaise("Please specify a component id " 199 "with 'component -i [component-id]'") 200 201 # call our callback after connecting 202 d = self.getRootCommand().loginDeferred 203 d.addCallback(self._callback) 204 d.addErrback(self._errback)
205
206 - def _callback(self, result):
207 208 def getUIStateCb(uiState): 209 self.uiState = uiState
210 211 componentCommand = self.parentCommand 212 model = componentCommand.parentCommand.medium 213 d = model.componentCallRemote( 214 componentCommand.componentState, 'getUIState') 215 d.addCallback(getUIStateCb) 216 return d
217
218 - def _errback(self, failure):
219 failure.trap(errors.SleepingComponentError) 220 common.errorRaise("Component '%s' is sleeping." % 221 self.parentCommand.componentId)
222 223
224 -class Start(common.AdminCommand):
225 description = "Start a component." 226
227 - def doCallback(self, args):
228 if not self.parentCommand.componentId: 229 common.errorRaise("Please specify a component id " 230 "with 'component -i [component-id]'") 231 232 p = self.parentCommand 233 moodValue = p.componentState.get('mood') 234 if moodValue == moods.happy.value: 235 self.stdout.write("Component is already happy.\n") 236 return 0 237 238 d = self.getRootCommand().medium.callRemote('componentStart', 239 self.parentCommand.componentState) 240 241 def cb(result): 242 self.stdout.write("Started component.\n")
243 244 def eb(failure): 245 if failure.trap(errors.ComponentMoodError): 246 common.errorRaise("Component '%s' is in the wrong mood." % 247 self.parentCommand.componentId) 248 else: 249 common.errorRaise(log.getFailureMessage(failure))
250 251 d.addCallback(cb) 252 d.addErrback(eb) 253 254 return d 255 256
257 -class Stop(common.AdminCommand):
258 description = "Stop a component." 259
260 - def doCallback(self, args):
261 if not self.parentCommand.componentId: 262 common.errorRaise("Please specify a component id " 263 "with 'component -i [component-id]'") 264 265 p = self.parentCommand 266 moodValue = p.componentState.get('mood') 267 if moodValue == moods.sleeping.value: 268 self.stdout.write("Component is already sleeping.\n") 269 return 0 270 271 d = self.getRootCommand().medium.callRemote('componentStop', 272 self.parentCommand.componentState) 273 274 def cb(result): 275 self.stdout.write("Stopped component.\n")
276 277 def eb(failure): 278 if failure.trap(errors.ComponentMoodError): 279 common.errorRaise("Component '%s' is in the wrong mood." % 280 self.parentCommand.componentId) 281 else: 282 common.errorRaise(log.getFailureMessage(failure))
283 284 d.addCallback(cb) 285 d.addErrback(eb) 286 287 return d 288 289
290 -class Component(util.LogCommand):
291 """ 292 @ivar componentId: the component id, passed as an argument 293 @ivar componentState: the component state; set when logged in to manager. 294 @type componentState: L{flumotion.common.state.AdminComponentState} 295 @ivar planetState: the planet state; set when logged in to manager. 296 @type planetState: L{flumotion.common.state.AdminPlanetState} 297 """ 298 description = "Act on a component." 299 usage = "-i [component id]" 300 301 subCommandClasses = [Delete, Invoke, List, Mood, Property, Start, Stop] 302 303 componentId = None 304 componentState = None 305 planetState = None 306
307 - def addOptions(self):
308 self.parser.add_option('-i', '--component-id', 309 action="store", dest="componentId", 310 help="component id of the component")
311
312 - def handleOptions(self, options):
313 self.componentId = options.componentId 314 # call our callback after connecting 315 self.getRootCommand().loginDeferred.addCallback(self._callback)
316
317 - def _callback(self, result):
318 d = self.parentCommand.medium.callRemote('getPlanetState') 319 320 def gotPlanetStateCb(result): 321 self.planetState = result 322 self.debug('gotPlanetStateCb') 323 324 # only get componentState if we got passed an argument for it 325 if not self.componentId: 326 return 327 328 try: 329 self.componentState = util.findComponent(result, 330 self.componentId) 331 except Exception, e: 332 self.debug(log.getExceptionMessage(e)) 333 common.errorRaise("Invalid component id '%s'" % 334 self.componentId) 335 self.debug('gotPlanetStateCb') 336 if not self.componentState: 337 common.errorRaise('Could not find component %s' % 338 self.componentId)
339 340 d.addCallback(gotPlanetStateCb) 341 return d
342