From 484562c3c8345dd032c00923d3250c1be4a12191 Mon Sep 17 00:00:00 2001 From: Tarik Zegmott Date: Thu, 20 Jun 2024 15:12:44 -0400 Subject: [PATCH 1/2] fix(cadcclient): check canfar status --- dtcli/utilities/cadcclient.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/dtcli/utilities/cadcclient.py b/dtcli/utilities/cadcclient.py index 52900fd..b44a8cb 100644 --- a/dtcli/utilities/cadcclient.py +++ b/dtcli/utilities/cadcclient.py @@ -9,9 +9,11 @@ import cadcutils import dill +import requests from cadcdata import StorageInventoryClient from cadctap import CadcTapClient from cadcutils import net +from requests.exceptions import HTTPError from rich.traceback import install from dtcli.config import procure @@ -364,3 +366,33 @@ def query( content = buffer.getvalue() sys.stdout = sys.__stdout__ return [line.split(",") for line in content.split("\n")] + + +def status( + url: str = "https://ws-uv.canfar.net/minoc/capabilities", + certfile: Optional[str] = None, +) -> bool: + """Check the status of Minoc. + + Args: + url: Minoc capabilities HEAD endpoint. + certfile: Canfar certificate file. + + Returns: + bool: True if Minoc is up, False otherwise. + """ + if not certfile: + certfile = procure(key="vospace_certfile") + response = requests.get(url, cert=certfile, allow_redirects=True) + try: + response.raise_for_status() + except HTTPError as error: + logger.error(error) + logger.error("Canfar is down.") + return False + authorised = response.headers.get("x-vo-authenticated") + if isinstance(authorised, str): + return True + else: + logger.error("Canfar certificate is not valid.") + return False From 6a0d6dfc4f3490dc407c08d106d7740bb5d892e3 Mon Sep 17 00:00:00 2001 From: Tarik Zegmott Date: Thu, 20 Jun 2024 15:16:09 -0400 Subject: [PATCH 2/2] fix(cli): check minoc status --- dtcli/ps.py | 8 ++++++++ dtcli/pull.py | 13 ++++++++++--- dtcli/scout.py | 11 +++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/dtcli/ps.py b/dtcli/ps.py index eb4cf1f..68e277d 100644 --- a/dtcli/ps.py +++ b/dtcli/ps.py @@ -66,6 +66,14 @@ def ps( except Exception as e: error_console.print(e) return None + + # Check Canfar status. + canfar_up = cadcclient.status() + if not canfar_up: + error_console.print( + "Either Minoc is down or certificate is invalid.", style="bold yellow" + ) + try: files, policies = functions.ps(scope, dataset, verbose, quiet) if isinstance(files, str) or isinstance(policies, str): diff --git a/dtcli/pull.py b/dtcli/pull.py index a487adb..724fec6 100644 --- a/dtcli/pull.py +++ b/dtcli/pull.py @@ -10,7 +10,7 @@ from dtcli.config import procure from dtcli.src.functions import find_missing_dataset_files, get_files -from dtcli.utilities.cadcclient import size +from dtcli.utilities import cadcclient from dtcli.utilities.utilities import set_log_level, validate_scope logger = logging.getLogger("pull") @@ -42,7 +42,7 @@ @click.option("-q", "--quiet", is_flag=True, help="Set log level to ERROR.") @click.option("--force", "-f", is_flag=True, help="Do not prompt for confirmation.") @click.pass_context -def pull( +def pull( # noqa: C901 ctx: click.Context, scope: str, dataset: str, @@ -97,6 +97,13 @@ def pull( error_console.print(e) return None + # Check Canfar status. + canfar_up = cadcclient.status() + if not canfar_up: + error_console.print( + "Either Minoc is down or certificate is invalid.", style="bold yellow" + ) + # Find files missing from localhost. console.print(f"\nSearching for files for {dataset} {scope}...\n") files = find_missing_dataset_files(scope, dataset, directory, verbose) @@ -111,7 +118,7 @@ def pull( if len(files_paths) > 0: common_path = path.commonpath(["/" + f for f in files_paths]) try: - to_download_size = size(common_path) + to_download_size = cadcclient.size(common_path) except SSLError: error_console.print( """ diff --git a/dtcli/scout.py b/dtcli/scout.py index 31133e7..168e55a 100644 --- a/dtcli/scout.py +++ b/dtcli/scout.py @@ -78,6 +78,13 @@ def scout( # noqa: C901 ) return {"error": "No config. Create one with `datatrail config init`."} + # Check Canfar status. + canfar_up = cadcclient.status() + if not canfar_up: + error_console.print( + "Either Minoc is down or certificate is invalid.", style="bold yellow" + ) + # Scout dataset. endpoint = ( f"/query/dataset/scout?name={dataset}" @@ -113,6 +120,10 @@ def scout( # noqa: C901 error_console.print("Query failed.") error_console.print(error) return None + except Exception as error: + error_console.print("Query failed.") + error_console.print(error) + return None data[scope]["observed"]["minoc"] = count keys_missing_in_observed = list(