k8s refactor #595

Merged
telackey merged 9 commits from dboreham/k8s-refactor into main 2023-10-24 20:44:49 +00:00
5 changed files with 84 additions and 19 deletions
Showing only changes of commit f60717fbc3 - Show all commits

View File

@ -26,6 +26,7 @@ import click
from pathlib import 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_factory import getDeployer
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
@ -57,8 +58,8 @@ def command(ctx, include, exclude, env_file, cluster):
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)
# 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,
compose_env_file=cluster_context.env_file)
deployer = getDeployer(compose_files=cluster_context.compose_files, compose_project_name=cluster_context.cluster,
compose_env_file=cluster_context.env_file)
return DeployCommandContext(stack, cluster_context, deployer)

View File

@ -14,7 +14,7 @@
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
from python_on_whales import DockerClient, DockerException
from app.deployer import Deployer
from app.deployer import Deployer, DeployerException
class DockerDeployer(Deployer):
@ -23,19 +23,43 @@ class DockerDeployer(Deployer):
compose_env_file=compose_env_file)
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):
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):
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):
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):
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):
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]):
docker = ctx.docker
deployer = ctx.deployer
container_image = _container_image_from_service(ctx.stack, service)
docker_volumes = _volumes_to_docker(mounts)
if ctx.cluster_context.options.debug:
print(f"Running this command in {service} container: {command}")
docker_output = docker.run(
docker_output = deployer.run(
container_image,
["-c", command], entrypoint="sh",
user=f"{os.getuid()}:{os.getgid()}",

View File

@ -14,20 +14,39 @@
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
from abc import ABC, abstractmethod
from app.deploy_k8s import K8sDeployer
from app.deploy_docker import DockerDeployer
class Deployer(ABC):
@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
def getDeployer(compose_files, compose_project_name, compose_env_file):
return DockerDeployer(compose_files, compose_project_name, compose_env_file)
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)