Skip to content

Commit a6702c4

Browse files
authored
Merge pull request #2314 from valory-xyz/feat/multiarch-build-image
Feat: Support multi platform docker image builds
2 parents 89ae715 + f1c7d18 commit a6702c4

File tree

8 files changed

+67
-41
lines changed

8 files changed

+67
-41
lines changed

autonomy/cli/build_images.py

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
),
6868
help="Specify custom dockerfile for building the agent",
6969
)
70+
@click.option(
71+
"--platform",
72+
type=str,
73+
help="Specify the target architecture platform for the image.",
74+
)
75+
@click.option("--push", is_flag=True, help="Push image to docker hub.", default=False)
7076
@image_author_option
7177
def build_image( # pylint: disable=too-many-arguments
7278
agent: Optional[PublicId],
@@ -77,6 +83,8 @@ def build_image( # pylint: disable=too-many-arguments
7783
dev: bool = False,
7884
version: Optional[str] = None,
7985
image_author: Optional[str] = None,
86+
platform: Optional[str] = None,
87+
push: bool = False,
8088
) -> None:
8189
"""Build runtime images for autonomous agents."""
8290
if dev:
@@ -93,4 +101,6 @@ def build_image( # pylint: disable=too-many-arguments
93101
version=version,
94102
image_author=image_author,
95103
dockerfile=dockerfile,
104+
platform=platform,
105+
push=push,
96106
)

autonomy/cli/helpers/image.py

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def build_image( # pylint: disable=too-many-arguments
3737
image_author: Optional[str] = None,
3838
extra_dependencies: Optional[Tuple[Dependency, ...]] = None,
3939
dockerfile: Optional[Path] = None,
40+
platform: Optional[str] = None,
41+
push: bool = False,
4042
) -> None:
4143
"""Build agent/service image."""
4244
extra_dependencies = extra_dependencies or ()
@@ -54,4 +56,6 @@ def build_image( # pylint: disable=too-many-arguments
5456
image_author=image_author,
5557
extra_dependencies=extra_dependencies,
5658
dockerfile=dockerfile,
59+
platform=platform,
60+
push=push,
5761
)

autonomy/deploy/image.py

+31-34
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
"""Image building."""
2121

2222

23-
import json
23+
import subprocess # nosec
2424
from pathlib import Path
25-
from typing import Dict, Optional, Tuple
25+
from typing import Optional, Tuple
2626

2727
from aea.cli.utils.config import get_default_author_from_cli_config
2828
from aea.configurations.data_types import Dependency, PublicId
@@ -35,7 +35,6 @@
3535
)
3636
from autonomy.data import DATA_DIR
3737
from autonomy.deploy.constants import DOCKERFILES
38-
from autonomy.deploy.generators.docker_compose.base import get_docker_client
3938

4039

4140
def generate_dependency_flag_var(dependencies: Tuple[Dependency, ...]) -> str:
@@ -64,20 +63,13 @@ def build_image( # pylint: disable=too-many-arguments,too-many-locals
6463
image_author: Optional[str] = None,
6564
extra_dependencies: Optional[Tuple[Dependency, ...]] = None,
6665
dockerfile: Optional[Path] = None,
66+
platform: Optional[str] = None,
67+
push: bool = False,
6768
) -> None:
6869
"""Command to build images from for skaffold deployment."""
6970

7071
tag: str
7172
path: str
72-
buildargs: Dict[str, str]
73-
docker_client = get_docker_client()
74-
buildargs = {
75-
"AUTONOMY_IMAGE_NAME": AUTONOMY_IMAGE_NAME,
76-
"AUTONOMY_IMAGE_VERSION": AUTONOMY_IMAGE_VERSION,
77-
"AEA_AGENT": str(agent),
78-
"AUTHOR": get_default_author_from_cli_config(),
79-
"EXTRA_DEPENDENCIES": generate_dependency_flag_var(extra_dependencies or ()),
80-
}
8173

8274
image_version = version or agent.hash
8375
path = str(DATA_DIR / DOCKERFILES / "agent")
@@ -90,30 +82,35 @@ def build_image( # pylint: disable=too-many-arguments,too-many-locals
9082
version=image_version,
9183
)
9284

93-
stream = docker_client.api.build(
94-
path=path,
95-
tag=tag,
96-
buildargs=buildargs,
97-
pull=pull,
85+
build_process = subprocess.run( # nosec # pylint: disable=subprocess-run-check
86+
[
87+
"docker",
88+
"build",
89+
"--build-arg",
90+
f"AUTONOMY_IMAGE_NAME={AUTONOMY_IMAGE_NAME}",
91+
"--build-arg",
92+
f"AUTONOMY_IMAGE_VERSION={AUTONOMY_IMAGE_VERSION}",
93+
"--build-arg",
94+
f"AUTHOR={get_default_author_from_cli_config()}",
95+
"--build-arg",
96+
f"AEA_AGENT={str(agent)}",
97+
"--build-arg",
98+
f"EXTRA_DEPENDENCIES={generate_dependency_flag_var(extra_dependencies or ())}",
99+
"--tag",
100+
tag,
101+
"--no-cache",
102+
]
103+
+ (["--platform", platform] if platform else [])
104+
+ []
105+
+ (["--push"] if push else [])
106+
+ []
107+
+ (["--pull"] if pull else [])
108+
+ [
109+
path,
110+
]
98111
)
99112

100-
for stream_obj in stream:
101-
for line in stream_obj.decode().split("\n"):
102-
line = line.strip()
103-
if not line:
104-
continue
105-
stream_data = json.loads(line)
106-
if "stream" in stream_data:
107-
print("[docker]" + stream_data["stream"], end="")
108-
elif "errorDetail" in stream_data:
109-
raise ImageBuildFailed(
110-
"Image build failed with error "
111-
+ stream_data["errorDetail"]["message"]
112-
)
113-
elif "aux" in stream_data:
114-
print("[docker]" + stream_data["aux"]["ID"], end="")
115-
elif "status" in stream_data: # pragma: no cover
116-
print("[docker]" + stream_data["status"], end="")
113+
build_process.check_returncode()
117114

118115

119116
class ImageBuildFailed(Exception):

deployments/Dockerfiles/autonomy/scripts/install.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ aea build || exit 1
1717
echo "Successfully built the host dependencies."
1818

1919
echo "Installing the necessary python dependencies!"
20-
aea install --timeout 600 $EXTRA_DEPENDENCIES || exit 1
20+
aea install --timeout 1200 $EXTRA_DEPENDENCIES || exit 1
2121
echo "Successfully Installed the python dependencies."
2222

2323
echo "Done."

docs/api/cli/build_images.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ Build images.
4747
),
4848
help="Specify custom dockerfile for building the agent",
4949
)
50+
@click.option(
51+
"--platform",
52+
type=str,
53+
help="Specify the target architecture platform for the image.",
54+
)
55+
@click.option("--push",
56+
is_flag=True,
57+
help="Push image to docker hub.",
58+
default=False)
5059
@image_author_option
5160
def build_image(agent: Optional[PublicId],
5261
service_dir: Optional[Path],
@@ -55,7 +64,9 @@ def build_image(agent: Optional[PublicId],
5564
pull: bool = False,
5665
dev: bool = False,
5766
version: Optional[str] = None,
58-
image_author: Optional[str] = None) -> None
67+
image_author: Optional[str] = None,
68+
platform: Optional[str] = None,
69+
push: bool = False) -> None
5970
```
6071

6172
Build runtime images for autonomous agents.

docs/api/cli/helpers/image.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ def build_image(agent: Optional[PublicId],
1515
version: Optional[str] = None,
1616
image_author: Optional[str] = None,
1717
extra_dependencies: Optional[Tuple[Dependency, ...]] = None,
18-
dockerfile: Optional[Path] = None) -> None
18+
dockerfile: Optional[Path] = None,
19+
platform: Optional[str] = None,
20+
push: bool = False) -> None
1921
```
2022

2123
Build agent/service image.

docs/api/deploy/image.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def build_image(agent: PublicId,
3434
version: Optional[str] = None,
3535
image_author: Optional[str] = None,
3636
extra_dependencies: Optional[Tuple[Dependency, ...]] = None,
37-
dockerfile: Optional[Path] = None) -> None
37+
dockerfile: Optional[Path] = None,
38+
platform: Optional[str] = None,
39+
push: bool = False) -> None
3840
```
3941

4042
Command to build images from for skaffold deployment.

tests/test_autonomy/test_cli/test_build_image.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ def test_service_file_missing(
180180
def test_image_build_fail(self) -> None:
181181
"""Test prod build."""
182182

183-
result = self.run_cli(
183+
exit_code, stdout, stderr = self.run_cli_subprocess(
184184
commands=(
185185
"valory/agent:bafybeihyasfforsfualp6jnhh2jj7nreqmws2ygyfnh4p3idmfkm5yxu11",
186186
)
187187
)
188188

189-
assert result.exit_code == 1, result.output
190-
assert "Error occured while downloading agent" in result.output
189+
assert exit_code == 1, stdout
190+
assert "Error occured while downloading agent" in stderr

0 commit comments

Comments
 (0)