1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 21:03:00 +00:00
kiwi-scp/src/kiwi/subcommands/list.py

105 lines
2.8 KiB
Python
Raw Normal View History

2020-08-18 14:36:39 +00:00
# system
import logging
import os
import subprocess
import yaml
2020-08-18 14:36:39 +00:00
# local
from ._subcommand import FlexCommand
from .utils.dockercommand import DockerCommand
from .utils.project import Projects
2020-08-18 14:36:39 +00:00
def _print_list(strings):
if isinstance(strings, list):
for string in strings:
print(f" - {string}")
elif isinstance(strings, str):
_print_list(strings.strip().split('\n'))
elif isinstance(strings, bytes):
_print_list(str(strings, 'utf-8'))
class ListCommand(FlexCommand):
"""kiwi list"""
def __init__(self):
super().__init__(
'list', "Listing",
description="List projects in this instance, services inside a project or service(s) inside a project"
)
def _run_instance(self, runner, config, args):
print(f"kiwi-config instance at '{os.getcwd()}'")
print("#########")
projects = Projects.all()
enableds = [
project.get_name()
for project in projects
if project.is_enabled()
]
if enableds:
print(f"Enabled projects:")
_print_list(enableds)
disableds = [
project.get_name()
for project in projects
if project.is_disabled()
]
if disableds:
print(f"Disabled projects:")
_print_list(disableds)
2020-08-18 14:36:39 +00:00
return True
def _run_project(self, runner, config, args):
project = Projects.from_args(args)[0]
if not project.exists():
logging.error(f"Project '{project.get_name()}' not found")
return False
print(f"Services in project '{project.get_name()}':")
print("#########")
2020-08-18 14:36:39 +00:00
ps = DockerCommand('docker-compose').run(
config, args, ['config', '--services'],
stdout=subprocess.PIPE
)
_print_list(ps.stdout)
2020-08-18 14:36:39 +00:00
return True
def _run_services(self, runner, config, args, services):
project = Projects.from_args(args)[0]
if not project.exists():
logging.error(f"Project '{project.get_name()}' not found")
return False
2020-08-18 14:36:39 +00:00
print(f"Configuration of services {services} in project '{project.get_name()}':")
print("#########")
2020-08-18 14:36:39 +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:
print(yaml.dump(
{service_name: docker_compose_yml['services'][service_name]},
default_flow_style=False, sort_keys=False
).strip())
return True
except yaml.YAMLError as exc:
logging.error(exc)
return False