DockerCommand is now project-sensitive
This commit is contained in:
parent
082c5f220c
commit
feda828c06
3 changed files with 61 additions and 38 deletions
|
@ -20,33 +20,19 @@ class LogsCommand(ServiceCommand):
|
||||||
)
|
)
|
||||||
|
|
||||||
def run(self, config, args):
|
def run(self, config, args):
|
||||||
project_name = args.project
|
compose_args = ['logs', '-t']
|
||||||
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']
|
|
||||||
if args.follow:
|
if args.follow:
|
||||||
process_args = [*process_args, '-f', '--tail=10']
|
compose_args = [*compose_args, '-f', '--tail=10']
|
||||||
|
|
||||||
if args.services:
|
if args.services:
|
||||||
process_args = [*process_args, *args.services]
|
compose_args = [*compose_args, *args.services]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if args.follow:
|
if args.follow:
|
||||||
DockerCommand('docker-compose').run(
|
DockerCommand('docker-compose').run(config, args, compose_args)
|
||||||
process_args,
|
|
||||||
cwd=project_dir, env=environment
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
DockerCommand('docker-compose').run_less(
|
DockerCommand('docker-compose').run_less(config, args, compose_args)
|
||||||
process_args,
|
|
||||||
cwd=project_dir, env=environment
|
|
||||||
)
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logging.debug("Subprocess aborted.")
|
logging.debug("Subprocess aborted.")
|
||||||
print()
|
print()
|
||||||
|
|
|
@ -1,10 +1,35 @@
|
||||||
# system
|
# system
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
# local
|
# local
|
||||||
from .executable import Executable
|
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):
|
class DockerCommand(Executable):
|
||||||
__requires_root = None
|
__requires_root = None
|
||||||
|
|
||||||
|
@ -21,24 +46,19 @@ class DockerCommand(Executable):
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
DockerCommand.__requires_root = True
|
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
|
# equivalent to 'super().run' but agnostic of nested class construct
|
||||||
super().__getattr__("run")(
|
super().__getattr__("run")(
|
||||||
args, DockerCommand.__requires_root,
|
process_args, DockerCommand.__requires_root,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def run_less(self, args, **kwargs):
|
def run_less(self, config, args, process_args, **kwargs):
|
||||||
process = self.Popen(
|
kwargs = _update_kwargs(config, args, **kwargs)
|
||||||
args, DockerCommand.__requires_root,
|
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
super().__getattr__("run_less")(
|
||||||
|
process_args, DockerCommand.__requires_root,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
less_process = Executable('less').run(
|
|
||||||
['-R', '+G'],
|
|
||||||
stdin=process.stdout
|
|
||||||
)
|
|
||||||
|
|
||||||
process.communicate()
|
|
||||||
return less_process
|
|
||||||
|
|
|
@ -36,18 +36,35 @@ class Executable:
|
||||||
logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
|
logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
def run(self, args, requires_root=False, **kwargs):
|
def run(self, process_args, requires_root=False, **kwargs):
|
||||||
return subprocess.run(
|
return subprocess.run(
|
||||||
self.__build_cmd(args, requires_root, **kwargs),
|
self.__build_cmd(process_args, requires_root, **kwargs),
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def Popen(self, args, requires_root=False, **kwargs):
|
def Popen(self, process_args, requires_root=False, **kwargs):
|
||||||
return subprocess.Popen(
|
return subprocess.Popen(
|
||||||
self.__build_cmd(args, requires_root, **kwargs),
|
self.__build_cmd(process_args, requires_root, **kwargs),
|
||||||
**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
|
__exe_name = None
|
||||||
__instances = {}
|
__instances = {}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue