Skip to content

Commit 0b5b646

Browse files
committed
add env file
1 parent c962312 commit 0b5b646

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

auto_dev/commands/run.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ def dev(ctx, agent_public_id: PublicId, verbose: bool, force: bool, fetch: bool)
117117
@click.option("--fetch/--no-fetch", help="Fetch from registry or use local service package", default=True)
118118
@click.option("--keysfile", help="Path to the private keys file.", type=click.File(), default="keys.json")
119119
@click.option("--number_of_agents", "-n", help="Number of agents to run.", type=int, default=1)
120+
@click.option(
121+
"--env_file",
122+
help="Path to the environment file.",
123+
type=click.File(),
124+
default=".env" if Path(".env").exists() else None,
125+
)
120126
@click.pass_context
121127
def prod(
122128
ctx,
@@ -126,6 +132,7 @@ def prod(
126132
fetch: bool,
127133
keysfile: click.File,
128134
number_of_agents: int,
135+
env_file: click.File,
129136
) -> None:
130137
"""Run an agent in production mode.
131138
@@ -154,6 +161,9 @@ def prod(
154161
if not Path(keysfile.name).exists():
155162
logger.error(f"Keys file not found at {keysfile.name}")
156163
sys.exit(1)
164+
if not Path(env_file.name).exists():
165+
logger.error(f"Environment file not found at {env_file.name}")
166+
sys.exit(1)
157167

158168
runner = ProdAgentRunner(
159169
service_public_id=service_public_id,
@@ -163,6 +173,7 @@ def prod(
163173
fetch=fetch,
164174
keysfile=Path(keysfile.name).absolute(),
165175
number_of_agents=number_of_agents,
176+
env_file=Path(env_file.name).absolute(),
166177
)
167178
runner.run()
168179
logger.info("Agent run complete. 😎")

auto_dev/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class FileType(Enum):
1010
JSON = "json"
1111
TEXT = "txt"
1212
PYTHON = "py"
13+
ENV = "env"
1314

1415

1516
class FileOperation(Enum):

auto_dev/services/runner/prod_runner.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
from dataclasses import dataclass
1111

1212
import rich
13+
from dotenv import dotenv_values
1314
from aea.skills.base import PublicId
1415
from aea.cli.push_all import push_all_packages
1516
from aea.configurations.base import PackageType
1617
from aea.cli.registry.settings import REGISTRY_REMOTE
1718
from aea.configurations.constants import PACKAGES, SERVICES, DEFAULT_SERVICE_CONFIG_FILE
1819
from autonomy.configurations.base import PACKAGE_TYPE_TO_CONFIG_CLASS
1920

20-
from auto_dev.utils import change_dir, load_autonolas_yaml
21+
from auto_dev.utils import FileType, change_dir, write_to_file, load_autonolas_yaml
2122
from auto_dev.exceptions import UserInputError
2223
from auto_dev.cli_executor import CommandExecutor
23-
from auto_dev.services.runner.base import AgentRunner
2424
from auto_dev.workflow_manager import Task
25+
from auto_dev.services.runner.base import AgentRunner
2526

2627

2728
TENDERMINT_RESET_TIMEOUT = 10
@@ -48,6 +49,7 @@ class ProdAgentRunner(AgentRunner):
4849
fetch: bool = False
4950
keysfile: Path = "keys.json"
5051
number_of_agents: int = 1
52+
env_file: Path = ".env"
5153

5254
def run(self) -> None:
5355
"""Run the agent."""
@@ -145,6 +147,12 @@ def validate(self) -> None:
145147
if not self.keysfile.is_file():
146148
self.logger.error(f"Keys file {self.keysfile} is not a file.")
147149
sys.exit(1)
150+
if not self.env_file.exists():
151+
self.logger.error(f"Environment file {self.env_file} not found.")
152+
sys.exit(1)
153+
if not self.env_file.is_file():
154+
self.logger.error(f"Environment file {self.env_file} is not a file.")
155+
sys.exit(1)
148156

149157
available_keys = json.loads(self.keysfile.read_text())
150158
if len(available_keys) < 1:
@@ -191,10 +199,20 @@ def build_deployment(self) -> None:
191199
"""Build the deployment."""
192200
self.logger.info("Building the deployment...")
193201
env_vars = self.generate_env_vars()
202+
194203
self.execute_command(
195-
f"autonomy deploy build {self.keysfile} --o abci_build",
204+
f"autonomy deploy build {self.keysfile} --aev --o abci_build",
196205
env_vars=env_vars,
197206
)
207+
208+
# Note: autonomy deploy build doesn't write the env vars to the agent env files. So we do it manually here.
209+
for agent_id in range(self.number_of_agents):
210+
agent_env_path = Path(f"abci_build/agent_{agent_id}.env")
211+
if agent_env_path.exists():
212+
existing_env = dotenv_values(agent_env_path)
213+
agent_env_vars = {**env_vars, **existing_env}
214+
write_to_file(agent_env_path, agent_env_vars, file_type=FileType.ENV)
215+
198216
self.logger.info("Deployment built successfully. 🎉")
199217

200218
def manage_keys(
@@ -206,8 +224,10 @@ def manage_keys(
206224

207225
def generate_env_vars(self) -> dict:
208226
"""Generate the environment variables for the deployment."""
227+
env_vars = dotenv_values(self.env_file)
209228
return {
210229
"ALL_PARTICIPANTS": json.dumps(self.all_participants),
230+
**env_vars,
211231
}
212232

213233
def execute_agent(
@@ -220,7 +240,8 @@ def execute_agent(
220240

221241
task = Task(command="docker compose up -d --remove-orphans", working_dir="abci_build").work()
222242
if task.is_failed:
223-
raise RuntimeError(f"Agent execution failed. {task.client.output}")
243+
msg = f"Agent execution failed. {task.client.output}"
244+
raise RuntimeError(msg)
224245
self.logger.info("Agent execution complete. 😎")
225246

226247
def execute_command(self, command: str, verbose=None, env_vars=None, spinner=False) -> None:

auto_dev/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ def write_to_file(file_path: str, content: Any, file_type: FileType = FileType.T
357357
json.dump(content, f, **json_kwargs)
358358
elif file_type is FileType.PYTHON:
359359
f.write(content)
360+
elif file_type is FileType.ENV:
361+
for key, value in content.items():
362+
f.write(f"{key}={value}\n")
360363
else:
361364
msg = f"Invalid file_type {file_type}, must be one of {list(FileType)}."
362365
raise ValueError(msg)

0 commit comments

Comments
 (0)