From 9217007a877b8f3895056fc4220632b906614ac8 Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Thu, 10 Aug 2023 20:52:33 +0100 Subject: [PATCH] Add remote checker task --- pydatalab/tasks.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pydatalab/tasks.py b/pydatalab/tasks.py index cd1f19787..8c77f534e 100644 --- a/pydatalab/tasks.py +++ b/pydatalab/tasks.py @@ -289,6 +289,53 @@ def check_item_validity(_, base_url: str | None = None, starting_materials: bool admin.add_task(check_item_validity) +@task +def check_remotes(_, base_url: str = None): + """This task looks up all configured remotes and checks that they + can be synced. + + Requires the environment variable DATALAB_API_KEY to be set. + + Parameters: + base_url: The API URL. + + """ + + import os + + import requests + + api_key = os.environ.get("DATALAB_API_KEY") + if not api_key: + raise SystemExit("DATALAB_API_KEY env var not set") + + log = setup_log("check_remotes") + + user_response = requests.get( + f"{base_url}/get-current-user/", headers={"DATALAB-API-KEY": api_key} + ) + if not user_response.status_code == 200: + raise SystemExit(f"Could not get current user: {user_response.json()['message']}") + + directory_response = requests.get( + f"{base_url}/list-remote-directories/", headers={"DATALAB-API-KEY": api_key} + ) + + if directory_response.status_code != 200: + raise SystemExit(f"Could not get remote directories: {directory_response}") + + directory_structure = directory_response.json() + + for d in directory_structure: + if d["type"] == "error": + log.error(f"ꙮ {d['name']!r}: {d['details']!r}") + elif d["type"] == "toplevel": + log.info(f"✓ {d['name']!r}") + + +admin.add_task(check_remotes) + + @task def import_cheminventory(_, filename: str): import random