-
Notifications
You must be signed in to change notification settings - Fork 14
Integrate logging to application/features/. #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IreneLime
wants to merge
10
commits into
junhaoliao:main
Choose a base branch
from
IreneLime:add-log-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
49c74e1
Integrate logging to application/features/.
IreneLime a4e548a
Change string formatting to replace f-strings
IreneLime 1a2c832
Log exceptions
IreneLime 28179e8
Add new line after each file
IreneLime ec6cf58
Enhancing logging messages to be more specific.
IreneLime 8c1a1d8
Remove potentially sensitive information
IreneLime 3d0469b
Update import libraries for consistency
IreneLime 269fd83
Ensure consistency among format of logs
IreneLime ffb8fc4
Modify log string formatting
IreneLime 3bccae0
Remove potentially sensitive information
IreneLime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,27 +27,30 @@ | |
import paramiko | ||
import select | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class ForwardServerHandler(socketserver.BaseRequestHandler): | ||
def handle(self): | ||
junhaoliao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.server: ForwardServer | ||
try: | ||
logger.debug("Connection: Open forward server channel") | ||
chan = self.server.ssh_transport.open_channel( | ||
"direct-tcpip", | ||
("127.0.0.1", self.server.chain_port), | ||
self.request.getpeername(), | ||
) | ||
except Exception as e: | ||
logger.exception("Connection: Incoming request to 127.0.0.1:%d failed", self.server.chain_port) | ||
return False, "Incoming request to %s:%d failed: %s" % ( | ||
"127.0.0.1", self.server.chain_port, repr(e)) | ||
|
||
print( | ||
"Connected! Tunnel open %r -> %r -> %r" | ||
% ( | ||
self.request.getpeername(), | ||
chan.getpeername(), | ||
("127.0.0.1", self.server.chain_port), | ||
) | ||
logger.info( | ||
"Connected! Tunnel open %r -> %r -> %r", | ||
self.request.getpeername(), | ||
chan.getpeername(), | ||
("127.0.0.1", self.server.chain_port), | ||
) | ||
|
||
try: | ||
|
@@ -64,13 +67,15 @@ def handle(self): | |
break | ||
self.request.send(data) | ||
except Exception as e: | ||
print(e) | ||
logger.exception("Connection: Error occurred during data transfer") | ||
|
||
try: | ||
logger.debug("Connection: Close forward server channel") | ||
chan.close() | ||
self.server.shutdown() | ||
except Exception as e: | ||
print(e) | ||
IreneLime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.exception("Connection: Close forward server channel failed") | ||
|
||
|
||
class ForwardServer(socketserver.ThreadingTCPServer): | ||
|
@@ -102,6 +107,9 @@ def __del__(self): | |
def _client_connect(self, client: paramiko.SSHClient, | ||
host, username, | ||
password=None, key_filename=None, private_key_str=None): | ||
if self._jump_channel is not None: | ||
logger.debug("Connection: Connection initialized through Jump Channel") | ||
logger.debug("Connection: Connecting to %s@%s", username, host) | ||
if password is not None: | ||
client.connect(host, username=username, password=password, timeout=15, sock=self._jump_channel) | ||
elif key_filename is not None: | ||
|
@@ -128,23 +136,26 @@ def _init_jump_channel(self, host, username, **auth_methods): | |
|
||
self._jump_client = paramiko.SSHClient() | ||
self._jump_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | ||
logger.debug("Connection: Initialize Jump Client for connection to %[email protected]", username) | ||
self._client_connect(self._jump_client, 'remote.ecf.utoronto.ca', username, **auth_methods) | ||
logger.debug("Connection: Open Jump channel connection to %s at port 22", host) | ||
self._jump_channel = self._jump_client.get_transport().open_channel('direct-tcpip', | ||
(host, 22), | ||
('127.0.0.1', 22)) | ||
|
||
def connect(self, host: str, username: str, **auth_methods): | ||
junhaoliao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try: | ||
logger.debug("Connection: Connection attempt to %s@%s", username, host) | ||
self._init_jump_channel(host, username, **auth_methods) | ||
self._client_connect(self.client, host, username, **auth_methods) | ||
except Exception as e: | ||
# raise e | ||
# print('Connection::connect() exception:') | ||
logger.exception("Connection: Connection attempt to %s@%s failed", username, host) | ||
return False, str(e) | ||
|
||
self.host = host | ||
self.username = username | ||
|
||
logger.debug("Connection: Successfully connected to %s@%s", username, host) | ||
return True, '' | ||
|
||
@staticmethod | ||
|
@@ -160,9 +171,11 @@ def ssh_keygen(key_filename=None, key_file_obj=None, public_key_comment=''): | |
|
||
# save the private key | ||
if key_filename is not None: | ||
logger.debug("Connection: RSA SSH private key written to %s", key_filename) | ||
rsa_key.write_private_key_file(key_filename) | ||
elif key_file_obj is not None: | ||
rsa_key.write_private_key(key_file_obj) | ||
logger.debug("Connection: RSA SSH private key written to %s", key_file_obj) | ||
else: | ||
raise ValueError('Neither key_filename nor key_file_obj is provided.') | ||
|
||
|
@@ -192,6 +205,7 @@ def save_keys(self, key_filename=None, key_file_obj=None, public_key_comment='') | |
"mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '%s' >> ~/.ssh/authorized_keys" % pub_key) | ||
if exit_status != 0: | ||
return False, "Connection::save_keys: unable to save public key; Check for disk quota and permissions with any conventional SSH clients. " | ||
logger.debug("Connection: Public ssh key saved to remove server ~/.ssh/authorized_keys") | ||
|
||
return True, "" | ||
|
||
|
@@ -217,22 +231,30 @@ def exec_command_blocking_large(self, command): | |
return '\n'.join(stdout) + '\n' + '\n'.join(stderr) | ||
|
||
def _port_forward_thread(self, local_port, remote_port): | ||
logger.debug("Connection: Port forward thread started") | ||
forward_server = ForwardServer(("", local_port), ForwardServerHandler) | ||
|
||
forward_server.ssh_transport = self.client.get_transport() | ||
forward_server.chain_port = remote_port | ||
|
||
forward_server.serve_forever() | ||
forward_server.server_close() | ||
logger.debug("Connection: Port forward thread ended") | ||
|
||
def port_forward(self, *args): | ||
forwarding_thread = threading.Thread(target=self._port_forward_thread, args=args) | ||
forwarding_thread.start() | ||
|
||
def is_eecg(self): | ||
return 'eecg' in self.host | ||
if 'eecg' in self.host: | ||
logger.debug("Connection: Target host is eecg") | ||
return True | ||
|
||
return False | ||
|
||
def is_ecf(self): | ||
if 'ecf' in self.host: | ||
logger.debug("Connection: Target host is ecf") | ||
return 'ecf' in self.host | ||
IreneLime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def is_uoft(self): | ||
|
@@ -256,6 +278,9 @@ def is_load_high(self): | |
|
||
my_pts_count = len(output) - 1 # -1: excluding the `uptime` output | ||
|
||
logger.debug("Connection: pts count: %d; my pts count: %d", pts_count, my_pts_count) | ||
logger.debug("Connection: load sum: %d", load_sum) | ||
|
||
if pts_count > my_pts_count: # there are more terminals than mine | ||
return True | ||
elif load_sum > 1.0: | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.