kiwi-scp/src/kiwi/subcommands/logs.py

65 lines
1.7 KiB
Python
Raw Normal View History

2020-08-11 10:33:21 +00:00
import logging
2020-08-10 15:40:56 +00:00
from ..config import LoadedConfig
2020-08-10 14:36:05 +00:00
from ..core import Parser
2020-08-10 15:40:56 +00:00
from ._utils import SubCommand, DockerCommand
class LogsCommand(SubCommand):
def __init__(self):
super().__init__(
'logs',
2020-08-11 10:33:21 +00:00
description="Show logs of a project or service(s) of a project"
2020-08-10 14:36:05 +00:00
)
2020-08-11 10:33:21 +00:00
self._parser.add_argument(
2020-08-10 15:40:56 +00:00
'-f', '--follow', action='store_true',
2020-08-10 14:36:05 +00:00
help="output appended data as log grows"
2020-08-10 09:24:39 +00:00
)
2020-08-11 10:33:21 +00:00
self._parser.add_argument(
'project', type=str,
help="select a project in this instance"
2020-08-10 15:40:56 +00:00
)
2020-08-11 10:33:21 +00:00
self._parser.add_argument(
'services', metavar='service', nargs='*', type=str,
help="select service(s) in a project"
2020-08-10 15:40:56 +00:00
)
def run(self):
2020-08-10 15:40:56 +00:00
config = LoadedConfig.get()
2020-08-11 10:33:21 +00:00
cli_args = Parser().get_args()
2020-08-10 15:40:56 +00:00
2020-08-11 10:33:21 +00:00
project_name = cli_args.project
2020-08-10 15:40:56 +00:00
project_marker = config['markers:project']
project_dir = f'{project_name}{project_marker}'
environment = {
2020-08-11 10:33:21 +00:00
'KIWI_HUB_NAME': config['network:name'],
2020-08-10 15:40:56 +00:00
'COMPOSE_PROJECT_NAME': project_name
}
2020-08-10 14:36:05 +00:00
args = ['logs', '-t']
2020-08-11 10:33:21 +00:00
if cli_args.follow:
2020-08-10 14:36:05 +00:00
args = [*args, '-f', '--tail=10']
2020-08-11 10:33:21 +00:00
if cli_args.services:
args = [*args, *cli_args.services]
try:
if cli_args.follow:
DockerCommand('docker-compose').run(
args,
cwd=project_dir, env=environment
)
else:
DockerCommand('docker-compose').run_less(
args,
cwd=project_dir, env=environment
)
except KeyboardInterrupt:
logging.debug("Subprocess aborted.")
print()