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

#273: Added jupyterenv/bin to environment variable PATH inside the Entrypoint #314

Merged
merged 16 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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: 2 additions & 1 deletion doc/changes/changes_2.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Version: 2.1.0
* #279: Made the notebooks tests running in SaaS as well as in the Docker-DB.
* #19: Added SLC notebook
* #301: Added CloudFront distribution for example data S3 bucket
* #273: Added `jupyterenv/bin` to environment variable `PATH` inside the DockerContainer
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* #273: Added `jupyterenv/bin` to environment variable `PATH` inside the DockerContainer
* #273: Added `jupyterenv/bin` to environment variable `PATH` inside the Entrypoint

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks!
See next push.


## Security

Expand All @@ -41,4 +42,4 @@ Version: 2.1.0
* #193: Ignored warnings in notebook tests
* #297: Reduced log level for transitive libraries in notebook tests
* #307: Made the notebook tests running in parallel;
moved common steps from test jobs to a composite action
moved common steps from test jobs to a composite action
5 changes: 4 additions & 1 deletion exasol/ds/sandbox/lib/dss_docker/create_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def jupyter():
user_home = get_fact(facts, "jupyter", "home")
password = get_fact(facts, "jupyter", "password")
logfile = get_fact(facts, "jupyter", "logfile")
virtualenv = get_fact(facts, "jupyter", "virtualenv")
return [
"--home", user_home,
"--jupyter-server", command,
Expand All @@ -56,6 +57,7 @@ def jupyter():
"--docker-group", docker_group,
"--password", password,
"--jupyter-logfile", logfile,
"--venv", virtualenv,
]

entrypoint = get_fact(facts, "entrypoint")
Expand Down Expand Up @@ -166,6 +168,7 @@ def _commit_container(
port = get_fact(facts, "jupyter", "port")
notebook_folder_final = get_fact(facts, "notebook_folder", "final")
notebook_folder_initial = get_fact(facts, "notebook_folder", "initial")

conf = {
"Entrypoint": entrypoint(facts),
"Cmd": [],
Expand All @@ -175,7 +178,7 @@ def _commit_container(
"Env": [
f"VIRTUAL_ENV={virtualenv}",
f"NOTEBOOK_FOLDER_FINAL={notebook_folder_final}",
f"NOTEBOOK_FOLDER_INITIAL={notebook_folder_initial}"
f"NOTEBOOK_FOLDER_INITIAL={notebook_folder_initial}",
],
}
img = container.commit(repository=self.image_name, conf=conf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def arg_parser():
"--notebooks", type=Path,
help="destination location for notebook files to copy",
)
parser.add_argument(
"--venv", type=Path, metavar="<PATH-TO-VIRTUAL-ENVIRONMENT>",
help="Prepend subdirectory bin to environment variable PATH before starting Jupyter server",
)
parser.add_argument(
"--jupyter-server", metavar="<PATH-TO-JUPYTER-BINARY>",
help="start server for Jupyter notebooks",
Expand Down Expand Up @@ -75,6 +79,11 @@ def arg_parser():
return parser


def command_with_venv(venv_activate: Path, command: List[str]) -> List[str]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah - left over from an experiment.
Thanks!
Removed in next push.

bash_cmd = [ "source", str(venv_activate), ";" ] + command
return [ "bash", "-c", " ".join(bash_cmd) ]


def start_jupyter_server(
home_directory: str,
binary_path: str,
Expand All @@ -83,6 +92,7 @@ def start_jupyter_server(
logfile: Path,
user: str,
password: str,
venv: Path,
poll_sleep: float = 1,
):
"""
Expand All @@ -107,6 +117,11 @@ def exit_on_error(rc):

env = os.environ.copy()
env["HOME"] = home_directory
if venv:
venv_bin = str(venv / "bin")
path = env.get("PATH")
env["PATH"] = f"{venv_bin}:{path}" if path else venv_bin
_logger.info(f'Changed environment variable PATH to {env["PATH"]}')
with open(logfile, "w") as f:
p = subprocess.Popen(command_line, stdout=f, stderr=f, env=env)

Expand Down Expand Up @@ -350,6 +365,7 @@ def main():
args.jupyter_logfile,
args.user,
args.password,
args.venv,
)
else:
sleep_infinity()
Expand Down
3 changes: 3 additions & 0 deletions test/unit/entrypoint/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ def test_jupyter(mocker):
port = "1234"
notebook_folder = Path("/root/notebooks")
logfile = Path("/root/jupyter-server.log")
venv = Path("/root/jupyterenv")
mocker.patch("sys.argv", [
"app",
"--home", "home-directory",
"--notebooks", str(notebook_folder),
"--venv", str(venv),
"--jupyter-server", jupyter,
"--port", port,
"--user", "usr",
Expand All @@ -106,6 +108,7 @@ def test_jupyter(mocker):
logfile,
"usr",
"pwd",
venv,
)
assert entrypoint.start_jupyter_server.call_args == expected
assert not entrypoint.sleep_infinity.called
2 changes: 2 additions & 0 deletions test/unit/entrypoint/test_start_jupyter_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def run(self):
self.logfile,
"user",
"password",
Path("venv"),
poll_sleep = 0.1,
)
return self
Expand All @@ -62,5 +63,6 @@ def test_late_error(tmp_path, caplog):
with pytest.raises(SystemExit) as ex:
testee = Testee(tmp_path).create_script(after="exit 23").run()
assert ex.value.code == 23
assert "Changed environment variable PATH to venv/bin:" in caplog.text
assert "Server for Jupyter has been started successfully." in caplog.text
assert "Jupyter Server terminated with error code 23" in caplog.text
2 changes: 2 additions & 0 deletions test/unit/test_dss_docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_entrypoint_with_copy_args():
"dss_facts": {
"docker_group": "docker-group-name",
"jupyter": {
"virtualenv": "/home/jupyter/jupyterenv",
"command": "/home/jupyter/jupyterenv/bin/jupyter-lab",
"port": "port",
"user": "jupyter-user-name",
Expand All @@ -101,6 +102,7 @@ def fact(*args):
"--notebook-defaults": fact("notebook_folder", "initial"),
"--notebooks": fact("notebook_folder", "final"),
"--home": fact("jupyter", "home"),
"--venv": fact("jupyter", "virtualenv"),
"--jupyter-server": fact("jupyter", "command"),
"--port": fact("jupyter", "port"),
"--user": fact("jupyter", "user"),
Expand Down
Loading