Exemplo n.º 1
0
 def __init__(self, local_repo, remote_repo, submitBroker):
     self.logging = logging.getLogger(__name__)
     self.pluginManager = PluginManager(local_repository=local_repo,
                             remote_repository=remote_repo)
     self.executePlugin = ExecutePlugin()
     self.submitBroker = submitBroker
     self.cache = {}
Exemplo n.º 2
0
class ReportRequestExecutor():
    '''Don't share this class over multiple threads/processes.'''

    def __init__(self, local_repo, remote_repo, submitBroker):
        self.logging = logging.getLogger(__name__)
        self.pluginManager = PluginManager(local_repository=local_repo,
                                remote_repository=remote_repo)
        self.executePlugin = ExecutePlugin()
        self.submitBroker = submitBroker
        self.cache = {}

    def do(self,doc):
        try:
            self.logging.info('Executing a request with destination %s:%s' % (doc['destination']['name'], doc['destination']['subject']))
            request = Request(doc=doc)
            command = self.pluginManager.getExecutable(command=request.plugin['name'], hash=request.plugin['hash'])
            output = self.executePlugin.do(request.plugin['name'], command, request.plugin['parameters'], request.plugin['timeout'])
            (raw, verbose, metrics) = self.processOutput(request.plugin['name'],output)
            request.insertPluginOutput(raw, verbose, metrics)            
            self.submitBroker.put(request.answer)
        except Exception as err:
            self.logging.warning('There is a problem executing %s:%s. Reason: %s' % (doc['destination']['name'], doc['destination']['subject'], err))
            
    def processOutput(self,name,data):
        output = []
        verbose = []
        dictionary = {}
        while len(data) != 0:
            line = data.pop(0)
            if str(line) == '~==.==~\n':
                for i,v in enumerate(data):
                    data[i] = v.rstrip('\n')
                verbose = data
                break
            else:
                output.append(line)
                try:
                    key_value = line.split(":")
                    dictionary[self.__cleanKey(key_value[0])] = key_value[1].rstrip('\n')
                except Exception as err:
                    self.logging.warn('Possible dirty key value list produced by plugin. Reason: %s' % (err))
        #Add epoch time
        dictionary["epoch"] = round(time.time())
        #Extend the metrics with the previous ones.
        metrics = self.__cache(name, dictionary)
        return (output, verbose, metrics)
    
    def __cache(self, plugin, dictionary):
        merged_dictionary={}
        cached_dictionary = self.cache.get(plugin, dictionary)
        for value in cached_dictionary:
            merged_dictionary['pre_' + value] = cached_dictionary[value]
        merged_dictionary.update(dictionary)
        self.cache[plugin] = dictionary
        return merged_dictionary

    def __cleanKey(self,key):
        '''Keys can only contains numbers, letters, dots and underscores.  All the rest is filtered out.'''
        return ''.join(findall('\w|\d|\.|_',key))
Exemplo n.º 3
0
from globals import current
from html import *
from validators import *
from http import redirect, HTTP
from dal import DAL, Field
from sqlhtml import SQLFORM, SQLTABLE
from compileapp import LOAD

# Dummy code to enable code completion in IDE's.
if 0:
    from globals import Request, Response, Session
    from cache import Cache
    from languages import translator
    from tools import Auth, Crud, Mail, Service, PluginManager

    # API objects
    request = Request()
    response = Response()
    session = Session()
    cache = Cache(request)
    T = translator(request)

    # Objects commonly defined in application model files
    # (names are conventions only -- not part of API)
    db = DAL()
    auth = Auth(db)
    crud = Crud(db)
    mail = Mail()
    service = Service()
    plugins = PluginManager()