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

feat: support post commands in image spec #5118

Merged
merged 2 commits into from
Dec 10, 2024
Merged
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
8 changes: 7 additions & 1 deletion src/_bentoml_sdk/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Image:
commands: t.List[str] = attrs.field(factory=list)
lock_python_packages: bool = True
python_requirements: str = ""
post_commands: t.List[str] = attrs.field(factory=list)
_after_pip_install: bool = attrs.field(init=False, default=False, repr=False)

def requirements_file(self, file_path: str) -> t.Self:
"""Add a requirements file to the image. Supports chaining call.
Expand All @@ -45,6 +47,7 @@ def requirements_file(self, file_path: str) -> t.Self:
image = Image("debian:latest").requirements_file("requirements.txt")
"""
self.python_requirements += Path(file_path).read_text()
self._after_pip_install = True
return self

def python_packages(self, *packages: str) -> t.Self:
Expand All @@ -59,6 +62,7 @@ def python_packages(self, *packages: str) -> t.Self:
.requirements_file("requirements.txt")
"""
self.python_requirements += "\n".join(packages)
self._after_pip_install = True
return self

def run(self, command: str) -> t.Self:
Expand All @@ -70,7 +74,8 @@ def run(self, command: str) -> t.Self:

image = Image("debian:latest").run("echo 'Hello, World!'")
"""
self.commands.append(command)
commands = self.post_commands if self._after_pip_install else self.commands
commands.append(command)
return self

def freeze(self, platform_: str | None = None) -> ImageInfo:
Expand All @@ -84,6 +89,7 @@ def freeze(self, platform_: str | None = None) -> ImageInfo:
python_version=self.python_version,
commands=self.commands,
python_requirements=python_requirements,
post_commands=self.post_commands,
)

def _freeze_python_requirements(self, platform_: str | None = None) -> str:
Expand Down
1 change: 1 addition & 0 deletions src/bentoml/_internal/bento/bento.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ class ImageInfo:
python_version: str = ""
commands: t.List[str] = attr.field(factory=list)
python_requirements: str = ""
post_commands: t.List[str] = attr.field(factory=list)

def write_to_bento(self, bento_fs: FS, envs: list[BentoEnvSchema]) -> None:
from importlib import resources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ RUN curl -LO https://astral.sh/uv/install.sh && \
COPY --chown={{ bento__user }}:{{ bento__user }} ./env/python ./env/python/
# install python packages
{% call common.RUN(__enable_buildkit__) -%} {{ __pip_cache__ }} {% endcall -%} uv pip install -r ./env/python/requirements.txt

{% for command in __options__post_commands %}
RUN {{ command }}
{% endfor %}

COPY --chown={{ bento__user }}:{{ bento__user }} . ./
{% endblock %}

Expand Down
Loading