Exemplo n.º 1
0
class Cli(Cmd):
    def __init__(self, prompt):
        super(Cli, self).__init__(prompt + 'rabbitmq')
        self.myprompt = prompt + 'rabbitmq'
        self.amqp = Amqp()
        self.amqp.start()
        self.current_exchange = None

    def do_cd(self, queue):
        print("[TODO] rabbit cd to queue")

    def do_ls(self, queue=None):
        if self.current_exchange is None:
            exchanges_names = self.amqp.exchanges.keys()
            i = 0

            for name in exchanges_names:
                if i > 0:
                    stdout.write("\t")

                stdout.write(name + " ")
                i += 1

            stdout.write("\n")
            proc = Popen("rabbitmqadmin list queues", shell=True, stdout=PIPE)
            stdout_value = proc.communicate()[0]
            print(stdout_value)

        # else:

    def do_purge(self, queue=None):
        print("[TODO] rabbit purge queue (erase events)")

    def do_top(self, queue=None):
        print("[TODO] global status of queues")
Exemplo n.º 2
0
class Cli(Cmd):
    def __init__(self, prompt):
        super(Cli, self).__init__(prompt + "rabbitmq")
        self.myprompt = prompt + "rabbitmq"
        self.amqp = Amqp()
        self.amqp.start()
        self.current_exchange = None

    def do_cd(self, queue):
        print("[TODO] rabbit cd to queue")

    def do_ls(self, queue=None):
        if self.current_exchange is None:
            exchanges_names = self.amqp.exchanges.keys()
            i = 0

            for name in exchanges_names:
                if i > 0:
                    stdout.write("\t")

                stdout.write(name + " ")
                i += 1

            stdout.write("\n")
            proc = Popen("rabbitmqadmin list queues", shell=True, stdout=PIPE)
            stdout_value = proc.communicate()[0]
            print(stdout_value)

        # else:

    def do_purge(self, queue=None):
        print("[TODO] rabbit purge queue (erase events)")

    def do_top(self, queue=None):
        print("[TODO] global status of queues")
Exemplo n.º 3
0
    def setUpClass(cls):

        amqp_conf = Configuration.load(os.path.join('etc', 'amqp.conf'), Ini)
        cls.amqp_uri = 'amqp://{}:{}@{}:{}/{}'.format(
            amqp_conf['master']['userid'],
            amqp_conf['master']['password'],
            amqp_conf['master']['host'],
            amqp_conf['master']['port'],
            amqp_conf['master']['virtual_host']
        )

        cls.conn = Connection(cls.amqp_uri)
        cls.producers = pools.Producers(limit=1)
        cls.exchange_name = "canopsis"

        cls.amqp = Amqp(logging_level='INFO',
                        logging_name='Amqp')
        cls.amqp.producers = cls.producers
        cls.amqp.conn = cls.conn

        cls.event = {
            'connector': 'test_amqp',
            'connector_name': 'test_amqp',
            'source_type': 'resource',
            'event_type': 'check',
            'component': 'test',
            'resource': 'test'
        }
Exemplo n.º 4
0
    def __init__(self, *args, **kwargs):
        super(WebServer, self).__init__(*args, **kwargs)

        self.log_name = 'webserver'

        # TODO: Replace with MongoStorage
        self.db = get_storage(account=Account(user='******', group='root'))
        self.amqp = Amqp()
        self.stopping = False

        self.webmodules = {}
        self.auth_backends = {}
Exemplo n.º 5
0
 def __init__(self, prompt):
     super(Cli, self).__init__(prompt + 'rabbitmq')
     self.myprompt = prompt + 'rabbitmq'
     self.amqp = Amqp()
     self.amqp.start()
     self.current_exchange = None
Exemplo n.º 6
0
class WebServer(Configurable):
    @property
    def debug(self):
        return setdefaultattr(self, '_debug', False)

    @debug.setter
    def debug(self, value):
        self._debug = value

    @property
    def enable_crossdomain_send_events(self):
        return setdefaultattr(self, '_crossdomain_evt', False)

    @enable_crossdomain_send_events.setter
    def enable_crossdomain_send_events(self, value):
        self._crossdomain_evt = value

    @property
    def root_directory(self):
        return setdefaultattr(
            self, '_rootdir',
            os.path.expanduser('~/var/www/')
        )

    @root_directory.setter
    def root_directory(self, value):
        value = os.path.expanduser(value)

        if os.path.exists(value):
            self._rootdir = value

    @property
    def providers(self):
        return setdefaultattr(self, '_providers', [])

    @providers.setter
    def providers(self, value):
        self._providers = value

    @property
    def cookie_expires(self):
        return setdefaultattr(self, '_cookie', 300)

    @cookie_expires.setter
    def cookie_expires(self, value):
        self._cookie = value

    @property
    def secret(self):
        return setdefaultattr(self, '_secret', 'canopsis')

    @secret.setter
    def secret(self, value):
        self._secret = value

    @property
    def data_dir(self):
        return setdefaultattr(
            self, '_datadir',
            os.path.expanduser('~/var/cache/canopsis/webcore/')
        )

    @data_dir.setter
    def data_dir(self, value):
        value = os.path.expanduser(value)

        if os.path.exists(value):
            self._datadir = value

    # dict properties do not need setters

    @property
    def webservices(self):
        if not hasattr(self, '_webservices'):
            self._webservices = {}

        return self._webservices

    @property
    def beaker_url(self):
        return '{0}.beaker'.format(self.db.uri)

    def __init__(self, *args, **kwargs):
        super(WebServer, self).__init__(*args, **kwargs)

        self.log_name = 'webserver'

        # TODO: Replace with MongoStorage
        self.db = get_storage(account=Account(user='******', group='root'))
        self.amqp = Amqp()
        self.stopping = False

        self.webmodules = {}
        self.auth_backends = {}

    def __call__(self):
        self.logger.info('Initialize gevent signal-handlers')
        gevent.signal(SIGTERM, self.exit)
        gevent.signal(SIGINT, self.exit)

        self.logger.info('Start AMQP thread')
        self.amqp.start()

        self.logger.info('Initialize WSGI Application')
        self.app = BottleApplication()

        self.load_auth_backends()
        self.load_webservices()
        self.load_session()

        return self

    def _load_webservice(self, name):
        modname = 'canopsis.webcore.services.{0}'.format(name)

        if name in self.webmodules:
            return True

        self.logger.info('Loading webservice: {0}'.format(name))

        try:
            mod = importlib.import_module(modname)

        except ImportError as err:
            self.logger.error(
                'Impossible to load webservice {0}: {1}'.format(name, err)
            )

            return False

        else:
            if hasattr(mod, 'exports'):
                self.webmodules[name] = mod
                mod.exports(self)

            else:
                self.logger.error(
                    'Invalid module {0}, no exports()'.format(name)
                )

                return False

        return True

    def load_webservices(self):
        for webservice in self.webservices:
            if self.webservices[webservice]:
                self._load_webservice(webservice)

    def _load_auth_backend(self, name):
        modname = 'canopsis.auth.{0}'.format(name)

        if name in self.auth_backends:
            return True

        self.logger.info('Load authentication backend: {0}'.format(name))

        try:
            mod = importlib.import_module(modname)

        except ImportError as err:
            self.logger.error(
                'Impossible to load authentication backend {}: {}'.format(
                    name, err
                )
            )

            return False

        else:
            backend = mod.get_backend(self)
            self.auth_backends[backend.name] = backend
            self.app.install(backend)

        return True

    def load_auth_backends(self):
        for provider in self.providers:
            self._load_auth_backend(provider)

        # Always add this backend which returns 401 when the login fails
        backend = EnsureAuthenticated(self)
        self.auth_backends[backend.name] = backend
        self.app.install(backend)

    def load_session(self):
        self.app = SessionMiddleware(self.app, {
            'session.type': 'mongodb',
            'session.cookie_expires': self.cookie_expires,
            'session.url': self.beaker_url,
            'session.secret': self.secret,
            'session.lock_dir': self.data_dir
        })

    def unload_session(self):
        pass

    def unload_auth_backends(self):
        pass

    def unload_webservices(self):
        pass

    def exit(self):
        if not self.stopping:
            self.stopping = True

            self.unload_session()
            self.unload_webservices()
            self.unload_auth_backends()

            self.amqp.stop()
            # TODO: self.amqp.wait() not implemented

            sys.exit(0)

    @property
    def application(self):
        return self.app

    @property
    def skip_login(self):
        return [bname for bname in self.auth_backends.keys()]

    @property
    def skip_logout(self):
        return [
            bname
            for bname in self.auth_backends.keys()
            if not self.auth_backends[bname].handle_logout
        ]

    def require(self, modname):
        if not self._load_webservice(modname):
            raise ImportError(
                'Impossible to import webservice: {0}'.format(modname)
            )

        return self.webmodules[modname]

    class Error(Exception):
        pass
Exemplo n.º 7
0
 def __init__(self, prompt):
     super(Cli, self).__init__(prompt + "rabbitmq")
     self.myprompt = prompt + "rabbitmq"
     self.amqp = Amqp()
     self.amqp.start()
     self.current_exchange = None