1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2025-12-07 17:13:09 +00:00
kiwi-scp/src/kiwi/subcommands/utils/dockercommand.py

68 lines
2 KiB
Python
Raw Normal View History

2020-08-12 15:46:50 +00:00
# system
2020-08-13 09:34:30 +00:00
import logging
import os
import subprocess
2020-08-12 15:46:50 +00:00
# local
from .executable import Executable
2020-08-13 09:34:30 +00:00
def _update_kwargs(config, args, **kwargs):
2020-08-17 11:14:54 +00:00
if args is not None and 'projects' in args and args.projects is not None:
2020-08-17 09:57:08 +00:00
# command affects a project in this instance
2020-08-13 09:34:30 +00:00
2020-08-17 11:14:54 +00:00
project_name = args.projects
if isinstance(project_name, list) and len(project_name) > 0:
project_name = project_name[0]
2020-08-17 09:57:08 +00:00
project_marker = config['markers:project']
project_dir = f'{project_name}{project_marker}'
kwargs['cwd'] = project_dir
2020-08-13 09:34:30 +00:00
2020-08-17 09:57:08 +00:00
if 'env' not in kwargs:
kwargs['env'] = {}
2020-08-13 09:34:30 +00:00
2020-08-17 09:57:08 +00:00
kwargs['env'].update({
'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}")
2020-08-13 09:34:30 +00:00
return kwargs
2020-08-12 15:39:55 +00:00
class DockerCommand(Executable):
__requires_root = None
def __init__(self, exe_name):
2020-08-12 15:39:55 +00:00
super().__init__(exe_name)
2020-08-10 15:40:56 +00:00
if DockerCommand.__requires_root is None:
try:
Executable('docker').run(
2020-08-12 15:39:55 +00:00
['ps'],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
2020-08-10 15:40:56 +00:00
DockerCommand.__requires_root = False
except subprocess.CalledProcessError:
2020-08-10 15:40:56 +00:00
DockerCommand.__requires_root = True
2020-08-13 09:34:30 +00:00
def run(self, config, args, process_args, **kwargs):
kwargs = _update_kwargs(config, args, **kwargs)
2020-08-12 15:39:55 +00:00
# equivalent to 'super().run' but agnostic of nested class construct
2020-08-17 09:57:08 +00:00
return super().__getattr__("run")(
process_args, config, DockerCommand.__requires_root,
2020-08-12 15:39:55 +00:00
**kwargs
)
2020-08-13 09:34:30 +00:00
def run_less(self, config, args, process_args, **kwargs):
kwargs = _update_kwargs(config, args, **kwargs)
2020-08-17 09:57:08 +00:00
return super().__getattr__("run_less")(
process_args, config, DockerCommand.__requires_root,
2020-08-13 09:34:30 +00:00
**kwargs
)