mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-12-24 02:12:59 +00:00
utils.executable based on PATH instead of YML
This commit is contained in:
parent
daced4dd7d
commit
8c2cdbda3a
5 changed files with 62 additions and 85 deletions
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
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
|
||||
|
|
|
@ -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
|
||||
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)
|
||||
|
|
|
@ -8,7 +8,3 @@ markers:
|
|||
network:
|
||||
name: kiwihub
|
||||
cidr: 10.22.46.0/24
|
||||
executables:
|
||||
docker: null
|
||||
docker-compose: null
|
||||
sudo: null
|
||||
|
|
Loading…
Reference in a new issue