FlexCommand: project_name / services in params

This commit is contained in:
Jörn-Michael Miehe 2020-08-18 14:04:10 +02:00
parent 6615982e95
commit c336de4d9c
8 changed files with 71 additions and 58 deletions

View file

@ -1,5 +1,5 @@
# local
from .utils.project import get_project_name, list_projects
from .utils._misc import get_project_name, get_services, list_projects
# parent
from ..parser import Parser
@ -69,6 +69,8 @@ class ServiceCommand(ProjectCommand):
class FlexCommand(ServiceCommand):
"""this command concerns the entire instance, a whole project or just service(s) in a project"""
def __init__(self, name, **kwargs):
super().__init__(
name, num_projects='?', num_services='*',
@ -84,21 +86,23 @@ class FlexCommand(ServiceCommand):
return result
def _run_project(self, runner, config, args):
def _run_project(self, runner, config, args, project_name):
pass
def _run_services(self, runner, config, args):
def _run_services(self, runner, config, args, project_name, services):
pass
def run(self, runner, config, args):
if 'projects' not in args or args.projects is None:
# command for entire instance
project_name = get_project_name(args)
services = get_services(args)
if project_name is None:
# no project given, run for entire instance
return self._run_instance(runner, config, args)
elif 'services' not in args or not args.services:
# command for whole project
return self._run_project(runner, config, args)
if services is None:
# no services given, run for whole project
return self._run_project(runner, config, args, project_name)
else:
# command for service(s) inside project
return self._run_services(runner, config, args)
# run for service(s) inside project
return self._run_services(runner, config, args, project_name, services)

View file

@ -5,7 +5,7 @@ import subprocess
# local
from ._subcommand import SubCommand
from .utils.project import list_projects, get_project_dir
from .utils._misc import list_projects, get_project_dir
from .utils.rootkit import Rootkit, prefix_path_mnt
# parent

View file

@ -4,8 +4,7 @@ import logging
# local
from ._subcommand import FlexCommand
from .utils.dockercommand import DockerCommand
from .utils.project import get_project_name, list_projects
from .utils.user_input import are_you_sure
from .utils._misc import are_you_sure
class DownCommand(FlexCommand):
@ -18,25 +17,25 @@ class DownCommand(FlexCommand):
def _run_instance(self, runner, config, args):
if are_you_sure("This will bring down the entire instance."):
super()._run_instance(runner, config, args)
return super()._run_instance(runner, config, args)
return False
def _run_project(self, runner, config, args):
logging.info(f"Bringing down project '{get_project_name(args)}'")
def _run_project(self, runner, config, args, project_name):
logging.info(f"Bringing down project '{project_name}'")
DockerCommand('docker-compose').run(
config, args, ['down']
)
return True
def _run_services(self, runner, config, args):
logging.info(f"Bringing down services {args.services} in project '{get_project_name(args)}'")
def _run_services(self, runner, config, args, project_name, services):
logging.info(f"Bringing down services {services} in project '{project_name}'")
DockerCommand('docker-compose').run(
config, args, ['stop', *args.services]
config, args, ['stop', *services]
)
DockerCommand('docker-compose').run(
config, args, ['rm', '-f', *args.services]
config, args, ['rm', '-f', *services]
)
return True

View file

@ -5,7 +5,7 @@ import subprocess
# local
from ._subcommand import SubCommand
from .utils.dockercommand import DockerCommand
from .utils.user_input import are_you_sure
from .utils._misc import are_you_sure
def _find_net(config, args):

View file

@ -4,7 +4,6 @@ import logging
# local
from ._subcommand import FlexCommand
from .utils.dockercommand import DockerCommand
from .utils.project import get_project_name, list_projects
class UpCommand(FlexCommand):
@ -15,18 +14,18 @@ class UpCommand(FlexCommand):
'up', description="Bring up the whole instance, a project or service(s) inside a project"
)
def _run_project(self, runner, config, args):
logging.info(f"Bringing up project '{get_project_name(args)}'")
def _run_project(self, runner, config, args, project_name):
logging.info(f"Bringing up project '{project_name}'")
DockerCommand('docker-compose').run(
config, args, ['up', '-d']
)
return True
def _run_services(self, runner, config, args):
logging.info(f"Bringing up services {args.services} in project '{get_project_name(args)}'")
def _run_services(self, runner, config, args, project_name, services):
logging.info(f"Bringing up services {services} in project '{project_name}'")
DockerCommand('docker-compose').run(
config, args, ['up', '-d', *args.services]
config, args, ['up', '-d', *services]
)
return True

View file

@ -13,6 +13,15 @@ def get_project_name(args):
return None
def get_services(args):
"""get services list from CLI args"""
if args is not None and 'services' in args:
return args.services
return None
def get_project_dir(config, project_name):
"""get project directory"""
@ -41,3 +50,33 @@ def list_projects(config):
projects = [p[:-len(config['markers:project'])] for p in project_dirs]
return projects
def _surround(string, bang):
midlane = f"{bang * 3} {string} {bang * 3}"
sidelane = bang*len(midlane)
return f"{sidelane}\n{midlane}\n{sidelane}"
def are_you_sure(prompt, default="no"):
if default.lower() == 'yes':
suffix = "[YES|no]"
else:
suffix = "[yes|NO]"
answer = input(
f"{_surround('CAREFULING IN PROGRESS', '!')}\n"
f"\n"
f">>> {prompt} <<<\n"
f"\n"
f"Are you sure you want to proceed? {suffix} "
).strip().lower()
if answer == '':
answer = default
while answer not in ['yes', 'no']:
answer = input("Please type 'yes' or 'no' explicitly: ").strip().lower()
return answer == 'yes'

View file

@ -1,10 +1,11 @@
# system
import logging
import os
import subprocess
# local
from .executable import Executable
from .project import *
from ._misc import get_project_dir, get_project_name
# parent
from ..._constants import CONF_DIRECTORY_NAME

View file

@ -1,29 +0,0 @@
def _surround(string, bang):
midlane = f"{bang * 3} {string} {bang * 3}"
sidelane = bang*len(midlane)
return f"{sidelane}\n{midlane}\n{sidelane}"
def are_you_sure(prompt, default="no"):
if default.lower() == 'yes':
suffix = "[YES|no]"
else:
suffix = "[yes|NO]"
answer = input(
f"{_surround('CAREFULING IN PROGRESS', '!')}\n"
f"\n"
f">>> {prompt} <<<\n"
f"\n"
f"Are you sure you want to proceed? {suffix} "
).strip().lower()
if answer == '':
answer = default
while answer not in ['yes', 'no']:
answer = input("Please type 'yes' or 'no' explicitly: ").strip().lower()
return answer == 'yes'