Changeset View
Changeset View
Standalone View
Standalone View
swh/icinga_plugins/deposit.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import datetime | import datetime | ||||
import sys | import sys | ||||
import time | import time | ||||
from typing import Any, Dict, Optional | |||||
from swh.deposit.client import PublicApiDepositClient | from swh.deposit.client import PublicApiDepositClient | ||||
from .base_check import BaseCheck | from .base_check import BaseCheck | ||||
class DepositCheck(BaseCheck): | class DepositCheck(BaseCheck): | ||||
TYPE = "DEPOSIT" | TYPE = "DEPOSIT" | ||||
DEFAULT_WARNING_THRESHOLD = 120 | DEFAULT_WARNING_THRESHOLD = 120 | ||||
DEFAULT_CRITICAL_THRESHOLD = 3600 | DEFAULT_CRITICAL_THRESHOLD = 3600 | ||||
def __init__(self, obj): | def __init__(self, obj): | ||||
super().__init__(obj) | super().__init__(obj) | ||||
self._poll_interval = obj["poll_interval"] | self._poll_interval = obj["poll_interval"] | ||||
self._archive_path = obj["archive"] | self._archive_path = obj["archive"] | ||||
self._metadata_path = obj["metadata"] | self._metadata_path = obj["metadata"] | ||||
self._collection = obj["collection"] | self._collection = obj["collection"] | ||||
self._slug: Optional[str] = None | |||||
self._client = PublicApiDepositClient( | self._client = PublicApiDepositClient( | ||||
{ | { | ||||
"url": obj["server"], | "url": obj["server"], | ||||
"auth": {"username": obj["username"], "password": obj["password"]}, | "auth": {"username": obj["username"], "password": obj["password"]}, | ||||
} | } | ||||
) | ) | ||||
def upload_deposit(self): | def upload_deposit(self): | ||||
slug = "check-deposit-%s" % datetime.datetime.now().isoformat() | |||||
result = self._client.deposit_create( | result = self._client.deposit_create( | ||||
archive=self._archive_path, | archive=self._archive_path, | ||||
metadata=self._metadata_path, | metadata=self._metadata_path, | ||||
collection=self._collection, | collection=self._collection, | ||||
in_progress=False, | in_progress=False, | ||||
slug="check-deposit-%s" % datetime.datetime.now().isoformat(), | slug=slug, | ||||
) | ) | ||||
self._slug = slug | |||||
self._deposit_id = result["deposit_id"] | self._deposit_id = result["deposit_id"] | ||||
return result | return result | ||||
def update_deposit_with_metadata(self) -> Dict[str, Any]: | |||||
"""Trigger a metadata update on the deposit once it's completed. | |||||
""" | |||||
deposit = self.get_deposit_status() | |||||
assert deposit["deposit_status"] == "done" | |||||
swhid = deposit["deposit_swh_id"] | |||||
assert deposit["deposit_id"] == self._deposit_id | |||||
# We can reuse the initial metadata file we already sent | |||||
return self._client.deposit_update( | |||||
self._collection, | |||||
self._deposit_id, | |||||
self._slug, | |||||
metadata=self._metadata_path, | |||||
swhid=swhid, | |||||
) | |||||
def get_deposit_status(self): | def get_deposit_status(self): | ||||
return self._client.deposit_status( | return self._client.deposit_status( | ||||
collection=self._collection, deposit_id=self._deposit_id | collection=self._collection, deposit_id=self._deposit_id | ||||
) | ) | ||||
def wait_while_status(self, statuses, start_time, metrics, result): | def wait_while_status(self, statuses, start_time, metrics, result): | ||||
while result["deposit_status"] in statuses: | while result["deposit_status"] in statuses: | ||||
metrics["total_time"] = time.time() - start_time | metrics["total_time"] = time.time() - start_time | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | def main(self): | ||||
self.print_result( | self.print_result( | ||||
"CRITICAL", | "CRITICAL", | ||||
f'Deposit got unexpected status: {result["deposit_status"]} ' | f'Deposit got unexpected status: {result["deposit_status"]} ' | ||||
f'({result["deposit_status_detail"]})', | f'({result["deposit_status_detail"]})', | ||||
**metrics, | **metrics, | ||||
) | ) | ||||
return 2 | return 2 | ||||
# Everything went fine, check total time wasn't too large and | |||||
# print result | |||||
(status_code, status) = self.get_status(metrics["total_time"]) | (status_code, status) = self.get_status(metrics["total_time"]) | ||||
self.print_result( | self.print_result( | ||||
status, | status, | ||||
f'Deposit took {metrics["total_time"]:.2f}s and succeeded.', | f'Deposit took {metrics["total_time"]:.2f}s and succeeded.', | ||||
**metrics, | **metrics, | ||||
) | ) | ||||
result = self.update_deposit_with_metadata() | |||||
metrics["total_time"] = time.time() - start_time | |||||
metrics["update_time"] = ( | |||||
metrics["total_time"] | |||||
- metrics["upload_time"] | |||||
- metrics["validation_time"] | |||||
- metrics["load_time"] | |||||
) | |||||
if "error" in result: | |||||
self.print_result( | |||||
"CRITICAL", | |||||
f'Deposit metadata update failed: {result["error"]} ', | |||||
**metrics, | |||||
) | |||||
(status_code, status) = self.get_status(metrics["total_time"]) | |||||
ardumont: currently adding that missing test | |||||
self.print_result( | |||||
status, | |||||
f'Deposit Metadata update took {metrics["update_time"]:.2f}s ' | |||||
"and succeeded.", | |||||
**metrics, | |||||
) | |||||
return status_code | return status_code |
currently adding that missing test