diff --git a/app/deploy_util.py b/app/deploy_util.py index c853fc83..814f8001 100644 --- a/app/deploy_util.py +++ b/app/deploy_util.py @@ -13,13 +13,29 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import os from typing import List from dataclasses import dataclass from app.deploy_types import DeployCommandContext, VolumeMapping +from app.util import get_parsed_stack_config, get_yaml, get_compose_file_dir -def _container_image_from_service(service: str): - return "cerc/test-container:local" +def _container_image_from_service(stack:str, service: str): + # Parse the compose files looking for the image name of the specified service + image_name = None + parsed_stack = get_parsed_stack_config(stack) + pods = parsed_stack["pods"] + yaml = get_yaml() + for pod in pods: + pod_file_path = os.path.join(get_compose_file_dir(), f"docker-compose-{pod}.yml") + parsed_pod_file = yaml.load(open(pod_file_path, "r")) + if "services" in parsed_pod_file: + services = parsed_pod_file["services"] + if service in services: + service_definition = services[service] + if "image" in service_definition: + image_name = service_definition["image"] + return image_name def _volumes_to_docker(mounts: List[VolumeMapping]): @@ -33,7 +49,7 @@ def _volumes_to_docker(mounts: List[VolumeMapping]): def run_container_command(ctx: DeployCommandContext, service: str, command: str, mounts: List[VolumeMapping]): docker = ctx.docker - container_image = _container_image_from_service(service) + container_image = _container_image_from_service(ctx.stack, service) docker_volumes = _volumes_to_docker(mounts) docker_output = docker.run(container_image, ["-c", command], entrypoint="bash", volumes=docker_volumes) # There doesn't seem to be a way to get an exit code from docker.run() diff --git a/app/deployment_create.py b/app/deployment_create.py index 0766e22b..47f96e34 100644 --- a/app/deployment_create.py +++ b/app/deployment_create.py @@ -20,7 +20,7 @@ import os from pathlib import Path from shutil import copyfile, copytree import sys -from app.util import get_stack_file_path, get_parsed_deployment_spec, get_parsed_stack_config, global_options, get_yaml +from app.util import get_stack_file_path, get_parsed_deployment_spec, get_parsed_stack_config, global_options, get_yaml, get_compose_file_dir from app.deploy_types import DeploymentContext, DeployCommandContext @@ -28,14 +28,6 @@ def _make_default_deployment_dir(): return "deployment-001" -def _get_compose_file_dir(): - # TODO: refactor to use common code with deploy command - # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure - data_dir = Path(__file__).absolute().parent.joinpath("data") - source_compose_dir = data_dir.joinpath("compose") - return source_compose_dir - - def _get_named_volumes(stack): # Parse the compose files looking for named volumes named_volumes = [] @@ -43,7 +35,7 @@ def _get_named_volumes(stack): pods = parsed_stack["pods"] yaml = get_yaml() for pod in pods: - pod_file_path = os.path.join(_get_compose_file_dir(), f"docker-compose-{pod}.yml") + pod_file_path = os.path.join(get_compose_file_dir(), f"docker-compose-{pod}.yml") parsed_pod_file = yaml.load(open(pod_file_path, "r")) if "volumes" in parsed_pod_file: volumes = parsed_pod_file["volumes"] diff --git a/app/util.py b/app/util.py index 2b12cfbc..42a4673e 100644 --- a/app/util.py +++ b/app/util.py @@ -56,6 +56,14 @@ def get_parsed_stack_config(stack): sys.exit(1) +def get_compose_file_dir(): + # TODO: refactor to use common code with deploy command + # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure + data_dir = Path(__file__).absolute().parent.joinpath("data") + source_compose_dir = data_dir.joinpath("compose") + return source_compose_dir + + def get_parsed_deployment_spec(spec_file): spec_file_path = Path(spec_file) try: