Skip to content

Commit

Permalink
Merge pull request #253 from bogdant36/CTX-6230
Browse files Browse the repository at this point in the history
CTX-6230: coretex node logs command implementation based on criteria …
  • Loading branch information
dule1322 authored Aug 21, 2024
2 parents 0067e65 + da35ce6 commit bce24ed
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
16 changes: 15 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 @@ -193,6 +194,18 @@ def status() -> None:
ui.stdEcho(f"Current status of node is {statusEcho}.")


@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.")
def logs(tail: Optional[int], follow: bool, timestamps: bool) -> None:
if not node_module.isRunning():
ui.errorEcho("There is no currently running Node on the machine.")
return

node_module.showLogs(tail, follow, timestamps)


@click.group()
@onBeforeCommandExecute(docker.isDockerAvailable)
@onBeforeCommandExecute(initializeUserSession)
Expand All @@ -207,3 +220,4 @@ def node() -> None:
node.add_command(update, "update")
node.add_command(config, "config")
node.add_command(status, "status")
node.add_command(logs, "logs")
4 changes: 4 additions & 0 deletions coretex/cli/modules/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ 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 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)

0 comments on commit bce24ed

Please sign in to comment.