diff --git a/.gitignore b/.gitignore index 1e5c982f9..461a08230 100644 --- a/.gitignore +++ b/.gitignore @@ -94,6 +94,9 @@ venv.bak/ .spyderproject .spyproject +# Pycharm project setting +.idea/ + # Rope project settings .ropeproject diff --git a/docs/index.rst b/docs/index.rst index a5058c5b4..1a5578cc6 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -125,6 +125,7 @@ Topic guides provide in-depth explanations of specific topics. topic/authenticator-configuration topic/escape-hatch topic/idle-culler + topic/env-var-for-tljh-service Troubleshooting diff --git a/docs/topic/env-var-for-tljh-service.rst b/docs/topic/env-var-for-tljh-service.rst new file mode 100644 index 000000000..63b17a9d3 --- /dev/null +++ b/docs/topic/env-var-for-tljh-service.rst @@ -0,0 +1,57 @@ +.. _topic/env-var-for-tljh-service: + +=========================================================== +Locale Setting & Other Environment Variables for Jupyterhub +=========================================================== + +Sometimes you need to run TLJH in the other language environment rather than English. +This will cause some unexpected problems. + +e.g. + +- The file whose name is not English can not display properly +- Can not open or create the file whose name is not English + + + | You may find that no matter what you set in the shell, you will find that the locale is right in the shell only. And in the jupyter notebook/lab which is spawned by TLJH nothing has been changed. + +*The reason is that TLJH starts jupyterhub as a systemed service.* + +*As a systemed service unit, its environment variables should be set in its config.* + + +So, we provide a convenient way to set the locale or other environment variables for the service. +We will create a config ``environment_for_system_service.conf`` in the ``/srv/src/tljh`` when the installer is running. +You can add or modify any environment variables in it. + +e.g. + +.. code-block:: bash + + LANG=zh_CN.UTF-8 + +**Notice that before you modify the language setting in the conf, install the related fonts and packages first.** + + + +After you do some changes, you should use + +.. code-block:: bash + + sudo systemctl daemon-reload + +to reload jupyterhub.service`s config. + +and + +.. code-block:: bash + + sudo tljh-config reload hub + +to reload tljh. + +**You should be aware that everything set in ``environment_for_system_service.conf`` is only for the ``jupyterhub.service`` .** + +It means that you`d better just add the environment variables which the jupyterhub needs instead of all the environment variables in your environment. + + diff --git a/tljh/installer.py b/tljh/installer.py index 05055e260..b2b18cfc9 100644 --- a/tljh/installer.py +++ b/tljh/installer.py @@ -38,6 +38,7 @@ logger = logging.getLogger("tljh") + def ensure_node(): """ Ensure nodejs from nodesource is installed @@ -100,6 +101,7 @@ def ensure_node(): apt.add_source('nodesource', 'https://deb.nodesource.com/node_10.x', 'main') apt.install_packages(['nodejs']) + def remove_chp(): """ Ensure CHP is not running @@ -134,11 +136,15 @@ def ensure_jupyterhub_service(prefix): with open(os.path.join(HERE, 'systemd-units', 'jupyterhub.service')) as f: hub_unit_template = f.read() - with open(os.path.join(HERE, 'systemd-units', 'traefik.service')) as f: traefik_unit_template = f.read() - #Set up proxy / hub secret token if it is not already setup + # Get the default locale setting + with open(os.path.join(HERE, 'environment_for_system_service.conf'), 'a+', encoding='utf-8') as f: + r = os.popen("locale").read() + f.write(r) + + # Set up proxy / hub secret token if it is not already setup proxy_secret_path = os.path.join(STATE_DIR, 'traefik-api.secret') if not os.path.exists(proxy_secret_path): with open(proxy_secret_path, 'w') as f: @@ -149,6 +155,7 @@ def ensure_jupyterhub_service(prefix): unit_params = dict( python_interpreter_path=sys.executable, jupyterhub_config_path=os.path.join(HERE, 'jupyterhub_config.py'), + env_conf=os.path.join(HERE, 'environment_for_system_service.conf'), install_prefix=INSTALL_PREFIX, ) systemd.install_unit('jupyterhub.service', hub_unit_template.format(**unit_params)) @@ -316,9 +323,9 @@ def ensure_jupyterhub_running(times=20): # Everything else should immediately abort raise except requests.ConnectionError: - # Hub isn't up yet, sleep & loop - time.sleep(1) - continue + # Hub isn't up yet, sleep & loop + time.sleep(1) + continue except Exception: # Everything else should immediately abort raise diff --git a/tljh/systemd-units/jupyterhub.service b/tljh/systemd-units/jupyterhub.service index 477d04b76..453b05086 100644 --- a/tljh/systemd-units/jupyterhub.service +++ b/tljh/systemd-units/jupyterhub.service @@ -17,6 +17,7 @@ PrivateDevices=yes ProtectKernelTunables=yes ProtectKernelModules=yes Environment=TLJH_INSTALL_PREFIX={install_prefix} +EnvironmentFile={env_conf} # Run upgrade-db before starting, in case Hub version has changed # This is a no-op when no db exists or no upgrades are needed ExecStart={python_interpreter_path} -m jupyterhub.app -f {jupyterhub_config_path} --upgrade-db