From 7ee7d5f3007dd7c9ecc658115ce601535bbe1d6e Mon Sep 17 00:00:00 2001 From: ldericher <40151420+ldericher@users.noreply.github.com> Date: Sat, 13 Nov 2021 03:27:27 +0100 Subject: [PATCH] "kiwi cmd" --- kiwi_scp/commands/cmd_cmd.py | 32 +++++++++++++++++++++ kiwi_scp/instance.py | 54 ++++++++++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 kiwi_scp/commands/cmd_cmd.py diff --git a/kiwi_scp/commands/cmd_cmd.py b/kiwi_scp/commands/cmd_cmd.py new file mode 100644 index 0000000..4d13a67 --- /dev/null +++ b/kiwi_scp/commands/cmd_cmd.py @@ -0,0 +1,32 @@ +from typing import Tuple + +import click + +from .cli import KiwiCommand, KiwiCommandType +from .decorators import kiwi_command +from ..executable import COMPOSE_EXE +from ..instance import Instance, Project + + +@click.argument( + "compose_args", + metavar="[ARG]...", + nargs=-1, +) +@click.argument( + "compose_cmd", + metavar="CMD", +) +@kiwi_command( + "cmd", + KiwiCommandType.PROJECT, + short_help="Run docker-compose command", +) +class CMD(KiwiCommand): + """Run raw docker-compose command in a project""" + + @classmethod + def run_for_existing_project(cls, instance: Instance, project: Project, compose_cmd: str = None, + compose_args: Tuple[str] = None) -> None: + if project.project_config.enabled: + COMPOSE_EXE.run([compose_cmd, *compose_args], **project.process_kwargs) diff --git a/kiwi_scp/instance.py b/kiwi_scp/instance.py index a3b2189..b94a4fb 100644 --- a/kiwi_scp/instance.py +++ b/kiwi_scp/instance.py @@ -1,13 +1,13 @@ import functools import re from pathlib import Path -from typing import Generator, List, Tuple, Optional +from typing import Generator, List, Optional, Dict, Any import attr from ruamel.yaml.comments import CommentedMap -from ._constants import COMPOSE_FILE_NAME -from .config import KiwiConfig +from ._constants import COMPOSE_FILE_NAME, CONF_DIRECTORY_NAME +from .config import KiwiConfig, ProjectConfig from .misc import YAML _RE_CONFDIR = re.compile(r"^\s*\$(?:CONFDIR|{CONFDIR})/+(.*)$", flags=re.UNICODE) @@ -57,6 +57,7 @@ class Services: @attr.s class Project: directory: Path = attr.ib() + config: KiwiConfig = attr.ib() @staticmethod @functools.lru_cache(maxsize=10) @@ -68,6 +69,34 @@ class Project: def name(self) -> str: return self.directory.name + @property + def project_config(self) -> ProjectConfig: + return self.config.get_project_config(self.name) + + @property + def process_kwargs(self) -> Dict[str, Any]: + directory: Path = self.directory + project_name: str = self.name + kiwi_hub_name: str = self.config.network.name + target_root_dir: Path = self.config.storage.directory + conf_dir: Path = target_root_dir.joinpath(CONF_DIRECTORY_NAME) + target_dir: Path = target_root_dir.joinpath(project_name) + + result: Dict[str, Any] = { + "cwd": str(directory), + "env": { + "COMPOSE_PROJECT_NAME": project_name, + "KIWI_HUB_NAME": kiwi_hub_name, + "TARGETROOT": str(target_root_dir), + "CONFDIR": str(conf_dir), + "TARGETDIR": str(target_dir), + }, + } + + result["env"].update(self.config.environment) + + return result + @property def services(self) -> Services: yml = Project._parse_compose_file(self.directory) @@ -88,7 +117,20 @@ class Instance: return KiwiConfig.from_directory(self.directory) - def get_project(self, project_name: str) -> Optional[Project]: - for project in self.config.projects: + @staticmethod + @functools.lru_cache(maxsize=None) + def __get_project(instance_directory: Path, project_name: str) -> Optional[Project]: + instance = Instance(instance_directory) + config = instance.config + + for project in config.projects: if project.name == project_name: - return Project(self.directory.joinpath(project.name)) + return Project( + directory=instance_directory.joinpath(project.name), + config=config, + ) + + def get_project(self, project_name: str) -> Optional[Project]: + project = Instance.__get_project(self.directory, project_name) + project.instance = self + return project