2020-08-10 15:40:56 +00:00
|
|
|
import logging
|
2020-08-08 17:41:11 +00:00
|
|
|
import subprocess
|
|
|
|
|
|
2020-08-12 14:43:13 +00:00
|
|
|
from ...config import LoadedConfig
|
|
|
|
|
from .executable import get_exe_key
|
2020-08-08 17:41:11 +00:00
|
|
|
|
|
|
|
|
|
2020-08-10 15:40:56 +00:00
|
|
|
class DockerCommand:
|
|
|
|
|
class __DockerCommand:
|
2020-08-10 14:28:42 +00:00
|
|
|
__cmd = []
|
|
|
|
|
|
|
|
|
|
def __init__(self, exe_name):
|
|
|
|
|
config = LoadedConfig.get()
|
|
|
|
|
self.__cmd = [config[get_exe_key(exe_name)]]
|
|
|
|
|
|
2020-08-10 15:40:56 +00:00
|
|
|
if DockerCommand.__requires_root:
|
2020-08-10 14:28:42 +00:00
|
|
|
self.__cmd = [config[get_exe_key("sudo")], *self.__cmd]
|
|
|
|
|
|
2020-08-11 10:08:03 +00:00
|
|
|
def __build_cmd(self, args, **kwargs):
|
2020-08-10 14:28:42 +00:00
|
|
|
cmd = [*self.__cmd, *args]
|
2020-08-11 10:08:03 +00:00
|
|
|
logging.debug(f"DockerProgram cmd{cmd}, kwargs{kwargs}")
|
2020-08-10 15:40:56 +00:00
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
|
def run(self, args, **kwargs):
|
|
|
|
|
return subprocess.run(
|
2020-08-11 10:08:03 +00:00
|
|
|
self.__build_cmd(args, **kwargs),
|
2020-08-10 15:40:56 +00:00
|
|
|
**kwargs
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def run_less(self, args, **kwargs):
|
|
|
|
|
process = subprocess.Popen(
|
2020-08-11 10:08:03 +00:00
|
|
|
self.__build_cmd(args, **kwargs),
|
2020-08-10 15:40:56 +00:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
|
|
|
|
**kwargs
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
less_process = subprocess.run(
|
|
|
|
|
['less', '-R', '+G'],
|
|
|
|
|
stdin=process.stdout
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
process.communicate()
|
|
|
|
|
return less_process
|
2020-08-10 14:28:42 +00:00
|
|
|
|
|
|
|
|
__exe_name = None
|
|
|
|
|
__instances = {}
|
2020-08-08 17:41:11 +00:00
|
|
|
__requires_root = None
|
|
|
|
|
|
2020-08-10 14:28:42 +00:00
|
|
|
def __init__(self, exe_name):
|
2020-08-10 15:40:56 +00:00
|
|
|
if DockerCommand.__requires_root is None:
|
2020-08-08 17:41:11 +00:00
|
|
|
try:
|
|
|
|
|
config = LoadedConfig.get()
|
|
|
|
|
subprocess.run(
|
2020-08-10 12:50:47 +00:00
|
|
|
[config[get_exe_key('docker')], 'ps'],
|
2020-08-10 14:28:42 +00:00
|
|
|
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
2020-08-08 17:41:11 +00:00
|
|
|
)
|
2020-08-10 15:40:56 +00:00
|
|
|
DockerCommand.__requires_root = False
|
2020-08-08 17:41:11 +00:00
|
|
|
except subprocess.CalledProcessError:
|
2020-08-10 15:40:56 +00:00
|
|
|
DockerCommand.__requires_root = True
|
2020-08-08 17:41:11 +00:00
|
|
|
|
2020-08-10 14:28:42 +00:00
|
|
|
self.__exe_name = exe_name
|
2020-08-08 17:41:11 +00:00
|
|
|
|
2020-08-10 15:40:56 +00:00
|
|
|
if exe_name not in DockerCommand.__instances:
|
|
|
|
|
DockerCommand.__instances[exe_name] = DockerCommand.__DockerCommand(exe_name)
|
2020-08-08 17:41:11 +00:00
|
|
|
|
2020-08-10 14:28:42 +00:00
|
|
|
def __getattr__(self, item):
|
2020-08-12 14:43:13 +00:00
|
|
|
return getattr(self.__instances[self.__exe_name], item)
|