First part of deployments for external repos

This commit is contained in:
David Boreham 2023-10-03 15:50:24 -06:00
parent 3c5489681f
commit 2e9489cfc9
4 changed files with 46 additions and 15 deletions

View File

@ -20,13 +20,12 @@ import copy
import os import os
import sys import sys
from dataclasses import dataclass from dataclasses import dataclass
from decouple import config
from importlib import resources from importlib import resources
import subprocess import subprocess
from python_on_whales import DockerClient, DockerException from python_on_whales import DockerClient, DockerException
import click import click
from pathlib import Path 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.deploy_types import ClusterContext, DeployCommandContext
from app.deployment_create import create as deployment_create from app.deployment_create import create as deployment_create
from app.deployment_create import init as deployment_init 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 # 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): def _make_cluster_context(ctx, stack, include, exclude, cluster, env_file):
if ctx.local_stack: dev_root_path = get_dev_root_path(ctx)
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"))
# TODO: huge hack, fix this # 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 # If the caller passed a path for the stack file, then we know that we can get the compose files

View File

@ -16,14 +16,14 @@
import os import os
from typing import List from typing import List
from app.deploy_types import DeployCommandContext, VolumeMapping 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): def _container_image_from_service(stack: str, service: str):
# Parse the compose files looking for the image name of the specified service # Parse the compose files looking for the image name of the specified service
image_name = None image_name = None
parsed_stack = get_parsed_stack_config(stack) parsed_stack = get_parsed_stack_config(stack)
pods = parsed_stack["pods"] pods = get_pod_list(parsed_stack)
yaml = get_yaml() yaml = get_yaml()
for pod in pods: 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")

View File

@ -20,7 +20,8 @@ from pathlib import Path
import random import random
from shutil import copyfile, copytree from shutil import copyfile, copytree
import sys 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.util import get_compose_file_dir
from app.deploy_types import DeploymentContext, LaconicStackSetupCommand from app.deploy_types import DeploymentContext, LaconicStackSetupCommand
@ -32,10 +33,10 @@ def _make_default_deployment_dir():
def _get_ports(stack): def _get_ports(stack):
ports = {} ports = {}
parsed_stack = get_parsed_stack_config(stack) parsed_stack = get_parsed_stack_config(stack)
pods = parsed_stack["pods"] pods = get_pod_list(parsed_stack)
yaml = get_yaml() yaml = get_yaml()
for pod in pods: 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")) parsed_pod_file = yaml.load(open(pod_file_path, "r"))
if "services" in parsed_pod_file: if "services" in parsed_pod_file:
for svc_name, svc in parsed_pod_file["services"].items(): 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 # Parse the compose files looking for named volumes
named_volumes = [] named_volumes = []
parsed_stack = get_parsed_stack_config(stack) parsed_stack = get_parsed_stack_config(stack)
pods = parsed_stack["pods"] pods = get_pod_list(parsed_stack)
yaml = get_yaml() yaml = get_yaml()
for pod in pods: 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")) parsed_pod_file = yaml.load(open(pod_file_path, "r"))
if "volumes" in parsed_pod_file: if "volumes" in parsed_pod_file:
volumes = parsed_pod_file["volumes"] 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): def _write_config_file(spec_file: Path, config_env_file: Path):
spec_content = get_parsed_deployment_spec(spec_file) 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"] config_vars = spec_content["config"]
if config_vars: if config_vars:
with open(config_env_file, "w") as output_file: with open(config_env_file, "w") as output_file:

View File

@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http:#www.gnu.org/licenses/>. # along with this program. If not, see <http:#www.gnu.org/licenses/>.
from decouple import config
import os.path import os.path
import sys import sys
import ruamel.yaml import ruamel.yaml
@ -37,6 +38,16 @@ def get_stack_file_path(stack):
return stack_file_path 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 # Caller can pass either the name of a stack, or a path to a stack file
def get_parsed_stack_config(stack): def get_parsed_stack_config(stack):
stack_file_path = stack if isinstance(stack, os.PathLike) else get_stack_file_path(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) 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(): def get_compose_file_dir():
# TODO: refactor to use common code with deploy command # TODO: refactor to use common code with deploy command
# See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure