From 9713762d78ad8192689e60507e610e43beaf95dd Mon Sep 17 00:00:00 2001 From: Jiri Otoupal Date: Mon, 18 Mar 2024 10:38:26 +0100 Subject: [PATCH] fix display bug --- abst/__version__.py | 2 +- abst/bastion_support/oci_bastion.py | 13 +++++++++---- abst/cli_commands/create_cli/commands.py | 1 + abst/utils/misc_funcs.py | 24 +++++++++++++++++++++++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/abst/__version__.py b/abst/__version__.py index 93a12d3..138d9c0 100644 --- a/abst/__version__.py +++ b/abst/__version__.py @@ -10,7 +10,7 @@ "CLI Command making OCI Bastion and kubernetes usage simple and fast" ) -__version__ = "2.3.52" +__version__ = "2.3.53" __author__ = "Jiri Otoupal" __author_email__ = "jiri-otoupal@ips-database.eu" __license__ = "MIT" diff --git a/abst/bastion_support/oci_bastion.py b/abst/bastion_support/oci_bastion.py index 7bbbb15..6b64ed4 100644 --- a/abst/bastion_support/oci_bastion.py +++ b/abst/bastion_support/oci_bastion.py @@ -18,6 +18,7 @@ default_contexts_location, default_conf_path, \ default_conf_contents, get_public_key, default_parallel_sets_location, broadcast_shm_name from abst.sharing.local_broadcast import LocalBroadcast +from abst.utils.misc_funcs import run_once from abst.wrappers import mark_on_exit @@ -678,10 +679,7 @@ def __run_ssh_tunnel(self, ssh_tunnel_arg_str, shell, already_split=False): self.connected = False return False if "pledge:" in line: - rich.print(f"({self.get_print_name()}) Success !") - rich.print( - f"({self.get_print_name()}) SSH Tunnel Running from " - f"{datetime.datetime.now()}") + self.print_succeeded() self.connected = True self.current_status = "connected" self.tries = 10 @@ -715,3 +713,10 @@ def __run_ssh_tunnel(self, ssh_tunnel_arg_str, shell, already_split=False): self.current_status = f"failed {self.tries} left" self.connected = False return True + + @run_once + def print_succeeded(self): + rich.print(f"({self.get_print_name()}) Success !") + rich.print( + f"({self.get_print_name()}) SSH Tunnel Running from " + f"{datetime.datetime.now()}") diff --git a/abst/cli_commands/create_cli/commands.py b/abst/cli_commands/create_cli/commands.py index 133f339..7701966 100644 --- a/abst/cli_commands/create_cli/commands.py +++ b/abst/cli_commands/create_cli/commands.py @@ -91,5 +91,6 @@ def managed(shell, debug, context_name): bastion.kill() exit(0) + _do.add_command(forward) _do.add_command(managed) diff --git a/abst/utils/misc_funcs.py b/abst/utils/misc_funcs.py index 97c5a42..c22209e 100644 --- a/abst/utils/misc_funcs.py +++ b/abst/utils/misc_funcs.py @@ -3,6 +3,9 @@ import os import signal import subprocess +import threading +from datetime import datetime, timedelta +from functools import wraps from pathlib import Path from threading import Thread from time import sleep @@ -11,7 +14,6 @@ import rich from rich.logging import RichHandler -from abst.bastion_support.oci_bastion import Bastion from abst.config import default_contexts_location, default_parallel_sets_location @@ -34,6 +36,7 @@ def setup_debug(debug): def print_eligible(searched: str): + from abst.bastion_support.oci_bastion import Bastion contexts = Bastion.get_contexts() rich.print("[bold]Configured contexts:[/bold]") for key, context in contexts.items(): @@ -169,3 +172,22 @@ def exit_gracefully(bastion, signum, frame): if bastion is not None: bastion.kill() exit(0) + + +def run_once(func): + """ + A decorator that runs a function only once. + """ + + func_last_run = threading.local() + seconds = 10 + + @wraps(func) + def wrapper(*args, **kwargs): + now = datetime.now() + last_run = getattr(func_last_run, 'last_run', None) + if last_run is None or now - last_run >= timedelta(seconds=seconds): + func_last_run.last_run = now + return func(*args, **kwargs) + + return wrapper