diff --git a/src/drunc/apps/__main_pm_shell__.py b/src/drunc/apps/__main_pm_shell__.py index 251bbca..ef1b428 100644 --- a/src/drunc/apps/__main_pm_shell__.py +++ b/src/drunc/apps/__main_pm_shell__.py @@ -1,11 +1,8 @@ - def main(): from drunc.process_manager.interface.context import ProcessManagerContext context = ProcessManagerContext() - try: from drunc.process_manager.interface.shell import process_manager_shell - process_manager_shell(obj = context) except Exception as e: @@ -16,7 +13,5 @@ def main(): rprint(f'Exiting...') exit(1) - - if __name__ == '__main__': main() diff --git a/src/drunc/process_manager/interface/context.py b/src/drunc/process_manager/interface/context.py index dba488c..eef3c31 100644 --- a/src/drunc/process_manager/interface/context.py +++ b/src/drunc/process_manager/interface/context.py @@ -4,7 +4,9 @@ from drunc.utils.shell_utils import ShellContext, GRPCDriver class ProcessManagerContext(ShellContext): # boilerplatefest - status_receiver = None + def __init__(self): + self.status_receiver = None + super(ProcessManagerContext, self).__init__() def reset(self, address:str=None): self.address = address diff --git a/src/drunc/process_manager/interface/process_manager.py b/src/drunc/process_manager/interface/process_manager.py index 41abca6..1a99390 100644 --- a/src/drunc/process_manager/interface/process_manager.py +++ b/src/drunc/process_manager/interface/process_manager.py @@ -4,13 +4,12 @@ import os import logging import getpass -from rich.console import Console from rich.logging import RichHandler from drunc.utils.utils import log_levels, setup_root_logger, get_logger from drunc.process_manager.utils import get_log_path, get_pm_conf_name_from_dir _cleanup_coroutines = [] -def run_pm(pm_conf:str, pm_address:str, log_level:str, override_logs:bool, log_path:str=None, user:str=getpass.getuser(), ready_event:bool=None, signal_handler:bool=None, generated_port:bool=None): +def run_pm(pm_conf:str, pm_address:str, log_level:str, override_logs:bool, log_path:str=None, user:str=getpass.getuser(), ready_event:bool=None, signal_handler:bool=None, generated_port:bool=None) -> None: appName = "process_manager" pmConfFileName = get_pm_conf_name_from_dir(pm_conf) # Treating the pm conf data filename as the session @@ -34,8 +33,6 @@ def run_pm(pm_conf:str, pm_address:str, log_level:str, override_logs:bool, log_p from drunc.utils.utils import parent_death_pact parent_death_pact() # If the parent dies (for example unified shell), we die too - # from rich.console import Console - # console = Console() log.debug(f'Using \'{pm_conf}\' as the ProcessManager configuration') from drunc.process_manager.process_manager import ProcessManager @@ -54,6 +51,7 @@ def run_pm(pm_conf:str, pm_address:str, log_level:str, override_logs:bool, log_p loop = asyncio.get_event_loop() async def serve(address:str) -> None: + log.debug("serve called") if not address: from drunc.exceptions import DruncSetupException raise DruncSetupException('The address on which to expect commands/send status wasn\'t specified') @@ -90,10 +88,7 @@ async def server_shutdown(): ) except Exception as e: log.error("Serving the ProcessManager received an Exception") - import os - from rich.console import Console - console = Console() - console.print_exception(width=os.get_terminal_size()[0]) + log.exception(e) finally: if _cleanup_coroutines: log.info("Clearing coroutines") @@ -110,13 +105,16 @@ async def server_shutdown(): log_levels.keys(), case_sensitive=False ), - default='DEBUG', + default='INFO', help='Set the log level' ) @click.option('-o/-no', '--override-logs/--no-override-logs', type=bool, default=True, help="Override logs, if --no-override-logs filenames have the timestamp of the run.") @click.option('-lp', '--log-path', type=str, default=None, help="Log path of process_manager logs.") @click.option('-u', '--user', type=str, default=getpass.getuser(), help="Username for process_manager logs.") -def process_manager_cli(pm_conf:str, pm_port:int, log_level:str, override_logs:bool, log_path:str, user:str): +def process_manager_cli(pm_conf:str, pm_port:int, log_level:str, override_logs:bool, log_path:str, user:str) -> None: + from drunc.utils.utils import setup_root_logger + setup_root_logger(log_level) + from drunc.process_manager.configuration import get_process_manager_configuration pm_conf = get_process_manager_configuration(pm_conf) run_pm( diff --git a/src/drunc/process_manager/interface/shell.py b/src/drunc/process_manager/interface/shell.py index 7ecdb25..ed12dd1 100644 --- a/src/drunc/process_manager/interface/shell.py +++ b/src/drunc/process_manager/interface/shell.py @@ -1,7 +1,9 @@ import click_shell import click import os +import getpass +from drunc.utils.grpc_utils import ServerUnreachable from drunc.utils.utils import CONTEXT_SETTINGS, log_levels,validate_command_facility @click_shell.shell(prompt='drunc-process-manager > ', chain=True, context_settings=CONTEXT_SETTINGS, hist_file=os.path.expanduser('~')+'/.drunc-pm-shell.history') @@ -9,31 +11,35 @@ @click.argument('process-manager-address', type=str, callback=validate_command_facility) @click.pass_context def process_manager_shell(ctx, process_manager_address:str, log_level:str) -> None: - from drunc.utils.utils import setup_logger - setup_logger(log_level) - + from drunc.utils.utils import setup_root_logger + setup_root_logger(log_level) + log = get_logger( + logger_name = "process_manager_shell", + rich_handler = True + ) + log.debug("Resetting the context instance address") ctx.obj.reset( address = process_manager_address, ) - from drunc.utils.grpc_utils import ServerUnreachable - + desc = None + log.info(f"Connecting to process_manager at address {process_manager_address}") try: import asyncio desc = asyncio.get_event_loop().run_until_complete( ctx.obj.get_driver('process_manager').describe() ) except ServerUnreachable as e: - ctx.obj.critical(f'Could not connect to the process manager') - raise e + log.critical(f'Could not connect to the process manager') + log.exception(e) + exit(1) - ctx.obj.info(f'{process_manager_address} is \'{desc.data.name}.{desc.data.session}\' (name.session), starting listening...') + log.info(f'{process_manager_address} is \'{desc.data.name}.{desc.data.session}\' (name.session), starting listening...') if desc.data.HasField('broadcast'): ctx.obj.start_listening(desc.data.broadcast) def cleanup(): ctx.obj.terminate() - ctx.call_on_close(cleanup) from drunc.process_manager.interface.commands import boot, terminate, kill, flush, logs, restart, ps, dummy_boot @@ -44,4 +50,6 @@ def cleanup(): ctx.command.add_command(logs, 'logs') ctx.command.add_command(restart, 'restart') ctx.command.add_command(ps, 'ps') - ctx.command.add_command(dummy_boot, 'dummy_boot') \ No newline at end of file + ctx.command.add_command(dummy_boot, 'dummy_boot') + + log.info("process_manager_shell ready") \ No newline at end of file diff --git a/src/drunc/process_manager/process_manager.py b/src/drunc/process_manager/process_manager.py index bcfbe84..aca09e9 100644 --- a/src/drunc/process_manager/process_manager.py +++ b/src/drunc/process_manager/process_manager.py @@ -26,10 +26,7 @@ class ProcessManager(abc.ABC, ProcessManagerServicer): def __init__(self, configuration:ProcessManagerConfHandler, name:str, session:str=None, **kwargs): super().__init__() - self.log = get_logger( - logger_name = f"{name}.{configuration.data.type._name_}_process_manager", - rich_handler = True - ) + self.log = get_logger(logger_name = f"process_manager.{configuration.data.type._name_}_process_manager") self.log.debug(pid_info_str()) self.log.debug("Initialized ProcessManager") @@ -491,7 +488,7 @@ def _get_process_uid(self, query:ProcessQuery, in_boot_request:bool=False) -> [s @staticmethod def get(conf, **kwargs): from drunc.utils.utils import get_logger - log = get_logger("process_manager.get", rich_handler = True) + log = get_logger("process_manager.get") if conf.data.type == ProcessManagerTypes.SSH: log.info(f'Starting [green]SSH process_manager[/green]', extra={'markup': True}) diff --git a/src/drunc/process_manager/process_manager_driver.py b/src/drunc/process_manager/process_manager_driver.py index fd27b7e..ab0a168 100644 --- a/src/drunc/process_manager/process_manager_driver.py +++ b/src/drunc/process_manager/process_manager_driver.py @@ -23,7 +23,7 @@ def __init__(self, address:str, token, **kwargs): token = token, **kwargs ) - self.log.debug(f"set up process_manager.driver") + self.log.debug("set up process_manager.driver") def create_stub(self, channel): from druncschema.process_manager_pb2_grpc import ProcessManagerStub diff --git a/src/drunc/unified_shell/shell.py b/src/drunc/unified_shell/shell.py index 504170b..ced68b1 100644 --- a/src/drunc/unified_shell/shell.py +++ b/src/drunc/unified_shell/shell.py @@ -103,7 +103,7 @@ def unified_shell( else: # user provided an address process_manager_address = process_manager.replace('grpc://', '') # remove the grpc scheme unified_shell_log.info(f"Connecting to process manager at \'{process_manager}\' at address [green]{process_manager_address}[/green]") - unified_shell_log.info(f"Unified shell connected to the process_manager") + unified_shell_log.info(f"process_manager connected to the unified_shell") ctx.obj.reset( address_pm = process_manager_address, @@ -127,7 +127,7 @@ def unified_shell( ctx.obj.session_name = session_name unified_shell_log.debug(f'{process_manager_address} is \'{desc.name}.{desc.session}\' (name.session), starting listening...') - unified_shell_log.info(f"process_manager listening...") + unified_shell_log.debug(f"process_manager listening...") if desc.HasField('broadcast'): ctx.obj.start_listening_pm( @@ -207,4 +207,4 @@ def cleanup(): ctx.command.add_command(exclude, 'exclude') ctx.command.add_command(wait, 'wait') - unified_shell_log.debug("Unified shell ready") \ No newline at end of file + unified_shell_log.info("unified_shell ready with process_manager and controller commands") \ No newline at end of file diff --git a/src/drunc/utils/utils.py b/src/drunc/utils/utils.py index 08f2988..551b2d9 100644 --- a/src/drunc/utils/utils.py +++ b/src/drunc/utils/utils.py @@ -36,7 +36,7 @@ def regex_match(regex, string): log_level = logging.INFO -def print_traceback(with_rich:bool=True): # RETURNTOME - make this false +def print_traceback(with_rich:bool=True): # RETURNTOME - rename to print_console_traceback if with_rich: from rich.console import Console c = Console() @@ -62,8 +62,8 @@ def setup_root_logger(stream_log_level:str) -> None: return stream_log_level = log_levels[stream_log_level] - logger.setLevel(stream_log_level) - for handler in logger.handlers: + root_logger.setLevel(stream_log_level) + for handler in root_logger.handlers: handler.setLevel(stream_log_level) # And then manually tweak 'sh.command' logger. Sigh. @@ -82,15 +82,16 @@ def setup_root_logger(stream_log_level:str) -> None: for handler in kafka_command_logger.handlers: handler.setLevel(kafka_command_level) -def get_logger(logger_name:str, log_file_path:str = None, log_file_log_level:str = None, rich_handler:bool = False, rich_log_level:str = None): +def get_logger(logger_name:str, log_file_path:str = None, log_file_log_level:str = None, override_log_file:bool = True, rich_handler:bool = False, rich_log_level:str = None): if logger_name == "": raise DruncSetupException("This was an attempt to set up the root logger `drunc`, this should be corrected to command `setup_root_logger`.") if "drunc" not in logging.Logger.manager.loggerDict: raise DruncSetupException("The root logger has not been initialized, exiting.") - if logger_name == "process_manager" and not log_file_path and not 'drunc.process_manager' in logging.Logger.manager.loggerDict: - raise DruncSetupException("process_manager setup requires a log path.") - if logger_name == "process_manager" and not rich_handler: - raise DruncSetupException("process_manager requires a rich handler.") + if logger_name == "process_manager" and not 'drunc.process_manager' in logging.Logger.manager.loggerDict: + if not log_file_path: + raise DruncSetupException("process_manager setup requires a log path.") + if not rich_handler: + raise DruncSetupException("process_manager requires a rich handler.") root_logger_level = logging.getLogger('drunc').level if not log_file_log_level: @@ -117,7 +118,7 @@ def get_logger(logger_name:str, log_file_path:str = None, log_file_log_level:str logger.debug(f"Logger {logger_name} already exists, not overwriting handlers") return logger - if log_file_path and os.path.isfile(log_file_path): + if override_log_file and log_file_path and os.path.isfile(log_file_path): os.remove(log_file_path) while logger.hasHandlers():