Tolerate missing deployment plugin source code files

This commit is contained in:
David Boreham 2023-08-17 13:38:16 -06:00
parent 97dc45549d
commit 51ab056c69

View File

@ -14,7 +14,6 @@
# along with this program. If not, see <http:#www.gnu.org/licenses/>. # along with this program. If not, see <http:#www.gnu.org/licenses/>.
import click import click
from dataclasses import dataclass
from importlib import util from importlib import util
import os import os
from pathlib import Path from pathlib import Path
@ -27,6 +26,7 @@ from app.deploy_types import DeploymentContext, DeployCommandContext
def _make_default_deployment_dir(): def _make_default_deployment_dir():
return "deployment-001" return "deployment-001"
def _get_ports(stack): def _get_ports(stack):
ports = {} ports = {}
parsed_stack = get_parsed_stack_config(stack) parsed_stack = get_parsed_stack_config(stack)
@ -42,6 +42,7 @@ def _get_ports(stack):
ports[svc_name] = [ str(x) for x in svc["ports"] ] ports[svc_name] = [ str(x) for x in svc["ports"] ]
return ports return ports
def _get_named_volumes(stack): 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 = []
@ -76,30 +77,30 @@ def _create_bind_dir_if_relative(volume, path_string, compose_dir):
# See: https://stackoverflow.com/questions/45699189/editing-docker-compose-yml-with-pyyaml # See: https://stackoverflow.com/questions/45699189/editing-docker-compose-yml-with-pyyaml
def _fixup_pod_file(pod, spec, compose_dir): def _fixup_pod_file(pod, spec, compose_dir):
# Fix up volumes # Fix up volumes
if "volumes" in spec: if "volumes" in spec:
spec_volumes = spec["volumes"] spec_volumes = spec["volumes"]
if "volumes" in pod: if "volumes" in pod:
pod_volumes = pod["volumes"] pod_volumes = pod["volumes"]
for volume in pod_volumes.keys(): for volume in pod_volumes.keys():
if volume in spec_volumes: if volume in spec_volumes:
volume_spec = spec_volumes[volume] volume_spec = spec_volumes[volume]
volume_spec_fixedup = volume_spec if Path(volume_spec).is_absolute() else f".{volume_spec}" volume_spec_fixedup = volume_spec if Path(volume_spec).is_absolute() else f".{volume_spec}"
_create_bind_dir_if_relative(volume, volume_spec, compose_dir) _create_bind_dir_if_relative(volume, volume_spec, compose_dir)
new_volume_spec = {"driver": "local", new_volume_spec = {"driver": "local",
"driver_opts": { "driver_opts": {
"type": "none", "type": "none",
"device": volume_spec_fixedup, "device": volume_spec_fixedup,
"o": "bind" "o": "bind"
} }
} }
pod["volumes"][volume] = new_volume_spec pod["volumes"][volume] = new_volume_spec
# Fix up ports # Fix up ports
if "ports" in spec: if "ports" in spec:
spec_ports = spec["ports"] spec_ports = spec["ports"]
for container_name, container_ports in spec_ports.items(): for container_name, container_ports in spec_ports.items():
if container_name in pod["services"]: if container_name in pod["services"]:
pod["services"][container_name]["ports"] = container_ports pod["services"][container_name]["ports"] = container_ports
def call_stack_deploy_init(deploy_command_context): def call_stack_deploy_init(deploy_command_context):
@ -107,10 +108,13 @@ def call_stack_deploy_init(deploy_command_context):
# Call a function in it # Call a function in it
# If no function found, return None # If no function found, return None
python_file_path = get_stack_file_path(deploy_command_context.stack).parent.joinpath("deploy", "commands.py") python_file_path = get_stack_file_path(deploy_command_context.stack).parent.joinpath("deploy", "commands.py")
spec = util.spec_from_file_location("commands", python_file_path) if python_file_path.exists():
imported_stack = util.module_from_spec(spec) spec = util.spec_from_file_location("commands", python_file_path)
spec.loader.exec_module(imported_stack) imported_stack = util.module_from_spec(spec)
return imported_stack.init(deploy_command_context) spec.loader.exec_module(imported_stack)
return imported_stack.init(deploy_command_context)
else:
return None
# TODO: fold this with function above # TODO: fold this with function above
@ -119,10 +123,13 @@ def call_stack_deploy_setup(deploy_command_context, extra_args):
# Call a function in it # Call a function in it
# If no function found, return None # If no function found, return None
python_file_path = get_stack_file_path(deploy_command_context.stack).parent.joinpath("deploy", "commands.py") python_file_path = get_stack_file_path(deploy_command_context.stack).parent.joinpath("deploy", "commands.py")
spec = util.spec_from_file_location("commands", python_file_path) if python_file_path.exists():
imported_stack = util.module_from_spec(spec) spec = util.spec_from_file_location("commands", python_file_path)
spec.loader.exec_module(imported_stack) imported_stack = util.module_from_spec(spec)
return imported_stack.setup(deploy_command_context, extra_args) spec.loader.exec_module(imported_stack)
return imported_stack.setup(deploy_command_context, extra_args)
else:
return None
# TODO: fold this with function above # TODO: fold this with function above
@ -131,10 +138,13 @@ def call_stack_deploy_create(deployment_context):
# Call a function in it # Call a function in it
# If no function found, return None # If no function found, return None
python_file_path = get_stack_file_path(deployment_context.command_context.stack).parent.joinpath("deploy", "commands.py") python_file_path = get_stack_file_path(deployment_context.command_context.stack).parent.joinpath("deploy", "commands.py")
spec = util.spec_from_file_location("commands", python_file_path) if python_file_path.exists():
imported_stack = util.module_from_spec(spec) spec = util.spec_from_file_location("commands", python_file_path)
spec.loader.exec_module(imported_stack) imported_stack = util.module_from_spec(spec)
return imported_stack.create(deployment_context) spec.loader.exec_module(imported_stack)
return imported_stack.create(deployment_context)
else:
return None
# Inspect the pod yaml to find config files referenced in subdirectories # Inspect the pod yaml to find config files referenced in subdirectories