Skip to content

Commit

Permalink
Merge pull request #2270 from valory-xyz/feat/dict-overrides
Browse files Browse the repository at this point in the history
Implement dictionary overrides
  • Loading branch information
Adamantios authored Oct 18, 2024
2 parents 6b8bb66 + 11eed68 commit 3a07abf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
11 changes: 4 additions & 7 deletions autonomy/deploy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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(
Expand Down
27 changes: 18 additions & 9 deletions autonomy/deploy/generators/docker_compose/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# ------------------------------------------------------------------------------

"""Docker-compose Deployment Generator."""

import ipaddress
import os
from pathlib import Path
Expand Down Expand Up @@ -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,
Expand All @@ -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"],
Expand All @@ -167,13 +179,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(
Expand Down Expand Up @@ -354,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,
Expand Down
5 changes: 1 addition & 4 deletions autonomy/deploy/generators/docker_compose/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion deployments/Dockerfiles/autonomy/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ..
11 changes: 11 additions & 0 deletions docs/api/deploy/generators/docker_compose/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@ def build_tendermint_node_config(

Build tendermint node config for docker compose.

<a id="autonomy.deploy.generators.docker_compose.base.to_env_file"></a>

#### 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.

<a id="autonomy.deploy.generators.docker_compose.base.build_agent_config"></a>

#### build`_`agent`_`config

```python
def build_agent_config(node_id: int,
build_dir: Path,
container_name: str,
agent_vars: Dict,
runtime_image: str,
Expand Down

0 comments on commit 3a07abf

Please sign in to comment.