Esempio n. 1
0
def execute_command(ssh_connection,
                    cmd,
                    get_stderr=False,
                    raise_when_error=True):
    """Execute specified command remotely using existing ssh connection.

    Return exit code, stdout data and stderr data of the executed command.
    """
    chan = ssh_connection.get_transport().open_session()
    chan.exec_command(cmd)
    ret_code = chan.recv_exit_status()

    stdout = ''
    while chan.recv_ready():
        stdout += chan.recv(1024)

    stderr = ''
    while chan.recv_stderr_ready():
        stderr += chan.recv_stderr(1024)

    if ret_code and raise_when_error:
        raise ex.RemoteCommandException(cmd=cmd,
                                        ret_code=ret_code,
                                        stdout=stdout,
                                        stderr=stderr)

    if get_stderr:
        return ret_code, stdout, stderr

    else:
        return ret_code, stdout
Esempio n. 2
0
    def test_get_free_device_path(self, p_ex_cmd, p_get_username):
        p_get_username.return_value = 'root'

        instance = r.InstanceResource({
            'instance_id': '123454321',
            'node_group': {}
        })

        stdout = """major minor  #blocks  name

   8        0  488386584 vda
   8        1     102400 vda1"""
        p_ex_cmd.return_value = (0, stdout)
        self.assertEqual(volumes._get_free_device_path(instance), '/dev/vdb')

        stdout = """major minor  #blocks  name

   8        0  488386584 xvda
   8        1     102400 xvda1"""
        p_ex_cmd.return_value = (0, stdout)
        self.assertEqual(volumes._get_free_device_path(instance), '/dev/xvdb')

        stdout = "major minor  #blocks  name\n"
        for idx in range(0, 26):
            line = "   8        0  488386584 vd" + chr(ord('a') + idx) + '\n'
            stdout += line

        p_ex_cmd.return_value = (0, stdout)
        self.assertRaises(RuntimeError, volumes._get_free_device_path,
                          instance)

        p_ex_cmd.side_effect = ex.RemoteCommandException('cmd')
        self.assertRaises(ex.RemoteCommandException,
                          volumes._get_free_device_path, instance)
Esempio n. 3
0
    def test_mount_volume(self, p_ex_cmd):
        instance = r.InstanceResource({'instance_id': '123454321'})

        p_ex_cmd.return_value = (0, None)
        self.assertIsNone(volumes._mount_volume(instance, '123', '456'))
        self.assertEqual(p_ex_cmd.call_count, 3)
        p_ex_cmd.reset_mock()

        p_ex_cmd.side_effect = ex.RemoteCommandException('cmd')
        self.assertRaises(ex.RemoteCommandException, volumes._mount_volume,
                          instance, '123', '456')
Esempio n. 4
0
    def test_mount_volume(self, p_get_username):
        p_get_username.return_value = 'root'

        instance = self._get_instance()
        execute_com = instance.remote().execute_command

        self.assertIsNone(volumes._mount_volume(instance, '123', '456'))
        self.assertEqual(execute_com.call_count, 3)

        execute_com.side_effect = ex.RemoteCommandException('cmd')
        self.assertRaises(ex.RemoteCommandException, volumes._mount_volume,
                          instance, '123', '456')
Esempio n. 5
0
    def test_mount_volume(self, p_ex_cmd, p_get_username, _acq, _rel,
                          run_in_sub, start_sub, get_conn_params, p_close):
        p_get_username.return_value = 'root'

        instance = r.InstanceResource({
            'instance_id': '123454321',
            'node_group': {}
        })

        p_ex_cmd.return_value = (0, None)
        self.assertIsNone(volumes._mount_volume(instance, '123', '456'))
        self.assertEqual(p_ex_cmd.call_count, 3)
        p_ex_cmd.reset_mock()

        p_ex_cmd.side_effect = ex.RemoteCommandException('cmd')
        self.assertRaises(ex.RemoteCommandException, volumes._mount_volume,
                          instance, '123', '456')
Esempio n. 6
0
def _execute_command(cmd, get_stderr=False, raise_when_error=True):
    global _ssh

    chan = _ssh.get_transport().open_session()
    chan.exec_command(cmd)

    # todo(dmitryme): that could hang if stderr buffer overflows
    stdout = _read_paramimko_stream(chan.recv)
    stderr = _read_paramimko_stream(chan.recv_stderr)

    ret_code = chan.recv_exit_status()

    if ret_code and raise_when_error:
        raise ex.RemoteCommandException(cmd=cmd,
                                        ret_code=ret_code,
                                        stdout=stdout,
                                        stderr=stderr)

    if get_stderr:
        return ret_code, stdout, stderr
    else:
        return ret_code, stdout