Refactoring

This commit is contained in:
David Boreham 2023-11-08 01:05:51 -07:00
parent f7eef0162f
commit 4d839329e7
4 changed files with 20 additions and 18 deletions

View File

@ -64,7 +64,7 @@ def command(ctx, include, exclude, env_file, cluster, deploy_to):
def create_deploy_context( def create_deploy_context(
global_context, deployment_context: DeploymentContext, stack, include, exclude, cluster, env_file, deployer): global_context, deployment_context: DeploymentContext, stack, include, exclude, cluster, env_file, deployer):
cluster_context = _make_cluster_context(global_context, stack, include, exclude, cluster, env_file) cluster_context = _make_cluster_context(global_context, stack, include, exclude, cluster, env_file)
deployment_dir = deployment_context.dir if deployment_context else None deployment_dir = deployment_context.deployment_dir if deployment_context else None
# See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/ # See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/
deployer = getDeployer(deployer, deployment_dir, compose_files=cluster_context.compose_files, deployer = getDeployer(deployer, deployment_dir, compose_files=cluster_context.compose_files,
compose_project_name=cluster_context.cluster, compose_project_name=cluster_context.cluster,

View File

@ -21,26 +21,26 @@ from stack_orchestrator.deploy.spec import Spec
class DeploymentContext: class DeploymentContext:
dir: Path deployment_dir: Path
spec: Spec spec: Spec
stack: Stack stack: Stack
def get_stack_file(self): def get_stack_file(self):
return self.dir.joinpath("stack.yml") return self.deployment_dir.joinpath("stack.yml")
def get_spec_file(self): def get_spec_file(self):
return self.dir.joinpath("spec.yml") return self.deployment_dir.joinpath("spec.yml")
def get_env_file(self): def get_env_file(self):
return self.dir.joinpath("config.env") return self.deployment_dir.joinpath("config.env")
# TODO: implement me # TODO: implement me
def get_cluster_name(self): def get_cluster_name(self):
return None return None
def init(self, dir): def init(self, dir):
self.dir = dir self.deployment_dir = dir
self.stack = Stack()
self.stack.init_from_file(self.get_stack_file())
self.spec = Spec() self.spec = Spec()
self.spec.init_from_file(self.get_spec_file()) self.spec.init_from_file(self.get_spec_file())
self.stack = Stack(self.spec.obj["stack"])
self.stack.init_from_file(self.get_stack_file())

View File

@ -24,7 +24,7 @@ import sys
from stack_orchestrator.util import (get_stack_file_path, get_parsed_deployment_spec, get_parsed_stack_config, from stack_orchestrator.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, pod_has_scripts, global_options, get_yaml, get_pod_list, get_pod_file_path, pod_has_scripts,
get_pod_script_paths, get_plugin_code_paths) get_pod_script_paths, get_plugin_code_paths)
from stack_orchestrator.deploy.deploy_types import DeployCommandContext, LaconicStackSetupCommand from stack_orchestrator.deploy.deploy_types import LaconicStackSetupCommand
from stack_orchestrator.deploy.deployer_factory import getDeployerConfigGenerator from stack_orchestrator.deploy.deployer_factory import getDeployerConfigGenerator
from stack_orchestrator.deploy.deployment_context import DeploymentContext from stack_orchestrator.deploy.deployment_context import DeploymentContext
@ -109,8 +109,8 @@ def _fixup_pod_file(pod, spec, compose_dir):
pod["services"][container_name]["ports"] = container_ports pod["services"][container_name]["ports"] = container_ports
def _commands_plugin_paths(ctx: DeployCommandContext): def _commands_plugin_paths(stack_name: str):
plugin_paths = get_plugin_code_paths(ctx.stack) plugin_paths = get_plugin_code_paths(stack_name)
ret = [p.joinpath("deploy", "commands.py") for p in plugin_paths] ret = [p.joinpath("deploy", "commands.py") for p in plugin_paths]
return ret return ret
@ -124,7 +124,7 @@ def call_stack_deploy_init(deploy_command_context):
# Link with the python file in the stack # Link with the python file in the stack
# Call a function in it # Call a function in it
# If no function found, return None # If no function found, return None
python_file_paths = _commands_plugin_paths(deploy_command_context) python_file_paths = _commands_plugin_paths(deploy_command_context.stack)
ret = None ret = None
init_done = False init_done = False
@ -148,7 +148,7 @@ def call_stack_deploy_setup(deploy_command_context, parameters: LaconicStackSetu
# Link with the python file in the stack # Link with the python file in the stack
# Call a function in it # Call a function in it
# If no function found, return None # If no function found, return None
python_file_paths = _commands_plugin_paths(deploy_command_context) python_file_paths = _commands_plugin_paths(deploy_command_context.stack)
for python_file_path in python_file_paths: for python_file_path in python_file_paths:
if python_file_path.exists(): if python_file_path.exists():
spec = util.spec_from_file_location("commands", python_file_path) spec = util.spec_from_file_location("commands", python_file_path)
@ -163,7 +163,7 @@ def call_stack_deploy_create(deployment_context, extra_args):
# Link with the python file in the stack # Link with the python file in the stack
# Call a function in it # Call a function in it
# If no function found, return None # If no function found, return None
python_file_paths = _commands_plugin_paths(deployment_context.command_context) python_file_paths = _commands_plugin_paths(deployment_context.stack.name)
for python_file_path in python_file_paths: for python_file_path in python_file_paths:
if python_file_path.exists(): if python_file_path.exists():
spec = util.spec_from_file_location("commands", python_file_path) spec = util.spec_from_file_location("commands", python_file_path)
@ -312,7 +312,7 @@ def _copy_files_to_directory(file_paths: List[Path], directory: Path):
def create(ctx, spec_file, deployment_dir, network_dir, initial_peers): def create(ctx, spec_file, deployment_dir, network_dir, initial_peers):
# This function fails with a useful error message if the file doens't exist # This function fails with a useful error message if the file doens't exist
parsed_spec = get_parsed_deployment_spec(spec_file) parsed_spec = get_parsed_deployment_spec(spec_file)
stack_name = parsed_spec['stack'] stack_name = parsed_spec["stack"]
stack_file = get_stack_file_path(stack_name) stack_file = get_stack_file_path(stack_name)
parsed_stack = get_parsed_stack_config(stack_name) parsed_stack = get_parsed_stack_config(stack_name)
if global_options(ctx).debug: if global_options(ctx).debug:
@ -368,7 +368,8 @@ def create(ctx, spec_file, deployment_dir, network_dir, initial_peers):
# stack member here. # stack member here.
deployment_command_context = ctx.obj deployment_command_context = ctx.obj
deployment_command_context.stack = stack_name deployment_command_context.stack = stack_name
deployment_context = DeploymentContext(Path(deployment_dir), deployment_command_context) deployment_context = DeploymentContext()
deployment_context.init(Path(deployment_dir))
# Call the deployer to generate any deployer-specific files (e.g. for kind) # Call the deployer to generate any deployer-specific files (e.g. for kind)
deployer_config_generator = getDeployerConfigGenerator(parsed_spec["deploy-to"]) deployer_config_generator = getDeployerConfigGenerator(parsed_spec["deploy-to"])
# TODO: make deployment_dir a Path above # TODO: make deployment_dir a Path above

View File

@ -20,10 +20,11 @@ from stack_orchestrator.util import get_yaml
class Stack: class Stack:
name: str
obj: typing.Any obj: typing.Any
def __init__(self) -> None: def __init__(self, name: str) -> None:
pass self.name = name
def init_from_file(self, file_path: Path): def init_from_file(self, file_path: Path):
with file_path: with file_path: