Skip to content

Commit

Permalink
Take the logs for node connections out of the with clause (#23)
Browse files Browse the repository at this point in the history
* Take the logs for node connections out of the with clause

* Fix lint errors
  • Loading branch information
erl-hpe authored Aug 1, 2024
1 parent ff8d9a9 commit 5090bb3
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions vtds_cluster_kvm/private/api_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def non_cluster_network(self, network_name):
return network.get('non_cluster', False)


# pylint: disable=too-many-instance-attributes
class PrivateNodeConnection(NodeConnection):
"""Private implementation of the NodeConnection Cluster Layer API
Class.
Expand All @@ -226,6 +227,8 @@ def __init__(self, common, node_class, instance, remote_port):
self.loc_port = None
self.subprocess = None
self.blade_connection = None
self.out_log = None
self.err_log = None
self.options = [
'-o', 'NoHostAuthenticationForLocalhost=yes',
'-o', 'StrictHostKeyChecking=no',
Expand All @@ -249,6 +252,12 @@ def __exit__(
if self.subprocess:
self.subprocess.kill()
self.subprocess = None
if self.out_log is not None:
self.out_log.close()
self.out_log = None
if self.err_log is not None:
self.err_log.close()
self.err_log = None

def _connect(self):
"""Set up the port forwarding connection to the node.
Expand All @@ -274,32 +283,40 @@ def _connect(self):
with TCPServer((self.loc_ip, 0), None) as tmp:
self.loc_port = tmp.server_address[1]

with logfile(out_path) as out, logfile(err_path) as err:
# Not using 'with' for the Popen because the Popen
# object becomes part of this class instance for the
# duration of the class instance's life cycle. The
# instance itself is a context manager which will
# disconnect and destroy the Popen object when the
# context ends.
#
# pylint: disable=consider-using-with
cmd = [
'ssh',
'-L', "%s:%s:%s:%s" % (
self.loc_ip, str(self.loc_port),
node_ip, str(self.rem_port)
),
*self.options,
'-N',
'-p', str(ssh_port),
'-i', ssh_key_path,
"root@%s" % ssh_ip
]
self.subprocess = Popen(
cmd,
stdout=out, stderr=err,
text=True, encoding='UTF-8'
)
# Not using with for these files because they want to
# survive the return from the function.
#
# pylint: disable=consider-using-with
self.out_log = open(out_path, 'w', encoding='UTF-8')
# pylint: disable=consider-using-with
self.err_log = open(err_path, 'w', encoding='UTF-8')

# Not using 'with' for the Popen because the Popen
# object becomes part of this class instance for the
# duration of the class instance's life cycle. The
# instance itself is a context manager which will
# disconnect and destroy the Popen object when the
# context ends.
#
# pylint: disable=consider-using-with
cmd = [
'ssh',
'-L', "%s:%s:%s:%s" % (
self.loc_ip, str(self.loc_port),
node_ip, str(self.rem_port)
),
*self.options,
'-N',
'-vvv',
'-p', str(ssh_port),
'-i', ssh_key_path,
"root@%s" % ssh_ip
]
self.subprocess = Popen(
cmd,
stdout=self.out_log, stderr=self.err_log,
text=True, encoding='UTF-8'
)

# Wait for the tunnel to be established before returning.
retries = 60
Expand Down

0 comments on commit 5090bb3

Please sign in to comment.