From bcccddfd986adfdc6efcff7342deb8cb3cfb0961 Mon Sep 17 00:00:00 2001 From: Adamantios Date: Fri, 18 Oct 2024 11:53:10 +0300 Subject: [PATCH 1/4] refactor: more readable and simplified --- autonomy/deploy/generators/docker_compose/base.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/autonomy/deploy/generators/docker_compose/base.py b/autonomy/deploy/generators/docker_compose/base.py index 28e4bbcf0e..95133c7691 100644 --- a/autonomy/deploy/generators/docker_compose/base.py +++ b/autonomy/deploy/generators/docker_compose/base.py @@ -167,13 +167,9 @@ def build_agent_config( # pylint: disable=too-many-arguments,too-many-locals config += f" - {open_aea_dir}:/open-aea\n" if extra_volumes is not None: - if all(isinstance(i, int) for i in extra_volumes): - extra_volumes = extra_volumes.get(node_id, {}) # type: ignore - for host_dir, container_dir in extra_volumes.items(): # type: ignore - vol_dir = Path(host_dir).resolve() - if not vol_dir.exists(): - vol_dir.mkdir(parents=True) - config += f" - {vol_dir}:{container_dir}:Z\n" + for host_dir, container_dir in extra_volumes.items(): + config += f" - {host_dir}:{container_dir}:Z\n" + Path(host_dir).resolve().mkdir(exist_ok=True, parents=True) if agent_ports is not None: port_mappings = map( From b6e997656d7a5f995df41714c3e82c1b018a769e Mon Sep 17 00:00:00 2001 From: Adamantios Date: Fri, 18 Oct 2024 11:56:34 +0300 Subject: [PATCH 2/4] feat: use an env file instead This will allow for dictionary overriding on the service level. --- .../deploy/generators/docker_compose/base.py | 17 +++++++++++++++-- .../generators/docker_compose/templates.py | 5 +---- .../deploy/generators/docker_compose/base.md | 11 +++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/autonomy/deploy/generators/docker_compose/base.py b/autonomy/deploy/generators/docker_compose/base.py index 95133c7691..d1fe7b2a21 100644 --- a/autonomy/deploy/generators/docker_compose/base.py +++ b/autonomy/deploy/generators/docker_compose/base.py @@ -18,6 +18,7 @@ # ------------------------------------------------------------------------------ """Docker-compose Deployment Generator.""" + import ipaddress import os from pathlib import Path @@ -131,8 +132,19 @@ def build_tendermint_node_config( # pylint: disable=too-many-arguments return config +def to_env_file(agent_vars: Dict, node_id: int, build_dir: Path) -> None: + """Create a env file under the `agent_build` folder.""" + agent_vars["PYTHONHASHSEED"] = 0 + agent_vars["LOG_FILE"] = f"/logs/aea_{node_id}.txt" + env_file_path = build_dir / f"agent_{node_id}.env" + with open(env_file_path, "w", encoding=DEFAULT_ENCODING) as env_file: + for key, value in agent_vars.items(): + env_file.write(f"{key}={value}\n") + + def build_agent_config( # pylint: disable=too-many-arguments,too-many-locals node_id: int, + build_dir: Path, container_name: str, agent_vars: Dict, runtime_image: str, @@ -147,13 +159,13 @@ def build_agent_config( # pylint: disable=too-many-arguments,too-many-locals ) -> str: """Build agent config.""" resources = resources if resources is not None else DEFAULT_RESOURCE_VALUES - agent_vars_string = "\n".join([f" - {k}={v}" for k, v in agent_vars.items()]) + to_env_file(agent_vars, node_id, build_dir) config = ABCI_NODE_TEMPLATE.format( node_id=node_id, container_name=container_name, - agent_vars=agent_vars_string, network_address=network_address, runtime_image=runtime_image, + env_file=f"agent_{node_id}.env", network_name=network_name, agent_memory_request=resources["agent"]["requested"]["memory"], agent_cpu_limit=resources["agent"]["limit"]["cpu"], @@ -350,6 +362,7 @@ def generate( container_name=self.service_builder.get_abci_container_name( index=i ), + build_dir=self.build_dir, runtime_image=runtime_image, agent_vars=agent_vars[i], dev_mode=self.dev_mode, diff --git a/autonomy/deploy/generators/docker_compose/templates.py b/autonomy/deploy/generators/docker_compose/templates.py index 7d52be35c9..b6d01efe4b 100644 --- a/autonomy/deploy/generators/docker_compose/templates.py +++ b/autonomy/deploy/generators/docker_compose/templates.py @@ -104,10 +104,7 @@ cpus: {agent_cpu_limit} container_name: {container_name} image: {runtime_image} - environment: - - PYTHONHASHSEED=0 - - LOG_FILE=/logs/aea_{node_id}.txt -{agent_vars} + env_file: {env_file} networks: {network_name}: ipv4_address: {network_address} diff --git a/docs/api/deploy/generators/docker_compose/base.md b/docs/api/deploy/generators/docker_compose/base.md index 71cb562363..e53288602f 100644 --- a/docs/api/deploy/generators/docker_compose/base.md +++ b/docs/api/deploy/generators/docker_compose/base.md @@ -32,12 +32,23 @@ def build_tendermint_node_config( Build tendermint node config for docker compose. + + +#### to`_`env`_`file + +```python +def to_env_file(agent_vars: Dict, node_id: int, build_dir: Path) -> None +``` + +Create a env file under the `agent_build` folder. + #### build`_`agent`_`config ```python def build_agent_config(node_id: int, + build_dir: Path, container_name: str, agent_vars: Dict, runtime_image: str, From b4381021f24b3891f6e370172726a64af91c8534 Mon Sep 17 00:00:00 2001 From: Adamantios Date: Fri, 18 Oct 2024 12:00:37 +0300 Subject: [PATCH 3/4] fix: variable for aea password --- autonomy/deploy/base.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/autonomy/deploy/base.py b/autonomy/deploy/base.py index 44adc93c85..4da4061523 100644 --- a/autonomy/deploy/base.py +++ b/autonomy/deploy/base.py @@ -731,13 +731,10 @@ def generate_common_vars(self, agent_n: int) -> Dict: ENV_VAR_ID: agent_n, ENV_VAR_AEA_AGENT: self.service.agent, ENV_VAR_LOG_LEVEL: self.log_level, - } - if self.deplopyment_type == DOCKER_COMPOSE_DEPLOYMENT: - agent_vars[ENV_VAR_AEA_PASSWORD] = "$OPEN_AUTONOMY_PRIVATE_KEY_PASSWORD" - else: - agent_vars[ENV_VAR_AEA_PASSWORD] = os.environ.get( + ENV_VAR_AEA_PASSWORD: os.environ.get( "OPEN_AUTONOMY_PRIVATE_KEY_PASSWORD", "" - ) + ), + } return agent_vars def generate_agent( From 11eed684d1daa8046b2fd755a1492a1fdc153847 Mon Sep 17 00:00:00 2001 From: Adamantios Date: Fri, 18 Oct 2024 12:01:02 +0300 Subject: [PATCH 4/4] style: fix typos --- autonomy/deploy/base.py | 2 +- deployments/Dockerfiles/autonomy/scripts/install.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/autonomy/deploy/base.py b/autonomy/deploy/base.py index 4da4061523..1ea737f508 100644 --- a/autonomy/deploy/base.py +++ b/autonomy/deploy/base.py @@ -169,7 +169,7 @@ def __init__( # pylint: disable=too-many-arguments if apply_environment_variables: warn( # pragma: no cover "`apply_environment_variables` argument is deprecated and will be removed in v1.0.0, " - "usage of environment varibales is default now.", + "usage of environment variables is default now.", DeprecationWarning, stacklevel=2, ) diff --git a/deployments/Dockerfiles/autonomy/scripts/install.sh b/deployments/Dockerfiles/autonomy/scripts/install.sh index 311e8a0f73..696129a475 100644 --- a/deployments/Dockerfiles/autonomy/scripts/install.sh +++ b/deployments/Dockerfiles/autonomy/scripts/install.sh @@ -18,7 +18,7 @@ echo "Successfully built the host dependencies." echo "Installing the necessary python dependencies!" aea install --timeout 600 $EXTRA_DEPENDENCIES || exit 1 -echo "Successfully Installed the python dependecies." +echo "Successfully Installed the python dependencies." echo "Done." cd ..