Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTX-6230: coretex node logs command implementation based on criteria … #253

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion coretex/cli/commands/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from typing import Optional
from pathlib import Path

import click

Expand All @@ -26,7 +27,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()
Expand Down Expand Up @@ -179,6 +180,23 @@ 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", "-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]:
dule1322 marked this conversation as resolved.
Show resolved Hide resolved
ui.errorEcho("There is no currently running Node on the machine.")

if not verbose:
node_module.showLogs(tail, follow, timestamps)
return
dule1322 marked this conversation as resolved.
Show resolved Hide resolved

node_module.showDebugLogs(Path(nodeConfig.storagePath).joinpath("logs", "node"))


@click.group()
@onBeforeCommandExecute(docker.isDockerAvailable)
@onBeforeCommandExecute(initializeUserSession)
Expand All @@ -192,3 +210,4 @@ def node() -> None:
node.add_command(stop, "stop")
node.add_command(update, "update")
node.add_command(config, "config")
node.add_command(logs, "logs")
17 changes: 17 additions & 0 deletions coretex/cli/modules/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ def shouldUpdate(image: str) -> bool:
return True


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:
dule1322 marked this conversation as resolved.
Show resolved Hide resolved
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)
dule1322 marked this conversation as resolved.
Show resolved Hide resolved

with latestLogFile.open("r") as nodeLogsFile:
ui.stdEcho(nodeLogsFile.read())


def registerNode(
name: str,
nodeMode: NodeMode,
Expand Down
2 changes: 1 addition & 1 deletion coretex/configuration/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions coretex/utils/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,17 @@ def getDockerSwapLimit() -> int:
return getTotalSwapMemory()

return int(swapLimit / 1024)


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)])

if timestamps:
runCommand.append("-t")

if follow:
runCommand.append("-f")

command(runCommand)
Loading