2020-08-18 14:36:39 +00:00
|
|
|
# system
|
|
|
|
import logging
|
|
|
|
import os
|
2021-11-02 16:21:31 +00:00
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
import yaml
|
2020-08-18 14:36:39 +00:00
|
|
|
|
2020-08-19 15:22:40 +00:00
|
|
|
from ..project import Project
|
|
|
|
from ..projects import Projects
|
2021-09-22 01:06:43 +00:00
|
|
|
# local
|
|
|
|
from ..subcommand import ServiceCommand
|
2020-08-18 14:36:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _print_list(strings):
|
2020-08-19 14:10:56 +00:00
|
|
|
if isinstance(strings, str):
|
|
|
|
print(f" - {strings}")
|
|
|
|
|
|
|
|
elif isinstance(strings, Project):
|
|
|
|
_print_list(strings.get_name())
|
2020-08-18 14:36:39 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
elif isinstance(strings, list):
|
|
|
|
for string in strings:
|
|
|
|
_print_list(string)
|
2020-08-18 14:36:39 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
else:
|
|
|
|
_print_list(list(strings))
|
2020-08-18 14:36:39 +00:00
|
|
|
|
|
|
|
|
2020-08-25 14:27:16 +00:00
|
|
|
class ShowCommand(ServiceCommand):
|
|
|
|
"""kiwi show"""
|
2020-08-18 14:36:39 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
2020-08-25 14:27:16 +00:00
|
|
|
'show', num_projects='?', num_services='*',
|
|
|
|
action="Showing",
|
|
|
|
description="Show projects in this instance, services inside a project or service(s) inside a project"
|
2020-08-18 14:36:39 +00:00
|
|
|
)
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_instance(self, runner, args):
|
2020-08-26 11:56:51 +00:00
|
|
|
print(f"kiwi-scp instance at '{os.getcwd()}'")
|
2020-08-19 09:58:13 +00:00
|
|
|
print("#########")
|
2020-08-19 14:10:56 +00:00
|
|
|
projects = Projects.from_dir()
|
2020-08-19 09:58:13 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
enabled_projects = projects.filter_enabled()
|
2020-08-20 13:08:41 +00:00
|
|
|
if enabled_projects:
|
2020-08-19 09:58:13 +00:00
|
|
|
print(f"Enabled projects:")
|
2020-08-19 14:10:56 +00:00
|
|
|
_print_list(enabled_projects)
|
2020-08-19 09:58:13 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
disabled_projects = projects.filter_disabled()
|
2020-08-20 13:08:41 +00:00
|
|
|
if disabled_projects:
|
2020-08-19 09:58:13 +00:00
|
|
|
print(f"Disabled projects:")
|
2020-08-19 14:10:56 +00:00
|
|
|
_print_list(disabled_projects)
|
2020-08-18 14:36:39 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2020-08-20 13:09:15 +00:00
|
|
|
def _run_project(self, runner, args, project):
|
2020-08-19 09:58:13 +00:00
|
|
|
if not project.exists():
|
2020-08-19 14:10:56 +00:00
|
|
|
logging.warning(f"Project '{project.get_name()}' not found")
|
2020-08-19 09:58:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
print(f"Services in project '{project.get_name()}':")
|
|
|
|
print("#########")
|
2020-08-18 14:36:39 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
with open(project.compose_file_name(), 'r') as stream:
|
|
|
|
try:
|
|
|
|
docker_compose_yml = yaml.safe_load(stream)
|
|
|
|
_print_list(docker_compose_yml['services'].keys())
|
2020-08-18 14:36:39 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
except yaml.YAMLError as exc:
|
|
|
|
logging.error(exc)
|
2020-08-18 14:36:39 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
return True
|
2020-08-19 09:58:13 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_services(self, runner, args, project, services):
|
2020-08-19 09:58:13 +00:00
|
|
|
if not project.exists():
|
|
|
|
logging.error(f"Project '{project.get_name()}' not found")
|
|
|
|
return False
|
2020-08-18 14:36:39 +00:00
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
print(f"Configuration of services {services} in project '{project.get_name()}':")
|
|
|
|
print("#########")
|
2020-08-18 14:36:39 +00:00
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
with open(project.compose_file_name(), 'r') as stream:
|
2020-08-18 14:36:39 +00:00
|
|
|
try:
|
|
|
|
docker_compose_yml = yaml.safe_load(stream)
|
|
|
|
|
|
|
|
for service_name in services:
|
2020-08-19 14:10:56 +00:00
|
|
|
try:
|
|
|
|
print(yaml.dump(
|
|
|
|
{service_name: docker_compose_yml['services'][service_name]},
|
|
|
|
default_flow_style=False, sort_keys=False
|
|
|
|
).strip())
|
|
|
|
except KeyError:
|
|
|
|
logging.error(f"Service '{service_name}' not found")
|
2020-08-18 14:36:39 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
except yaml.YAMLError as exc:
|
|
|
|
logging.error(exc)
|
|
|
|
|
|
|
|
return False
|