def add_apps_to_server(server_model, server_romeo): """add apps to server""" for app in apps: if not server_romeo.isRelated(app): continue log('%s should run %s' % (server_model.hostname, app.VALUE)) application = App(app.VALUE) application.runsOn(server_model)
def loadAll(): my_dir = os.path.dirname(__file__) for filename in os.listdir(my_dir): if not filename.endswith('.py'): continue if filename == '__init__.py': continue modname = filename[:-3] log('Loading event-handler module %s' % modname) __import__(__name__ + '.' + modname)
def loadRoster(self): if not os.path.exists(self.rosterPath): log('no roster exists for the %s team, so it has no members!' % self.name, warning=True) return for line in open(self.rosterPath): line = line.strip() if not line or line.startswith('#'): continue jid = line agent = SupportAgent(jid) self.agents.add(agent)
def issueResolved(self, issue): if issue != self.currentIssue: #got resolved by someone else return log("Agent %s resolved issue \"%s\" resolution=%s" % (self.name, issue.description, issue.resolution), type='issues') self.currentIssue = None self.sop = None if self.available and any(self.issues): self.engage( self.issues.next() ) return issue
def issueResolved(self, issue): if issue != self.currentIssue: #got resolved by someone else return log("Agent %s resolved issue \"%s\" resolution=%s" % (self.name, issue.description, issue.resolution), type='issues') self.currentIssue = None self.sop = None if self.available and any(self.issues): self.engage(self.issues.next()) return issue
def restart(self): """convenient restart method for an Application Instance""" result = None try: if self.running: d = self.stop() wfd = defer.waitForDeferred(d) yield wfd wfd.getResult() d = self.start() wfd = defer.waitForDeferred(d) yield wfd result = wfd.getResult() except: result = Failure() log('Unhandled exception\n' + result.getTraceback()) yield result
def loadAll(): """loads all of the responders""" responders.clear() my_dir = os.path.dirname(__file__) for filename in os.listdir(my_dir): if not filename.endswith('.py'): continue if filename == '__init__.py': continue modname = filename[:-3] log('Loading responder module %s' % modname) try: #python2.4 __import__ implementation doesn't accept **kwargs mod = __import__(__name__ + '.' + modname, {}, {}, [modname]) except: err('skipping responder module %s due to errors' % modname) continue for name,obj in vars(mod).items(): if getattr(obj, 'is_responder', False) and hasattr(obj, 'pattern'): try: responders[ re.compile(obj.pattern) ] = obj obj.module = modname except: err("Failed to compile pattern \"%s\" for %s" % (obj.pattern, obj)) raise
def loadAll(): """loads all of the responders""" responders.clear() my_dir = os.path.dirname(__file__) for filename in os.listdir(my_dir): if not filename.endswith('.py'): continue if filename == '__init__.py': continue modname = filename[:-3] log('Loading responder module %s' % modname) try: #python2.4 __import__ implementation doesn't accept **kwargs mod = __import__(__name__ + '.' + modname, {}, {}, [modname]) except: err('skipping responder module %s due to errors' % modname) continue for name, obj in vars(mod).items(): if getattr(obj, 'is_responder', False) and hasattr(obj, 'pattern'): try: responders[re.compile(obj.pattern)] = obj obj.module = modname except: err("Failed to compile pattern \"%s\" for %s" % (obj.pattern, obj)) raise
def command(executable, args, env, path, usePTY, childFD, protocol, *proto_args, **proto_kwargs): """command(host, port, protocol, *proto_args, **proto_kwargs) Initiate a process connection to the given application with args using the given protocol class. *proto_args and **proto_kwargs are passed to the protocol constructor, and the last positional argument will be a Deferred for the result of the task. The protocol constructor must take at least this one argument. """ deferredResult = defer.Deferred() proto_args += (deferredResult, ) if 'timeout' in proto_kwargs: timeout = proto_kwargs.pop('timeout') else: timeout = None #for convience argfix = proto_kwargs.pop('fixargs', True) #setup the command line to re-enter this file and daemonize if proto_kwargs.get('daemonize', False): from droned.management import dmx from droned.logging import log log('DroneD is daemonizing "%s" on your behalf' % (executable, )) if usePTY: env['DRONED_USE_TTY'] = "1" #massage the commandline args if path: env['DRONED_PATH'] = path #the path to the dmx utility path = os.path.sep.join( os.path.abspath(os.path.dirname(dmx.__file__)).split( os.path.sep)[:-1]) #arguments to dmx args = ('dmx', executable) + args #switch out the executable for the same one that launched DroneD executable = sys.executable #laziness hack def _fix_args(cmd, a): """I fix your commandline args b/c you are probably lazy like me""" first = cmd.split(os.path.sep)[-1] if not len(a): a = (first, ) return a if first != a[0]: a = (first, ) + tuple(a) return a if argfix: newargs = [] #sanitize arguments, b/c devs do silly things ... including myself for i in list(_fix_args(executable, args)): newargs += i.split(' ') args = tuple(newargs) try: #dmx work around import config reactor = config.reactor except: from twisted.internet import reactor #setup the client application to run app = MyClientCreator(reactor, protocol, *proto_args, **proto_kwargs) deferredSpawn = app.spawn(executable, args, env, path, usePTY, childFD) #If the spawn fails the protocol task fails deferredSpawn.addErrback(lambda failure: deferredResult.called or deferredResult.errback(failure)) from kitt.decorators import debugCall if 'debug' in proto_kwargs and proto_kwargs['debug'] == True: deferredResult.errback = debugCall(deferredResult.errback) deferredResult.callback = debugCall(deferredResult.callback) if timeout: reactor.callLater(timeout, cancelTask, deferredResult) #Inject the executable and args into the results in the callback chain def injectInfo(outcome): try: if isinstance(outcome, dict): outcome['executable'] = executable outcome['args'] = args elif isinstance(outcome, Failure): if not outcome.check(DroneCommandFailed): outcome = { 'error': outcome, 'executable': executable, 'args': args, } except: pass #some process failures don't have resultContext return outcome if 'debug' in proto_kwargs and proto_kwargs['debug'] == True: injectInfo = debugCall(injectInfo) deferredResult.addBoth(injectInfo) return deferredResult
def command(executable, args, env, path, usePTY, childFD, protocol, *proto_args, **proto_kwargs): """command(host, port, protocol, *proto_args, **proto_kwargs) Initiate a process connection to the given application with args using the given protocol class. *proto_args and **proto_kwargs are passed to the protocol constructor, and the last positional argument will be a Deferred for the result of the task. The protocol constructor must take at least this one argument. """ deferredResult = defer.Deferred() proto_args += (deferredResult,) if 'timeout' in proto_kwargs: timeout = proto_kwargs.pop('timeout') else: timeout = None #for convience argfix = proto_kwargs.pop('fixargs', True) #setup the command line to re-enter this file and daemonize if proto_kwargs.get('daemonize', False): from droned.management import dmx from droned.logging import log log('DroneD is daemonizing "%s" on your behalf' % (executable,)) #massage the commandline args if path: env['DRONED_PATH'] = path #the path to the dmx utility path = os.path.sep.join( os.path.abspath( os.path.dirname(dmx.__file__) ).split(os.path.sep)[:-1] ) #arguments to dmx args = ('dmx', executable) + args #switch out the executable for the same one that launched DroneD executable = sys.executable #laziness hack def _fix_args(cmd, a): """I fix your commandline args b/c you are probably lazy like me""" first = cmd.split(os.path.sep)[-1] if not len(a): a = (first,) return a if first != a[0]: a = (first,) + tuple(a) return a if argfix: newargs = [] #sanitize arguments, b/c devs do silly things ... including myself for i in list(_fix_args(executable, args)): newargs += filter(lambda b: b, i.split(' ')) #FIXME lambda in filter args = tuple(newargs) from twisted.internet import reactor #setup the client application to run app = MyClientCreator(reactor, protocol, *proto_args, **proto_kwargs) deferredSpawn = app.spawn(executable, args, env, path, usePTY, childFD) #If the spawn fails the protocol task fails deferredSpawn.addErrback(lambda failure: deferredResult.called or deferredResult.errback(failure) ) from kitt.decorators import debugCall if 'debug' in proto_kwargs and proto_kwargs['debug'] == True: deferredResult.errback = debugCall( deferredResult.errback ) deferredResult.callback = debugCall( deferredResult.callback ) if timeout: reactor.callLater(timeout, cancelTask, deferredResult) #Inject the executable and args into the results in the callback chain def injectInfo(outcome): try: if isinstance(outcome, dict): outcome['executable'] = executable outcome['args'] = args elif isinstance(outcome, Failure): if not outcome.check(DroneCommandFailed): outcome = { 'error' : outcome, 'executable' : executable, 'args' : args, } except: pass #some process failures don't have resultContext return outcome if 'debug' in proto_kwargs and proto_kwargs['debug'] == True: injectInfo = debugCall( injectInfo ) deferredResult.addBoth(injectInfo) return deferredResult