From 6ee528f4cb55227b33764fe27d0dead71bb408c8 Mon Sep 17 00:00:00 2001 From: ldericher Date: Mon, 10 Aug 2020 17:40:56 +0200 Subject: [PATCH] DockerCommand: __build_cmd, run_less --- src/kiwi/subcommands/_utils.py | 44 +++++++++++++++++++++++++--------- src/kiwi/subcommands/logs.py | 41 ++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 19 deletions(-) diff --git a/src/kiwi/subcommands/_utils.py b/src/kiwi/subcommands/_utils.py index 42d1e11..7b615d5 100644 --- a/src/kiwi/subcommands/_utils.py +++ b/src/kiwi/subcommands/_utils.py @@ -1,3 +1,4 @@ +import logging import os import subprocess @@ -43,42 +44,63 @@ class SubCommand: pass -class DockerProgram: - class __DockerProgram: +class DockerCommand: + class __DockerCommand: __cmd = [] def __init__(self, exe_name): config = LoadedConfig.get() self.__cmd = [config[get_exe_key(exe_name)]] - if DockerProgram.__requires_root: + if DockerCommand.__requires_root: self.__cmd = [config[get_exe_key("sudo")], *self.__cmd] - def run(self, args, **kwargs): + def __build_cmd(self, args): cmd = [*self.__cmd, *args] - print(cmd) - return subprocess.run(cmd, **kwargs) + logging.debug(f"DockerProgram: {cmd}") + return cmd + + def run(self, args, **kwargs): + return subprocess.run( + self.__build_cmd(args), + **kwargs + ) + + def run_less(self, args, **kwargs): + process = subprocess.Popen( + self.__build_cmd(args), + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, + **kwargs + ) + + less_process = subprocess.run( + ['less', '-R', '+G'], + stdin=process.stdout + ) + + process.communicate() + return less_process __exe_name = None __instances = {} __requires_root = None def __init__(self, exe_name): - if DockerProgram.__requires_root is None: + if DockerCommand.__requires_root is None: try: config = LoadedConfig.get() subprocess.run( [config[get_exe_key('docker')], 'ps'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) - DockerProgram.__requires_root = False + DockerCommand.__requires_root = False except subprocess.CalledProcessError: - DockerProgram.__requires_root = True + DockerCommand.__requires_root = True self.__exe_name = exe_name - if exe_name not in DockerProgram.__instances: - DockerProgram.__instances[exe_name] = DockerProgram.__DockerProgram(exe_name) + if exe_name not in DockerCommand.__instances: + DockerCommand.__instances[exe_name] = DockerCommand.__DockerCommand(exe_name) def __getattr__(self, item): return getattr(self.__instances[self.__exe_name], item) diff --git a/src/kiwi/subcommands/logs.py b/src/kiwi/subcommands/logs.py index 5ee7935..e7fb766 100644 --- a/src/kiwi/subcommands/logs.py +++ b/src/kiwi/subcommands/logs.py @@ -1,6 +1,7 @@ +from ..config import LoadedConfig from ..core import Parser -from ._utils import SubCommand, DockerProgram +from ._utils import SubCommand, DockerCommand class LogsCommand(SubCommand): @@ -11,18 +12,42 @@ class LogsCommand(SubCommand): ) self.get_parser().add_argument( - '-f', '--follow', - action='store_true', + '-f', '--follow', action='store_true', help="output appended data as log grows" ) + self.get_parser().add_argument( + 'project', + help="narf" + ) + + self.get_parser().add_argument( + 'service', nargs='?', + help="narf" + ) + def run(self): + config = LoadedConfig.get() + + project_name = Parser().get_args().project + project_marker = config['markers:project'] + project_dir = f'{project_name}{project_marker}' + + environment = { + 'DOCKERNET': config['network:name'], + 'COMPOSE_PROJECT_NAME': project_name + } + args = ['logs', '-t'] if Parser().get_args().follow: args = [*args, '-f', '--tail=10'] - DockerProgram('docker-compose').run( - args, - cwd='hello-world.project', - env={'COMPOSE_PROJECT_NAME': 'hello-world'} - ) + DockerCommand('docker-compose').run( + args, + cwd=project_dir, env=environment + ) + else: + DockerCommand('docker-compose').run_less( + args, + cwd=project_dir, env=environment + )