def check_db_url(self, **data): """ Action on DB-URL validate button. """ try: storage_path = data[self.settingsservice.KEY_STORAGE] if os.path.isfile(storage_path): raise InvalidSettingsException( 'TVB Storage should be set to a folder and not a file.') if not os.path.isdir(storage_path): try: os.mkdir(storage_path) except OSError: return { 'status': 'not ok', 'message': 'Could not create root storage for TVB. Please check write permissions!' } self.settingsservice.check_db_url( data[self.settingsservice.KEY_DB_URL]) return {'status': 'ok', 'message': 'The database URL is valid.'} except InvalidSettingsException as excep: self.logger.error(excep) return { 'status': 'not ok', 'message': 'The database URL is not valid.' }
def encryption_enabled(): if not TvbProfile.current.web.ENCRYPT_STORAGE: return False if not TvbProfile.current.web.CAN_ENCRYPT_STORAGE: raise InvalidSettingsException( "We can not enable STORAGE ENCRYPTION. Most probably syncrypto is not installed!" ) return True
def check_db_url(self, url): """Validate DB URL, that a connection can be done.""" try: engine = create_engine(url) connection = engine.connect() connection.close() except Exception as excep: self.logger.exception(excep) raise InvalidSettingsException('Could not connect to DB! ' 'Invalid URL:' + str(url))
def _check_tvb_folder(self, storage_path): """ Check if the storage folder is compatible (should be an empty or new folder, with rights to write inside). """ if not os.path.exists(storage_path): return True if not os.path.isdir(storage_path): raise InvalidSettingsException('TVB Storage should be a folder') if not os.access(storage_path, os.W_OK): raise InvalidSettingsException('TVB Storage folder should have write access for tvb process') list_content = os.listdir(storage_path) list_content.remove("TEMP") if len(list_content) > 0: raise InvalidSettingsException( 'TVB Storage should be empty, please set another folder than {}.'.format(storage_path)) return True
def _get_backend_client(): # type: () -> BackendClient if TvbProfile.current.hpc.IS_HPC_RUN: if not TvbProfile.current.hpc.CAN_RUN_HPC: raise InvalidSettingsException( "We can not enable HPC run. Most probably pyunicore is not installed!" ) # Return an entity capable to submit jobs to HPC. return HPCSchedulerClient() if TvbProfile.current.cluster.IS_DEPLOY: # Return an entity capable to submit jobs to the cluster. return ClusterSchedulerClient() # Return a thread launcher. return StandAloneClient()
def _get_backend_client(adapter_instance): # type: (ABCAdapter) -> BackendClient # For the moment run only simulations on HPC if TvbProfile.current.hpc.IS_HPC_RUN and type(adapter_instance) is get_class_by_name( "{}.{}".format(SIMULATOR_MODULE, SIMULATOR_CLASS)): if not TvbProfile.current.hpc.CAN_RUN_HPC: raise InvalidSettingsException("We can not enable HPC run. Most probably pyunicore is not installed!") # Return an entity capable to submit jobs to HPC. return HPCSchedulerClient() if TvbProfile.current.cluster.IS_DEPLOY: # Return an entity capable to submit jobs to the cluster. return ClusterSchedulerClient() # Return a thread launcher. return StandAloneClient()
def save_settings(self, **data): """ Check if new settings are correct. Make necessary changes, then save new data in configuration file. :returns: two boolean values -there were any changes to the configuration; -a reset should be performed on the TVB relaunch. """ new_storage = data[self.KEY_STORAGE] previous_storage = TvbProfile.current.TVB_STORAGE new_db = data[self.KEY_SELECTED_DB] previous_db = TvbProfile.current.db.SELECTED_DB db_changed = new_db != previous_db storage_changed = new_storage != previous_storage matlab_exec = data[self.KEY_MATLAB_EXECUTABLE] if matlab_exec == 'None': data[self.KEY_MATLAB_EXECUTABLE] = '' #Storage changed but DB didn't, just copy TVB storage to new one. if storage_changed and not db_changed: if os.path.exists(new_storage): if os.access(new_storage, os.W_OK): shutil.rmtree(new_storage) else: raise InvalidSettingsException( "No Write access on storage folder!!") shutil.copytree(previous_storage, new_storage) if not os.path.isdir(new_storage): os.makedirs(new_storage) max_space = data[self.KEY_MAX_DISK_SPACE_USR] available_mem_kb = SettingsService.get_disk_free_space(new_storage) kb_value = int(max_space) * 2**10 if not (0 < kb_value < available_mem_kb): raise InvalidSettingsException( "Not enough disk space. There is a maximum of %d MB available on this disk " "or partition. Wanted %d" % (available_mem_kb / (2**10), max_space)) data[self.KEY_MAX_DISK_SPACE_USR] = kb_value #Save data to file, all while checking if any data has changed first_run = TvbProfile.is_first_run() if first_run: data[ stored. KEY_LAST_CHECKED_FILE_VERSION] = TvbProfile.current.version.DATA_VERSION data[ stored. KEY_LAST_CHECKED_CODE_VERSION] = TvbProfile.current.version.SVN_VERSION file_data = data if self.KEY_ADMIN_PWD in data: data[self.KEY_ADMIN_PWD] = md5( data[self.KEY_ADMIN_PWD]).hexdigest() anything_changed = True else: file_data = TvbProfile.current.manager.stored_settings anything_changed = False for key in file_data: if key in data and str(data[key]) != str(file_data[key]): anything_changed = True file_data[key] = data[key] if db_changed: file_data[self.KEY_DB_URL] = TvbProfile.current.db.DB_URL for key in data: if key not in file_data: anything_changed = True file_data[key] = data[key] # Write in file new data if anything_changed: TvbProfile.current.manager.write_config_data(file_data) os.chmod(TvbProfile.current.TVB_CONFIG_FILE, 0644) return anything_changed, first_run or db_changed