From 595914cb3e1000f896bd3fe79b663b0f9a5f9e12 Mon Sep 17 00:00:00 2001 From: wqh17101 <597935261@qq.com> Date: Wed, 7 Aug 2019 16:49:29 +0800 Subject: [PATCH 1/3] add locale and other env-var for jupyterhub.service --- .gitignore | 3 ++ docs/index.rst | 1 + docs/topic/env-var-for-tljh-service.rst | 57 +++++++++++++++++++++++++ tljh/installer.py | 25 +++++++---- tljh/systemd-units/jupyterhub.service | 2 + 5 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 docs/topic/env-var-for-tljh-service.rst 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..45c2574ea 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-bar-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..c6a6b85bc --- /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 + + systemctl daemon-reload + +to reload jupyterhub.service`s config. + +and + +.. code-block:: bash + + sudo tljh reload hub + +to reload tljh. + +**You should mention 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..7282dfaa4 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)) @@ -173,10 +180,10 @@ def ensure_jupyterlab_extensions(): '@jupyter-widgets/jupyterlab-manager' ] utils.run_subprocess([ - os.path.join(USER_ENV_PREFIX, 'bin/jupyter'), - 'labextension', - 'install' - ] + extensions) + os.path.join(USER_ENV_PREFIX, 'bin/jupyter'), + 'labextension', + 'install' + ] + extensions) def ensure_jupyterhub_package(prefix): @@ -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..d535214cf 100644 --- a/tljh/systemd-units/jupyterhub.service +++ b/tljh/systemd-units/jupyterhub.service @@ -17,6 +17,8 @@ PrivateDevices=yes ProtectKernelTunables=yes ProtectKernelModules=yes Environment=TLJH_INSTALL_PREFIX={install_prefix} +# load the environment config , notice that before you modify the language setting in the conf , install the related fonts and packages first +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 From a78e0a8d8725d5fbd651e6b37d91ebf45a9317dd Mon Sep 17 00:00:00 2001 From: wqh17101 <597935261@qq.com> Date: Wed, 7 Aug 2019 16:51:50 +0800 Subject: [PATCH 2/3] fix doc --- docs/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.rst b/docs/index.rst index 45c2574ea..1a5578cc6 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -125,7 +125,7 @@ Topic guides provide in-depth explanations of specific topics. topic/authenticator-configuration topic/escape-hatch topic/idle-culler - topic/env-bar-for-tljh-service + topic/env-var-for-tljh-service Troubleshooting From e387ea5c7973193282121900284f6ac1b30a18de Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 22 Oct 2021 15:52:57 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Erik Sundell --- docs/topic/env-var-for-tljh-service.rst | 14 +++++++------- tljh/installer.py | 8 ++++---- tljh/systemd-units/jupyterhub.service | 1 - 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/docs/topic/env-var-for-tljh-service.rst b/docs/topic/env-var-for-tljh-service.rst index c6a6b85bc..63b17a9d3 100644 --- a/docs/topic/env-var-for-tljh-service.rst +++ b/docs/topic/env-var-for-tljh-service.rst @@ -13,14 +13,14 @@ e.g. - 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. + | 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. +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. @@ -30,15 +30,15 @@ e.g. LANG=zh_CN.UTF-8 -**Notice that before you modify the language setting in the conf , install the related fonts and packages first.** +**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 +After you do some changes, you should use .. code-block:: bash - systemctl daemon-reload + sudo systemctl daemon-reload to reload jupyterhub.service`s config. @@ -46,11 +46,11 @@ and .. code-block:: bash - sudo tljh reload hub + sudo tljh-config reload hub to reload tljh. -**You should mention that everything set in ``environment_for_system_service.conf`` is only for the ``jupyterhub.service`` .** +**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 7282dfaa4..b2b18cfc9 100644 --- a/tljh/installer.py +++ b/tljh/installer.py @@ -180,10 +180,10 @@ def ensure_jupyterlab_extensions(): '@jupyter-widgets/jupyterlab-manager' ] utils.run_subprocess([ - os.path.join(USER_ENV_PREFIX, 'bin/jupyter'), - 'labextension', - 'install' - ] + extensions) + os.path.join(USER_ENV_PREFIX, 'bin/jupyter'), + 'labextension', + 'install' + ] + extensions) def ensure_jupyterhub_package(prefix): diff --git a/tljh/systemd-units/jupyterhub.service b/tljh/systemd-units/jupyterhub.service index d535214cf..453b05086 100644 --- a/tljh/systemd-units/jupyterhub.service +++ b/tljh/systemd-units/jupyterhub.service @@ -17,7 +17,6 @@ PrivateDevices=yes ProtectKernelTunables=yes ProtectKernelModules=yes Environment=TLJH_INSTALL_PREFIX={install_prefix} -# load the environment config , notice that before you modify the language setting in the conf , install the related fonts and packages first 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