Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locale and other environment variables support for systemd service jupyterhub #414

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ venv.bak/
.spyderproject
.spyproject

# Pycharm project setting
.idea/

# Rope project settings
.ropeproject

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions docs/topic/env-var-for-tljh-service.rst
Original file line number Diff line number Diff line change
@@ -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.


17 changes: 12 additions & 5 deletions tljh/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

logger = logging.getLogger("tljh")


def ensure_node():
"""
Ensure nodejs from nodesource is installed
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tljh/systemd-units/jupyterhub.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down