From 2e9489cfc9d92254611a7720d65280add3de1499 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Tue, 3 Oct 2023 15:50:24 -0600 Subject: [PATCH] First part of deployments for external repos --- app/deploy.py | 9 ++------- app/deploy_util.py | 4 ++-- app/deployment_create.py | 13 +++++++------ app/util.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/app/deploy.py b/app/deploy.py index 51749ff9..b5587c71 100644 --- a/app/deploy.py +++ b/app/deploy.py @@ -20,13 +20,12 @@ import copy import os import sys from dataclasses import dataclass -from decouple import config from importlib import resources import subprocess from python_on_whales import DockerClient, DockerException import click from pathlib import Path -from app.util import include_exclude_check, get_parsed_stack_config, global_options2 +from app.util import include_exclude_check, get_parsed_stack_config, global_options2, get_dev_root_path from app.deploy_types import ClusterContext, DeployCommandContext from app.deployment_create import create as deployment_create from app.deployment_create import init as deployment_init @@ -235,11 +234,7 @@ def _make_runtime_env(ctx): # stack has to be either PathLike pointing to a stack yml file, or a string with the name of a known stack def _make_cluster_context(ctx, stack, include, exclude, cluster, env_file): - if ctx.local_stack: - dev_root_path = os.getcwd()[0:os.getcwd().rindex("stack-orchestrator")] - print(f'Local stack dev_root_path (CERC_REPO_BASE_DIR) overridden to: {dev_root_path}') - else: - dev_root_path = os.path.expanduser(config("CERC_REPO_BASE_DIR", default="~/cerc")) + dev_root_path = get_dev_root_path(ctx) # TODO: huge hack, fix this # If the caller passed a path for the stack file, then we know that we can get the compose files diff --git a/app/deploy_util.py b/app/deploy_util.py index 2f5f0188..498e3dfd 100644 --- a/app/deploy_util.py +++ b/app/deploy_util.py @@ -16,14 +16,14 @@ import os from typing import List from app.deploy_types import DeployCommandContext, VolumeMapping -from app.util import get_parsed_stack_config, get_yaml, get_compose_file_dir +from app.util import get_parsed_stack_config, get_yaml, get_compose_file_dir, get_pod_list 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"] + pods = get_pod_list(parsed_stack) yaml = get_yaml() for pod in pods: pod_file_path = os.path.join(get_compose_file_dir(), f"docker-compose-{pod}.yml") diff --git a/app/deployment_create.py b/app/deployment_create.py index 76016262..9693b9c8 100644 --- a/app/deployment_create.py +++ b/app/deployment_create.py @@ -20,7 +20,8 @@ from pathlib import Path import random 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_pod_list, get_pod_file_path) from app.util import get_compose_file_dir from app.deploy_types import DeploymentContext, LaconicStackSetupCommand @@ -32,10 +33,10 @@ def _make_default_deployment_dir(): def _get_ports(stack): ports = {} parsed_stack = get_parsed_stack_config(stack) - pods = parsed_stack["pods"] + pods = get_pod_list(parsed_stack) 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 = get_pod_file_path(parsed_stack, pod) parsed_pod_file = yaml.load(open(pod_file_path, "r")) if "services" in parsed_pod_file: for svc_name, svc in parsed_pod_file["services"].items(): @@ -49,10 +50,10 @@ def _get_named_volumes(stack): # Parse the compose files looking for named volumes named_volumes = [] parsed_stack = get_parsed_stack_config(stack) - pods = parsed_stack["pods"] + pods = get_pod_list(parsed_stack) 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 = get_pod_file_path(parsed_stack, pod) parsed_pod_file = yaml.load(open(pod_file_path, "r")) if "volumes" in parsed_pod_file: volumes = parsed_pod_file["volumes"] @@ -263,7 +264,7 @@ def init(ctx, config, output, map_ports_to_host): def _write_config_file(spec_file: Path, config_env_file: Path): spec_content = get_parsed_deployment_spec(spec_file) - if spec_content["config"]: + if "config" in spec_content and spec_content["config"]: config_vars = spec_content["config"] if config_vars: with open(config_env_file, "w") as output_file: diff --git a/app/util.py b/app/util.py index 9d9eaa33..dd581f1d 100644 --- a/app/util.py +++ b/app/util.py @@ -13,6 +13,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from decouple import config import os.path import sys import ruamel.yaml @@ -37,6 +38,16 @@ def get_stack_file_path(stack): return stack_file_path +def get_dev_root_path(ctx): + if ctx and ctx.local_stack: + # TODO: This code probably doesn't work + dev_root_path = os.getcwd()[0:os.getcwd().rindex("stack-orchestrator")] + print(f'Local stack dev_root_path (CERC_REPO_BASE_DIR) overridden to: {dev_root_path}') + else: + dev_root_path = os.path.expanduser(config("CERC_REPO_BASE_DIR", default="~/cerc")) + return dev_root_path + + # Caller can pass either the name of a stack, or a path to a stack file def get_parsed_stack_config(stack): stack_file_path = stack if isinstance(stack, os.PathLike) else get_stack_file_path(stack) @@ -56,6 +67,30 @@ def get_parsed_stack_config(stack): sys.exit(1) +def get_pod_list(parsed_stack): + # Handle both old and new format + pods = parsed_stack["pods"] + if type(pods[0]) is str: + result = pods + else: + result = [] + for pod in pods: + result.append(pod["name"]) + return result + + +def get_pod_file_path(parsed_stack, pod_name: str): + pods = parsed_stack["pods"] + if type(pods[0]) is str: + result = os.path.join(get_compose_file_dir(), f"docker-compose-{pod_name}.yml") + else: + for pod in pods: + if pod["name"] == pod_name: + pod_root_dir = os.path.join(get_dev_root_path(None), pod["repository"].split("/")[-1], pod["path"]) + result = os.path.join(pod_root_dir, "docker-compose.yml") + return result + + 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