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

Source Code for Module flumotion.admin.command.common

  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  common functionality for flumotion-admin-command 
 24  """ 
 25   
 26  import sys 
 27   
 28  # we need to have the unjelliers registered 
 29  # FIXME: why is this not in flumotion.admin.admin ? 
 30  from flumotion.common import componentui, common, errors 
 31  from flumotion.admin import admin 
 32   
 33  # FIXME: move 
 34  from flumotion.monitor.nagios import util 
 35   
 36  __version__ = "$Rev: 6562 $" 
 37   
 38  # explain the complicated arguments system for the invoke methods 
 39  ARGUMENTS_DESCRIPTION = """ 
 40  Arguments to the method are passed using an argument list string, and the 
 41  arguments (matching the argument list string). 
 42   
 43  Example: "method ss one two" would invoke remote_method("one", "two") 
 44  """ 
 45   
 46  # code copied over from old flumotion-command 
 47   
 48   
49 -class ParseException(Exception):
50 pass
51 52 # FIXME: don't print darn it 53 54
55 -def parseTypedArgs(spec, args):
56 57 def _readFile(filename): 58 try: 59 f = open(filename) 60 contents = f.read() 61 f.close() 62 return contents 63 except OSError: 64 raise ParseException("Failed to read file %s" % (filename, ))
65 66 def _doParseTypedArgs(spec, args): 67 accum = [] 68 while spec: 69 argtype = spec.pop(0) 70 parsers = {'i': int, 's': str, 'b': common.strToBool, 71 'F': _readFile, 'N': (lambda _: None)} 72 if argtype == ')': 73 return tuple(accum) 74 elif argtype == '(': 75 accum.append(_doParseTypedArgs(spec, args)) 76 elif argtype == '}': 77 return dict(accum) 78 elif argtype == '{': 79 accum.append(_doParseTypedArgs(spec, args)) 80 elif argtype == ']': 81 return accum 82 elif argtype == '[': 83 accum.append(_doParseTypedArgs(spec, args)) 84 elif argtype not in parsers: 85 raise ParseException('Unknown argument type: %r' 86 % argtype) 87 else: 88 parser = parsers[argtype] 89 try: 90 arg = args.pop(0) 91 except IndexError: 92 raise ParseException('Missing argument of type %r' 93 % parser) 94 try: 95 accum.append(parser(arg)) 96 except Exception, e: 97 raise ParseException('Failed to parse %s as %r: %s' 98 % (arg, parser, e)) 99 100 spec = list(spec) + [')'] 101 args = list(args) 102 103 try: 104 res = _doParseTypedArgs(spec, args) 105 except ParseException, e: 106 print e.args[0] 107 return None 108 109 if args: 110 print 'Left over arguments:', args 111 return None 112 else: 113 return res 114 115 # helper subclass for leaf commands 116 117
118 -class AdminCommand(util.LogCommand):
119
120 - def do(self, args):
121 # call our callback after connecting 122 self.getRootCommand().loginDeferred.addCallback(self._callback, args)
123
124 - def _callback(self, result, args):
125 self.debug('invoking doCallback with args %r', args) 126 return self.doCallback(args)
127
128 - def doCallback(self, args):
129 """ 130 Subclasses should implement this as an alternative to the normal do 131 method. It will be called after a connection to the manager is made. 132 133 Don't forget to return a deferred you create to properly chain 134 execution. 135 """ 136 raise NotImplementedError( 137 "subclass %r should implement doCallback" % self.__class__)
138
139 - def connectToManager(self, connection):
140 """ 141 Connect to a manager. 142 143 @type connection: L{flumotion.common.connection.PBConnectionInfo} 144 145 @rtype: L{defer.Deferred} firing L{flumotion.admin.admin.AdminModel} 146 """ 147 from flumotion.twisted import pb as fpb 148 if not connection.authenticator: 149 connection.authenticator = fpb.Authenticator( 150 username=connection.username, 151 password=connection.password, 152 address=connection.host) 153 # platform-3/trunk compatibility stuff to guard against 154 # gratuitous changes 155 try: 156 # platform-3 157 adminMedium = admin.AdminModel(connection.authenticator) 158 self.debug("code is platform-3") 159 except TypeError: 160 # trunk 161 adminMedium = admin.AdminModel() 162 self.debug("code is trunk") 163 164 if hasattr(adminMedium, 'connectToHost'): 165 # platform-3 166 d = adminMedium.connectToHost(connection.host, 167 connection.port, not connection.use_ssl) 168 else: 169 d = adminMedium.connectToManager(connection) 170 171 d.addCallback(self._connectToManagerCb, adminMedium) 172 d.addErrback(self._connectToManagerEb) 173 174 return d
175
176 - def _connectToManagerCb(self, result, adminMedium):
177 self.debug('Connected to manager.') 178 return adminMedium
179
180 - def _connectToManagerEb(self, failure):
181 if failure.check(errors.ConnectionFailedError): 182 self.stderr.write("Unable to connect to manager.\n") 183 if failure.check(errors.ConnectionRefusedError): 184 self.stderr.write("Manager refused connection.\n") 185 return failure
186 187
188 -class Exited(Exception):
189 """ 190 Raised when the code wants the program to exit with a return value and 191 a message. 192 """ 193
194 - def __init__(self, code, msg=None):
195 self.args = (code, msg) 196 self.code = code 197 self.msg = msg
198 199
200 -def errorRaise(msg):
201 raise Exited(1, "ERROR: " + msg)
202 203
204 -def errorReturn(msg):
205 sys.stderr.write("ERROR: " + msg + '\n') 206 return 1
207