Skip to content
This repository was archived by the owner on Feb 25, 2024. It is now read-only.

Commit e1b572a

Browse files
frostmingaarnphm
andauthored
fix: copy models to bento directory (#210)
* chore: switch to PDM Signed-off-by: Frost Ming <[email protected]> * automated release workflow Signed-off-by: Frost Ming <[email protected]> * ignore certain patterns Signed-off-by: Frost Ming <[email protected]> * fix lint errors Signed-off-by: Frost Ming <[email protected]> * fix: copy models to bento dir Signed-off-by: Frost Ming <[email protected]> --------- Signed-off-by: Frost Ming <[email protected]> Co-authored-by: Aaron Pham <[email protected]>
1 parent fdb8fae commit e1b572a

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

bentoctl/cli/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131

3232
logger = logging.getLogger(__name__)
3333
try:
34-
from bentoml_cli.utils import validate_docker_tag
34+
from bentoml_cli.utils import validate_container_tag
3535
except ImportError:
3636
logger.warning(
3737
"'bentoml._internal.utils.docker.validate_tag' not imported. "
3838
"Validation dissabled."
3939
)
40-
validate_docker_tag = None
40+
validate_container_tag = None
4141

4242

4343
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
@@ -143,7 +143,7 @@ def generate(deployment_config_file, values_only, save_path):
143143
"--docker-image-tag",
144144
help="Name and optionally a tag (format: 'name:tag'), defaults to bento tag.",
145145
required=False,
146-
callback=validate_docker_tag,
146+
callback=validate_container_tag,
147147
multiple=True,
148148
)
149149
@click.option(

bentoctl/deployment_config.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
import logging
33
import os
44
import typing as t
5+
from contextlib import contextmanager
56
from pathlib import Path
67

78
import bentoml
89
import cerberus
910
import fs
11+
import fs.mirror
1012
import yaml
1113
from bentoml import Bento
1214
from bentoml.exceptions import NotFound
15+
from bentoml.models import get as get_model
1316

1417
from bentoctl.exceptions import (
1518
BentoNotFound,
@@ -241,6 +244,18 @@ def generate(self, destination_dir=os.curdir, values_only=False):
241244

242245
return generated_files
243246

247+
@contextmanager
248+
def _prepare_bento_dir(self) -> t.Generator[str, None, None]:
249+
assert self.bento is not None
250+
with fs.open_fs("temp://") as temp_fs, fs.open_fs(self.bento.path) as bento_fs:
251+
fs.mirror.mirror(bento_fs, temp_fs)
252+
models_fs = temp_fs.makedirs("models", recreate=True)
253+
for model_info in self.bento.info.models:
254+
model = get_model(model_info.tag)
255+
model_fs = models_fs.makedirs(model_info.tag.path())
256+
fs.mirror.mirror(model.path, model_fs)
257+
yield temp_fs.getsyspath("/")
258+
244259
def create_deployable(self, destination_dir=os.curdir) -> str:
245260
"""
246261
Creates the deployable in the destination_dir and returns
@@ -249,12 +264,13 @@ def create_deployable(self, destination_dir=os.curdir) -> str:
249264
# NOTE: In the case of debug mode, we want to keep the deployable
250265
# for debugging purpose. So by setting overwrite_deployable to false,
251266
# we don't delete the deployable after the build.
252-
return self.operator.create_deployable(
253-
bento_path=self.bento.path,
254-
destination_dir=destination_dir,
255-
bento_metadata=get_bento_metadata(self.bento.path),
256-
overwrite_deployable=not is_debug_mode(),
257-
)
267+
with self._prepare_bento_dir() as bento_path:
268+
return self.operator.create_deployable(
269+
bento_path=bento_path,
270+
destination_dir=destination_dir,
271+
bento_metadata=get_bento_metadata(bento_path),
272+
overwrite_deployable=not is_debug_mode(),
273+
)
258274

259275
def create_repository(self):
260276
(

bentoctl/docker_utils.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections import OrderedDict
55

66
import docker
7-
from bentoml._internal.utils import buildx
7+
from bentoml import container
88
from rich.live import Live
99

1010
from bentoctl.console import console
@@ -78,12 +78,12 @@ def generate_deployable_container(
7878
console.print(
7979
f"In debug mode. Intermediate bento saved to [b]{dist_dir}[/b]"
8080
)
81-
env = {"DOCKER_BUILDKIT": "1", "DOCKER_SCAN_SUGGEST": "false"}
8281
buildx_args = {
83-
"subprocess_env": env,
84-
"cwd": deployment_config.create_deployable(destination_dir=str(dist_dir)),
82+
"context_path": deployment_config.create_deployable(
83+
destination_dir=str(dist_dir)
84+
),
8585
"file": DOCKERFILE_PATH,
86-
"tags": tags,
86+
"tag": tags,
8787
"add_host": None,
8888
"allow": allow,
8989
"build_args": build_args,
@@ -112,10 +112,12 @@ def generate_deployable_container(
112112
"target": target,
113113
"ulimit": None,
114114
}
115+
buildx_args = {k: v or None for k, v in buildx_args.items()}
115116

116117
# run health check whether buildx is install locally
117-
buildx.health()
118-
buildx.build(**buildx_args)
118+
container.health("buildx")
119+
backend = container.get_backend("buildx")
120+
backend.build(**buildx_args)
119121

120122

121123
def tag_docker_image(image_name, image_tag):

0 commit comments

Comments
 (0)