Skip to content

Commit

Permalink
Fix: Make local deployment compatible with middleware
Browse files Browse the repository at this point in the history
Signed-off-by: OjusWiZard <[email protected]>
  • Loading branch information
OjusWiZard committed Nov 28, 2024
1 parent 325972c commit 32a9115
Show file tree
Hide file tree
Showing 15 changed files with 1,093 additions and 722 deletions.
2 changes: 1 addition & 1 deletion autonomy/cli/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def run(
)
click.echo(f"Running build @ {build_dir}")
if deployment_type == "localhost":
run_host_deployment(build_dir)
run_host_deployment(build_dir, detach)
else:
run_deployment(build_dir, no_recreate, remove_orphans, detach=detach)

Expand Down
29 changes: 19 additions & 10 deletions autonomy/cli/helpers/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
import sys
import time
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from typing import Callable, Dict, List, Optional, Tuple

import click
from aea.configurations.constants import AGENT
from aea.configurations.data_types import PublicId
from aea.helpers.base import cd
from compose.cli import main as docker_compose
Expand Down Expand Up @@ -178,15 +179,19 @@ def _get_deattached_creation_flags() -> int:
return DEATTACH_WINDOWS_FLAG if platform.system() == "Windows" else 0


def _start_localhost_agent(working_dir: Path) -> None:
def _start_localhost_agent(working_dir: Path, detach: bool) -> None:
"""Start localhost agent process."""
env = json.loads((working_dir / AGENT_VARS_CONFIG_FILE).read_text())
subprocess.run( # pylint: disable=subprocess-run-check # nosec
process_fn: Callable = subprocess.Popen if detach else subprocess.run # type: ignore[assignment]
process = process_fn( # pylint: disable=subprocess-run-check # nosec
args=[sys.executable, "-m", "aea.cli", "run"],
cwd=working_dir,
cwd=working_dir / AGENT,
env={**os.environ, **env},
creationflags=_get_deattached_creation_flags(), # Detach process from the main process
)
(working_dir / "agent.pid").write_text(
data=str(process.pid),
)


def _start_localhost_tendermint(working_dir: Path) -> subprocess.Popen:
Expand Down Expand Up @@ -215,13 +220,17 @@ def _start_localhost_tendermint(working_dir: Path) -> subprocess.Popen:
return process


def run_host_deployment(build_dir: Path) -> None:
def run_host_deployment(build_dir: Path, detach: bool = False) -> None:
"""Run host deployment."""
tm_process = _start_localhost_tendermint(build_dir)
try:
_start_localhost_agent(build_dir)
finally:
_start_localhost_agent(build_dir, detach)
except Exception as e: # pylint: disable=broad-except
click.echo(e)
tm_process.terminate()
finally:
if not detach:
tm_process.terminate()


def stop_deployment(build_dir: Path) -> None:
Expand Down Expand Up @@ -271,9 +280,6 @@ def build_deployment( # pylint: disable=too-many-arguments, too-many-locals

click.echo(f"Building deployment @ {build_dir}")
build_dir.mkdir()
if mkdir is not None:
_build_dirs(build_dir, mkdir)

if service_hash_id is None:
service_hash_id = build_hash_id()

Expand All @@ -300,6 +306,9 @@ def build_deployment( # pylint: disable=too-many-arguments, too-many-locals
image_author=image_author,
resources=resources,
)
if mkdir is not None:
_build_dirs(build_dir, mkdir)

click.echo(report)


Expand Down
2 changes: 1 addition & 1 deletion autonomy/deploy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
TENDERMINT_VARS_CONFIG_FILE = "tendermint.json"
AGENT_VARS_CONFIG_FILE = "agent.json"
TENDERMINT_FLASK_APP_PATH = (
Path("deployments") / "Dockerfiles" / "tendermint" / "app.py"
Path("autonomy") / "deploy" / "generators" / "localhost" / "tendermint" / "app.py"
)
DEATTACH_WINDOWS_FLAG = 0x00000008

Expand Down
17 changes: 7 additions & 10 deletions autonomy/deploy/generators/localhost/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
import json
import subprocess # nosec
import typing as t
from pathlib import Path

from aea.configurations.constants import (
DEFAULT_AEA_CONFIG_FILE,
DEFAULT_LEDGER,
LEDGER,
PRIVATE_KEY,
PRIVATE_KEY_PATH_SCHEMA,
)
from aea.helpers.io import open_file
from aea.helpers.yaml_utils import yaml_load_all

from autonomy.deploy.base import BaseDeploymentGenerator
from autonomy.deploy.constants import (
Expand All @@ -47,7 +45,6 @@
TM_STATE_DIR,
)
from autonomy.deploy.generators.localhost.utils import (
_run_aea_cmd,
check_tendermint_version,
setup_agent,
)
Expand All @@ -59,6 +56,11 @@ class HostDeploymentGenerator(BaseDeploymentGenerator):
output_name: str = AGENT_VARS_CONFIG_FILE
deployment_type: str = "localhost"

@property
def agent_dir(self) -> Path:
"""Path to the agent directory."""
return self.build_dir / "agent"

def generate_config_tendermint(self) -> "HostDeploymentGenerator":
"""Generate tendermint configuration."""
tmhome = str(self.build_dir / "node")
Expand Down Expand Up @@ -115,7 +117,7 @@ def _populate_keys(self) -> None:
ledger = kp.get(LEDGER, DEFAULT_LEDGER)
keys_file = self.build_dir / PRIVATE_KEY_PATH_SCHEMA.format(ledger)
keys_file.write_text(key, encoding=DEFAULT_ENCODING)
_run_aea_cmd(["issue-certificates"], cwd=self.build_dir)
setup_agent(self.build_dir, self.agent_dir, keys_file)

def _populate_keys_multiledger(self) -> None:
"""Populate the keys directory with multiple set of keys"""
Expand All @@ -131,9 +133,4 @@ def populate_private_keys(self) -> "HostDeploymentGenerator":
def write_config(self) -> "BaseDeploymentGenerator":
"""Write output to build dir"""
super().write_config()
# copy private keys
with open_file(DEFAULT_AEA_CONFIG_FILE, "r") as fp:
aea_config = yaml_load_all(fp)

setup_agent(self.build_dir, aea_config[0])
return self
20 changes: 20 additions & 0 deletions autonomy/deploy/generators/localhost/tendermint/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""Tendermint flask app."""
Loading

0 comments on commit 32a9115

Please sign in to comment.