Skip to content

Commit

Permalink
Merge pull request #97 from CHIMEFRB/96-bug-issue-with-datatrail-cli-…
Browse files Browse the repository at this point in the history
…scout-function

fix(cli): check minoc status
  • Loading branch information
tjzegmott authored Jun 25, 2024
2 parents d5c33b8 + 6a0d6df commit a955ab1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
8 changes: 8 additions & 0 deletions dtcli/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
13 changes: 10 additions & 3 deletions dtcli/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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(
"""
Expand Down
11 changes: 11 additions & 0 deletions dtcli/scout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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(
Expand Down
32 changes: 32 additions & 0 deletions dtcli/utilities/cadcclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit a955ab1

Please sign in to comment.