From ecb01d266f7dd612d3c35c7fc1b465d995ad75a6 Mon Sep 17 00:00:00 2001 From: Pan Date: Fri, 23 Feb 2018 21:03:12 +0100 Subject: [PATCH] Set parallel client host to actual destination when using proxy for native client - resolves #120 --- Changelog.rst | 8 ++++++++ pssh/clients/native/parallel.py | 7 ++++--- pssh/clients/native/single.py | 11 +++++++--- setup.py | 3 +-- tests/test_native_parallel_client.py | 30 ++++++++++++++-------------- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/Changelog.rst b/Changelog.rst index f09c4815..da0e35dd 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -1,6 +1,14 @@ Change Log ============ +1.6.1 ++++++++ + +Fixes +------- + +* Host would always be `127.0.0.1` when using ``proxy_host`` on native client - #120. + 1.6.0 ++++++ diff --git a/pssh/clients/native/parallel.py b/pssh/clients/native/parallel.py index 984e2682..634019d2 100644 --- a/pssh/clients/native/parallel.py +++ b/pssh/clients/native/parallel.py @@ -316,12 +316,13 @@ def _make_ssh_client(self, host): if self.proxy_host is not None: tunnel = self._start_tunnel(host) _user, _port, _password, _pkey = self._get_host_config_values(host) - _host = host if self.proxy_host is None else '127.0.0.1' + proxy_host = None if self.proxy_host is None else '127.0.0.1' _port = _port if self.proxy_host is None else tunnel.listen_port self.host_clients[host] = SSHClient( - _host, user=_user, password=_password, port=_port, pkey=_pkey, + host, user=_user, password=_password, port=_port, pkey=_pkey, num_retries=self.num_retries, timeout=self.timeout, - allow_agent=self.allow_agent, retry_delay=self.retry_delay) + allow_agent=self.allow_agent, retry_delay=self.retry_delay, + proxy_host=proxy_host) def copy_file(self, local_file, remote_file, recurse=False, copy_args=None): """Copy local file to remote file in parallel diff --git a/pssh/clients/native/single.py b/pssh/clients/native/single.py index 79d6e93e..3ee46cd2 100644 --- a/pssh/clients/native/single.py +++ b/pssh/clients/native/single.py @@ -64,7 +64,8 @@ def __init__(self, host, num_retries=DEFAULT_RETRIES, retry_delay=RETRY_DELAY, allow_agent=True, timeout=None, - forward_ssh_agent=True): + forward_ssh_agent=True, + proxy_host=None): """:param host: Host name or IP to connect to. :type host: str :param user: User to connect as. Defaults to logged in user. @@ -93,6 +94,9 @@ def __init__(self, host, equivalent to `ssh -A` from the `ssh` command line utility. Defaults to True if not set. :type forward_ssh_agent: bool + :param proxy_host: Connection to host is via provided proxy host + and client should use self.proxy_host for connection attempts. + :type proxy_host: str """ self.host = host self.user = user if user else None @@ -111,7 +115,8 @@ def __init__(self, host, self.forward_ssh_agent = forward_ssh_agent self._forward_requested = False self.session = None - self._connect(self.host, self.port) + self._host = proxy_host if proxy_host else host + self._connect(self._host, self.port) THREAD_POOL.apply(self._init) def disconnect(self): @@ -139,7 +144,7 @@ def _connect_init_retry(self, retries): if not self.sock.closed: self.sock.close() sleep(self.retry_delay) - self._connect(self.host, self.port, retries=retries) + self._connect(self._host, self.port, retries=retries) return self._init(retries=retries) def _init(self, retries=1): diff --git a/setup.py b/setup.py index d89e47fe..5b083ff1 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ 'optimize.use_switch': True, 'wraparound': False, } -_embedded_lib = bool(os.environ.get('EMBEDDED_LIB', 1)) +_embedded_lib = bool(int(os.environ.get('EMBEDDED_LIB', 1))) cython_args = {'cython_directives': cython_directives, 'cython_compile_time_env': {'EMBEDDED_LIB': _embedded_lib}, @@ -66,7 +66,6 @@ **cython_args )] - package_data = {} if ON_WINDOWS: diff --git a/tests/test_native_parallel_client.py b/tests/test_native_parallel_client.py index 1a439502..35a5be99 100644 --- a/tests/test_native_parallel_client.py +++ b/tests/test_native_parallel_client.py @@ -1389,8 +1389,8 @@ def test_scp_send_dir(self): gevent.joinall(cmds, raise_error=True) self.assertTrue(os.path.isdir(remote_test_dir_abspath)) self.assertTrue(os.path.isfile(remote_file_abspath)) - remote_file_data = open(remote_file_abspath, 'r').readlines() - self.assertEqual(remote_file_data[0].strip(), test_file_data) + remote_file_data = open(remote_file_abspath, 'r').read() + self.assertEqual(remote_file_data.strip(), test_file_data) except Exception: raise finally: @@ -1496,19 +1496,19 @@ def test_scp_recv(self): shutil.rmtree(remote_test_path_abs) shutil.rmtree(local_copied_dir) - ## OpenSSHServer needs to run in its own thread for this test to work - ## Race conditions otherwise. - # - # def test_tunnel(self): - # proxy_host = '127.0.0.9' - # server = OpenSSHServer(listen_ip=proxy_host, port=self.port) - # server.start_server() - # client = ParallelSSHClient( - # [self.host], port=self.port, pkey=self.user_key, - # proxy_host=proxy_host, proxy_port=self.port, num_retries=1, - # proxy_pkey=self.user_key, - # timeout=2) - # client.join(client.run_command('echo me')) + # This is a unit test, no output is checked, due to race conditions + # with running server in same thread. + def test_tunnel(self): + proxy_host = '127.0.0.9' + server = OpenSSHServer(listen_ip=proxy_host, port=self.port) + server.start_server() + client = ParallelSSHClient( + [self.host], port=self.port, pkey=self.user_key, + proxy_host=proxy_host, proxy_port=self.port, num_retries=1, + proxy_pkey=self.user_key, + timeout=2) + output = client.run_command('echo me', stop_on_errors=False) + self.assertEqual(self.host, list(output.keys())[0]) # def test_proxy_remote_host_failure_timeout(self): # """Test that timeout setting is passed on to proxy to be used for the