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

[1464] Move z2jh.py to python and distribution agnostic path #1478

Merged
merged 1 commit into from
Nov 13, 2019

Conversation

mrow4a
Copy link
Contributor

@mrow4a mrow4a commented Nov 12, 2019

Path /usr/local/lib/python3.6/dist-packages binds chart to Ubuntu-like distribution and Python 3.6 docker images.

Currently, configuration script is placed in /etc/jupyterhub/jupyterhub_config.py and JupyterHub execs it.

jupyterhub
--config /etc/jupyterhub/jupyterhub_config.py
...

Thus, it is not enough that z2jh.py is in /etc/jupyterhub/ as it is not from where the python config is executed. Thus, the optimal solution could be to add the script to PYTHONPATH to container spec.

Closes #1464
@consideRatio @mkjpryor-stfc

@manics
Copy link
Member

manics commented Nov 12, 2019

This potentially overrides a PYTHONPATH set by the user.

Since this is mostly internal to the image how about insert /etc/jupyterhub/ to sys.path, e.g. in jupyterhub_config.py

import sys
sys.path.insert(0, '/etc/jupyterhub')
from z2jh import get_config, set_config_if_not_none

(I haven't tested this)

@mrow4a
Copy link
Contributor Author

mrow4a commented Nov 12, 2019

@manics how would you define PYTHONPATH for the container before env is set for the container?

If you set PYTHONPATH before container starts (as part of container ENV definition like in this PR), all other operations in entrypoint (where python is loaded too) will just append to that PYTHONPATH. If you have some custom PYTHONPATH, you probably would do it as part of entrypoint (which will append to PYTHONPATH), but firstly defined is PYTHONPATH container env. Am I right?

@manics
Copy link
Member

manics commented Nov 12, 2019

It could be set with hub.extraEnv:

@manics
Copy link
Member

manics commented Nov 12, 2019

Expanding on my rationale: I think of PYTHONPATH as a last resort for users when there's no other way of configuring Python other than completely rebuilding the image, so where possible I think we should keep this option open to them (in all projects, not just this one).

Copy link
Member

@consideRatio consideRatio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I consider this PR very relevant, so it needs to be fixed somehow. Either by PYTHONPATH, or by modifying the sys.path etc... I semi-verified that it would work to use @manics suggestion in #1478 (comment).

The JupyterHub binary should be allowed to be anywhere when it is started.
The configuration file should preferably not assume stuff, but it is quite nice that we would ask for it to use specifically /etc/jupyterhub as that kinda make some sense to load stuff from, of all places... I don't know for sure what I think is best.
The helm chart is already mounting the z2jh, so it makes sense that it would also manage information about how to access it through a change to PYTHONPATH for example.

I'm open to both using PYTHONPATH and sys.path.insert. As I don't feel strongly I prefer to sidestep on that discussion.

jupyterhub/templates/hub/deployment.yaml Outdated Show resolved Hide resolved
jupyterhub/templates/hub/deployment.yaml Outdated Show resolved Hide resolved
@mrow4a
Copy link
Contributor Author

mrow4a commented Nov 13, 2019

@consideRatio @manics I took an approach of sys.insert. To me it looks clean - all modules belonging to the configuration script should be in the directory where configuration script is.

@@ -6,6 +6,10 @@
from kubernetes import client
from jupyterhub.utils import url_path_join

# Make sure that modules placed in the same directory as the jupyterhub config are added to the pythonpath
configuration_directory = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, configuration_directory)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

One thing I'm not sure about, @consideRatio may know better: k8s may symlink config files into place, is there any situation where the real parent directories of this file and z2jh.py would be different?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, I think though that if this works for the TravisCI test pipeline, than I think it is robust enough as it worked in that k8s setup at least.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manics if you mount it like

- mountPath: /etc/jupyterhub/z2jh.py
   subPath: z2jh.py
   name: config

the file is regular file, not a symlink. Symlink happens only in case of mounting secrets as a directory, which in z2jh chart is not possible to do into /etc/jupyterhub.

- mountPath: /etc/jupyterhub/config/
  name: config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyways even if this would be moved to ConfigMap directory, the directory there is still a directory as https://docs.python.org/2/library/os.path.html#os.path.realpath ignores symlinks

$ root@hub-755dbdc69c-tt7r4:/srv/jupyterhub# python -c "import os;print(os.path.realpath('/etc/jupyterhub/config/z2jh.py'))"
/etc/jupyterhub/config/..2019_11_13_14_20_12.271060545/z2jh.py

@consideRatio
Copy link
Member

consideRatio commented Nov 13, 2019

This LGTM! Want to merge @manics ? :)

Thank you @mrow4a for your work to report, explain, and solve this issue! 🎉 ❤️ 🌻 !

@manics manics merged commit b052ab1 into jupyterhub:master Nov 13, 2019
@manics
Copy link
Member

manics commented Nov 13, 2019

Thanks @mrow4a!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Putting z2jh.py next to jupyterhub_config.py in the hub image
3 participants