Skip to content
Closed
11 changes: 7 additions & 4 deletions src/drunc/process_manager/interface/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from drunc.process_manager.interface.context import ProcessManagerContext
from drunc.process_manager.utils import tabulate_process_instance_list
from drunc.utils.shell_utils import InterruptedCommand, log_pm_cmd
from drunc.utils.utils import get_logger
from drunc.utils.utils import get_logger, resolve_context_peer


@click.command("boot")
Expand Down Expand Up @@ -45,11 +45,13 @@ def boot(
) -> None:
log = get_logger("process_manager.shell")
log_pm_cmd(obj)
processes = obj.get_driver("process_manager").ps(ProcessQuery(user=user))
processes = obj.get_driver("process_manager").ps(
ProcessQuery(user=user, session=session_name)
)

if len(processes.values) > 0:
click.confirm(
f"You already have {len(processes.values)} processes running, are you sure you want to boot a session?",
f"You already have {len(processes.values)} processes running for {session_name}, are you sure you want to boot a session?",
abort=True,
)
Comment on lines 52 to 56

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the process manager shell, I've modified this to basically check if someone wants to boot a session with the same name as another session, theres a click.confirm here.

Do we want to keep this as a confirm or do we want to outright disallow this behaviour?


Expand Down Expand Up @@ -78,6 +80,7 @@ def boot(
raise e

controller_address = obj.get_driver("process_manager").controller_address
controller_address = resolve_context_peer(controller_address)
if controller_address:
obj.print(
Panel(
Expand Down Expand Up @@ -286,7 +289,7 @@ def logs_impl(
log = get_logger("process_manager.shell")
# TODO: MOVE BACK TO DEBUG BEFORE MERGE
# THIS IS USEFUL FOR TESTING THOUGH
log.error(f"Running logs with query {query}")
log.debug(f"Running logs with query {query}")
log_req = LogRequest(
how_far=how_far,
query=query,
Expand Down
14 changes: 12 additions & 2 deletions src/drunc/process_manager/process_manager_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from collections.abc import Iterator
from time import sleep
from typing import Dict, List
from urllib.parse import urlparse

import conffwk
import grpc
Expand Down Expand Up @@ -287,7 +288,11 @@ def _build_boot_request(
override_logs: bool,
pwd: str,
) -> BootRequest:
host = format_hostname(app["restriction"])
# Run mapping to physical hostname to enable multi host usage
host = resolve_localhost_to_hostname(format_hostname(app["restriction"]))
self.log.info(f"boot resolve {host}") # keep this until big PR gets merged

# this is one of the two minimal changes needed to get this working in general?
name = app["name"]
exe = app["type"]
args = app["args"]
Expand Down Expand Up @@ -546,7 +551,12 @@ def _connect_to_service(
connection_port = session_dal.connectivity_service.service.port

if connection_server == "localhost":
resolved_server = resolve_localhost_to_hostname(connection_server)
# resolve localhost to hostname to enable multi host
resolved_address = resolve_localhost_to_hostname(connection_server)
if "://" not in resolved_address:
resolved_address = "grpc://" + resolved_address

resolved_server = urlparse(resolved_address).hostname
self.log.debug(
f"Resolved connection server 'localhost' to '{resolved_server}' to avoid K8s hairpinning."
)
Expand Down
2 changes: 1 addition & 1 deletion src/drunc/process_manager/ssh_process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def __boot(self, boot_request: BootRequest, uuid: str) -> ProcessInstance:
self.log.info(
f"Booted '{boot_request.process_description.metadata.name}' "
f"from session '{boot_request.process_description.metadata.session}' "
f"with UUID {uuid}"
f"with UUID {uuid} in hosts '{boot_request.process_restriction.allowed_hosts}"
)

# Query current process status
Expand Down
12 changes: 6 additions & 6 deletions src/drunc/unified_shell/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ def boot(
override_logs_boot = obj.override_logs
else:
override_logs_boot = override_logs
# if len(processes.values) > 0:
# log.error(
# f"Cannot boot: session {session_name} already has {len(processes.values)} processes running. "
# "Please terminate the existing session first."
# )
# return
if len(processes.values) > 0:
log.error(
f"Cannot boot: session {session_name} already has {len(processes.values)} processes running. "
"Please terminate the existing session first."
)
return

try:
results = obj.get_driver("process_manager").boot(
Expand Down
12 changes: 12 additions & 0 deletions src/drunc/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,18 @@ def resolve_context_peer(peer: str) -> str:
str: The original peer string, or a resolved ``host:port`` representation.
"""

if not peer:
return peer

# Some callers pass a plain host:port string without a transport prefix.
# Handle those directly instead of assuming the first token is always a transport.
if peer.startswith("[") or peer.count(":") == 1:
parsed = _parse_host_port(peer)
if parsed is not None:
host, port = parsed
resolved_host = _resolve_host(host)
return f"{resolved_host}:{port}"

match = re.match(r"^(?P<transport>[^:]+):(?P<address>.+)$", peer)
if not match:
return peer
Expand Down
6 changes: 4 additions & 2 deletions tests/process_manager/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
to be back in line with druncschema definitions.
"""

import socket

import google.protobuf.any_pb2
import pytest
from druncschema.description_pb2 import Description
Expand Down Expand Up @@ -36,7 +38,7 @@ def app_data():
Provides a mock application dictionary with required keys.
"""
return {
"restriction": "localhost",
"restriction": socket.gethostname(),
"name": "TestApp",
"type": "binary",
"args": ["--arg1"],
Expand All @@ -55,7 +57,7 @@ def bootrequest(app_data):
user="test_user",
session="session1",
name=app_data["name"],
hostname="localhost",
hostname=socket.gethostname(),
tree_id=app_data["tree_id"],
),
executable_and_arguments=[{"exec": "binary", "args": ["--arg1"]}],
Expand Down
5 changes: 3 additions & 2 deletions tests/process_manager/interface/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ def test_boot_exiting_processes_abort(boot_arguments):
# check that 'boot' was never called
mock_driver.boot.assert_not_called()

# conf-id-123 from session name in this file
assert (
"You already have 2 processes running, are you sure you want to boot a session?"
"You already have 2 processes running for conf-id-123, are you sure you want to boot a session?"
in result.output
)

Expand Down Expand Up @@ -267,7 +268,7 @@ def test_boot_exiting_processes_user_confirm(boot_arguments):
mock_driver.boot.assert_called()

assert (
"You already have 2 processes running, are you sure you want to boot a session?"
"You already have 2 processes running for conf-id-123, are you sure you want to boot a session?"
in result.output
)

Expand Down
7 changes: 7 additions & 0 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
now_str,
parent_death_pact,
regex_match,
resolve_context_peer,
resolve_localhost_and_127_ip_to_network_ip,
resolve_localhost_to_hostname,
validate_command_facility,
Expand Down Expand Up @@ -157,6 +158,12 @@ def test_resolve_localhost_and_127_ip_to_network_ip():
assert resolved == generate_address(this_ip)


def test_resolve_context_peer():
assert resolve_context_peer("grpc:np04-srv-028:50000") == "np04-srv-028:50000"
assert resolve_context_peer("np04-srv-028:50000") == "np04-srv-028:50000"
assert resolve_context_peer("") == ""


def test_host_is_local():
this_ip = socket.gethostbyname(socket.gethostname())
hostname = socket.gethostname()
Expand Down
Loading