From 52d54eddad66c27d0a9f7b1f145dc37cb33b72e7 Mon Sep 17 00:00:00 2001 From: ldericher <40151420+ldericher@users.noreply.github.com> Date: Mon, 21 Feb 2022 16:42:12 +0100 Subject: [PATCH] net-up and net-down functionality --- kiwi_scp/commands/cmd_down.py | 3 +- kiwi_scp/commands/cmd_up.py | 6 ++-- kiwi_scp/instance.py | 53 ++++++++++++++++++++++++++++++++ kiwi_scp/service.py | 1 - kiwi_scp/subcommands/_hidden.py | 54 --------------------------------- 5 files changed, 57 insertions(+), 60 deletions(-) delete mode 100644 kiwi_scp/subcommands/_hidden.py diff --git a/kiwi_scp/commands/cmd_down.py b/kiwi_scp/commands/cmd_down.py index 7476013..2236e17 100644 --- a/kiwi_scp/commands/cmd_down.py +++ b/kiwi_scp/commands/cmd_down.py @@ -36,8 +36,7 @@ class DownCommand(KiwiCommand): return super().run_for_instance(instance) - - # TODO net-down + instance.remove_net() @classmethod def run_for_project(cls, instance: Instance, project: Project, **kwargs) -> None: diff --git a/kiwi_scp/commands/cmd_up.py b/kiwi_scp/commands/cmd_up.py index 7c6a777..d1004ce 100644 --- a/kiwi_scp/commands/cmd_up.py +++ b/kiwi_scp/commands/cmd_up.py @@ -20,9 +20,6 @@ class UpCommand(KiwiCommand): @classmethod def run_for_filtered_services(cls, instance: Instance, project: Project, services: Services, new_service_names: List[str], **kwargs) -> None: - # TODO conf-copy - # TODO net-up - if not services: if not click.confirm( "Did not find any of those services. \n" @@ -31,4 +28,7 @@ class UpCommand(KiwiCommand): ): return + instance.create_net() + services.copy_configs() + COMPOSE_EXE.run(["up", "-d", *services.names], **project.process_kwargs) diff --git a/kiwi_scp/instance.py b/kiwi_scp/instance.py index 3ce304b..c40e1f7 100644 --- a/kiwi_scp/instance.py +++ b/kiwi_scp/instance.py @@ -1,3 +1,5 @@ +import logging +import subprocess from pathlib import Path from typing import Generator, Dict, Sequence @@ -5,8 +7,11 @@ import attr from ._constants import KIWI_CONF_NAME, CONFIG_DIRECTORY_NAME from .config import KiwiConfig +from .executable import DOCKER_EXE from .project import Project +_logger = logging.getLogger(__name__) + @attr.s class Instance: @@ -30,6 +35,54 @@ class Instance: def storage_config_directory(self): return self.config.storage.directory.joinpath(CONFIG_DIRECTORY_NAME) + @staticmethod + def __find_net(net_name): + ps = DOCKER_EXE.run([ + "network", "ls", "--filter", f"name={net_name}", "--format", "{{.Name}}" + ], stdout=subprocess.PIPE) + + net_found = str(ps.stdout, 'utf-8').strip() + + return net_found == net_name + + def create_net(self): + net_name = self.config.network.name + net_cidr = str(self.config.network.cidr) + + if self.__find_net(net_name): + _logger.info(f"Network '{net_name}' already exists") + return + + try: + DOCKER_EXE.run([ + "network", "create", + "--driver", "bridge", + "--internal", + "--subnet", net_cidr, + net_name + ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + _logger.info(f"Network '{net_name}' created") + + except subprocess.CalledProcessError: + _logger.error(f"Error creating network '{net_name}'") + + def remove_net(self): + net_name = self.config.network.name + + if not self.__find_net(net_name): + _logger.info(f"Network '{net_name}' does not exist") + return + + try: + DOCKER_EXE.run([ + "network", "rm", + net_name + ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + _logger.info(f"Network '{net_name}' removed") + + except subprocess.CalledProcessError: + _logger.error(f"Error removing network '{net_name}'") + @property def projects(self) -> Generator[Project, None, None]: for project in self.config.projects: diff --git a/kiwi_scp/service.py b/kiwi_scp/service.py index c85b82f..e2caa41 100644 --- a/kiwi_scp/service.py +++ b/kiwi_scp/service.py @@ -11,7 +11,6 @@ from ruamel.yaml import CommentedMap from .executable import COMPOSE_EXE if TYPE_CHECKING: - from .instance import Instance from .project import Project _logger = logging.getLogger(__name__) diff --git a/kiwi_scp/subcommands/_hidden.py b/kiwi_scp/subcommands/_hidden.py deleted file mode 100644 index 4751a9a..0000000 --- a/kiwi_scp/subcommands/_hidden.py +++ /dev/null @@ -1,54 +0,0 @@ -# system -import logging -import subprocess - -# local -from ..config import LoadedConfig -from ..executable import Executable -from ..subcommand import SubCommand - - -def _find_net(net_name): - ps = Executable('docker').run([ - 'network', 'ls', '--filter', f"name={net_name}", '--format', '{{.Name}}' - ], stdout=subprocess.PIPE) - - net_found = str(ps.stdout, 'utf-8').strip() - - return net_found == net_name - - -class NetUpCommand(SubCommand): - """kiwi net-up""" - - def __init__(self): - super().__init__( - 'net-up', - action="Creating the local network hub for", add_parser=False, - description="Create the local network hub for this instance" - ) - - def _run_instance(self, runner, args): - config = LoadedConfig.get() - net_name = config['network:name'] - net_cidr = config['network:cidr'] - - if _find_net(net_name): - logging.info(f"Network '{net_name}' already exists") - return True - - try: - Executable('docker').run([ - 'network', 'create', - '--driver', 'bridge', - '--internal', - '--subnet', net_cidr, - net_name - ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) - logging.info(f"Network '{net_name}' created") - - except subprocess.CalledProcessError: - logging.error(f"Error creating network '{net_name}'") - return False - - return True