This repository was archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcreate_deployable.py
83 lines (68 loc) · 2.4 KB
/
create_deployable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from __future__ import annotations
import os
import shutil
from pathlib import Path
from sys import version_info
from typing import Any
from attr import asdict
from bentoml._internal.bento.bento import BentoInfo
from bentoml._internal.bento.build_config import DockerOptions
from bentoml._internal.bento.gen import generate_dockerfile
if version_info >= (3, 8):
from shutil import copytree
else:
from backports.shutil_copytree import copytree
root_dir = Path(os.path.abspath(os.path.dirname(__file__)), "sagemaker")
SERVICE_PATH = os.path.join(root_dir, "service.py")
TEMPLATE_PATH = os.path.join(root_dir, "template.j2")
SERVE_PATH = os.path.join(root_dir, "serve")
def create_deployable(
bento_path: str,
destination_dir: str,
bento_metadata: dict[str, Any],
overwrite_deployable: bool,
) -> str:
"""
The deployable is the bento along with all the modifications (if any)
requried to deploy to the cloud service.
Parameters
----------
bento_path: str
Path to the bento from the bento store.
destination_dir: str
directory to create the deployable into.
bento_metadata: dict
metadata about the bento.
Returns
-------
docker_context_path : str
path to the docker context.
"""
deployable_path = Path(destination_dir)
# copy over the bento bundle
copytree(bento_path, deployable_path, dirs_exist_ok=True)
bento_metafile = Path(bento_path, "bento.yaml")
with bento_metafile.open("r", encoding="utf-8") as metafile:
info = BentoInfo.from_yaml_file(metafile)
options = asdict(info.docker)
options["dockerfile_template"] = TEMPLATE_PATH
dockerfile_path = deployable_path.joinpath("env", "docker", "Dockerfile")
with dockerfile_path.open("w", encoding="utf-8") as dockerfile:
dockerfile.write(
generate_dockerfile(
DockerOptions(**options).with_defaults(),
str(deployable_path),
use_conda=not info.conda.is_empty(),
)
)
# copy sagemaker service.py
shutil.copy(
SERVICE_PATH,
os.path.join(deployable_path, "sagemaker_service.py"),
)
# then copy the serve script
serve_fspath = os.path.join(deployable_path, "serve")
shutil.copy(SERVE_PATH, serve_fspath)
# permission 755 is required for entry script 'serve'
os.chmod(serve_fspath, 0o755)
return str(deployable_path)