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

Source Code for Module flumotion.admin.command.main

  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  main for flumotion-command 
 24  """ 
 25   
 26  import sys 
 27   
 28  from twisted.internet import reactor, defer 
 29   
 30  from flumotion.common import errors, log 
 31  from flumotion.admin import connections, admin 
 32   
 33  from flumotion.monitor.nagios import util 
 34   
 35  from flumotion.admin.command import component, manager, worker, common 
 36   
 37  from flumotion.common.common import version 
 38   
 39  __version__ = "$Rev: 6562 $" 
 40   
 41  # Because we run a reactor and use deferreds, the flow is slightly different 
 42  # from the usual Command flow. 
 43   
 44  # Nagios will first create a loginDeferred instance variable, which will 
 45  # allow subcommands to hook into the connection and schedule callbacks. 
 46   
 47  # Nagios will then parse the command line, allowing all subcommands to 
 48  # hook into this step with their respective handleOptions/parse/do methods. 
 49   
 50  # Subcommands are expected to use the ok/warning/critical methods to report 
 51  # a message and set the exit state. 
 52   
 53  # The Nagios root command will take care of stopping the reactor and returning 
 54  # the exit value. 
 55   
 56   
57 -class Command(util.LogCommand):
58 usage = "%prog %command" 59 description = "Run commands on Flumotion manager." 60 61 loginDeferred = None # deferred that fires upon connection 62 medium = None # medium giving access over the connection 63 64 subCommandClasses = [component.Component, manager.Manager, worker.Worker] 65
66 - def addOptions(self):
67 self.parser.add_option('-v', '--version', 68 action="store_true", dest="version", 69 help="show version information") 70 default = "user:test@localhost:7531" 71 self.parser.add_option('-m', '--manager', 72 action="store", type="string", dest="manager", 73 help="the manager connection string, " \ 74 "in the form [username[:password]@]host:port " \ 75 "(defaults to %s)" % default, 76 default=default) 77 self.parser.add_option('-T', '--transport', 78 action="store", type="string", dest="transport", 79 help="transport protocol to use (tcp/ssl) [default ssl]", 80 default="ssl")
81
82 - def handleOptions(self, options):
83 self.debug('command: handleOptions') 84 if options.version: 85 print version("flumotion-admin-command") 86 return 0
87
88 - def parse(self, argv):
89 # instantiated here so our subcommands can chain to it 90 self.loginDeferred = defer.Deferred() 91 92 self.debug('parse: chain up') 93 # chain up to parent first 94 # all subcommands will have a chance to chain up to the deferred 95 ret = util.LogCommand.parse(self, argv) 96 97 if ret is None: 98 self.debug('parse returned None, help/usage printed') 99 return ret 100 101 if ret: 102 self.debug('parse returned %r' % ret) 103 return ret 104 105 if self.parser.help_printed or self.parser.usage_printed: 106 return 0 107 108 # now connect 109 self.debug('parse: connect') 110 self.connect(self.options) 111 112 # chain up an exit after our child commands have had the chance. 113 114 def cb(result): 115 self.debug('parse: cb: done') 116 reactor.callLater(0, reactor.stop)
117 118 def eb(failure): 119 self.debug('parse: eb: failure %s' % 120 log.getFailureMessage(failure)) 121 if failure.check(common.Exited): 122 sys.stderr.write(failure.value.msg + '\n') 123 reactor.exitStatus = failure.value.code 124 else: 125 sys.stderr.write(log.getFailureMessage(failure) + '\n') 126 reactor.exitStatus = 1 127 128 reactor.callLater(0, reactor.stop) 129 return
130 131 self.loginDeferred.addCallback(cb) 132 self.loginDeferred.addErrback(eb) 133 134 # now run the reactor 135 self.debug('parse: run the reactor') 136 self.run() 137 self.debug('parse: ran the reactor') 138 139 return reactor.exitStatus 140
141 - def run(self):
142 """ 143 Run the reactor. 144 145 Resets .exitStatus, and returns its value after running the reactor. 146 """ 147 # run the reactor 148 149 self.debug('running reactor') 150 # We cheat by putting the exit code in the reactor. 151 reactor.exitStatus = 0 152 reactor.run() 153 self.debug('ran reactor') 154 155 return reactor.exitStatus
156
157 - def connect(self, options):
158 connection = connections.parsePBConnectionInfo(options.manager, 159 use_ssl=options.transport == 'ssl') 160 161 # platform-3/trunk compatibility stuff to guard against 162 # gratuitous changes 163 try: 164 # platform-3 165 self.medium = admin.AdminModel(connection.authenticator) 166 self.debug("code is platform-3") 167 except TypeError: 168 # trunk 169 self.medium = admin.AdminModel() 170 self.debug("code is trunk") 171 172 if hasattr(self.medium, 'connectToHost'): 173 # platform-3 174 d = self.medium.connectToHost(connection.host, 175 connection.port, not connection.use_ssl) 176 else: 177 d = self.medium.connectToManager(connection) 178 179 d.addCallback(self._connectedCb) 180 d.addErrback(self._connectedEb)
181
182 - def _connectedCb(self, result):
183 self.debug('Connected to manager.') 184 self.loginDeferred.callback(result)
185
186 - def _connectedEb(self, failure):
187 if failure.check(errors.ConnectionFailedError): 188 sys.stderr.write("Unable to connect to manager.\n") 189 if failure.check(errors.ConnectionRefusedError): 190 sys.stderr.write("Manager refused connection.\n") 191 self.loginDeferred.errback(failure)
192 193
194 -def main(args):
195 c = Command() 196 try: 197 ret = c.parse(args[1:]) 198 except common.Exited, e: 199 ret = e.code 200 if ret == 0: 201 sys.stdout.write(e.msg + '\n') 202 else: 203 sys.stderr.write(e.msg + '\n') 204 205 return ret
206