1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 12:53:00 +00:00

DockerProgram Singleton dictionary and simple logs implementation

This commit is contained in:
Jörn-Michael Miehe 2020-08-10 16:28:42 +02:00
parent 81127096cd
commit 90bfb32289
2 changed files with 34 additions and 27 deletions

View file

@ -43,37 +43,42 @@ class SubCommand:
pass pass
class Docker: class DockerProgram:
class __DockerProgram:
__cmd = []
def __init__(self, exe_name):
config = LoadedConfig.get()
self.__cmd = [config[get_exe_key(exe_name)]]
if DockerProgram.__requires_root:
self.__cmd = [config[get_exe_key("sudo")], *self.__cmd]
def run(self, args, **kwargs):
cmd = [*self.__cmd, *args]
print(cmd)
return subprocess.run(cmd, **kwargs)
__exe_name = None
__instances = {}
__requires_root = None __requires_root = None
@classmethod def __init__(self, exe_name):
def __check_requires_root(cls): if DockerProgram.__requires_root is None:
if cls.__requires_root is None:
try: try:
config = LoadedConfig.get() config = LoadedConfig.get()
subprocess.run( subprocess.run(
[config[get_exe_key('docker')], 'ps'], [config[get_exe_key('docker')], 'ps'],
check=True, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
) )
cls.__requires_root = False DockerProgram.__requires_root = False
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
cls.__requires_root = True DockerProgram.__requires_root = True
return cls.__requires_root self.__exe_name = exe_name
@classmethod if exe_name not in DockerProgram.__instances:
def run_command(cls, exe_key, args, cwd=None, env=None): DockerProgram.__instances[exe_name] = DockerProgram.__DockerProgram(exe_name)
config = LoadedConfig.get()
cmd = [config[get_exe_key(exe_key)], *args]
if cls.__check_requires_root(): def __getattr__(self, item):
cmd = [config[get_exe_key('sudo')], *cmd] return getattr(self.__instances[self.__exe_name], item)
print(cmd)
return subprocess.run(
cmd,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE,
cwd=cwd, env=env
)

View file

@ -1,6 +1,4 @@
from ..core import Parser from ._utils import SubCommand, DockerProgram
from ._utils import SubCommand, Docker
class LogsCommand(SubCommand): class LogsCommand(SubCommand):
@ -11,4 +9,8 @@ class LogsCommand(SubCommand):
) )
def run(self): def run(self):
print(Docker.run_command('docker-compose', ['logs', '-tf', '--tail=10'], cwd='hello-world.project', env={'COMPOSE_PROJECT_NAME': 'hello-world'})) DockerProgram('docker-compose').run(
['logs', '-tf', '--tail=10'],
cwd='hello-world.project',
env={'COMPOSE_PROJECT_NAME': 'hello-world'}
)