def _run_ssh(self, host, cmd_list, check_exit_code=False): command = ' '.join(pipes.quote(cmd_arg) for cmd_arg in cmd_list) connection = self.ssh_connections.get(host) if connection is None: ssh_name = self.configuration.maprfs_ssh_name password = self.configuration.maprfs_ssh_pw private_key = self.configuration.maprfs_ssh_private_key remote_ssh_port = self.configuration.maprfs_ssh_port ssh_conn_timeout = self.configuration.ssh_conn_timeout min_size = self.configuration.ssh_min_pool_conn max_size = self.configuration.ssh_max_pool_conn ssh_pool = utils.SSHPool(host, remote_ssh_port, ssh_conn_timeout, ssh_name, password=password, privatekey=private_key, min_size=min_size, max_size=max_size) ssh = ssh_pool.create() self.ssh_connections[host] = (ssh_pool, ssh) else: ssh_pool, ssh = connection if not ssh.get_transport().is_active(): ssh_pool.remove(ssh) ssh = ssh_pool.create() self.ssh_connections[host] = (ssh_pool, ssh) return processutils.ssh_execute( ssh, command, check_exit_code=check_exit_code)
def _run_ssh(self, host, cmd_list, check_exit_code=True): command = ' '.join(pipes.quote(cmd_arg) for cmd_arg in cmd_list) if not self.sshpool: gpfs_ssh_login = self.configuration.gpfs_ssh_login password = self.configuration.gpfs_ssh_password privatekey = self.configuration.gpfs_ssh_private_key gpfs_ssh_port = self.configuration.gpfs_ssh_port ssh_conn_timeout = self.configuration.ssh_conn_timeout min_size = self.configuration.ssh_min_pool_conn max_size = self.configuration.ssh_max_pool_conn self.sshpool = utils.SSHPool(host, gpfs_ssh_port, ssh_conn_timeout, gpfs_ssh_login, password=password, privatekey=privatekey, min_size=min_size, max_size=max_size) try: with self.sshpool.item() as ssh: return processutils.ssh_execute( ssh, command, check_exit_code=check_exit_code) except Exception as e: with excutils.save_and_reraise_exception(): msg = (_('Error running SSH command: %(cmd)s. ' 'Error: %(excmsg)s.'), { 'cmd': command, 'excmsg': six.text_type(e) }) LOG.error(msg) raise exception.GPFSException(msg)
def _ssh_exec(self, server, command, check_exit_code=True): connection = self.ssh_connections.get(server['instance_id']) ssh_conn_timeout = self.configuration.ssh_conn_timeout if not connection: ssh_pool = utils.SSHPool(server['ip'], 22, ssh_conn_timeout, server['username'], server.get('password'), server.get('pk_path'), max_size=1) ssh = ssh_pool.create() self.ssh_connections[server['instance_id']] = (ssh_pool, ssh) else: ssh_pool, ssh = connection if not ssh.get_transport().is_active(): ssh_pool.remove(ssh) ssh = ssh_pool.create() self.ssh_connections[server['instance_id']] = (ssh_pool, ssh) # (aovchinnikov): ssh_execute does not behave well when passed # parameters with spaces. wrap = lambda token: "\"" + token + "\"" command = [wrap(tkn) if tkn.count(' ') else tkn for tkn in command] return processutils.ssh_execute(ssh, ' '.join(command), check_exit_code=check_exit_code)
def test_closed_reopend_ssh_connections(self): with mock.patch.object(paramiko, "SSHClient", mock.Mock(return_value=FakeSSHClient())): sshpool = utils.SSHPool("127.0.0.1", 22, 10, "test", password="******", min_size=1, max_size=2) with sshpool.item() as ssh: first_id = ssh.id with sshpool.item() as ssh: second_id = ssh.id # Close the connection and test for a new connection ssh.get_transport().active = False self.assertEqual(first_id, second_id) paramiko.SSHClient.assert_called_once_with() # Expected new ssh pool with mock.patch.object(paramiko, "SSHClient", mock.Mock(return_value=FakeSSHClient())): with sshpool.item() as ssh: third_id = ssh.id self.assertNotEqual(first_id, third_id) paramiko.SSHClient.assert_called_once_with()
def test_create_ssh_with_nothing(self): fake_ssh_client = mock.Mock() ssh_pool = utils.SSHPool("127.0.0.1", 22, 10, "test") with mock.patch.object(paramiko, "SSHClient", return_value=fake_ssh_client): ssh_pool.create() fake_ssh_client.connect.assert_called_once_with( "127.0.0.1", port=22, username="******", password=None, key_filename=None, look_for_keys=True, timeout=10)
def test_create_ssh_error_connecting(self): attrs = {'connect.side_effect': paramiko.SSHException, } fake_ssh_client = mock.Mock(**attrs) ssh_pool = utils.SSHPool("127.0.0.1", 22, 10, "test") with mock.patch.object(paramiko, "SSHClient", return_value=fake_ssh_client): self.assertRaises(exception.SSHException, ssh_pool.create) fake_ssh_client.connect.assert_called_once_with( "127.0.0.1", port=22, username="******", password=None, key_filename=None, look_for_keys=True, timeout=10)
def test_sshpool_remove(self, mock_isfile, mock_sshclient, mock_open): ssh_to_remove = mock.Mock() mock_sshclient.side_effect = [mock.Mock(), ssh_to_remove, mock.Mock()] sshpool = utils.SSHPool("127.0.0.1", 22, 10, "test", password="******", min_size=3, max_size=3) self.assertIn(ssh_to_remove, list(sshpool.free_items)) sshpool.remove(ssh_to_remove) self.assertNotIn(ssh_to_remove, list(sshpool.free_items))
def _init_connect(self): if not (self.sshpool and self.ssh): self.sshpool = manila_utils.SSHPool(ip=self.nas_ip, port=self.port, conn_timeout=None, login=self.username, password=self.password, privatekey=self.ssh_key) self.ssh = self.sshpool.create() if not self.ssh.get_transport().is_active(): self.sshpool = manila_utils.SSHPool(ip=self.nas_ip, port=self.port, conn_timeout=None, login=self.username, password=self.password, privatekey=self.ssh_key) self.ssh = self.sshpool.create() LOG.debug('NAScmd [%s@%s] start!', self.username, self.nas_ip)
def test_create_ssh_with_key(self): path_to_private_key = "/fakepath/to/privatekey" fake_ssh_client = mock.Mock() ssh_pool = utils.SSHPool("127.0.0.1", 22, 10, "test", privatekey="/fakepath/to/privatekey") with mock.patch.object(paramiko, "SSHClient", return_value=fake_ssh_client): ssh_pool.create() fake_ssh_client.connect.assert_called_once_with( "127.0.0.1", port=22, username="******", password=None, key_filename=path_to_private_key, look_for_keys=False, timeout=10)
def __init__(self, configuration, debug=True): super(SSHConnector, self).__init__() self.storage_ip = configuration.emc_nas_server self.username = configuration.emc_nas_login self.password = configuration.emc_nas_password self.debug = debug self.sshpool = utils.SSHPool(ip=self.storage_ip, port=22, conn_timeout=None, login=self.username, password=self.password)
def test_single_ssh_connect(self): with mock.patch.object(paramiko, "SSHClient", mock.Mock(return_value=FakeSSHClient())): sshpool = utils.SSHPool("127.0.0.1", 22, 10, "test", password="******", min_size=1, max_size=1) with sshpool.item() as ssh: first_id = ssh.id with sshpool.item() as ssh: second_id = ssh.id self.assertEqual(first_id, second_id) paramiko.SSHClient.assert_called_once_with()
def test_sshpool_remove_object_not_in_pool(self, mock_isfile, mock_sshclient, mock_open): # create an SSH Client that is not a part of sshpool. ssh_to_remove = mock.Mock() mock_sshclient.side_effect = [mock.Mock(), mock.Mock()] sshpool = utils.SSHPool("127.0.0.1", 22, 10, "test", password="******", min_size=2, max_size=2) listBefore = list(sshpool.free_items) self.assertNotIn(ssh_to_remove, listBefore) sshpool.remove(ssh_to_remove) self.assertEqual(listBefore, list(sshpool.free_items))
def _execute(self, commands): command = ['ssc', '127.0.0.1'] if self.admin_ip0 is not None: command = ['ssc', '--smuauth', self.admin_ip0] command += ['console-context', '--evs', self.evs_id] commands = command + commands mutils.check_ssh_injection(commands) commands = ' '.join(commands) if not self.sshpool: self.sshpool = mutils.SSHPool(ip=self.ip, port=self.port, conn_timeout=None, login=self.user, password=self.password, privatekey=self.priv_key) with self.sshpool.item() as ssh: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: out, err = processutils.ssh_execute(ssh, commands, check_exit_code=True) LOG.debug( "Command %(cmd)s result: out = %(out)s - err = " "%(err)s.", { 'cmd': commands, 'out': out, 'err': err, }) return out, err except processutils.ProcessExecutionError as e: if 'Failed to establish SSC connection' in e.stderr: msg = _("Failed to establish SSC connection.") LOG.debug(msg) raise exception.HNASConnException(msg=msg) else: LOG.debug( "Error running SSH command. " "Command %(cmd)s result: out = %(out)s - err = " "%(err)s - exit = %(exit)s.", { 'cmd': e.cmd, 'out': e.stdout, 'err': e.stderr, 'exit': e.exit_code, }) raise
def _run_ssh(self, host, cmd_list, check_exit_code=False): command = ' '.join(pipes.quote(cmd_arg) for cmd_arg in cmd_list) connection = self.ssh_connections.get(host) if not connection: hdfs_ssh_name = self.configuration.hdfs_ssh_name password = self.configuration.hdfs_ssh_pw privatekey = self.configuration.hdfs_ssh_private_key hdfs_ssh_port = self.configuration.hdfs_ssh_port ssh_conn_timeout = self.configuration.ssh_conn_timeout min_size = self.configuration.ssh_min_pool_conn max_size = self.configuration.ssh_max_pool_conn ssh_pool = utils.SSHPool(host, hdfs_ssh_port, ssh_conn_timeout, hdfs_ssh_name, password=password, privatekey=privatekey, min_size=min_size, max_size=max_size) ssh = ssh_pool.create() self.ssh_connections[host] = (ssh_pool, ssh) else: ssh_pool, ssh = connection if not ssh.get_transport().is_active(): ssh_pool.remove(ssh) ssh = ssh_pool.create() self.ssh_connections[host] = (ssh_pool, ssh) try: return processutils.ssh_execute(ssh, command, check_exit_code=check_exit_code) except Exception as e: msg = (_('Error running SSH command: %(cmd)s. ' 'Error: %(excmsg)s.') % { 'cmd': command, 'excmsg': six.text_type(e) }) LOG.error(msg) raise exception.HDFSException(msg)
def _ssh_exec(self, server, command): connection = self.ssh_connections.get(server['instance_id']) if not connection: ssh_pool = utils.SSHPool(server['ip'], 22, None, server['username'], server['password'], server['pk_path'], max_size=1) ssh = ssh_pool.create() self.ssh_connections[server['instance_id']] = (ssh_pool, ssh) else: ssh_pool, ssh = connection if not ssh.get_transport().is_active(): ssh_pool.remove(ssh) ssh = ssh_pool.create() self.ssh_connections[server['instance_id']] = (ssh_pool, ssh) return processutils.ssh_execute(ssh, ' '.join(command))
def test_create_ssh_with_key(self): key = os.path.expanduser("fake_key") fake_ssh_client = mock.Mock() ssh_pool = utils.SSHPool("127.0.0.1", 22, 10, "test", privatekey="fake_key") with mock.patch.object(paramiko, "SSHClient", return_value=fake_ssh_client): with mock.patch.object(paramiko.RSAKey, "from_private_key_file", return_value=key) as from_private_key_mock: ssh_pool.create() from_private_key_mock.assert_called_once_with(key) fake_ssh_client.connect.assert_called_once_with( "127.0.0.1", port=22, username="******", password=None, pkey=key, timeout=10)
def __call__(self, cmd_list, check_exit_code=True, attempts=1): """SSH tool""" manila_utils.check_ssh_injection(cmd_list) command = ' '.join(cmd_list) if not self.sshpool: try: self.sshpool = manila_utils.SSHPool( self.host, self.port, self.ssh_conn_timeout, self.login, password=self.password, privatekey=self.privatekey, min_size=self.ssh_min_pool_size, max_size=self.ssh_max_pool_size) except paramiko.SSHException: LOG.error("Unable to create SSHPool") raise try: return self._ssh_execute(self.sshpool, command, check_exit_code, attempts) except Exception: LOG.error("Error running SSH command: %s", command) raise
def test_create_ssh_with_nothing(self): ssh_pool = utils.SSHPool("127.0.0.1", 22, 10, "test") with mock.patch.object(paramiko, "SSHClient"): self.assertRaises(paramiko.SSHException, ssh_pool.create)
def __init__(self, *args, **kwargs): self.pool = utils.SSHPool(*args, **kwargs)