Skip to content

Commit 9d0023d

Browse files
PawelPlesniakemmuhamm
authored andcommitted
WIP
1 parent f619adf commit 9d0023d

3 files changed

Lines changed: 78 additions & 21 deletions

File tree

src/drunc/process_manager/ssh_process_manager.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ def _ps_impl(self, query: ProcessQuery) -> ProcessInstanceList:
448448
boot_request_dict=self.boot_request,
449449
order_by="random",
450450
)
451+
if query.session:
452+
self.log.warning(
453+
f"{self.name} found {len(process_uuids)} processes matching {query} for ps"
454+
)
451455

452456
# Iterate through all processes matching the query
453457
for proc_uuid in process_uuids:
@@ -495,13 +499,24 @@ def _ps_impl(self, query: ProcessQuery) -> ProcessInstanceList:
495499
else:
496500
pi.remote_pid = remote_pid_result.reason
497501
ret += [pi]
498-
499-
return ProcessInstanceList(
502+
ret_fmt = ProcessInstanceList(
500503
name=self.name,
501504
token=None,
502505
values=ret,
503506
flag=ResponseFlag.EXECUTED_SUCCESSFULLY,
504507
)
508+
if query.session:
509+
self.log.critical(
510+
f"{self.name} returning {len(ret)} processes from ps query {query}"
511+
)
512+
self.log.critical(ret_fmt)
513+
self.log.critical(f"TEST: {ret_fmt=}")
514+
else:
515+
self.log.warning(
516+
f"{self.name} returning {len(ret)} processes from ps query {query}"
517+
)
518+
self.log.warning(ret)
519+
return ret_fmt
505520

506521
def _boot_impl(self, boot_request: BootRequest) -> ProcessInstanceList:
507522
self.log.debug(f"{self.name} running boot command")

src/drunc/unified_shell/commands.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from drunc.controller.interface.shell_utils import controller_setup
88
from drunc.exceptions import DruncSetupException
99
from drunc.process_manager.interface.context import ProcessManagerContext
10+
from drunc.process_manager.utils import tabulate_process_instance_list
1011
from drunc.unified_shell.context import UnifiedShellMode
1112
from drunc.utils.shell_utils import InterruptedCommand
1213
from drunc.utils.utils import get_logger
@@ -45,12 +46,12 @@ def boot(
4546
override_logs_boot = obj.override_logs
4647
else:
4748
override_logs_boot = override_logs
48-
if len(processes.values) > 0:
49-
log.error(
50-
f"Cannot boot: session {session_name} already has {len(processes.values)} processes running. "
51-
"Please terminate the existing session first."
52-
)
53-
return
49+
# if len(processes.values) > 0:
50+
# log.error(
51+
# f"Cannot boot: session {session_name} already has {len(processes.values)} processes running. "
52+
# "Please terminate the existing session first."
53+
# )
54+
# return
5455

5556
try:
5657
results = obj.get_driver("process_manager").boot(
@@ -124,6 +125,43 @@ def boot(
124125
sys.exit(1)
125126

126127

128+
@click.command("terminate")
129+
@click.pass_obj
130+
@click.pass_context
131+
def terminate(ctx, obj):
132+
"""
133+
Execute the process manager terminate command, but only do this for the current
134+
session
135+
"""
136+
137+
log = get_logger("unified_shell.terminate")
138+
session_query = ProcessQuery(session=ctx.obj.session_name)
139+
log.info(f"Terminating session [green]{ctx.obj.session_name}[/]")
140+
obj.get_driver("process_manager").kill(session_query)
141+
142+
143+
@click.command("ps")
144+
@click.pass_obj
145+
@click.pass_context
146+
def ps(ctx, obj):
147+
"""
148+
Execute the process manager terminate command, but only do this for the current
149+
session
150+
"""
151+
152+
log = get_logger("unified_shell.ps")
153+
session_query = ProcessQuery(session=ctx.obj.session_name)
154+
log.info(f"Listing session [green]{ctx.obj.session_name}[/]")
155+
results = obj.get_driver("process_manager").ps(session_query)
156+
obj.print(
157+
tabulate_process_instance_list(
158+
results, title=f"Processes running in session {ctx.obj.session_name}"
159+
),
160+
overflow="fold",
161+
soft_wrap=True,
162+
)
163+
164+
127165
@click.command("start-shell")
128166
@click.pass_obj
129167
@click.pass_context

src/drunc/unified_shell/shell.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@
5252
flush,
5353
kill,
5454
logs,
55-
ps,
55+
# ps,
5656
restart,
57-
terminate,
57+
# terminate,
5858
)
5959
from drunc.process_manager.interface.process_manager import run_pm
60-
from drunc.unified_shell.commands import boot, start_shell
60+
from drunc.unified_shell.commands import boot, ps, start_shell, terminate
6161
from drunc.unified_shell.context import UnifiedShellMode
6262
from drunc.unified_shell.shell_utils import generate_fsm_sequence_command
6363
from drunc.utils.configuration import ConfTypes, OKSKey
@@ -156,14 +156,14 @@ def unified_shell(
156156
ctx.obj.log.debug("Setting up the [green]unified_shell[/green] logger")
157157

158158
# Parse the process manager argument to determine if it's a config or an address
159-
unified_shell_log.critical(
159+
ctx.obj.log.critical(
160160
f"Parsing the process manager argument: {process_manager}"
161161
)
162162
process_manager_url: ParseResult = urlparse(process_manager)
163163
internal_pm: bool = True
164164
if process_manager_url.scheme == "grpc": # i.e. if it's an address
165165
internal_pm = False
166-
unified_shell_log.critical(
166+
ctx.obj.log.critical(
167167
f"Process manager argument parsed, internal_pm set to {internal_pm}"
168168
)
169169

@@ -178,7 +178,7 @@ def unified_shell(
178178
# )
179179
# sys.exit(1)
180180

181-
unified_shell_log.critical("TEST")
181+
ctx.obj.log.critical("TEST")
182182
# Setup configuration related context variables
183183
ctx.obj.configuration_file = f"oksconflibs:{configuration_file}"
184184
ctx.obj.configuration_id = configuration_id
@@ -267,7 +267,7 @@ def unified_shell(
267267
)
268268

269269
else: # Connect to an existing process manager at the provided address
270-
unified_shell_log.critical(
270+
ctx.obj.log.critical(
271271
"Connecting to an existing process manager at the provided address"
272272
)
273273
process_manager_address = process_manager.replace(
@@ -286,7 +286,7 @@ def unified_shell(
286286

287287
# Run a simple command (describe) to check the connection with the process manager
288288
desc: Description | None = None
289-
unified_shell_log.critical("Getting driver")
289+
ctx.obj.log.critical("Getting driver")
290290
try:
291291
desc = ctx.obj.get_driver().describe()
292292
except Exception as e:
@@ -313,6 +313,7 @@ def unified_shell(
313313
ctx.obj.pm_process.join()
314314

315315
sys.exit(1)
316+
ctx.obj.log.critical("Process manager described successfully")
316317

317318
# Broadcasting configuration if requested
318319
if desc.HasField("broadcast"):
@@ -323,18 +324,20 @@ def unified_shell(
323324

324325
# Add the unified shell Click commands to the CLI
325326
ctx.obj.log.debug("Adding [green]unified_shell[/green] commands")
326-
ctx.command.add_command(boot, "boot")
327-
ctx.obj.dynamic_commands.add("boot")
327+
unified_shell_commands = [boot, ps, terminate]
328+
for cmd in unified_shell_commands:
329+
ctx.command.add_command(cmd, format_name_for_cli(cmd.name))
330+
ctx.obj.dynamic_commands.add(format_name_for_cli(cmd.name))
328331

329332
# Add the process manager Click commands to the CLI
330333
ctx.obj.log.debug("Adding [green]process_manager[/green] commands")
331334
process_manager_commands: list[click.Command] = [
332335
kill,
333-
terminate,
336+
# terminate,
334337
flush,
335338
logs,
336339
restart,
337-
ps,
340+
# ps,
338341
]
339342
for cmd in process_manager_commands:
340343
ctx.command.add_command(cmd, format_name_for_cli(cmd.name))
@@ -475,7 +478,8 @@ def cleanup():
475478

476479
# Terminate any residual processes
477480
if ctx.obj.get_driver("process_manager"):
478-
ctx.obj.get_driver("process_manager").terminate()
481+
session_processes = ProcessQuery(session=ctx.obj.session_name)
482+
ctx.obj.get_driver("process_manager").kill(session_processes)
479483

480484
# Check if any processes are still running
481485
if (

0 commit comments

Comments
 (0)