mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-22 04:43:00 +00:00
DockerProgram Singleton dictionary and simple logs implementation
This commit is contained in:
parent
81127096cd
commit
90bfb32289
2 changed files with 34 additions and 27 deletions
|
@ -43,37 +43,42 @@ class SubCommand:
|
|||
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
|
||||
|
||||
@classmethod
|
||||
def __check_requires_root(cls):
|
||||
if cls.__requires_root is None:
|
||||
def __init__(self, exe_name):
|
||||
if DockerProgram.__requires_root is None:
|
||||
try:
|
||||
config = LoadedConfig.get()
|
||||
subprocess.run(
|
||||
[config[get_exe_key('docker')], 'ps'],
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
||||
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
||||
)
|
||||
cls.__requires_root = False
|
||||
DockerProgram.__requires_root = False
|
||||
except subprocess.CalledProcessError:
|
||||
cls.__requires_root = True
|
||||
DockerProgram.__requires_root = True
|
||||
|
||||
return cls.__requires_root
|
||||
self.__exe_name = exe_name
|
||||
|
||||
@classmethod
|
||||
def run_command(cls, exe_key, args, cwd=None, env=None):
|
||||
config = LoadedConfig.get()
|
||||
cmd = [config[get_exe_key(exe_key)], *args]
|
||||
if exe_name not in DockerProgram.__instances:
|
||||
DockerProgram.__instances[exe_name] = DockerProgram.__DockerProgram(exe_name)
|
||||
|
||||
if cls.__check_requires_root():
|
||||
cmd = [config[get_exe_key('sudo')], *cmd]
|
||||
|
||||
print(cmd)
|
||||
return subprocess.run(
|
||||
cmd,
|
||||
# stdout=subprocess.PIPE,
|
||||
# stderr=subprocess.PIPE,
|
||||
cwd=cwd, env=env
|
||||
)
|
||||
def __getattr__(self, item):
|
||||
return getattr(self.__instances[self.__exe_name], item)
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
from ..core import Parser
|
||||
|
||||
from ._utils import SubCommand, Docker
|
||||
from ._utils import SubCommand, DockerProgram
|
||||
|
||||
|
||||
class LogsCommand(SubCommand):
|
||||
|
@ -11,4 +9,8 @@ class LogsCommand(SubCommand):
|
|||
)
|
||||
|
||||
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'}
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue