2020-08-12 15:46:50 +00:00
|
|
|
# system
|
2020-08-13 09:34:30 +00:00
|
|
|
import logging
|
|
|
|
|
import os
|
2020-08-08 17:41:11 +00:00
|
|
|
import subprocess
|
|
|
|
|
|
2020-08-12 15:46:50 +00:00
|
|
|
# local
|
2020-08-12 15:18:20 +00:00
|
|
|
from .executable import Executable
|
2020-08-08 17:41:11 +00:00
|
|
|
|
|
|
|
|
|
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):
|
2020-08-08 17:41:11 +00:00
|
|
|
__requires_root = None
|
|
|
|
|
|
2020-08-10 14:28:42 +00:00
|
|
|
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:
|
2020-08-08 17:41:11 +00:00
|
|
|
try:
|
2020-08-12 15:18:20 +00:00
|
|
|
Executable('docker').run(
|
2020-08-12 15:39:55 +00:00
|
|
|
['ps'],
|
|
|
|
|
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
2020-08-08 17:41:11 +00:00
|
|
|
)
|
2020-08-10 15:40:56 +00:00
|
|
|
DockerCommand.__requires_root = False
|
2020-08-08 17:41:11 +00:00
|
|
|
except subprocess.CalledProcessError:
|
2020-08-10 15:40:56 +00:00
|
|
|
DockerCommand.__requires_root = True
|
2020-08-08 17:41:11 +00:00
|
|
|
|
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-12 15:18:20 +00:00
|
|
|
|
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-12 15:18:20 +00:00
|
|
|
|
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
|
2020-08-12 15:18:20 +00:00
|
|
|
)
|