Skip to content

Commit 04bbf5a

Browse files
committed
Remove use of globals, and add debug logs.
1 parent faeb497 commit 04bbf5a

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

enterprise_gateway/services/processproxies/processproxy.py

+34-13
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ def __init__(self, kernel_manager, proxy_config):
399399
self.ip = None
400400
self.pid = 0
401401
self.pgid = 0
402+
self.remote_user = None
403+
self.remote_pwd = None
402404

403405
@abc.abstractmethod
404406
async def launch_process(self, kernel_cmd, **kwargs):
@@ -584,14 +586,14 @@ def _get_ssh_client(self, host):
584586
"""
585587
ssh = None
586588

587-
global remote_user
588-
global remote_pwd
589589
use_gss = os.getenv("EG_REMOTE_GSS_SSH", None)
590590
if remote_user is None:
591-
remote_pwd = os.getenv("EG_REMOTE_PWD") # this should use password-less ssh
592-
remote_user = os.getenv("EG_REMOTE_USER", getpass.getuser())
591+
self.remote_pwd = os.getenv(
592+
"EG_REMOTE_PWD"
593+
) # this should use password-less ssh
594+
self.remote_user = os.getenv("EG_REMOTE_USER", getpass.getuser())
593595

594-
if use_gss and (remote_pwd or remote_user):
596+
if use_gss and (self.remote_pwd or self.remote_user):
595597
warnings.warn(
596598
"Both `EG_REMOTE_GSS_SSH` and one of `EG_REMOTE_PWD` or `EG_REMOTE_USER` is set. "
597599
"Those options are mutually exclusive, you configuration may be incorrect. "
@@ -603,18 +605,23 @@ def _get_ssh_client(self, host):
603605
ssh.load_system_host_keys()
604606
host_ip = gethostbyname(host)
605607
if use_gss:
608+
self.log.debug("Connecting to remote host via GSS.")
606609
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
607610
ssh.connect(host_ip, port=ssh_port, gss_auth=True)
608611
else:
609612
ssh.set_missing_host_key_policy(paramiko.RejectPolicy())
610-
if remote_pwd:
613+
if self.remote_pwd:
614+
self.log.debug(
615+
"Connecting to remote host with username and password."
616+
)
611617
ssh.connect(
612618
host_ip,
613619
port=ssh_port,
614-
username=remote_user,
615-
password=remote_pwd,
620+
username=self.remote_user,
621+
password=self.remote_pwd,
616622
)
617623
else:
624+
self.log.debug("Connecting to remote host with ssh key.")
618625
ssh.connect(host_ip, port=ssh_port, username=remote_user)
619626
except Exception as e:
620627
http_status_code = 500
@@ -1284,12 +1291,23 @@ def _send_listener_request(self, request, shutdown_socket=False):
12841291
sock.shutdown(SHUT_WR)
12851292
except Exception as e2:
12861293
if isinstance(e2, OSError) and e2.errno == errno.ENOTCONN:
1287-
pass # Listener is not connected. This is probably a follow-on to ECONNREFUSED on connect
1294+
# Listener is not connected. This is probably a follow-on to ECONNREFUSED on connect
1295+
self.log.debug(
1296+
"ERROR: OSError(ENOTCONN) raised on socket shutdown, "
1297+
"listener likely not connected. Cannot send {request}",
1298+
request=request,
1299+
)
12881300
else:
12891301
self.log.warning("Exception occurred attempting to shutdown communication socket to {}:{} "
12901302
"for KernelID '{}' (ignored): {}".format(self.comm_ip, self.comm_port,
12911303
self.kernel_id, str(e2)))
12921304
sock.close()
1305+
else:
1306+
self.log.debug(
1307+
"Invalid comm port, not sending request '{}' to comm_port '{}'.",
1308+
request,
1309+
self.comm_port,
1310+
)
12931311

12941312
def send_signal(self, signum):
12951313
"""
@@ -1303,17 +1321,20 @@ def send_signal(self, signum):
13031321
# using anything other than the socket-based signal (via signal_addr) will not work.
13041322

13051323
if self.comm_port > 0:
1306-
signal_request = dict()
1307-
signal_request['signum'] = signum
13081324

13091325
try:
1310-
self._send_listener_request(signal_request)
1326+
self._send_listener_request({"signum": signum})
13111327

13121328
if signum > 0: # Polling (signum == 0) is too frequent
13131329
self.log.debug("Signal ({}) sent via gateway communication port.".format(signum))
13141330
return None
13151331
except Exception as e:
1316-
if isinstance(e, OSError) and e.errno == errno.ECONNREFUSED: # Return False since there's no process.
1332+
if (
1333+
isinstance(e, OSError) and e.errno == errno.ECONNREFUSED
1334+
): # Return False since there's no process.
1335+
self.log.debug(
1336+
"ERROR: ECONNREFUSED, no process listening, cannot send signal."
1337+
)
13171338
return False
13181339

13191340
self.log.warning("An unexpected exception occurred sending signal ({}) for KernelID '{}': {}"

0 commit comments

Comments
 (0)