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

Refactor TrackedContainer run_detached/exec_cmd functions #2256

Merged
merged 6 commits into from
Mar 21, 2025

Conversation

mathbunnyru
Copy link
Member

Describe your changes

Issue ticket if applicable

Checklist (especially for first-time contributors)

  • I have performed a self-review of my code
  • If it is a core feature, I have added thorough tests
  • I will try not to use force-push to make the review process easier for reviewers
  • I have updated the documentation for significant changes

Sorry, something went wrong.

@@ -15,13 +15,13 @@
def test_cli_args(container: TrackedContainer, http_client: requests.Session) -> None:
"""Image should respect command line args (e.g., disabling token security)"""
host_port = find_free_port()
running_container = container.run_detached(
Copy link
Member Author

Choose a reason for hiding this comment

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

There is no need to return anything from run_detached - TrackedContainer has a container inside and it's better to provide specific functions we need (this way we're encapsulating docker functions we use)

command=["start-notebook.py", "--IdentityProvider.token=''"],
ports={"8888/tcp": host_port},
)
resp = http_client.get(f"http://localhost:{host_port}")
resp.raise_for_status()
logs = running_container.logs().decode()
logs = container.get_logs()
Copy link
Member Author

Choose a reason for hiding this comment

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

This is used often and it is cleaner to have a separate method

@@ -47,8 +47,7 @@ def test_nb_user_change(container: TrackedContainer) -> None:
)
command = f'stat -c "%F %U %G" /home/{nb_user}/.jupyter'
expected_output = f"directory {nb_user} users"
exec_result = running_container.exec_run(command, workdir=f"/home/{nb_user}")
output = exec_result.output.decode().strip("\n")
Copy link
Member Author

Choose a reason for hiding this comment

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

There were some .strin("\n") - I don't think they are actually needed, so let's try to remove them

if status == "healthy":
return status

return get_health(running_container, docker_client)
return status
Copy link
Member Author

Choose a reason for hiding this comment

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

Already have value above, no need to get it again

network=ipv6_network,
volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro,z"}},
tty=True,
)

command = ["python", f"{cont_data_dir}/check_listening.py"]
Copy link
Member Author

Choose a reason for hiding this comment

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

For simplicity made cmd always a string.
python docker allows both list or str, having str loogs good for us.

Additional improvement - if something goes wrong, copy-paste command will be easier

Comment on lines -48 to -50
exec_result = running_container.exec_run(command)
LOGGER.info(exec_result.output.decode())
assert exec_result.exit_code == 0
Copy link
Member Author

@mathbunnyru mathbunnyru Mar 21, 2025

Choose a reason for hiding this comment

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

There is no need to do any of this, because it's all part of a new method

@@ -109,25 +109,16 @@ def get_package_import_name(package: str) -> str:
return PACKAGE_MAPPING.get(package, package)


def _check_import_package(
Copy link
Member Author

Choose a reason for hiding this comment

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

With new method, this becomes one simple call, so removed some methods here

tty=True,
command=["bash", "-c", "sleep infinity"],
)

@staticmethod
def _conda_export_command(from_history: bool) -> list[str]:
Copy link
Member Author

Choose a reason for hiding this comment

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

In my opinion this adds complexity, but doesn't add too much value, so removed this method

return self.requested

def _execute_command(self, command: list[str]) -> str:
"""Execute a command on a running container"""
exec_result = self.running_container.exec_run(command, stderr=False)
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed stderr here, because mamba was updated recently and now the output is clean (as it was before)


def get_health(container: Container, client: docker.DockerClient) -> str:
Copy link
Member Author

Choose a reason for hiding this comment

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

Once again, let's not use docker.models.containers unless we really have to

@@ -19,22 +19,18 @@ class TrackedContainer:
Docker client instance
image_name: str
Name of the docker image to launch
**kwargs: dict, optional
Copy link
Member Author

Choose a reason for hiding this comment

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

We only construct TrackedContainer in one place, it doesn't make sense to allow this customization, and we simply add detach=True in run_detached

@@ -3,4 +3,4 @@
from tagging.utils.docker_runner import DockerRunner

with DockerRunner("ubuntu") as container:
DockerRunner.exec_cmd(container, cmd="env", print_output=True)
Copy link
Member Author

Choose a reason for hiding this comment

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

Easier to always print output

@mathbunnyru mathbunnyru merged commit dcd1c45 into jupyter:main Mar 21, 2025
97 checks passed
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.

None yet

1 participant