Skip to content

Commit

Permalink
feat: support post commands in image spec (#5118)
Browse files Browse the repository at this point in the history
* feat: support post commands in image spec

Signed-off-by: Frost Ming <me@frostming.com>

* fix: use post_commands

Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming authored Dec 10, 2024
1 parent bb47663 commit 8f3dc50
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
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

0 comments on commit 8f3dc50

Please sign in to comment.