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

DockerCommand is now project-sensitive

This commit is contained in:
Jörn-Michael Miehe 2020-08-13 11:34:30 +02:00
parent 082c5f220c
commit feda828c06
3 changed files with 61 additions and 38 deletions

View file

@ -20,33 +20,19 @@ class LogsCommand(ServiceCommand):
)
def run(self, config, args):
project_name = args.project
project_marker = config['markers:project']
project_dir = f'{project_name}{project_marker}'
environment = {
'KIWI_HUB_NAME': config['network:name'],
'COMPOSE_PROJECT_NAME': project_name
}
process_args = ['logs', '-t']
compose_args = ['logs', '-t']
if args.follow:
process_args = [*process_args, '-f', '--tail=10']
compose_args = [*compose_args, '-f', '--tail=10']
if args.services:
process_args = [*process_args, *args.services]
compose_args = [*compose_args, *args.services]
try:
if args.follow:
DockerCommand('docker-compose').run(
process_args,
cwd=project_dir, env=environment
)
DockerCommand('docker-compose').run(config, args, compose_args)
else:
DockerCommand('docker-compose').run_less(
process_args,
cwd=project_dir, env=environment
)
DockerCommand('docker-compose').run_less(config, args, compose_args)
except KeyboardInterrupt:
logging.debug("Subprocess aborted.")
print()

View file

@ -1,10 +1,35 @@
# system
import logging
import os
import subprocess
# local
from .executable import Executable
def _update_kwargs(config, args, **kwargs):
project_name = args.project
project_marker = config['markers:project']
project_dir = f'{project_name}{project_marker}'
kwargs['cwd'] = project_dir
if 'env' not in kwargs:
kwargs['env'] = {}
if config['runtime:env'] is not None:
kwargs['env'].update(config['runtime:env'])
kwargs['env'].update({
'KIWI_HUB_NAME': config['network:name'],
'COMPOSE_PROJECT_NAME': project_name,
'CONFDIR': os.path.join(config['runtime:storage'], 'conf'),
'TARGETDIR': os.path.join(config['runtime:storage'], project_dir)
})
logging.debug(f"kwargs updated: {kwargs}")
return kwargs
class DockerCommand(Executable):
__requires_root = None
@ -21,24 +46,19 @@ class DockerCommand(Executable):
except subprocess.CalledProcessError:
DockerCommand.__requires_root = True
def run(self, args, **kwargs):
def run(self, config, args, process_args, **kwargs):
kwargs = _update_kwargs(config, args, **kwargs)
# equivalent to 'super().run' but agnostic of nested class construct
super().__getattr__("run")(
args, DockerCommand.__requires_root,
process_args, DockerCommand.__requires_root,
**kwargs
)
def run_less(self, args, **kwargs):
process = self.Popen(
args, DockerCommand.__requires_root,
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
def run_less(self, config, args, process_args, **kwargs):
kwargs = _update_kwargs(config, args, **kwargs)
super().__getattr__("run_less")(
process_args, DockerCommand.__requires_root,
**kwargs
)
less_process = Executable('less').run(
['-R', '+G'],
stdin=process.stdout
)
process.communicate()
return less_process

View file

@ -36,18 +36,35 @@ class Executable:
logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
return cmd
def run(self, args, requires_root=False, **kwargs):
def run(self, process_args, requires_root=False, **kwargs):
return subprocess.run(
self.__build_cmd(args, requires_root, **kwargs),
self.__build_cmd(process_args, requires_root, **kwargs),
**kwargs
)
def Popen(self, args, requires_root=False, **kwargs):
def Popen(self, process_args, requires_root=False, **kwargs):
return subprocess.Popen(
self.__build_cmd(args, requires_root, **kwargs),
self.__build_cmd(process_args, requires_root, **kwargs),
**kwargs
)
def run_less(self, process_args, requires_root=False, **kwargs):
kwargs['stdout'] = subprocess.PIPE
kwargs['stderr'] = subprocess.DEVNULL
process = self.Popen(
process_args, requires_root,
**kwargs
)
less_process = Executable('less').run(
['-R', '+G'],
stdin=process.stdout
)
process.communicate()
return less_process
__exe_name = None
__instances = {}