From 7804b18f8eb690ef631be32578eab2bee5e07d0d Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Mon, 15 Apr 2024 18:06:32 +0200 Subject: [PATCH 1/4] Don't attempt to commit in dry_run mode Fixes https://github.com/galaxyproject/galaxy/issues/17959: ``` NotNullViolation: null value in column "workflow_step_id" of relation "workflow_output" violates not-null constraint DETAIL: Failing row contains (3843428, null, multiqc_fastqc_html, multiqc_fastqc_html, 1d8a0c348b6f446eb4d84770365f570f). File "sqlalchemy/engine/base.py", line 1890, in _execute_context self.dialect.do_executemany( File "sqlalchemy/dialects/postgresql/psycopg2.py", line 982, in do_executemany context._psycopg2_fetched_rows = xtras.execute_values( File "psycopg2/extras.py", line 1299, in execute_values cur.execute(b''.join(parts)) IntegrityError: (psycopg2.errors.NotNullViolation) null value in column "workflow_step_id" of relation "workflow_output" violates not-null constraint DETAIL: Failing row contains (3843428, null, multiqc_fastqc_html, multiqc_fastqc_html, 1d8a0c348b6f446eb4d84770365f570f). [SQL: INSERT INTO workflow_output (workflow_step_id, output_name, label, uuid) VALUES (%(workflow_step_id)s, %(output_name)s, %(label)s, %(uuid)s) RETURNING workflow_output.id] [parameters: ({'workflow_step_id': None, 'output_name': 'multiqc_fastqc_html', 'label': 'multiqc_fastqc_html', 'uuid': '1d8a0c348b6f446eb4d84770365f570f'}, {'workflow_step_id': None, 'output_name': 'multiqc_cutadapt_html', 'label': 'multiqc_cutadapt_html', 'uuid': '11fef12f5cf24904b31dac7a277f5fda'}, {'workflow_step_id': None, 'output_name': 'multiqc_star_html', 'label': 'multiqc_star_html', 'uuid': '383da02d5a4b4af7ac2933b9cfad316f'}, {'workflow_step_id': None, 'output_name': 'STAR_BAM', 'label': 'STAR_BAM', 'uuid': '6aed76c8a4a5491bacd2f995f16df86b'}, {'workflow_step_id': None,... File "starlette/applications.py", line 123, in __call__ await self.middleware_stack(scope, receive, send) File "starlette/middleware/errors.py", line 186, in __call__ raise exc File "starlette/middleware/errors.py", line 164, in __call__ await self.app(scope, receive, _send) File "starlette_context/middleware/raw_middleware.py", line 92, in __call__ await self.app(scope, receive, send_wrapper) File "starlette/middleware/base.py", line 189, in __call__ with collapse_excgroups(): File "contextlib.py", line 155, in __exit__ self.gen.throw(typ, value, traceback) File "starlette/_utils.py", line 93, in collapse_excgroups raise exc File "starlette/middleware/base.py", line 191, in __call__ response = await self.dispatch_func(request, call_next) File "galaxy/webapps/galaxy/fast_app.py", line 108, in add_x_frame_options response = await call_next(request) File "starlette/middleware/base.py", line 165, in call_next raise app_exc File "starlette/middleware/base.py", line 151, in coro await self.app(scope, receive_or_disconnect, send_no_error) File "starlette/middleware/exceptions.py", line 62, in __call__ await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "starlette/routing.py", line 758, in __call__ await self.middleware_stack(scope, receive, send) File "starlette/routing.py", line 778, in app await route.handle(scope, receive, send) File "starlette/routing.py", line 299, in handle await self.app(scope, receive, send) File "starlette/routing.py", line 79, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "starlette/routing.py", line 74, in app response = await func(request) File "fastapi/routing.py", line 278, in app raw_response = await run_endpoint_function( File "fastapi/routing.py", line 193, in run_endpoint_function return await run_in_threadpool(dependant.call, **values) File "starlette/concurrency.py", line 42, in run_in_threadpool return await anyio.to_thread.run_sync(func, *args) File "anyio/to_thread.py", line 56, in run_sync return await get_async_backend().run_sync_in_worker_thread( File "anyio/_backends/_asyncio.py", line 2144, in run_sync_in_worker_thread return await future File "anyio/_backends/_asyncio.py", line 851, in run result = context.run(func, *args) File "galaxy/webapps/galaxy/api/workflows.py", line 987, in refactor return self.service.refactor(trans, workflow_id, payload, instance or False) File "galaxy/webapps/galaxy/services/workflows.py", line 218, in refactor return self._workflow_contents_manager.refactor(trans, stored_workflow, payload) File "galaxy/managers/workflows.py", line 1963, in refactor refactored_workflow, action_executions = self.do_refactor(trans, stored_workflow, refactor_request) File "galaxy/managers/workflows.py", line 1948, in do_refactor refactored_workflow, errors = self.update_workflow_from_raw_description( File "galaxy/managers/workflows.py", line 728, in update_workflow_from_raw_description trans.tag_handler.set_tags_from_list( File "galaxy/model/tags.py", line 94, in set_tags_from_list self.apply_item_tags(user, item, unicodify(new_tags_str, "utf-8"), flush=flush) File "galaxy/model/tags.py", line 265, in apply_item_tags self.apply_item_tag(user, item, name, value, flush=flush) File "galaxy/model/tags.py", line 249, in apply_item_tag self.sa_session.commit() File "", line 2, in commit File "sqlalchemy/orm/session.py", line 1454, in commit self._transaction.commit(_to_root=self.future) File "sqlalchemy/orm/session.py", line 832, in commit self._prepare_impl() File "sqlalchemy/orm/session.py", line 811, in _prepare_impl self.session.flush() File "sqlalchemy/orm/session.py", line 3449, in flush self._flush(objects) File "sqlalchemy/orm/session.py", line 3588, in _flush with util.safe_reraise(): File "sqlalchemy/util/langhelpers.py", line 70, in __exit__ compat.raise_( File "sqlalchemy/util/compat.py", line 211, in raise_ raise exception File "sqlalchemy/orm/session.py", line 3549, in _flush flush_context.execute() File "sqlalchemy/orm/unitofwork.py", line 456, in execute rec.execute(self) File "sqlalchemy/orm/unitofwork.py", line 630, in execute util.preloaded.orm_persistence.save_obj( File "sqlalchemy/orm/persistence.py", line 245, in save_obj _emit_insert_statements( File "sqlalchemy/orm/persistence.py", line 1156, in _emit_insert_statements c = connection._execute_20( File "sqlalchemy/engine/base.py", line 1710, in _execute_20 return meth(self, args_10style, kwargs_10style, execution_options) File "sqlalchemy/sql/elements.py", line 334, in _execute_on_connection return connection._execute_clauseelement( File "sqlalchemy/engine/base.py", line 1577, in _execute_clauseelement ret = self._execute_context( File "sqlalchemy/engine/base.py", line 1953, in _execute_context self._handle_dbapi_exception( File "sqlalchemy/engine/base.py", line 2134, in _handle_dbapi_exception util.raise_( File "sqlalchemy/util/compat.py", line 211, in raise_ raise exception File "sqlalchemy/engine/base.py", line 1890, in _execute_context self.dialect.do_executemany( File "sqlalchemy/dialects/postgresql/psycopg2.py", line 982, in do_executemany context._psycopg2_fetched_rows = xtras.execute_values( File "psycopg2/extras.py", line 1299, in execute_values cur.execute(b''.join(parts)) ``` broken in https://github.com/galaxyproject/galaxy/commit/b00a94c1f7972dcd1060069fe83fece7b8943165 --- lib/galaxy/managers/workflows.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/galaxy/managers/workflows.py b/lib/galaxy/managers/workflows.py index d4c46b8b775..23f02f65f93 100644 --- a/lib/galaxy/managers/workflows.py +++ b/lib/galaxy/managers/workflows.py @@ -707,7 +707,8 @@ class WorkflowContentsManager(UsesAnnotations): trans.tag_handler.set_tags_from_list( trans.user, stored_workflow, - data.get("tags", []), + data["tags"], + flush=False, ) if workflow_update_options.update_stored_workflow_attributes: From ae64de668d85291e28bbcfbe36613c7d01b1612a Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Tue, 16 Apr 2024 12:54:10 +0200 Subject: [PATCH 2/4] Fix token retrieval for invenio Do not force to use the Vault and rely on the config template value. --- lib/galaxy/files/sources/_rdm.py | 34 +++++++++++--------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/lib/galaxy/files/sources/_rdm.py b/lib/galaxy/files/sources/_rdm.py index fb33cf44457..4f841871974 100644 --- a/lib/galaxy/files/sources/_rdm.py +++ b/lib/galaxy/files/sources/_rdm.py @@ -1,6 +1,5 @@ import logging from typing import ( - cast, List, NamedTuple, Optional, @@ -138,11 +137,10 @@ class RDMFilesSource(BaseFilesSource): def __init__(self, **kwd: Unpack[FilesSourceProperties]): props = self._parse_common_config_opts(kwd) - base_url = props.get("url", None) + base_url = props.get("url") if not base_url: raise Exception("URL for RDM repository must be provided in configuration") self._repository_url = base_url - self._token = props.get("token", None) self._props = props self._repository_interactor = self.get_repository_interactor(base_url) @@ -150,10 +148,6 @@ class RDMFilesSource(BaseFilesSource): def repository(self) -> RDMRepositoryInteractor: return self._repository_interactor - @property - def token(self) -> Optional[str]: - return self._token if self._token and not self._token.startswith("$") else None - def get_repository_interactor(self, repository_url: str) -> RDMRepositoryInteractor: """Returns an interactor compatible with the given repository URL. @@ -190,25 +184,19 @@ class RDMFilesSource(BaseFilesSource): def get_record_id_from_path(self, source_path: str) -> str: return self.parse_path(source_path, record_id_only=True).record_id - def _serialization_props(self, user_context: OptionalUserContext = None) -> RDMFilesSourceProperties: + def _serialization_props(self, user_context: OptionalUserContext = None): effective_props = {} for key, val in self._props.items(): effective_props[key] = self._evaluate_prop(val, user_context=user_context) - effective_props["url"] = self._repository_url - effective_props["token"] = self.safe_get_authorization_token(user_context) - return cast(RDMFilesSourceProperties, effective_props) + return effective_props def get_authorization_token(self, user_context: OptionalUserContext) -> str: - token = self.token - if not token and user_context: - vault = user_context.user_vault if user_context else None - token = vault.read_secret(f"preferences/{self.id}/token") if vault else None - if token is None: - raise AuthenticationRequired(f"No authorization token provided in user's settings for '{self.label}'") + token = None + if user_context: + effective_props = self._serialization_props(user_context) + token = effective_props.get("token") + if not token: + raise AuthenticationRequired( + f"Please provide a personal access token in your user's preferences for '{self.label}'" + ) return token - - def safe_get_authorization_token(self, user_context: OptionalUserContext) -> Optional[str]: - try: - return self.get_authorization_token(user_context) - except AuthenticationRequired: - return None From c02750ec7e4a8c314aa28d545b8844c228e7e41a Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:10:53 +0200 Subject: [PATCH 3/4] Fix public_name retrieval for invenio --- lib/galaxy/files/sources/_rdm.py | 9 ++++++++- lib/galaxy/files/sources/invenio.py | 30 +++++++++++++++++------------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/galaxy/files/sources/_rdm.py b/lib/galaxy/files/sources/_rdm.py index 4f841871974..14f7e9e1daa 100644 --- a/lib/galaxy/files/sources/_rdm.py +++ b/lib/galaxy/files/sources/_rdm.py @@ -25,6 +25,7 @@ OptionalUserContext = Optional[ProvidesUserFileSourcesUserContext] class RDMFilesSourceProperties(FilesSourceProperties): url: str token: str + public_name: str class RecordFilename(NamedTuple): @@ -79,7 +80,9 @@ class RDMRepositoryInteractor: """ raise NotImplementedError() - def create_draft_record(self, title: str, user_context: OptionalUserContext = None): + def create_draft_record( + self, title: str, public_name: Optional[str] = None, user_context: OptionalUserContext = None + ): """Creates a draft record (directory) in the repository with basic metadata. The metadata is usually just the title of the record and the user that created it. @@ -200,3 +203,7 @@ class RDMFilesSource(BaseFilesSource): f"Please provide a personal access token in your user's preferences for '{self.label}'" ) return token + + def get_public_name(self, user_context: OptionalUserContext) -> Optional[str]: + effective_props = self._serialization_props(user_context) + return effective_props.get("public_name") diff --git a/lib/galaxy/files/sources/invenio.py b/lib/galaxy/files/sources/invenio.py index 3ddf4fee107..6e22f4107e7 100644 --- a/lib/galaxy/files/sources/invenio.py +++ b/lib/galaxy/files/sources/invenio.py @@ -135,7 +135,8 @@ class InvenioRDMFilesSource(RDMFilesSource): user_context: OptionalUserContext = None, opts: Optional[FilesSourceOptions] = None, ) -> Entry: - record = self.repository.create_draft_record(entry_data["name"], user_context=user_context) + public_name = self.get_public_name(user_context) + record = self.repository.create_draft_record(entry_data["name"], public_name, user_context=user_context) return { "uri": self.repository.to_plugin_uri(record["id"]), "name": record["metadata"]["title"], @@ -198,9 +199,11 @@ class InvenioRepositoryInteractor(RDMRepositoryInteractor): response_data = self._get_response(user_context, request_url) return self._get_record_files_from_response(record_id, response_data) - def create_draft_record(self, title: str, user_context: OptionalUserContext = None) -> RemoteDirectory: + def create_draft_record( + self, title: str, public_name: Optional[str] = None, user_context: OptionalUserContext = None + ) -> RemoteDirectory: today = datetime.date.today().isoformat() - creator = self._get_creator_from_user_context(user_context) + creator = self._get_creator_from_public_name(public_name) create_record_request = { "files": {"enabled": True}, "metadata": { @@ -360,10 +363,9 @@ class InvenioRepositoryInteractor(RDMRepositoryInteractor): ) return rval - def _get_creator_from_user_context(self, user_context: OptionalUserContext): - public_name = self.get_user_preference_by_key("public_name", user_context) - family_name = "Galaxy User" + def _get_creator_from_public_name(self, public_name: Optional[str] = None) -> Creator: given_name = "Anonymous" + family_name = "Galaxy User" if public_name: tokens = public_name.split(", ") if len(tokens) == 2: @@ -371,12 +373,16 @@ class InvenioRepositoryInteractor(RDMRepositoryInteractor): given_name = tokens[1] else: given_name = public_name - return {"person_or_org": {"family_name": family_name, "given_name": given_name, "type": "personal"}} - - def get_user_preference_by_key(self, key: str, user_context: OptionalUserContext): - preferences = user_context.preferences if user_context else None - value = preferences.get(f"{self.plugin.id}|{key}", None) if preferences else None - return value + return { + "person_or_org": { + "name": f"{given_name} {family_name}", + "family_name": family_name, + "given_name": given_name, + "type": "personal", + "identifiers": [], + }, + "affiliations": [], + } def _get_response( self, user_context: OptionalUserContext, request_url: str, params: Optional[Dict[str, Any]] = None From 133730c221ddd308c3e13108342c91896033c1e6 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:24:52 +0200 Subject: [PATCH 4/4] Fix sample file source for Invenio --- lib/galaxy/config/sample/file_sources_conf.yml.sample | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/galaxy/config/sample/file_sources_conf.yml.sample b/lib/galaxy/config/sample/file_sources_conf.yml.sample index 76b1a861690..b21bb1339b6 100644 --- a/lib/galaxy/config/sample/file_sources_conf.yml.sample +++ b/lib/galaxy/config/sample/file_sources_conf.yml.sample @@ -199,10 +199,14 @@ doc: Make sure to define this generic drs file source if you have defined any other drs file sources, or stock drs download capability will be disabled. - type: inveniordm - id: invenio - doc: Invenio RDM turn-key research data management repository - label: Invenio RDM Demo Repository + id: invenio_sandbox + doc: This is the Sandbox instance of Invenio. It is used for testing purposes only, content is NOT preserved. DOIs created in this instance are not real and will not resolve. + label: Invenio RDM Sandbox Repository (TESTING ONLY) url: https://inveniordm.web.cern.ch/ + token: ${user.user_vault.read_secret('preferences/invenio_sandbox/token')} + # token: ${user.preferences['invenio_sandbox|token']} # Alternatively use this for retrieving the token from user preferences instead of the Vault + public_name: ${user.preferences['invenio_sandbox|public_name']} + writable: true - type: onedata id: onedata1