Esempio n. 1
0
    def _operation_finished(operation, simulator_gid):
        op_ident = dao.get_operation_process_for_operation(operation.id)
        # TODO: Handle login
        job = Job(
            Transport(os.environ[HPCSchedulerClient.CSCS_LOGIN_TOKEN_ENV_KEY]),
            op_ident.job_id)

        operation = dao.get_operation_by_id(operation.id)
        folder = HPCSchedulerClient.storage_interface.get_project_folder(
            operation.project.name)
        storage_interface = StorageInterface()
        if storage_interface.encryption_enabled():
            storage_interface.inc_project_usage_count(folder)
            storage_interface.sync_folders(folder)

        try:
            sim_h5_filenames, metric_op, metric_h5_filename = \
                HPCSchedulerClient.stage_out_to_operation_folder(job.working_dir, operation, simulator_gid)

            operation.mark_complete(STATUS_FINISHED)
            dao.store_entity(operation)
            HPCSchedulerClient().update_db_with_results(
                operation, sim_h5_filenames, metric_op, metric_h5_filename)

        except OperationException as exception:
            HPCOperationService.LOGGER.error(exception)
            HPCOperationService._operation_error(operation)

        finally:
            if storage_interface.encryption_enabled():
                storage_interface.sync_folders(folder)
                storage_interface.set_project_inactive(operation.project)
Esempio n. 2
0
    def _import_projects_from_folder(self, temp_folder):
        """
        Process each project from the uploaded pack, to extract names.
        """
        project_roots = []
        for root, _, files in os.walk(temp_folder):
            if StorageInterface.TVB_PROJECT_FILE in files:
                project_roots.append(root)

        for temp_project_path in project_roots:
            update_manager = ProjectUpdateManager(temp_project_path)
            update_manager.run_all_updates()
            project = self.__populate_project(temp_project_path)
            # Populate the internal list of create projects so far, for cleaning up folders, in case of failure
            self.created_projects.append(project)
            # Ensure project final folder exists on disk
            project_path = self.storage_interface.get_project_folder(project.name)
            shutil.move(os.path.join(temp_project_path, StorageInterface.TVB_PROJECT_FILE), project_path)
            # Now import project operations with their results
            self.import_project_operations(project, temp_project_path)
            # Import images and move them from temp into target
            self._store_imported_images(project, temp_project_path, project.name)
            if StorageInterface.encryption_enabled():
                StorageInterface.sync_folders(project_path)
                shutil.rmtree(project_path)
Esempio n. 3
0
    def editone(self, project_id=None, cancel=False, save=False, delete=False, **data):
        """
        Create or change Project. When project_id is empty we create a 
        new entity, otherwise we are to edit and existent one.
        """
        if cherrypy.request.method == 'POST' and cancel:
            raise cherrypy.HTTPRedirect('/project')
        if cherrypy.request.method == 'POST' and delete:
            self._remove_project(project_id)
            raise cherrypy.HTTPRedirect('/project/viewall')

        current_user = common.get_logged_user()
        is_create = False
        if project_id is None or not int(project_id):
            is_create = True
            data["administrator"] = current_user.display_name
            admin_username = current_user.username
        else:
            current_project = self.project_service.find_project(project_id)
            if not save:
                # Only when we do not have submitted data,
                # populate fields with initial values for edit.
                data = dict(name=current_project.name, description=current_project.description)
            data["administrator"] = current_project.administrator.display_name
            admin_username = current_project.administrator.username
            self._mark_selected(current_project)
        data["project_id"] = project_id

        template_specification = dict(mainContent="project/editone", data=data, isCreate=is_create,
                                      title="Create new project" if is_create else "Edit " + data["name"],
                                      editUsersEnabled=(current_user.username == admin_username))
        try:
            if cherrypy.request.method == 'POST' and save:
                data = EditForm().to_python(data)
                saved_project = self.project_service.store_project(current_user, is_create, project_id, **data)
                if StorageInterface.encryption_enabled() and is_create:
                    project_folder = StorageInterface().get_project_folder(saved_project.name)
                    StorageInterface.sync_folders(project_folder)
                    shutil.rmtree(project_folder)
                self._mark_selected(saved_project)
                raise cherrypy.HTTPRedirect('/project/viewall')
        except formencode.Invalid as excep:
            self.logger.debug(str(excep))
            template_specification[common.KEY_ERRORS] = excep.unpack_errors()
        except ProjectServiceException as excep:
            self.logger.debug(str(excep))
            common.set_error_message(excep.message)
            raise cherrypy.HTTPRedirect('/project/viewall')

        all_users, members, pages = self.user_service.get_users_for_project(current_user.username, project_id)
        template_specification['usersList'] = all_users
        template_specification['usersMembers'] = [m.id for m in members]
        template_specification['usersPages'] = pages
        template_specification['usersCurrentPage'] = 1
        return self.fill_default_attributes(template_specification, 'properties')
Esempio n. 4
0
def start_tvb(arguments, browser=True):
    """
    Fire CherryPy server and listen on a free port
    """

    if PARAM_RESET_DB in arguments:
        # When specified, clean everything in DB
        reset()
        arguments.remove(PARAM_RESET_DB)

    if not os.path.exists(TvbProfile.current.TVB_STORAGE):
        try:
            os.makedirs(TvbProfile.current.TVB_STORAGE)
        except Exception:
            sys.exit(
                "You do not have enough rights to use TVB storage folder:" +
                str(TvbProfile.current.TVB_STORAGE))

    try:
        initialize(arguments)
    except InvalidSettingsException as excep:
        LOGGER.exception(excep)
        sys.exit()

    # Mark that the interface is Web
    ABCDisplayer.VISUALIZERS_ROOT = TvbProfile.current.web.VISUALIZERS_ROOT

    init_cherrypy(arguments)
    if StorageInterface.encryption_enabled(
    ) and StorageInterface.app_encryption_handler():
        storage_interface = StorageInterface()
        storage_interface.start()
        storage_interface.startup_cleanup()

    # Fire a browser page at the end.
    if browser:
        run_browser()

    expose_rest_api()

    # Launch CherryPy loop forever.
    LOGGER.info("Finished starting TVB version %s in %.3f s",
                TvbProfile.current.version.CURRENT_VERSION,
                time.time() - STARTUP_TIC)
    cherrypy.engine.block()
    cherrypy.log.error_log
Esempio n. 5
0
    def logout(self):
        """
        Logging out user and clean session
        """
        user = common.remove_from_session(common.KEY_USER)
        if user is not None:
            self.logger.debug("User " + user.username +
                              " is just logging out!")
        current_project = common.get_current_project()
        storage_interface = StorageInterface()
        if current_project is not None and storage_interface.encryption_enabled(
        ):
            storage_interface.set_project_inactive(current_project)
        SimulatorContext().clean_project_data_from_session()
        common.set_info_message("Thank you for using The Virtual Brain!")

        common.expire_session()
        raise cherrypy.HTTPRedirect("/user")
Esempio n. 6
0
    def _import_project_from_folder(self, temp_folder):
        """
        Process each project from the uploaded pack, to extract names.
        """
        temp_project_path = None
        for root, _, files in os.walk(temp_folder):
            if StorageInterface.TVB_PROJECT_FILE in files:
                temp_project_path = root
                break

        if temp_project_path is not None:
            update_manager = ProjectUpdateManager(temp_project_path)

            if update_manager.checked_version < 3:
                raise ImportException(
                    'Importing projects with versions older than 3 is not supported in TVB 2! '
                    'Please import the project in TVB 1.5.8 and then launch the current version of '
                    'TVB in order to upgrade this project!')

            update_manager.run_all_updates()
            project = self.__populate_project(temp_project_path)
            # Populate the internal list of create projects so far, for cleaning up folders, in case of failure
            self.created_projects.append(project)
            # Ensure project final folder exists on disk
            project_path = self.storage_interface.get_project_folder(
                project.name)
            shutil.move(
                os.path.join(temp_project_path,
                             StorageInterface.TVB_PROJECT_FILE), project_path)
            # Now import project operations with their results
            self.import_list_of_operations(project, temp_project_path)
            # Import images and move them from temp into target
            self._store_imported_images(project, temp_project_path,
                                        project.name)
            if StorageInterface.encryption_enabled():
                self.storage_interface.remove_project(project, True)
Esempio n. 7
0
def init_cherrypy(arguments=None):
    # Mount static folders from modules marked for introspection
    arguments = arguments or []
    CONFIGUER = TvbProfile.current.web.CHERRYPY_CONFIGURATION
    if StorageInterface.encryption_enabled():
        CONFIGUER["/"]["tools.sessions.storage_class"] = CleanupSessionHandler
    for module in arguments:
        module_inst = importlib.import_module(str(module))
        module_path = os.path.dirname(os.path.abspath(module_inst.__file__))
        CONFIGUER["/static_" + str(module)] = {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': '.',
            'tools.staticdir.root': module_path
        }

    # Mount controllers, and specify the root URL for them.
    cherrypy.tree.mount(BaseController(), "/", config=CONFIGUER)
    cherrypy.tree.mount(UserController(), "/user/", config=CONFIGUER)
    cherrypy.tree.mount(ProjectController(), "/project/", config=CONFIGUER)
    cherrypy.tree.mount(FigureController(),
                        "/project/figure/",
                        config=CONFIGUER)
    cherrypy.tree.mount(FlowController(), "/flow/", config=CONFIGUER)
    cherrypy.tree.mount(SettingsController(), "/settings/", config=CONFIGUER)
    cherrypy.tree.mount(HelpController(), "/help/", config=CONFIGUER)
    cherrypy.tree.mount(SimulatorController(), "/burst/", config=CONFIGUER)
    cherrypy.tree.mount(ParameterExplorationController(),
                        "/burst/explore/",
                        config=CONFIGUER)
    cherrypy.tree.mount(DynamicModelController(),
                        "/burst/dynamic/",
                        config=CONFIGUER)
    cherrypy.tree.mount(SpatioTemporalController(),
                        "/spatial/",
                        config=CONFIGUER)
    cherrypy.tree.mount(RegionsModelParametersController(),
                        "/burst/modelparameters/regions/",
                        config=CONFIGUER)
    cherrypy.tree.mount(SurfaceModelParametersController(),
                        "/spatial/modelparameters/surface/",
                        config=CONFIGUER)
    cherrypy.tree.mount(RegionStimulusController(),
                        "/spatial/stimulus/region/",
                        config=CONFIGUER)
    cherrypy.tree.mount(SurfaceStimulusController(),
                        "/spatial/stimulus/surface/",
                        config=CONFIGUER)
    cherrypy.tree.mount(LocalConnectivityController(),
                        "/spatial/localconnectivity/",
                        config=CONFIGUER)
    cherrypy.tree.mount(NoiseConfigurationController(),
                        "/burst/noise/",
                        config=CONFIGUER)
    cherrypy.tree.mount(HPCController(), "/hpc/", config=CONFIGUER)
    cherrypy.tree.mount(KubeController(), "/kube/", config=CONFIGUER)

    cherrypy.config.update(CONFIGUER)

    # ----------------- Register additional request handlers -----------------
    # This tool checks for MAX upload size
    cherrypy.tools.upload = Tool('on_start_resource',
                                 RequestHandler.check_upload_size)
    # This tools clean up files on disk (mainly after export)
    cherrypy.tools.cleanup = Tool('on_end_request',
                                  RequestHandler.clean_files_on_disk)
    # ----------------- End register additional request handlers ----------------

    # Register housekeeping job
    if TvbProfile.current.hpc.IS_HPC_RUN and TvbProfile.current.hpc.CAN_RUN_HPC:
        cherrypy.engine.housekeeper = cherrypy.process.plugins.BackgroundTask(
            TvbProfile.current.hpc.BACKGROUND_JOB_INTERVAL,
            HPCOperationService.check_operations_job)
        cherrypy.engine.housekeeper.start()

    if not TvbProfile.current.web.OPENSHIFT_DEPLOY:
        operations_job = cherrypy.process.plugins.BackgroundTask(
            TvbProfile.current.OPERATIONS_BACKGROUND_JOB_INTERVAL,
            StandAloneClient.process_queued_operations,
            bus=cherrypy.engine)
        operations_job.start()

    # HTTP Server is fired now #
    cherrypy.engine.start()