def add_export(self, name, confdict): """Add an export to Ganesha specified by confdict.""" xid = confdict["EXPORT"]["Export_Id"] undos = [] _mkindex_called = False try: path = self._write_export(name, confdict) if self.ganesha_rados_store_enable: undos.append(lambda: self._rm_export_rados_object(name)) undos.append(lambda: self._rm_file(path)) else: undos.append(lambda: self._rm_export_file(name)) self._dbus_send_ganesha("AddExport", "string:" + path, "string:EXPORT(Export_Id=%d)" % xid) undos.append(lambda: self._remove_export_dbus(xid)) if self.ganesha_rados_store_enable: # Clean up temp export file used for the DBus call self._rm_file(path) self._add_rados_object_url_to_index(name) else: _mkindex_called = True self._mkindex() except exception.ProcessExecutionError as e: for u in undos: u() if not self.ganesha_rados_store_enable and not _mkindex_called: self._mkindex() raise exception.GaneshaCommandFailure( stdout=e.stdout, stderr=e.stderr, exit_code=e.exit_code, cmd=e.cmd)
def _check_file_exists(self, path): try: self.execute('test', '-f', path, makelog=False, run_as_root=False) return True except exception.GaneshaCommandFailure as e: if e.exit_code == 1: return False else: raise exception.GaneshaCommandFailure( stdout=e.stdout, stderr=e.stderr, exit_code=e.exit_code, cmd=e.cmd)
def _write_conf_file(self, name, data): """Write data to config file for name atomically.""" path = self._getpath(name) tmpf = self._write_tmp_conf_file(path, data) try: self.execute('mv', tmpf, path) except exception.ProcessExecutionError as e: LOG.error('mv temp file ({0}) to {1} failed.'.format(tmpf, path)) self.execute('rm', tmpf) raise exception.GaneshaCommandFailure( stdout=e.stdout, stderr=e.stderr, exit_code=e.exit_code, cmd=e.cmd) return path
def _execute(*args, **kwargs): msg = kwargs.pop('message', args[0]) makelog = kwargs.pop('makelog', True) try: return execute(*args, **kwargs) except exception.ProcessExecutionError as e: if makelog: LOG.error( ("Error while executing management command on " "Ganesha node %(tag)s: %(msg)s."), {'tag': tag, 'msg': msg}) raise exception.GaneshaCommandFailure( stdout=e.stdout, stderr=e.stderr, exit_code=e.exit_code, cmd=e.cmd)
def test_check_file_exists_error(self, exit_code): self.mock_object( self._manager, 'execute', mock.Mock(side_effect=exception.GaneshaCommandFailure( exit_code=exit_code)) ) if exit_code == 1: ret = self._manager._check_file_exists(test_path) self.assertFalse(ret) else: self.assertRaises(exception.GaneshaCommandFailure, self._manager._check_file_exists, test_path) self._manager.execute.assert_called_once_with( 'test', '-f', test_path, makelog=False, run_as_root=False)
def update_export(self, name, confdict): """Update an export to Ganesha specified by confdict.""" xid = confdict["EXPORT"]["Export_Id"] old_confdict = self._read_export(name) path = self._write_export(name, confdict) try: self._dbus_send_ganesha("UpdateExport", "string:" + path, "string:EXPORT(Export_Id=%d)" % xid) except exception.ProcessExecutionError as e: # Revert the export update. self._write_export(name, old_confdict) raise exception.GaneshaCommandFailure( stdout=e.stdout, stderr=e.stderr, exit_code=e.exit_code, cmd=e.cmd) finally: if self.ganesha_rados_store_enable: # Clean up temp export file used for the DBus update call self._rm_file(path)
def raise_exception(*args, **kwargs): if args == fake_args: raise exception.GaneshaCommandFailure()