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 9
Expand file tree
/
Copy pathcreate_deployable.py
More file actions
93 lines (75 loc) · 2.75 KB
/
create_deployable.py
File metadata and controls
93 lines (75 loc) · 2.75 KB
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
84
85
86
87
88
89
90
91
92
93
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
if version_info >= (3, 8):
from shutil import copytree
else:
from backports.shutil_copytree import copytree
from bentoml._internal.bento.bento import BentoInfo
from bentoml._internal.bento.build_config import DockerOptions
from bentoml._internal.bento.gen import generate_dockerfile
LAMBDA_DIR = Path(os.path.dirname(__file__), "aws_lambda")
TEMPLATE_PATH = LAMBDA_DIR.joinpath("template.j2")
APP_PATH = LAMBDA_DIR.joinpath("app.py")
ENTRY_SCRIPT = LAMBDA_DIR.joinpath("entry_script.sh")
AWS_LAMBDA_RIE = LAMBDA_DIR.joinpath("aws-lambda-rie-x86")
BENTOML_CONFIG_FILE = LAMBDA_DIR.joinpath("bentoml_server_config.yaml")
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)
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_generated = generate_dockerfile(
DockerOptions(**options).with_defaults(),
str(deployable_path),
use_conda=not info.conda.is_empty(),
)
dockerfile.write(dockerfile_generated)
# copy over app.py file
shutil.copy(str(APP_PATH), os.path.join(deployable_path, "app.py"))
# the entry_script.sh file that will be the entrypoint
shutil.copy(
str(ENTRY_SCRIPT),
os.path.join(deployable_path, "env", "docker", "entry_script.sh"),
)
# aws-lambda runtime-interface-emulator - check docs for more info
shutil.copy(
str(AWS_LAMBDA_RIE),
os.path.join(deployable_path, "aws-lambda-rie"),
)
# bentoml_config_file to dissable /metrics
shutil.copy(
str(BENTOML_CONFIG_FILE),
os.path.join(deployable_path, "bentoml_config.yaml"),
)
return str(deployable_path)