Network connection backports (#37529)

* Close & remove paramiko connection where appropriate (#37528)

* Update unit test

(cherry picked from commit 594840c1d6)

* Put back $PATH checking in ansible-connection call (#37933)

(cherry picked from commit 169209c32a)
pull/38347/head
Nathaniel Case 7 years ago committed by GitHub
parent 5cf1420251
commit 50c971df43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -858,9 +858,21 @@ class TaskExecutor:
master, slave = pty.openpty()
python = sys.executable
# Assume ansible-connection is in the same dir as sys.argv[0]
ansible_connection = os.path.join(os.path.dirname(sys.argv[0]), 'ansible-connection')
p = subprocess.Popen([python, ansible_connection, to_text(os.getppid())], stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def find_file_in_path(filename):
# Check $PATH first, followed by same directory as sys.argv[0]
paths = os.environ['PATH'].split(os.pathsep) + [os.path.dirname(sys.argv[0])]
for dirname in paths:
fullpath = os.path.join(dirname, filename)
if os.path.isfile(fullpath):
return fullpath
raise AnsibleError("Unable to find location of '%s'" % filename)
p = subprocess.Popen(
[python, find_file_in_path('ansible-connection'), to_text(os.getppid())],
stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
stdin = os.fdopen(master, 'wb', 0)
os.close(slave)

@ -370,8 +370,11 @@ class Connection(ConnectionBase):
self._ssh_shell.close()
self._ssh_shell = None
display.debug("cli session is now closed")
self.paramiko_conn.close()
self.paramiko_conn = None
display.debug("ssh connection has been closed successfully")
self._connected = False
display.debug("ssh connection has been closed successfully")
def receive(self, command=None, prompts=None, answer=None, newline=True, prompt_retry_check=False):
'''

@ -78,9 +78,21 @@ class Connection(ConnectionBase):
master, slave = pty.openpty()
python = sys.executable
# Assume ansible-connection is in the same dir as sys.argv[0]
ansible_connection = os.path.join(os.path.dirname(sys.argv[0]), 'ansible-connection')
p = subprocess.Popen([python, ansible_connection, to_text(os.getppid())], stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def find_file_in_path(filename):
# Check $PATH first, followed by same directory as sys.argv[0]
paths = os.environ['PATH'].split(os.pathsep) + [os.path.dirname(sys.argv[0])]
for dirname in paths:
fullpath = os.path.join(dirname, filename)
if os.path.isfile(fullpath):
return fullpath
raise AnsibleError("Unable to find location of '%s'" % filename)
p = subprocess.Popen(
[python, find_file_in_path('ansible-connection'), to_text(os.getppid())],
stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
stdin = os.fdopen(master, 'wb', 0)
os.close(slave)

@ -93,11 +93,13 @@ class TestConnectionClass(unittest.TestCase):
terminal = MagicMock(supports_multiplexing=False)
conn._terminal = terminal
conn._ssh_shell = MagicMock()
conn.paramiko_conn = MagicMock()
conn._connected = True
conn.close()
self.assertTrue(terminal.on_close_shell.called)
self.assertIsNone(conn._ssh_shell)
self.assertIsNone(conn.paramiko_conn)
@patch("ansible.plugins.connection.paramiko_ssh.Connection._connect")
def test_network_cli_exec_command(self, mocked_super):

Loading…
Cancel
Save