From 8c2cdbda3a4533d158c9dfa331678af232aa1d3f Mon Sep 17 00:00:00 2001 From: ldericher Date: Wed, 12 Aug 2020 17:18:20 +0200 Subject: [PATCH] utils.executable based on PATH instead of YML --- example/kiwi.yml | 5 -- src/kiwi/subcommands/init.py | 21 ------- src/kiwi/subcommands/utils/dockercommand.py | 69 ++++++--------------- src/kiwi/subcommands/utils/executable.py | 48 ++++++++++++-- src/kiwi_default.yml | 4 -- 5 files changed, 62 insertions(+), 85 deletions(-) diff --git a/example/kiwi.yml b/example/kiwi.yml index e83688b..139c3c4 100644 --- a/example/kiwi.yml +++ b/example/kiwi.yml @@ -15,8 +15,3 @@ markers: network: name: kiwihub cidr: 10.22.46.0/24 - -executables: - docker: /usr/bin/docker - docker-compose: /usr/local/bin/docker-compose - sudo: /usr/bin/sudo diff --git a/src/kiwi/subcommands/init.py b/src/kiwi/subcommands/init.py index c2e5469..12cb9f8 100644 --- a/src/kiwi/subcommands/init.py +++ b/src/kiwi/subcommands/init.py @@ -5,7 +5,6 @@ from .._constants import KIWI_CONF_NAME from ..config import DefaultConfig from ._subcommand import SubCommand -from .utils.executable import get_exe_key, is_executable, find_exe_file def user_input(config, key, prompt): @@ -21,21 +20,6 @@ def user_input(config, key, prompt): config[key] = result -def user_input_exe(config, exe_name): - key = get_exe_key(exe_name) - exe_file = config[key] - - if not is_executable(exe_file): - logging.info(f"Reconfiguring '{exe_name}' executable path.") - exe_file = find_exe_file(exe_name) - - if exe_file is not None: - logging.debug(f"Found executable at '{exe_file}'.") - config[key] = exe_file - else: - user_input(config, key, f"Enter path to '{exe_name}' executable") - - class InitCommand(SubCommand): def __init__(self): super().__init__( @@ -71,11 +55,6 @@ class InitCommand(SubCommand): user_input(config, 'network:name', "Enter name for local docker network") user_input(config, 'network:cidr', "Enter CIDR block for local docker network") - # executables - user_input_exe(config, 'docker') - user_input_exe(config, 'docker-compose') - user_input_exe(config, 'sudo') - config.save() except KeyboardInterrupt: diff --git a/src/kiwi/subcommands/utils/dockercommand.py b/src/kiwi/subcommands/utils/dockercommand.py index 725ac5d..c063930 100644 --- a/src/kiwi/subcommands/utils/dockercommand.py +++ b/src/kiwi/subcommands/utils/dockercommand.py @@ -1,67 +1,36 @@ -import logging import subprocess -from ...config import LoadedConfig -from .executable import get_exe_key +from .executable import Executable class DockerCommand: - class __DockerCommand: - __cmd = [] - - def __init__(self, exe_name): - config = LoadedConfig.get() - self.__cmd = [config[get_exe_key(exe_name)]] - - if DockerCommand.__requires_root: - self.__cmd = [config[get_exe_key("sudo")], *self.__cmd] - - def __build_cmd(self, args, **kwargs): - cmd = [*self.__cmd, *args] - logging.debug(f"DockerProgram cmd{cmd}, kwargs{kwargs}") - return cmd - - def run(self, args, **kwargs): - return subprocess.run( - self.__build_cmd(args, **kwargs), - **kwargs - ) - - def run_less(self, args, **kwargs): - process = subprocess.Popen( - self.__build_cmd(args, **kwargs), - 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 + __exe = None def __init__(self, exe_name): 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 + Executable('docker').run( + ['ps'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) DockerCommand.__requires_root = False except subprocess.CalledProcessError: DockerCommand.__requires_root = True - self.__exe_name = exe_name - - if exe_name not in DockerCommand.__instances: - DockerCommand.__instances[exe_name] = DockerCommand.__DockerCommand(exe_name) + self.__exe = Executable(exe_name, DockerCommand.__requires_root) def __getattr__(self, item): - return getattr(self.__instances[self.__exe_name], item) \ No newline at end of file + return getattr(self.__exe, item) + + def run_less(self, args, **kwargs): + process = self.Popen( + args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, + **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 58e2d1e..8d793d9 100644 --- a/src/kiwi/subcommands/utils/executable.py +++ b/src/kiwi/subcommands/utils/executable.py @@ -1,8 +1,6 @@ import os - - -def get_exe_key(exe_name): - return f'executables:{exe_name}' +import logging +import subprocess def is_executable(filename): @@ -18,4 +16,44 @@ def find_exe_file(exe_name): if is_executable(exe_file): return exe_file - return None \ No newline at end of file + raise FileNotFoundError(f"Executable '{exe_name}' not found in $PATH!") + + +class Executable: + __exe_name = None + __instances = {} + + class __Executable: + __cmd = None + + def __init__(self, exe_name, requires_root=False): + self.__cmd = [find_exe_file(exe_name)] + + if requires_root: + self.__cmd = [find_exe_file("sudo"), *self.__cmd] + + def __build_cmd(self, args, **kwargs): + cmd = [*self.__cmd, *args] + logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}") + return cmd + + def run(self, args, **kwargs): + return subprocess.run( + self.__build_cmd(args, **kwargs), + **kwargs + ) + + def Popen(self, args, **kwargs): + return subprocess.Popen( + self.__build_cmd(args, **kwargs), + **kwargs + ) + + def __init__(self, exe_name, requires_root=False): + self.__exe_name = exe_name + + if exe_name not in Executable.__instances: + Executable.__instances[exe_name] = Executable.__Executable(exe_name, requires_root) + + def __getattr__(self, item): + return getattr(self.__instances[self.__exe_name], item) diff --git a/src/kiwi_default.yml b/src/kiwi_default.yml index 9bfce99..0174e5f 100644 --- a/src/kiwi_default.yml +++ b/src/kiwi_default.yml @@ -8,7 +8,3 @@ markers: network: name: kiwihub cidr: 10.22.46.0/24 -executables: - docker: null - docker-compose: null - sudo: null