mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-22 12:53:00 +00:00
cleaner way of getting root access
This commit is contained in:
parent
500652c9d6
commit
9907e72244
2 changed files with 14 additions and 16 deletions
|
@ -33,27 +33,28 @@ def _update_kwargs(config, args, **kwargs):
|
||||||
|
|
||||||
|
|
||||||
class DockerCommand(Executable):
|
class DockerCommand(Executable):
|
||||||
__requires_root = None
|
__has_tried = False
|
||||||
|
|
||||||
def __init__(self, exe_name):
|
def __init__(self, exe_name):
|
||||||
super().__init__(exe_name)
|
super().__init__(exe_name)
|
||||||
|
|
||||||
if DockerCommand.__requires_root is None:
|
if not DockerCommand.__has_tried:
|
||||||
try:
|
try:
|
||||||
Executable('docker').run(
|
Executable('docker').run(
|
||||||
['ps'],
|
['ps'],
|
||||||
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
||||||
)
|
)
|
||||||
DockerCommand.__requires_root = False
|
DockerCommand.__has_tried = True
|
||||||
|
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
DockerCommand.__requires_root = True
|
raise PermissionError("Cannot access docker, please get into the docker group or run as root!")
|
||||||
|
|
||||||
def run(self, config, args, process_args, **kwargs):
|
def run(self, config, args, process_args, **kwargs):
|
||||||
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
|
||||||
return super().__getattr__("run")(
|
return super().__getattr__("run")(
|
||||||
process_args, config, DockerCommand.__requires_root,
|
process_args, config,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -61,6 +62,6 @@ class DockerCommand(Executable):
|
||||||
kwargs = _update_kwargs(config, args, **kwargs)
|
kwargs = _update_kwargs(config, args, **kwargs)
|
||||||
|
|
||||||
return super().__getattr__("run_less")(
|
return super().__getattr__("run_less")(
|
||||||
process_args, config, DockerCommand.__requires_root,
|
process_args, config,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
|
@ -42,37 +42,34 @@ class Executable:
|
||||||
def __init__(self, exe_name):
|
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, **kwargs):
|
||||||
cmd = [self.__exe_path, *args]
|
cmd = [self.__exe_path, *args]
|
||||||
|
|
||||||
if requires_root:
|
|
||||||
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, config=None, requires_root=False, **kwargs):
|
def run(self, process_args, config=None, **kwargs):
|
||||||
kwargs = _update_kwargs(config, **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, **kwargs),
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def Popen(self, process_args, config=None, requires_root=False, **kwargs):
|
def Popen(self, process_args, config=None, **kwargs):
|
||||||
kwargs = _update_kwargs(config, **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, **kwargs),
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def run_less(self, process_args, config=None, requires_root=False, **kwargs):
|
def run_less(self, process_args, config=None, **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, config, requires_root,
|
process_args, config,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue