Passes tests

This commit is contained in:
David Boreham 2023-10-20 14:20:54 -06:00
parent e1cdbe76aa
commit f60717fbc3
5 changed files with 84 additions and 19 deletions

View File

@ -26,6 +26,7 @@ import click
from pathlib import Path from pathlib import Path
from app.util import include_exclude_check, get_parsed_stack_config, global_options2, get_dev_root_path from app.util import include_exclude_check, get_parsed_stack_config, global_options2, get_dev_root_path
from app.deployer import Deployer, DeployerException from app.deployer import Deployer, DeployerException
from app.deployer_factory import getDeployer
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
@ -57,8 +58,8 @@ def command(ctx, include, exclude, env_file, cluster):
def create_deploy_context(global_context, stack, include, exclude, cluster, env_file): def create_deploy_context(global_context, stack, include, exclude, cluster, env_file):
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)
# See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/ # See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/
deployer = Deployer(compose_files=cluster_context.compose_files, compose_project_name=cluster_context.cluster, deployer = getDeployer(compose_files=cluster_context.compose_files, compose_project_name=cluster_context.cluster,
compose_env_file=cluster_context.env_file) compose_env_file=cluster_context.env_file)
return DeployCommandContext(stack, cluster_context, deployer) return DeployCommandContext(stack, cluster_context, deployer)

View File

@ -14,7 +14,7 @@
# 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 python_on_whales import DockerClient, DockerException from python_on_whales import DockerClient, DockerException
from app.deployer import Deployer from app.deployer import Deployer, DeployerException
class DockerDeployer(Deployer): class DockerDeployer(Deployer):
@ -23,19 +23,43 @@ class DockerDeployer(Deployer):
compose_env_file=compose_env_file) compose_env_file=compose_env_file)
def compose_up(self, detach, services): def compose_up(self, detach, services):
return self.docker.compose.up(detach=detach, services=services) try:
return self.docker.compose.up(detach=detach, services=services)
except DockerException as e:
raise DeployerException(e)
def compose_down(self, timeout, volumes): def compose_down(self, timeout, volumes):
return self.docker.compose.down(timeout=timeout, volumes=volumes) try:
return self.docker.compose.down(timeout=timeout, volumes=volumes)
except DockerException as e:
raise DeployerException(e)
def compose_ps(self): def compose_ps(self):
return self.docker.compose.ps() try:
return self.docker.compose.ps()
except DockerException as e:
raise DeployerException(e)
def compose_port(self, service, private_port): def compose_port(self, service, private_port):
return self.docker.compose.port(service=service, private_port=private_port) try:
return self.docker.compose.port(service=service, private_port=private_port)
except DockerException as e:
raise DeployerException(e)
def compose_execute(self, service_name, command, envs): def compose_execute(self, service_name, command, envs):
return self.docker.compose.execute(service_name=service_name, command=command, envs=envs) try:
return self.docker.compose.execute(service_name=service_name, command=command, envs=envs)
except DockerException as e:
raise DeployerException(e)
def compose_logs(self, services, tail, follow, stream): def compose_logs(self, services, tail, follow, stream):
return self.docker.compose.logs(services=services, tail=tail, follow=follow, stream=stream) try:
return self.docker.compose.logs(services=services, tail=tail, follow=follow, stream=stream)
except DockerException as e:
raise DeployerException(e)
def run(self, image, command, user, volumes, entrypoint=None):
try:
return self.docker.run(image=image, command=command, user=user, volumes=volumes, entrypoint=entrypoint)
except DockerException as e:
raise DeployerException(e)

View File

@ -47,12 +47,12 @@ def _volumes_to_docker(mounts: List[VolumeMapping]):
def run_container_command(ctx: DeployCommandContext, service: str, command: str, mounts: List[VolumeMapping]): def run_container_command(ctx: DeployCommandContext, service: str, command: str, mounts: List[VolumeMapping]):
docker = ctx.docker deployer = ctx.deployer
container_image = _container_image_from_service(ctx.stack, service) container_image = _container_image_from_service(ctx.stack, service)
docker_volumes = _volumes_to_docker(mounts) docker_volumes = _volumes_to_docker(mounts)
if ctx.cluster_context.options.debug: if ctx.cluster_context.options.debug:
print(f"Running this command in {service} container: {command}") print(f"Running this command in {service} container: {command}")
docker_output = docker.run( docker_output = deployer.run(
container_image, container_image,
["-c", command], entrypoint="sh", ["-c", command], entrypoint="sh",
user=f"{os.getuid()}:{os.getgid()}", user=f"{os.getuid()}:{os.getgid()}",

View File

@ -14,20 +14,39 @@
# 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 abc import ABC, abstractmethod from abc import ABC, abstractmethod
from app.deploy_k8s import K8sDeployer
from app.deploy_docker import DockerDeployer
class Deployer(ABC): class Deployer(ABC):
@abstractmethod @abstractmethod
def method(self, purchase): def compose_up(self, detach, services):
pass
@abstractmethod
def compose_down(self, timeout, volumes):
pass
@abstractmethod
def compose_ps(self):
pass
@abstractmethod
def compose_port(self, service, private_port):
pass
@abstractmethod
def compose_execute(self, service_name, command, envs):
pass
@abstractmethod
def compose_logs(self, services, tail, follow, stream):
pass
@abstractmethod
def run(self, image, command, user, volumes, entrypoint):
pass pass
def getDeployer(compose_files, compose_project_name, compose_env_file):
return DockerDeployer(compose_files, compose_project_name, compose_env_file)
class DeployerException(Exception): class DeployerException(Exception):
pass def __init__(self, *args: object) -> None:
super().__init__(*args)

21
app/deployer_factory.py Normal file
View File

@ -0,0 +1,21 @@
# Copyright © 2023 Vulcanize
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# 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/>.
from app.deploy_k8s import K8sDeployer
from app.deploy_docker import DockerDeployer
def getDeployer(compose_files, compose_project_name, compose_env_file):
return DockerDeployer(compose_files, compose_project_name, compose_env_file)