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/logs.py

59 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 ._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 12:03:00 +00:00
self._sub_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 12:03:00 +00:00
self._sub_parser.add_argument(
2020-08-11 10:33:21 +00:00
'project', type=str,
help="select a project in this instance"
2020-08-10 15:40:56 +00:00
)
2020-08-11 12:03:00 +00:00
self._sub_parser.add_argument(
2020-08-11 10:33:21 +00:00
'services', metavar='service', nargs='*', type=str,
help="select service(s) in a project"
2020-08-10 15:40:56 +00:00
)
2020-08-11 12:03:00 +00:00
def run(self, config, args):
project_name = 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-11 12:03:00 +00:00
process_args = ['logs', '-t']
if args.follow:
process_args = [*process_args, '-f', '--tail=10']
2020-08-10 14:36:05 +00:00
2020-08-11 12:03:00 +00:00
if args.services:
process_args = [*process_args, *args.services]
2020-08-11 10:33:21 +00:00
try:
2020-08-11 12:03:00 +00:00
if args.follow:
2020-08-11 10:33:21 +00:00
DockerCommand('docker-compose').run(
2020-08-11 12:03:00 +00:00
process_args,
2020-08-11 10:33:21 +00:00
cwd=project_dir, env=environment
)
else:
DockerCommand('docker-compose').run_less(
2020-08-11 12:03:00 +00:00
process_args,
2020-08-11 10:33:21 +00:00
cwd=project_dir, env=environment
)
except KeyboardInterrupt:
logging.debug("Subprocess aborted.")
print()