diff --git a/src/kiwi/subcommands/logs.py b/src/kiwi/subcommands/logs.py index abcf354..c38a635 100644 --- a/src/kiwi/subcommands/logs.py +++ b/src/kiwi/subcommands/logs.py @@ -20,33 +20,19 @@ class LogsCommand(ServiceCommand): ) def run(self, config, args): - project_name = args.project - project_marker = config['markers:project'] - project_dir = f'{project_name}{project_marker}' - - environment = { - 'KIWI_HUB_NAME': config['network:name'], - 'COMPOSE_PROJECT_NAME': project_name - } - - process_args = ['logs', '-t'] + compose_args = ['logs', '-t'] if args.follow: - process_args = [*process_args, '-f', '--tail=10'] + compose_args = [*compose_args, '-f', '--tail=10'] if args.services: - process_args = [*process_args, *args.services] + compose_args = [*compose_args, *args.services] try: if args.follow: - DockerCommand('docker-compose').run( - process_args, - cwd=project_dir, env=environment - ) + DockerCommand('docker-compose').run(config, args, compose_args) else: - DockerCommand('docker-compose').run_less( - process_args, - cwd=project_dir, env=environment - ) + DockerCommand('docker-compose').run_less(config, args, compose_args) + except KeyboardInterrupt: logging.debug("Subprocess aborted.") print() diff --git a/src/kiwi/subcommands/utils/dockercommand.py b/src/kiwi/subcommands/utils/dockercommand.py index 5d5a28d..00a2f46 100644 --- a/src/kiwi/subcommands/utils/dockercommand.py +++ b/src/kiwi/subcommands/utils/dockercommand.py @@ -1,10 +1,35 @@ # system +import logging +import os import subprocess # local from .executable import Executable +def _update_kwargs(config, args, **kwargs): + project_name = args.project + project_marker = config['markers:project'] + project_dir = f'{project_name}{project_marker}' + kwargs['cwd'] = project_dir + + if 'env' not in kwargs: + kwargs['env'] = {} + + if config['runtime:env'] is not None: + kwargs['env'].update(config['runtime:env']) + + kwargs['env'].update({ + 'KIWI_HUB_NAME': config['network:name'], + 'COMPOSE_PROJECT_NAME': project_name, + 'CONFDIR': os.path.join(config['runtime:storage'], 'conf'), + 'TARGETDIR': os.path.join(config['runtime:storage'], project_dir) + }) + + logging.debug(f"kwargs updated: {kwargs}") + return kwargs + + class DockerCommand(Executable): __requires_root = None @@ -21,24 +46,19 @@ class DockerCommand(Executable): except subprocess.CalledProcessError: DockerCommand.__requires_root = True - def run(self, args, **kwargs): + def run(self, config, args, process_args, **kwargs): + kwargs = _update_kwargs(config, args, **kwargs) + # equivalent to 'super().run' but agnostic of nested class construct super().__getattr__("run")( - args, DockerCommand.__requires_root, + process_args, DockerCommand.__requires_root, **kwargs ) - def run_less(self, args, **kwargs): - process = self.Popen( - args, DockerCommand.__requires_root, - stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, + def run_less(self, config, args, process_args, **kwargs): + kwargs = _update_kwargs(config, args, **kwargs) + + super().__getattr__("run_less")( + process_args, DockerCommand.__requires_root, **kwargs ) - - less_process = Executable('less').run( - ['-R', '+G'], - stdin=process.stdout - ) - - process.communicate() - return less_process diff --git a/src/kiwi/subcommands/utils/executable.py b/src/kiwi/subcommands/utils/executable.py index 2c1c56f..7ec5f65 100644 --- a/src/kiwi/subcommands/utils/executable.py +++ b/src/kiwi/subcommands/utils/executable.py @@ -36,18 +36,35 @@ class Executable: logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}") return cmd - def run(self, args, requires_root=False, **kwargs): + def run(self, process_args, requires_root=False, **kwargs): return subprocess.run( - self.__build_cmd(args, requires_root, **kwargs), + self.__build_cmd(process_args, requires_root, **kwargs), **kwargs ) - def Popen(self, args, requires_root=False, **kwargs): + def Popen(self, process_args, requires_root=False, **kwargs): return subprocess.Popen( - self.__build_cmd(args, requires_root, **kwargs), + self.__build_cmd(process_args, requires_root, **kwargs), **kwargs ) + def run_less(self, process_args, requires_root=False, **kwargs): + kwargs['stdout'] = subprocess.PIPE + kwargs['stderr'] = subprocess.DEVNULL + + process = self.Popen( + process_args, requires_root, + **kwargs + ) + + less_process = Executable('less').run( + ['-R', '+G'], + stdin=process.stdout + ) + + process.communicate() + return less_process + __exe_name = None __instances = {}