def remove_dir(self, host, dst, on_execute, on_completion): utils.ssh_execute(host, 'rm', '-rf', dst, on_execute=on_execute, on_completion=on_completion)
def create_dir(self, host, dst_path, on_execute, on_completion): utils.ssh_execute(host, 'mkdir', '-p', dst_path, on_execute=on_execute, on_completion=on_completion)
def migrate_disk_and_power_off(self, context, instance, dest, flavor, network_info, block_device_info=None, timeout=0, retry_interval=0): LOG.info("Starting migrate_disk_and_power_off", instance=instance) LOG.info("IIIIIIIIIIIIinstance: %s" % instance) if self._is_cold_migrate(instance, dest): LOG.info('This is cold migrate, start export') """ container_name = "nova-" + instance["uuid"] container_export_path = '/tmp/%s.tar' % container_name utils.execute('docker', 'export', '-o', container_export_path, container_name, run_as_root=True) utils.execute('scp', container_export_path, "%s:/tmp/" % dest, run_as_root=True) utils.ssh_execute(dest, 'cat', container_export_path, '|', 'docker', 'import', '-', container_name, run_as_root=True) """ container_name = "nova-" + instance["uuid"] utils.execute('docker', 'commit', container_name, container_name, run_as_root=True) utils.execute('docker', 'tag', container_name, PRIVATE_REPOSITORY + '/migrate:' + container_name, run_as_root=True) utils.execute('docker', 'push', PRIVATE_REPOSITORY + '/migrate', run_as_root=True) utils.ssh_execute(dest, 'docker', 'pull', PRIVATE_REPOSITORY + '/migrate:' + container_name, run_as_root=True) # power off self.power_off(instance, timeout=0, retry_interval=0)
def test_run_command(self): self._create_node() self._create_pm() self.mox.StubOutWithMock(self.pm, '_set_connection') self.mox.StubOutWithMock(nutils, 'ssh_execute') self.pm._set_connection().AndReturn(True) nutils.ssh_execute(None, '/usr/bin/VBoxManage test return', check_exit_code=True).AndReturn(("test\nreturn", "")) self.pm._matched_name = 'testNode' self.mox.ReplayAll() result = self.pm._run_command("test return") self.assertEqual(result, ['test', 'return']) self.mox.VerifyAll()
def test_run_command_raises_exception(self): self._create_node() self._create_pm() self.mox.StubOutWithMock(self.pm, '_set_connection') self.mox.StubOutWithMock(nutils, 'ssh_execute') self.pm._set_connection().AndReturn(True) nutils.ssh_execute(None, '/usr/bin/VBoxManage test return', check_exit_code=True).\ AndRaise(processutils.ProcessExecutionError) self.mox.ReplayAll() result = self.pm._run_command("test return") self.assertEqual(result, []) self.mox.VerifyAll()
def test_run_command_raises_exception(self): self._create_node() self._create_pm() self.mox.StubOutWithMock(self.pm, '_set_connection') self.mox.StubOutWithMock(nutils, 'ssh_execute') self.pm._set_connection().AndReturn(True) nutils.ssh_execute(None, '/usr/bin/VBoxManage test return', check_exit_code=True).\ AndRaise(exception.ProcessExecutionError) self.mox.ReplayAll() result = self.pm._run_command("test return") self.assertEqual(result, []) self.mox.VerifyAll()
def _run_command(self, cmd, check_exit_code=True): """Run a remote command using an active ssh connection. :param command: String with the command to run. If {_NodeName_} is in the command it will get replaced by the _matched_name value. base_cmd will also get prepended to the command. """ self._set_connection() cmd = cmd.replace('{_NodeName_}', self._matched_name) cmd = '%s %s' % (self._vp_cmd.base_cmd, cmd) try: stdout, stderr = utils.ssh_execute(self._connection, cmd, check_exit_code=check_exit_code) result = stdout.strip().splitlines() LOG.debug('Result for run_command: %s' % result) except exception.ProcessExecutionError: result = [] LOG.exception("Error running command: %s" % cmd) return result
def test_run_command(self): self.flags(virtual_power_ssh_host='127.0.0.1', group="baremetal") self.flags(virtual_power_host_user='******', group="baremetal") self.flags(virtual_power_host_pass='******', group="baremetal") self._create_node() self._create_pm() self.mox.StubOutWithMock(self.pm, '_set_connection') self.mox.StubOutWithMock(nutils, 'ssh_execute') self.pm._set_connection().AndReturn(True) nutils.ssh_execute(None, '/usr/bin/VBoxManage test return', check_exit_code=True).AndReturn(("test\nreturn", "")) self.pm._matched_name = 'testNode' self.mox.ReplayAll() result = self.pm._run_command("test return") self.assertEqual(result, ['test', 'return']) self.mox.VerifyAll()
def run_command(self, cmd, check_exit_code=True): """Run a remote command using an active ssh connection. :param command: String with the command to run. """ self._set_connection() stdout, stderr = utils.ssh_execute(self._connection, cmd, check_exit_code=check_exit_code) return stdout.strip().splitlines()
def run_vios_command(self, cmd, check_exit_code=True): """Run a remote command using an active ssh connection. :param command: String with the command to run. """ self._set_connection() stdout, stderr = utils.ssh_execute(self._connection, cmd, check_exit_code=check_exit_code) return stdout.strip().splitlines()
def _run_ssh(self, command, check_exit_code=True): #TODO(justinsb): SSH connection caching (?) ssh = self._connect_to_ssh() #TODO(justinsb): Reintroduce the retry hack ret = ssh_execute(ssh, command, check_exit_code=check_exit_code) ssh.close() return ret
def test_activate_node_with_exception(self): self._create_node() self._create_pm() self.mox.StubOutWithMock(self.pm, "_check_for_node") self.mox.StubOutWithMock(nutils, "ssh_execute") self.pm._check_for_node().AndReturn(["testNode"]) self.pm._check_for_node().AndReturn(["testNode"]) nutils.ssh_execute("test", "/usr/bin/VBoxManage startvm ", check_exit_code=True).AndRaise( exception.ProcessExecutionError ) nutils.ssh_execute("test", "/usr/bin/VBoxManage list runningvms", check_exit_code=True).AndRaise( exception.ProcessExecutionError ) self.mox.ReplayAll() self.pm._connection = "test" state = self.pm.activate_node() self.assertEqual(state, "error") self.mox.VerifyAll()
def test_activate_node_with_exception(self): self._create_node() self._create_pm() self.mox.StubOutWithMock(self.pm, '_check_for_node') self.mox.StubOutWithMock(nutils, 'ssh_execute') self.pm._check_for_node().AndReturn(['"testNode"']) self.pm._check_for_node().AndReturn(['"testNode"']) nutils.ssh_execute('test', '/usr/bin/VBoxManage startvm ', check_exit_code=True).\ AndRaise(processutils.ProcessExecutionError) nutils.ssh_execute('test', '/usr/bin/VBoxManage list runningvms', check_exit_code=True).\ AndRaise(processutils.ProcessExecutionError) self.mox.ReplayAll() self.pm._connection = 'test' state = self.pm.activate_node() self.assertEqual(state, 'error') self.mox.VerifyAll()
def test_activate_node_with_exception(self): self._create_node() self._create_pm() self.mox.StubOutWithMock(self.pm, '_check_for_node') self.mox.StubOutWithMock(nutils, 'ssh_execute') self.pm._check_for_node().AndReturn(['"testNode"']) self.pm._check_for_node().AndReturn(['"testNode"']) nutils.ssh_execute('test', '/usr/bin/VBoxManage startvm ', check_exit_code=True).\ AndRaise(exception.ProcessExecutionError) nutils.ssh_execute('test', '/usr/bin/VBoxManage list runningvms', check_exit_code=True).\ AndRaise(exception.ProcessExecutionError) self.mox.ReplayAll() self.pm._connection = 'test' state = self.pm.activate_node() self.assertEqual(state, 'error') self.mox.VerifyAll()
def run_vios_command(self, cmd, check_exit_code=True): """Run a remote command using an active ssh connection. :param command: String with the command to run. """ self._set_connection() stdout, stderr = utils.ssh_execute(self._connection, cmd, check_exit_code=check_exit_code) error_text = stderr.strip() if error_text: LOG.debug( _("Found error stream for command \"%(cmd)s\": %(error_text)s") % locals()) return stdout.strip().splitlines()
def test_ssh_execute(self): expected_args = ('ssh', '-o', 'BatchMode=yes', 'remotehost', 'ls', '-l') with mock.patch('nova.utils.execute') as mock_method: utils.ssh_execute('remotehost', 'ls', '-l') mock_method.assert_called_once_with(*expected_args)
def test_ssh_execute(self): expected_args = ("ssh", "-o", "BatchMode=yes", "remotehost", "ls", "-l") with mock.patch("nova.utils.execute") as mock_method: utils.ssh_execute("remotehost", "ls", "-l") mock_method.assert_called_once_with(*expected_args)
def test_ssh_execute(self, mock_execute): expected_args = ('ssh', '-o', 'BatchMode=yes', 'remotehost', 'ls', '-l') utils.ssh_execute('remotehost', 'ls', '-l') mock_execute.assert_called_once_with(*expected_args)
def run_command(conn_obj, cmd): stdout, stderr = utils.ssh_execute(conn_obj, cmd) return stdout.strip().splitlines()
def create_file(self, host, dst_path, on_execute, on_completion): utils.ssh_execute(host, 'touch', dst_path, on_execute=on_execute, on_completion=on_completion)