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