subprocess environment consistency
This commit is contained in:
parent
4c70b7b2c1
commit
20f6131cfc
2 changed files with 52 additions and 31 deletions
|
@ -8,6 +8,9 @@ from .executable import Executable
|
||||||
|
|
||||||
|
|
||||||
def _update_kwargs(config, args, **kwargs):
|
def _update_kwargs(config, args, **kwargs):
|
||||||
|
if 'project' in args:
|
||||||
|
# command affects a project in this instance
|
||||||
|
|
||||||
project_name = args.project
|
project_name = args.project
|
||||||
project_marker = config['markers:project']
|
project_marker = config['markers:project']
|
||||||
project_dir = f'{project_name}{project_marker}'
|
project_dir = f'{project_name}{project_marker}'
|
||||||
|
@ -16,17 +19,14 @@ def _update_kwargs(config, args, **kwargs):
|
||||||
if 'env' not in kwargs:
|
if 'env' not in kwargs:
|
||||||
kwargs['env'] = {}
|
kwargs['env'] = {}
|
||||||
|
|
||||||
if config['runtime:env'] is not None:
|
|
||||||
kwargs['env'].update(config['runtime:env'])
|
|
||||||
|
|
||||||
kwargs['env'].update({
|
kwargs['env'].update({
|
||||||
'KIWI_HUB_NAME': config['network:name'],
|
|
||||||
'COMPOSE_PROJECT_NAME': project_name,
|
'COMPOSE_PROJECT_NAME': project_name,
|
||||||
'CONFDIR': os.path.join(config['runtime:storage'], 'conf'),
|
'CONFDIR': os.path.join(config['runtime:storage'], 'conf'),
|
||||||
'TARGETDIR': os.path.join(config['runtime:storage'], project_dir)
|
'TARGETDIR': os.path.join(config['runtime:storage'], project_dir)
|
||||||
})
|
})
|
||||||
|
|
||||||
logging.debug(f"kwargs updated: {kwargs}")
|
logging.debug(f"kwargs updated: {kwargs}")
|
||||||
|
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,15 +50,15 @@ class DockerCommand(Executable):
|
||||||
kwargs = _update_kwargs(config, args, **kwargs)
|
kwargs = _update_kwargs(config, args, **kwargs)
|
||||||
|
|
||||||
# equivalent to 'super().run' but agnostic of nested class construct
|
# equivalent to 'super().run' but agnostic of nested class construct
|
||||||
super().__getattr__("run")(
|
return super().__getattr__("run")(
|
||||||
process_args, DockerCommand.__requires_root,
|
process_args, config, DockerCommand.__requires_root,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def run_less(self, config, args, process_args, **kwargs):
|
def run_less(self, config, args, process_args, **kwargs):
|
||||||
kwargs = _update_kwargs(config, args, **kwargs)
|
kwargs = _update_kwargs(config, args, **kwargs)
|
||||||
|
|
||||||
super().__getattr__("run_less")(
|
return super().__getattr__("run_less")(
|
||||||
process_args, DockerCommand.__requires_root,
|
process_args, config, DockerCommand.__requires_root,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,17 +4,34 @@ import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
def is_executable(filename):
|
def _update_kwargs(config, **kwargs):
|
||||||
|
if config is not None:
|
||||||
|
if config['runtime:env'] is not None:
|
||||||
|
kwargs['env'].update(config['runtime:env'])
|
||||||
|
|
||||||
|
if 'env' not in kwargs:
|
||||||
|
kwargs['env'] = {}
|
||||||
|
|
||||||
|
kwargs['env'].update({
|
||||||
|
'KIWI_HUB_NAME': config['network:name']
|
||||||
|
})
|
||||||
|
|
||||||
|
logging.debug(f"kwargs updated: {kwargs}")
|
||||||
|
|
||||||
|
return kwargs
|
||||||
|
|
||||||
|
|
||||||
|
def _is_executable(filename):
|
||||||
if filename is None:
|
if filename is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return os.path.isfile(filename) and os.access(filename, os.X_OK)
|
return os.path.isfile(filename) and os.access(filename, os.X_OK)
|
||||||
|
|
||||||
|
|
||||||
def find_exe_file(exe_name):
|
def _find_exe_file(exe_name):
|
||||||
for path in os.environ['PATH'].split(os.pathsep):
|
for path in os.environ['PATH'].split(os.pathsep):
|
||||||
exe_file = os.path.join(path, exe_name)
|
exe_file = os.path.join(path, exe_name)
|
||||||
if is_executable(exe_file):
|
if _is_executable(exe_file):
|
||||||
return exe_file
|
return exe_file
|
||||||
|
|
||||||
raise FileNotFoundError(f"Executable '{exe_name}' not found in $PATH!")
|
raise FileNotFoundError(f"Executable '{exe_name}' not found in $PATH!")
|
||||||
|
@ -24,36 +41,40 @@ class Executable:
|
||||||
class __Executable:
|
class __Executable:
|
||||||
__exe_path = None
|
__exe_path = None
|
||||||
|
|
||||||
def __init__(self, exe_name, requires_root=False):
|
def __init__(self, exe_name):
|
||||||
self.__exe_path = find_exe_file(exe_name)
|
self.__exe_path = _find_exe_file(exe_name)
|
||||||
|
|
||||||
def __build_cmd(self, args, requires_root=False, **kwargs):
|
def __build_cmd(self, args, requires_root=False, **kwargs):
|
||||||
cmd = [self.__exe_path, *args]
|
cmd = [self.__exe_path, *args]
|
||||||
|
|
||||||
if requires_root:
|
if requires_root:
|
||||||
self.__exe_path = [find_exe_file("sudo"), self.__exe_path]
|
self.__exe_path = [_find_exe_file("sudo"), self.__exe_path]
|
||||||
|
|
||||||
logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
|
logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
def run(self, process_args, requires_root=False, **kwargs):
|
def run(self, process_args, config=None, requires_root=False, **kwargs):
|
||||||
|
kwargs = _update_kwargs(config, **kwargs)
|
||||||
|
|
||||||
return subprocess.run(
|
return subprocess.run(
|
||||||
self.__build_cmd(process_args, requires_root, **kwargs),
|
self.__build_cmd(process_args, requires_root, **kwargs),
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def Popen(self, process_args, requires_root=False, **kwargs):
|
def Popen(self, process_args, config=None, requires_root=False, **kwargs):
|
||||||
|
kwargs = _update_kwargs(config, **kwargs)
|
||||||
|
|
||||||
return subprocess.Popen(
|
return subprocess.Popen(
|
||||||
self.__build_cmd(process_args, requires_root, **kwargs),
|
self.__build_cmd(process_args, requires_root, **kwargs),
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def run_less(self, process_args, requires_root=False, **kwargs):
|
def run_less(self, process_args, config=None, requires_root=False, **kwargs):
|
||||||
kwargs['stdout'] = subprocess.PIPE
|
kwargs['stdout'] = subprocess.PIPE
|
||||||
kwargs['stderr'] = subprocess.DEVNULL
|
kwargs['stderr'] = subprocess.DEVNULL
|
||||||
|
|
||||||
process = self.Popen(
|
process = self.Popen(
|
||||||
process_args, requires_root,
|
process_args, config, requires_root,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -68,11 +89,11 @@ class Executable:
|
||||||
__exe_name = None
|
__exe_name = None
|
||||||
__instances = {}
|
__instances = {}
|
||||||
|
|
||||||
def __init__(self, exe_name, requires_root=False):
|
def __init__(self, exe_name):
|
||||||
self.__exe_name = exe_name
|
self.__exe_name = exe_name
|
||||||
|
|
||||||
if exe_name not in Executable.__instances:
|
if exe_name not in Executable.__instances:
|
||||||
Executable.__instances[exe_name] = Executable.__Executable(exe_name, requires_root)
|
Executable.__instances[exe_name] = Executable.__Executable(exe_name)
|
||||||
|
|
||||||
def __getattr__(self, item):
|
def __getattr__(self, item):
|
||||||
return getattr(self.__instances[self.__exe_name], item)
|
return getattr(self.__instances[self.__exe_name], item)
|
||||||
|
|
Loading…
Reference in a new issue