def disable_cherrypy():
    """
    Disables internal kolibri web server.
    Kolibri will only run background tasks.
    Web must be provided by an external server, usually uwsgi + nginx
    """
    update_options_file('Server', "CHERRYPY_START", False, KOLIBRI_HOME)
def enable_redis_cache():
    """
    Set redis as the cache backend .
    When multiple processes run the server we need to use
    redis to ensure the cache is shared among them.
    """
    update_options_file('Cache', "CACHE_BACKEND",  "redis", KOLIBRI_HOME)
Esempio n. 3
0
def test_option_writing():
    """
    Checks that options can be written to a dummy options.ini file, validated, and then read back.
    """
    if sys.platform == "win32":
        _OLD_CONTENT_DIR = "C:\\mycontentdir"
        _NEW_CONTENT_DIR = "C:\\goodnessme"
    else:
        _OLD_CONTENT_DIR = "/mycontentdir"
        _NEW_CONTENT_DIR = "/goodnessme"
    _HTTP_PORT_GOOD = 7007
    _HTTP_PORT_BAD = "abba"

    _, tmp_ini_path = tempfile.mkstemp(prefix="options", suffix=".ini")
    with open(tmp_ini_path, "w") as f:
        f.write("\n".join([
            "[Paths]",
            "CONTENT_DIR = {dir}".format(dir=_OLD_CONTENT_DIR),
            "[Deployment]",
            "HTTP_PORT = {port}".format(port=_HTTP_PORT_GOOD),
        ]))

    with mock.patch.dict(os.environ, {
            "KOLIBRI_HTTP_PORT": "",
            "KOLIBRI_LISTEN_PORT": ""
    }):

        # check that values are set correctly to begin with
        OPTIONS = options.read_options_file(conf.KOLIBRI_HOME,
                                            ini_filename=tmp_ini_path)
        assert OPTIONS["Paths"]["CONTENT_DIR"] == _OLD_CONTENT_DIR
        assert OPTIONS["Deployment"]["HTTP_PORT"] == _HTTP_PORT_GOOD

        # change the content directory to something new
        options.update_options_file(
            "Paths",
            "CONTENT_DIR",
            _NEW_CONTENT_DIR,
            conf.KOLIBRI_HOME,
            ini_filename=tmp_ini_path,
        )

        # try changing the port to something bad, which should throw an error
        with pytest.raises(ValueError):
            options.update_options_file(
                "Deployment",
                "HTTP_PORT",
                _HTTP_PORT_BAD,
                conf.KOLIBRI_HOME,
                ini_filename=tmp_ini_path,
            )

        # check that the properly validated option was set correctly, and the invalid one wasn't
        OPTIONS = options.read_options_file(conf.KOLIBRI_HOME,
                                            ini_filename=tmp_ini_path)
        assert OPTIONS["Paths"]["CONTENT_DIR"] == _NEW_CONTENT_DIR
        assert OPTIONS["Deployment"]["HTTP_PORT"] == _HTTP_PORT_GOOD
Esempio n. 4
0
    def update_config_content_directory(self, dst):
        """
        Update kolibri_settings.json in KOLIBRI_HOME so that the variable
        CONTENT_DIRECTORY points to the destination content directory.
        """
        update_options_file("Paths", "CONTENT_DIR", dst, KOLIBRI_HOME)

        self.stdout.write(
            self.style.SUCCESS(
                "\nCurrent content directory is {}".format(dst)))
def enable_redis_cache():
    """
    Set redis as the cache backend.
    When multiple processes run the server we need to use
    redis to ensure the cache is shared among them.
    It also limits redis memory usage to avoid server problems
    if the cache grows too much
    """
    update_options_file("Cache", "CACHE_BACKEND", "redis")
    update_options_file("Cache", "CACHE_REDIS_MAXMEMORY_POLICY", "allkeys-lru")

    delete_redis_cache()
    server_memory = psutil.virtual_memory().total
    max_memory = round(server_memory / 10)
    if hasattr(process_cache, "get_master_client"):
        helper = RedisSettingsHelper(process_cache.get_master_client())
        redis_memory = helper.get_used_memory()
        if max_memory < redis_memory:
            max_memory = redis_memory + 2000

    update_options_file("Cache", "CACHE_REDIS_MAXMEMORY", max_memory)
def enable_cherrypy():
    """
    Enable internal kolibri web server.
    This option is incompatible with running kolibri-server
    """
    update_options_file('Server', "CHERRYPY_START", True, KOLIBRI_HOME)
def set_port(port):
    """
    Modify Kolibri options to set the TCP port the server will listen on
    """
    update_options_file("Deployment", "HTTP_PORT", port, KOLIBRI_HOME)
def set_zip_content_port(port):
    """
    Modify Kolibri options to set the TCP port the hashi files will be served on
    """
    update_options_file("Deployment", "ZIP_CONTENT_PORT", port)
def disable_redis_cache():
    """
    Set memory as the cache backend .
    If redis is not active, enabling it will break kolibri
    """
    update_options_file("Cache", "CACHE_BACKEND", "memory")