Exemplo n.º 1
0
 def test_config_lookup(self):
     inst = model.Model()
     config = inst.config_lookup('')
     self.assertItemsEqual(
         ['proxy_port', 'websockets_port', 'auth',
          'server_root', 'version'],
         config.keys()
     )
Exemplo n.º 2
0
 def test_config_lookup(self):
     inst = model.Model()
     config = inst.config_lookup('')
     self.assertListEqual(
         sorted(
             [
                 'proxy_port',
                 'websockets_port',
                 'auth',
                 'server_root',
                 'version',
                 'federation',
             ]
         ),
         sorted(list(config.keys())),
     )
Exemplo n.º 3
0
    def test_async_tasks_model(self):
        class task_except(Exception):
            pass

        def abnormal_op(cb, params):
            try:
                raise task_except
            except:
                cb("Exception raised", False)

        inst = model.Model()
        taskid = AsyncTask('', self._quick_op, 'Hello').id
        inst.task_wait(taskid)
        self.assertEquals('finished', inst.task_lookup(taskid)['status'])
        self.assertEquals('Hello', inst.task_lookup(taskid)['message'])

        params = {
            'delay': 3,
            'result': False,
            'message': 'It was not meant to be'
        }
        taskid = AsyncTask('', self._long_op, params).id
        self.assertEquals('running', inst.task_lookup(taskid)['status'])
        self.assertEquals('The request is being processing.',
                          inst.task_lookup(taskid)['message'])
        inst.task_wait(taskid)
        self.assertEquals('failed', inst.task_lookup(taskid)['status'])
        self.assertEquals('It was not meant to be',
                          inst.task_lookup(taskid)['message'])

        taskid = AsyncTask('', abnormal_op, {}).id
        inst.task_wait(taskid)
        self.assertEquals('Exception raised',
                          inst.task_lookup(taskid)['message'])
        self.assertEquals('failed', inst.task_lookup(taskid)['status'])

        taskid = AsyncTask('', self._continuous_ops, {'result': True}).id
        self.assertEquals('running', inst.task_lookup(taskid)['status'])
        inst.task_wait(taskid, timeout=10)
        self.assertEquals('finished', inst.task_lookup(taskid)['status'])
Exemplo n.º 4
0
    def __init__(self, options):
        # Update config.config with the command line values
        # So the whole application will have access to accurate values
        for sec in config.config.sections():
            for item in config.config.options(sec):
                if hasattr(options, item):
                    config.config.set(sec, item, str(getattr(options, item)))

        # Check proxy configuration
        if not hasattr(options, 'no_proxy') or not options.no_proxy:
            check_proxy_config()

        make_dirs = [
            os.path.abspath(config.get_log_download_path()),
            os.path.abspath(configParser.get("logging", "log_dir")),
            os.path.dirname(os.path.abspath(options.access_log)),
            os.path.dirname(os.path.abspath(options.error_log)),
            os.path.dirname(os.path.abspath(config.get_object_store())),
            os.path.abspath(config.get_wstokens_dir())
        ]
        for directory in make_dirs:
            if not os.path.isdir(directory):
                os.makedirs(directory)

        self.configObj = WokConfig()
        # We'll use the session timeout (= 10 minutes) and the
        # nginx timeout (= 10 minutes). This monitor isn't involved
        # in anything other than monitor the timeout of the connection,
        # thus it is safe to unsubscribe.
        cherrypy.engine.timeout_monitor.unsubscribe()
        cherrypy.tools.nocache = cherrypy.Tool('on_end_resource', set_no_cache)
        cherrypy.tools.wokauth = cherrypy.Tool('before_handler', auth.wokauth)

        # Setting host to 127.0.0.1. This makes wok run
        # as a localhost app, inaccessible to the outside
        # directly. You must go through the proxy.
        cherrypy.server.socket_host = '127.0.0.1'
        cherrypy.server.socket_port = options.cherrypy_port

        max_body_size_in_bytes = eval(options.max_body_size) * 1024
        cherrypy.server.max_request_body_size = max_body_size_in_bytes

        cherrypy.log.access_file = options.access_log
        cherrypy.log.error_file = options.error_log

        logLevel = LOGGING_LEVEL.get(options.log_level, logging.INFO)
        dev_env = options.environment != 'production'

        # Enable cherrypy screen logging if running environment
        # is not 'production'
        if dev_env:
            cherrypy.log.screen = True

        # close standard file handlers because we are going to use a
        # watchedfiled handler, otherwise we will have two file handlers
        # pointing to the same file, duplicating log enries
        for handler in cherrypy.log.access_log.handlers[:]:
            if isinstance(handler, logging.FileHandler):
                cherrypy.log.access_log.removeHandler(handler)

        for handler in cherrypy.log.error_log.handlers[:]:
            if isinstance(handler, logging.FileHandler):
                cherrypy.log.error_log.removeHandler(handler)

        # set logLevel
        cherrypy.log.access_log.setLevel(logLevel)
        cherrypy.log.error_log.setLevel(logLevel)

        # Create handler to access log file
        h = logging.handlers.WatchedFileHandler(options.access_log,
                                                'a',
                                                delay=1)
        h.setLevel(logLevel)
        h.setFormatter(cherrypy._cplogging.logfmt)

        # Add access log file to cherrypy configuration
        cherrypy.log.access_log.addHandler(h)

        # Create handler to error log file
        h = SafeWatchedFileHandler(options.error_log, 'a', delay=1)
        h.setLevel(logLevel)
        h.setFormatter(cherrypy._cplogging.logfmt)

        # Add error log file to cherrypy configuration
        cherrypy.log.error_log.addHandler(h)

        # start request logger
        self.reqLogger = RequestLogger()

        # Handling running mode
        if not dev_env:
            cherrypy.config.update({'environment': 'production'})

        for ident, node in sub_nodes.items():
            if node.url_auth:
                cfg = self.configObj
                ident = "/%s" % ident
                cfg[ident] = {'tools.wokauth.on': True}

        cherrypy.tree.mount(WokRoot(model.Model(), dev_env),
                            options.server_root, self.configObj)

        self._start_websocket_server()
        self._load_plugins()
        cherrypy.lib.sessions.init()
Exemplo n.º 5
0
    def __init__(self, options):
        # Launch reverse proxy
        start_proxy(options)

        make_dirs = [
            os.path.dirname(os.path.abspath(options.access_log)),
            os.path.dirname(os.path.abspath(options.error_log)),
            os.path.dirname(os.path.abspath(config.get_object_store()))
        ]
        for directory in make_dirs:
            if not os.path.isdir(directory):
                os.makedirs(directory)

        self.configObj = WokConfig()
        # We'll use the session timeout (= 10 minutes) and the
        # nginx timeout (= 10 minutes). This monitor isn't involved
        # in anything other than monitor the timeout of the connection,
        # thus it is safe to unsubscribe.
        cherrypy.engine.timeout_monitor.unsubscribe()
        cherrypy.tools.nocache = cherrypy.Tool('on_end_resource', set_no_cache)
        cherrypy.tools.wokauth = cherrypy.Tool('before_handler', auth.wokauth)

        # Setting host to 127.0.0.1. This makes wok run
        # as a localhost app, inaccessible to the outside
        # directly. You must go through the proxy.
        cherrypy.server.socket_host = '127.0.0.1'
        cherrypy.server.socket_port = options.cherrypy_port

        max_body_size_in_bytes = eval(options.max_body_size) * 1024
        cherrypy.server.max_request_body_size = max_body_size_in_bytes

        cherrypy.log.access_file = options.access_log
        cherrypy.log.error_file = options.error_log

        logLevel = LOGGING_LEVEL.get(options.log_level, logging.DEBUG)
        dev_env = options.environment != 'production'

        # Enable cherrypy screen logging if running environment
        # is not 'production'
        if dev_env:
            cherrypy.log.screen = True

        # Create handler to rotate access log file
        h = logging.handlers.RotatingFileHandler(options.access_log, 'a',
                                                 10000000, 1000)
        h.setLevel(logLevel)
        h.setFormatter(cherrypy._cplogging.logfmt)

        # Add access log file to cherrypy configuration
        cherrypy.log.access_log.addHandler(h)

        # Create handler to rotate error log file
        h = logging.handlers.RotatingFileHandler(options.error_log, 'a',
                                                 10000000, 1000)
        h.setLevel(logLevel)
        h.setFormatter(cherrypy._cplogging.logfmt)

        # Add rotating log file to cherrypy configuration
        cherrypy.log.error_log.addHandler(h)

        # Handling running mode
        if not dev_env:
            cherrypy.config.update({'environment': 'production'})

        if hasattr(options, 'model'):
            model_instance = options.model
        else:
            model_instance = model.Model()

        for ident, node in sub_nodes.items():
            if node.url_auth:
                cfg = self.configObj
                ident = "/%s" % ident
                cfg[ident] = {'tools.wokauth.on': True}

        self.app = cherrypy.tree.mount(WokRoot(model_instance, dev_env),
                                       config=self.configObj)
        self._load_plugins(options)

        # Terminate proxy when cherrypy server is terminated
        cherrypy.engine.subscribe('exit', terminate_proxy)

        cherrypy.lib.sessions.init()
Exemplo n.º 6
0
    def __init__(self, options):
        # Launch reverse proxy
        start_proxy(options)

        make_dirs = [
            os.path.abspath(config.get_log_download_path()),
            os.path.abspath(configParser.get("logging", "log_dir")),
            os.path.dirname(os.path.abspath(options.access_log)),
            os.path.dirname(os.path.abspath(options.error_log)),
            os.path.dirname(os.path.abspath(config.get_object_store()))
        ]
        for directory in make_dirs:
            if not os.path.isdir(directory):
                os.makedirs(directory)

        self.configObj = WokConfig()
        # We'll use the session timeout (= 10 minutes) and the
        # nginx timeout (= 10 minutes). This monitor isn't involved
        # in anything other than monitor the timeout of the connection,
        # thus it is safe to unsubscribe.
        cherrypy.engine.timeout_monitor.unsubscribe()
        cherrypy.tools.nocache = cherrypy.Tool('on_end_resource', set_no_cache)
        cherrypy.tools.wokauth = cherrypy.Tool('before_handler', auth.wokauth)

        # Setting host to 127.0.0.1. This makes wok run
        # as a localhost app, inaccessible to the outside
        # directly. You must go through the proxy.
        cherrypy.server.socket_host = '127.0.0.1'
        cherrypy.server.socket_port = options.cherrypy_port

        max_body_size_in_bytes = eval(options.max_body_size) * 1024
        cherrypy.server.max_request_body_size = max_body_size_in_bytes

        cherrypy.log.access_file = options.access_log
        cherrypy.log.error_file = options.error_log

        logLevel = LOGGING_LEVEL.get(options.log_level, logging.DEBUG)
        dev_env = options.environment != 'production'

        # Enable cherrypy screen logging if running environment
        # is not 'production'
        if dev_env:
            cherrypy.log.screen = True

        # close standard file handlers because we are going to use a
        # watchedfiled handler, otherwise we will have two file handlers
        # pointing to the same file, duplicating log enries
        for handler in cherrypy.log.access_log.handlers[:]:
            if isinstance(handler, logging.FileHandler):
                cherrypy.log.access_log.removeHandler(handler)

        for handler in cherrypy.log.error_log.handlers[:]:
            if isinstance(handler, logging.FileHandler):
                cherrypy.log.error_log.removeHandler(handler)

        # Create handler to access log file
        h = logging.handlers.WatchedFileHandler(options.access_log, 'a',
                                                delay=1)
        h.setLevel(logLevel)
        h.setFormatter(cherrypy._cplogging.logfmt)

        # Add access log file to cherrypy configuration
        cherrypy.log.access_log.addHandler(h)

        # Create handler to error log file
        h = SafeWatchedFileHandler(options.error_log, 'a', delay=1)
        h.setLevel(logLevel)
        h.setFormatter(cherrypy._cplogging.logfmt)

        # Add error log file to cherrypy configuration
        cherrypy.log.error_log.addHandler(h)

        # start request logger
        self.reqLogger = RequestLogger()

        # only add logrotate if wok is installed
        if paths.installed:

            # redefine logrotate configuration according to wok.conf
            data = Template(LOGROTATE_TEMPLATE)
            data = data.safe_substitute(
                log_dir=configParser.get("logging", "log_dir"),
                log_size=configParser.get("logging", "log_size")
            )

            # Write file to be used for nginx.
            config_file = open(os.path.join(paths.logrotate_dir, "wokd"), "w")
            config_file.write(data)
            config_file.close()

        # Handling running mode
        if not dev_env:
            cherrypy.config.update({'environment': 'production'})

        if hasattr(options, 'model'):
            model_instance = options.model
        else:
            model_instance = model.Model()

        for ident, node in sub_nodes.items():
            if node.url_auth:
                cfg = self.configObj
                ident = "/%s" % ident
                cfg[ident] = {'tools.wokauth.on': True}

        self.app = cherrypy.tree.mount(WokRoot(model_instance, dev_env),
                                       config=self.configObj)
        self._load_plugins(options)

        # Terminate proxy when cherrypy server is terminated
        cherrypy.engine.subscribe('exit', terminate_proxy)

        cherrypy.lib.sessions.init()
Exemplo n.º 7
0
 def test_config_reload(self, mock_restart):
     inst = model.Model()
     inst.config_reload('')
     mock_restart.assert_called_once_with()