From 8eabe7bf99060f71f866d330905fd14cc4b9c7e9 Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Tue, 20 Aug 2024 11:01:47 +0200 Subject: [PATCH 1/5] CTX-6230: coretex node logs command implementation based on criteria from task description. --- coretex/cli/commands/node.py | 15 ++++++++++++++- coretex/cli/modules/node.py | 4 ++++ coretex/configuration/user.py | 2 +- coretex/utils/docker.py | 14 ++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/coretex/cli/commands/node.py b/coretex/cli/commands/node.py index f5b2dcdd..52458920 100644 --- a/coretex/cli/commands/node.py +++ b/coretex/cli/commands/node.py @@ -26,7 +26,7 @@ from ..modules.utils import onBeforeCommandExecute, checkEnvironment from ..modules.update import activateAutoUpdate, getNodeStatus from ...utils import docker -from ...configuration import UserConfiguration, NodeConfiguration, InvalidConfiguration, ConfigurationNotFound +from ...configuration import NodeConfiguration, InvalidConfiguration, ConfigurationNotFound @click.command() @@ -179,6 +179,18 @@ def config(advanced: bool) -> None: activateAutoUpdate() +@click.command() +@click.option("--tail", "-n", type = int, help = "Shows N last logs.") +@click.option("--follow", "-f", is_flag = True, help = "Displays logs realtime.") +@click.option("--timestamps", "-t", is_flag = True, help = "Displays timestamps for logs.") +@click.option("--verbose", is_flag = True, help = "Displays debug logs.") +def logs(tail: Optional[int], follow: bool, timestamps: bool, verbose: bool) -> None: + if getNodeStatus() in [NodeStatus.inactive, NodeStatus.deleted]: + ui.errorEcho("There is no currently running Node on the machine.") + + node_module.showLogs(tail, follow, timestamps, verbose) + + @click.group() @onBeforeCommandExecute(docker.isDockerAvailable) @onBeforeCommandExecute(initializeUserSession) @@ -192,3 +204,4 @@ def node() -> None: node.add_command(stop, "stop") node.add_command(update, "update") node.add_command(config, "config") +node.add_command(logs, "logs") diff --git a/coretex/cli/modules/node.py b/coretex/cli/modules/node.py index ad2af94d..b5de7f9a 100644 --- a/coretex/cli/modules/node.py +++ b/coretex/cli/modules/node.py @@ -198,6 +198,10 @@ def shouldUpdate(image: str) -> bool: return True +def showLogs(tail: Optional[int], follow: bool, timestamps: bool, verbose: bool) -> None: + docker.getLogs(config_defaults.DOCKER_CONTAINER_NAME, tail, follow, timestamps, verbose) + + def registerNode( name: str, nodeMode: NodeMode, diff --git a/coretex/configuration/user.py b/coretex/configuration/user.py index b7a650dc..c81a82ec 100644 --- a/coretex/configuration/user.py +++ b/coretex/configuration/user.py @@ -117,7 +117,7 @@ def isTokenValid(self, tokenName: str) -> bool: return False try: - return datetime.now(timezone.utc) > decodeDate(tokenExpirationDate) + return datetime.now(timezone.utc) < decodeDate(tokenExpirationDate) except ValueError: return False diff --git a/coretex/utils/docker.py b/coretex/utils/docker.py index f7d6785d..1195b0cf 100644 --- a/coretex/utils/docker.py +++ b/coretex/utils/docker.py @@ -184,3 +184,17 @@ def getDockerSwapLimit() -> int: return getTotalSwapMemory() return int(swapLimit / 1024) + + +def getLogs(name: str, tail: Optional[int], follow: bool, timestamps: bool, verbose: bool) -> None: + runCommand = ["docker", "logs", name] + if isinstance(tail, int): + runCommand.extend(["--tail", str(tail)]) + + if timestamps: + runCommand.append("-t") + + if follow: + runCommand.append("-f") + + command(runCommand, ignoreStderr = verbose, ignoreStdout = verbose) From 08d3d1a6d9edbb0e9f303e61dc61a72718ee44d7 Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Tue, 20 Aug 2024 11:39:39 +0200 Subject: [PATCH 2/5] CTX-6230: Implemented rest of displying debug logs functionality. --- coretex/cli/commands/node.py | 10 ++++++++-- coretex/cli/modules/node.py | 17 +++++++++++++++-- coretex/utils/docker.py | 4 ++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/coretex/cli/commands/node.py b/coretex/cli/commands/node.py index 52458920..db54909d 100644 --- a/coretex/cli/commands/node.py +++ b/coretex/cli/commands/node.py @@ -16,6 +16,7 @@ # along with this program. If not, see . from typing import Optional +from pathlib import Path import click @@ -183,12 +184,17 @@ def config(advanced: bool) -> None: @click.option("--tail", "-n", type = int, help = "Shows N last logs.") @click.option("--follow", "-f", is_flag = True, help = "Displays logs realtime.") @click.option("--timestamps", "-t", is_flag = True, help = "Displays timestamps for logs.") -@click.option("--verbose", is_flag = True, help = "Displays debug logs.") +@click.option("--verbose", "-v", is_flag = True, help = "Displays debug logs.") def logs(tail: Optional[int], follow: bool, timestamps: bool, verbose: bool) -> None: + nodeConfig = NodeConfiguration.load() if getNodeStatus() in [NodeStatus.inactive, NodeStatus.deleted]: ui.errorEcho("There is no currently running Node on the machine.") - node_module.showLogs(tail, follow, timestamps, verbose) + if not verbose: + node_module.showLogs(tail, follow, timestamps) + return + + node_module.showDebugLogs(Path(nodeConfig.storagePath).joinpath("logs", "node")) @click.group() diff --git a/coretex/cli/modules/node.py b/coretex/cli/modules/node.py index b5de7f9a..53d2d227 100644 --- a/coretex/cli/modules/node.py +++ b/coretex/cli/modules/node.py @@ -198,8 +198,21 @@ def shouldUpdate(image: str) -> bool: return True -def showLogs(tail: Optional[int], follow: bool, timestamps: bool, verbose: bool) -> None: - docker.getLogs(config_defaults.DOCKER_CONTAINER_NAME, tail, follow, timestamps, verbose) +def showLogs(tail: Optional[int], follow: bool, timestamps: bool) -> None: + docker.getLogs(config_defaults.DOCKER_CONTAINER_NAME, tail, follow, timestamps) + + +def showDebugLogs(nodeLogsPath: Path) -> None: + logFiles = list(nodeLogsPath.glob('*')) + + if not logFiles: + ui.errorEcho("No log files found in the directory.") + return + + latestLogFile = max(logFiles, key = lambda file: file.stat().st_ctime) + + with latestLogFile.open("r") as nodeLogsFile: + ui.stdEcho(nodeLogsFile.read()) def registerNode( diff --git a/coretex/utils/docker.py b/coretex/utils/docker.py index 1195b0cf..8bd867fa 100644 --- a/coretex/utils/docker.py +++ b/coretex/utils/docker.py @@ -186,7 +186,7 @@ def getDockerSwapLimit() -> int: return int(swapLimit / 1024) -def getLogs(name: str, tail: Optional[int], follow: bool, timestamps: bool, verbose: bool) -> None: +def getLogs(name: str, tail: Optional[int], follow: bool, timestamps: bool) -> None: runCommand = ["docker", "logs", name] if isinstance(tail, int): runCommand.extend(["--tail", str(tail)]) @@ -197,4 +197,4 @@ def getLogs(name: str, tail: Optional[int], follow: bool, timestamps: bool, verb if follow: runCommand.append("-f") - command(runCommand, ignoreStderr = verbose, ignoreStdout = verbose) + command(runCommand) From 9d685bf6d3ffa61dcf52a2a7ec457febc157063f Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Tue, 20 Aug 2024 17:01:48 +0200 Subject: [PATCH 3/5] CTX-6230: Removing verbose per request. --- coretex/cli/commands/node.py | 9 ++------- coretex/cli/modules/node.py | 13 ------------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/coretex/cli/commands/node.py b/coretex/cli/commands/node.py index db54909d..57d4cad3 100644 --- a/coretex/cli/commands/node.py +++ b/coretex/cli/commands/node.py @@ -184,17 +184,12 @@ def config(advanced: bool) -> None: @click.option("--tail", "-n", type = int, help = "Shows N last logs.") @click.option("--follow", "-f", is_flag = True, help = "Displays logs realtime.") @click.option("--timestamps", "-t", is_flag = True, help = "Displays timestamps for logs.") -@click.option("--verbose", "-v", is_flag = True, help = "Displays debug logs.") -def logs(tail: Optional[int], follow: bool, timestamps: bool, verbose: bool) -> None: - nodeConfig = NodeConfiguration.load() +def logs(tail: Optional[int], follow: bool, timestamps: bool) -> None: if getNodeStatus() in [NodeStatus.inactive, NodeStatus.deleted]: ui.errorEcho("There is no currently running Node on the machine.") - - if not verbose: - node_module.showLogs(tail, follow, timestamps) return - node_module.showDebugLogs(Path(nodeConfig.storagePath).joinpath("logs", "node")) + node_module.showLogs(tail, follow, timestamps) @click.group() diff --git a/coretex/cli/modules/node.py b/coretex/cli/modules/node.py index 53d2d227..4d11a062 100644 --- a/coretex/cli/modules/node.py +++ b/coretex/cli/modules/node.py @@ -202,19 +202,6 @@ def showLogs(tail: Optional[int], follow: bool, timestamps: bool) -> None: docker.getLogs(config_defaults.DOCKER_CONTAINER_NAME, tail, follow, timestamps) -def showDebugLogs(nodeLogsPath: Path) -> None: - logFiles = list(nodeLogsPath.glob('*')) - - if not logFiles: - ui.errorEcho("No log files found in the directory.") - return - - latestLogFile = max(logFiles, key = lambda file: file.stat().st_ctime) - - with latestLogFile.open("r") as nodeLogsFile: - ui.stdEcho(nodeLogsFile.read()) - - def registerNode( name: str, nodeMode: NodeMode, From c297b8abf6cfa387b42fc3ca21abc9b3bae3ded0 Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Wed, 21 Aug 2024 11:28:21 +0200 Subject: [PATCH 4/5] CTX-6230: Discussion changes. --- coretex/cli/commands/node.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coretex/cli/commands/node.py b/coretex/cli/commands/node.py index 7f124aa4..a9a279e2 100644 --- a/coretex/cli/commands/node.py +++ b/coretex/cli/commands/node.py @@ -199,7 +199,7 @@ def status() -> None: @click.option("--follow", "-f", is_flag = True, help = "Displays logs realtime.") @click.option("--timestamps", "-t", is_flag = True, help = "Displays timestamps for logs.") def logs(tail: Optional[int], follow: bool, timestamps: bool) -> None: - if getNodeStatus() in [NodeStatus.inactive, NodeStatus.deleted]: + if not node_module.isRunning() or getNodeStatus() in [NodeStatus.inactive, NodeStatus.deleted]: ui.errorEcho("There is no currently running Node on the machine.") return From da35ce6e30bdf29ec830fd2ff91862f0b7d4950d Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Wed, 21 Aug 2024 11:48:11 +0200 Subject: [PATCH 5/5] CTX-6230: Discussion changes. --- coretex/cli/commands/node.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coretex/cli/commands/node.py b/coretex/cli/commands/node.py index a9a279e2..bf425b71 100644 --- a/coretex/cli/commands/node.py +++ b/coretex/cli/commands/node.py @@ -199,7 +199,7 @@ def status() -> None: @click.option("--follow", "-f", is_flag = True, help = "Displays logs realtime.") @click.option("--timestamps", "-t", is_flag = True, help = "Displays timestamps for logs.") def logs(tail: Optional[int], follow: bool, timestamps: bool) -> None: - if not node_module.isRunning() or getNodeStatus() in [NodeStatus.inactive, NodeStatus.deleted]: + if not node_module.isRunning(): ui.errorEcho("There is no currently running Node on the machine.") return