mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-22 04:43:00 +00:00
DockerCommand: __build_cmd, run_less
This commit is contained in:
parent
e667c00b04
commit
6ee528f4cb
2 changed files with 66 additions and 19 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
@ -43,42 +44,63 @@ class SubCommand:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DockerProgram:
|
class DockerCommand:
|
||||||
class __DockerProgram:
|
class __DockerCommand:
|
||||||
__cmd = []
|
__cmd = []
|
||||||
|
|
||||||
def __init__(self, exe_name):
|
def __init__(self, exe_name):
|
||||||
config = LoadedConfig.get()
|
config = LoadedConfig.get()
|
||||||
self.__cmd = [config[get_exe_key(exe_name)]]
|
self.__cmd = [config[get_exe_key(exe_name)]]
|
||||||
|
|
||||||
if DockerProgram.__requires_root:
|
if DockerCommand.__requires_root:
|
||||||
self.__cmd = [config[get_exe_key("sudo")], *self.__cmd]
|
self.__cmd = [config[get_exe_key("sudo")], *self.__cmd]
|
||||||
|
|
||||||
def run(self, args, **kwargs):
|
def __build_cmd(self, args):
|
||||||
cmd = [*self.__cmd, *args]
|
cmd = [*self.__cmd, *args]
|
||||||
print(cmd)
|
logging.debug(f"DockerProgram: {cmd}")
|
||||||
return subprocess.run(cmd, **kwargs)
|
return cmd
|
||||||
|
|
||||||
|
def run(self, args, **kwargs):
|
||||||
|
return subprocess.run(
|
||||||
|
self.__build_cmd(args),
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
def run_less(self, args, **kwargs):
|
||||||
|
process = subprocess.Popen(
|
||||||
|
self.__build_cmd(args),
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
less_process = subprocess.run(
|
||||||
|
['less', '-R', '+G'],
|
||||||
|
stdin=process.stdout
|
||||||
|
)
|
||||||
|
|
||||||
|
process.communicate()
|
||||||
|
return less_process
|
||||||
|
|
||||||
__exe_name = None
|
__exe_name = None
|
||||||
__instances = {}
|
__instances = {}
|
||||||
__requires_root = None
|
__requires_root = None
|
||||||
|
|
||||||
def __init__(self, exe_name):
|
def __init__(self, exe_name):
|
||||||
if DockerProgram.__requires_root is None:
|
if DockerCommand.__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, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
||||||
)
|
)
|
||||||
DockerProgram.__requires_root = False
|
DockerCommand.__requires_root = False
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
DockerProgram.__requires_root = True
|
DockerCommand.__requires_root = True
|
||||||
|
|
||||||
self.__exe_name = exe_name
|
self.__exe_name = exe_name
|
||||||
|
|
||||||
if exe_name not in DockerProgram.__instances:
|
if exe_name not in DockerCommand.__instances:
|
||||||
DockerProgram.__instances[exe_name] = DockerProgram.__DockerProgram(exe_name)
|
DockerCommand.__instances[exe_name] = DockerCommand.__DockerCommand(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)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
from ..config import LoadedConfig
|
||||||
from ..core import Parser
|
from ..core import Parser
|
||||||
|
|
||||||
from ._utils import SubCommand, DockerProgram
|
from ._utils import SubCommand, DockerCommand
|
||||||
|
|
||||||
|
|
||||||
class LogsCommand(SubCommand):
|
class LogsCommand(SubCommand):
|
||||||
|
@ -11,18 +12,42 @@ class LogsCommand(SubCommand):
|
||||||
)
|
)
|
||||||
|
|
||||||
self.get_parser().add_argument(
|
self.get_parser().add_argument(
|
||||||
'-f', '--follow',
|
'-f', '--follow', action='store_true',
|
||||||
action='store_true',
|
|
||||||
help="output appended data as log grows"
|
help="output appended data as log grows"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.get_parser().add_argument(
|
||||||
|
'project',
|
||||||
|
help="narf"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.get_parser().add_argument(
|
||||||
|
'service', nargs='?',
|
||||||
|
help="narf"
|
||||||
|
)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
config = LoadedConfig.get()
|
||||||
|
|
||||||
|
project_name = Parser().get_args().project
|
||||||
|
project_marker = config['markers:project']
|
||||||
|
project_dir = f'{project_name}{project_marker}'
|
||||||
|
|
||||||
|
environment = {
|
||||||
|
'DOCKERNET': config['network:name'],
|
||||||
|
'COMPOSE_PROJECT_NAME': project_name
|
||||||
|
}
|
||||||
|
|
||||||
args = ['logs', '-t']
|
args = ['logs', '-t']
|
||||||
if Parser().get_args().follow:
|
if Parser().get_args().follow:
|
||||||
args = [*args, '-f', '--tail=10']
|
args = [*args, '-f', '--tail=10']
|
||||||
|
|
||||||
DockerProgram('docker-compose').run(
|
DockerCommand('docker-compose').run(
|
||||||
args,
|
args,
|
||||||
cwd='hello-world.project',
|
cwd=project_dir, env=environment
|
||||||
env={'COMPOSE_PROJECT_NAME': 'hello-world'}
|
)
|
||||||
)
|
else:
|
||||||
|
DockerCommand('docker-compose').run_less(
|
||||||
|
args,
|
||||||
|
cwd=project_dir, env=environment
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue