Skip to content

Commit

Permalink
fixed up
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiri Otoupal committed Mar 11, 2024
1 parent eeda947 commit 82b9f56
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion abst/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"CLI Command making OCI Bastion and kubernetes usage simple and fast"
)

__version__ = "2.3.40"
__version__ = "2.3.41"
__author__ = "Jiri Otoupal"
__author_email__ = "jiri-otoupal@ips-database.eu"
__license__ = "MIT"
Expand Down
10 changes: 7 additions & 3 deletions abst/bastion_support/bastion_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import rich
from click import clear
from rich import print
from rich.align import Align

from abst.bastion_support.oci_bastion import Bastion
from abst.config import default_stack_location, default_stack_contents, \
default_contexts_location
default_contexts_location, broadcast_shm_name
from abst.sharing.local_broadcast import LocalBroadcast
from abst.wrappers import load_stack_decorator


Expand Down Expand Up @@ -172,14 +174,16 @@ def kill_all(cls, a=None, b=None, c=None):
blist_copy = list(cls.session_list)
for i, sess in enumerate(blist_copy):
try:
print(f"Killing {sess.bid}")
rich.print(f"[red]Killing[/red] {sess.bid}")
Bastion.current_status = "deleting"
sess.kill()
Bastion.delete_bastion_session(sess.bid, sess.region)
except Exception:
print(f"Looks like Bastion is already deleted {sess}")
pass
finally:
print(f"Deleting {i + 1}/{len(blist_copy)}")
rich.print("[green]Closing shared memory[/green]")
LocalBroadcast(broadcast_shm_name).close()
exit(0)

@classmethod
Expand Down
6 changes: 2 additions & 4 deletions abst/bastion_support/oci_bastion.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_print_name(self):
return self.context_name if self.context_name else "default"

def kill(self):
print(f"Killing Bastion {self.get_print_name()} SSH Tunnel")
rich.print(f"[red]Killing Bastion {self.get_print_name()} SSH Tunnel[/red]")
try:
Bastion.stopped = True
self.active_tunnel.send_signal(signal.SIGTERM)
Expand All @@ -99,12 +99,10 @@ def kill(self):
except Exception:
print(f"Looks like Bastion is already deleted {self.get_print_name()}")
finally:
self.lb.close()
self.lb.delete_context(self.context_name)

@classmethod
def delete_bastion_session(cls, sess_id, region=None):
print("Removing Bastion session")

try:
config = oci.config.from_file()
if region:
Expand Down
10 changes: 9 additions & 1 deletion abst/cli_commands/ssh_cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def ssh_lin(port, name, debug):
lb = LocalBroadcast(broadcast_shm_name)
data = lb.retrieve_json()

if len(data.keys()) == 0:
rich.print("[yellow]No connected sessions[/yellow]")

if name and len(keys := filter_keys_by_substring(data, name)) == 1:
do_ssh(keys[0], data[keys[0]]["username"], data[keys[0]]["port"])
return
Expand All @@ -26,7 +29,12 @@ def ssh_lin(port, name, debug):
do_ssh(keys[0], data[keys[0]]["username"], data[keys[0]]["port"])
return

longest_key = max(len(key) for key in data.keys())
if (key_lens := [len(key) for key in data.keys()]) and len(key_lens) > 0:
longest_key = max(key_lens)
else:
rich.print(f"[yellow]No alive contexts found[/yellow]")
return

questions = [{"name": f"{key.ljust(longest_key)} |= status: {data['status']}", "value": (key, data)} for key, data
in data.items()]

Expand Down
19 changes: 14 additions & 5 deletions abst/sharing/local_broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,30 @@ def store_json(self, data: dict) -> int:
data_before = self.retrieve_json()
for key, value in data.items():
for s_key in value.keys():
if type(data_before[key][s_key]) == type(data[key][s_key]):
if data_before.get(key, None) is not None and data_before.get(key, None).get(s_key,
None) is not None and type(
data_before[key][s_key]) == type(data[key][s_key]):
data_before[key].pop(s_key)

data_copy = always_merger.merge(data, data_before)

serialized_data = json.dumps(data_copy).encode('utf-8')
serialized_data = self.__write_json(data_copy)
return len(serialized_data)

def __write_json(self, data: dict):
serialized_data = json.dumps(data).encode('utf-8')
if len(serialized_data) > self._size:
raise ValueError("Data exceeds allocated shared memory size.")

# Write the data length to the length shared memory
self._len_shm.buf[:8] = struct.pack('Q', len(serialized_data))

# Write data to the main shared memory
self._data_shm.buf[:len(serialized_data)] = serialized_data
return len(serialized_data)
return serialized_data

def delete_context(self, context: str):
data_before = self.retrieve_json()
data_before.pop(context, None)
self.__write_json(data_before)

def retrieve_json(self) -> dict:
"""
Expand Down

0 comments on commit 82b9f56

Please sign in to comment.