From 91ec775ebb695b7a4e3a58951b51a6bd343dfc20 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Wed, 31 Jul 2019 16:54:25 +0200 Subject: [PATCH] Implement basic dependency system between diagnoser --- data/hooks/diagnosis/10-ip.py | 1 + data/hooks/diagnosis/12-dnsrecords.py | 1 + data/hooks/diagnosis/14-ports.py | 1 + data/hooks/diagnosis/16-http.py | 3 +-- data/hooks/diagnosis/30-services.py | 1 + data/hooks/diagnosis/50-diskusage.py | 1 + src/yunohost/diagnosis.py | 29 +++++++++++++++++---------- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/data/hooks/diagnosis/10-ip.py b/data/hooks/diagnosis/10-ip.py index a4cfc0a48..b29076467 100644 --- a/data/hooks/diagnosis/10-ip.py +++ b/data/hooks/diagnosis/10-ip.py @@ -14,6 +14,7 @@ class IPDiagnoser(Diagnoser): id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] cache_duration = 60 + dependencies = [] def run(self): diff --git a/data/hooks/diagnosis/12-dnsrecords.py b/data/hooks/diagnosis/12-dnsrecords.py index 0f47ff136..0e8aaa07e 100644 --- a/data/hooks/diagnosis/12-dnsrecords.py +++ b/data/hooks/diagnosis/12-dnsrecords.py @@ -13,6 +13,7 @@ class DNSRecordsDiagnoser(Diagnoser): id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] cache_duration = 3600 * 24 + dependencies = ["ip"] def run(self): diff --git a/data/hooks/diagnosis/14-ports.py b/data/hooks/diagnosis/14-ports.py index 8206474f8..82a44384a 100644 --- a/data/hooks/diagnosis/14-ports.py +++ b/data/hooks/diagnosis/14-ports.py @@ -11,6 +11,7 @@ class PortsDiagnoser(Diagnoser): id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] cache_duration = 3600 + dependencies = ["ip"] def run(self): diff --git a/data/hooks/diagnosis/16-http.py b/data/hooks/diagnosis/16-http.py index b6b92fc77..cc335df8b 100644 --- a/data/hooks/diagnosis/16-http.py +++ b/data/hooks/diagnosis/16-http.py @@ -13,6 +13,7 @@ class HttpDiagnoser(Diagnoser): id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] cache_duration = 3600 + dependencies = ["ip"] def run(self): @@ -28,7 +29,6 @@ class HttpDiagnoser(Diagnoser): try: r = requests.post('https://ynhdiagnoser.netlib.re/check-http', json={'domain': domain, "nonce": nonce}, timeout=30).json() - print(r) if "status" not in r.keys(): raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) elif r["status"] == "error" and ("code" not in r.keys() or r["code"] not in ["error_http_check_connection_error", "error_http_check_unknown_error"]): @@ -37,7 +37,6 @@ class HttpDiagnoser(Diagnoser): else: raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) except Exception as e: - print(e) raise YunohostError("diagnosis_http_could_not_diagnose", error=e) if r["status"] == "ok": diff --git a/data/hooks/diagnosis/30-services.py b/data/hooks/diagnosis/30-services.py index 5029e0a5d..6589d83f2 100644 --- a/data/hooks/diagnosis/30-services.py +++ b/data/hooks/diagnosis/30-services.py @@ -20,6 +20,7 @@ class ServicesDiagnoser(Diagnoser): id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] cache_duration = 300 + dependencies = [] def run(self): diff --git a/data/hooks/diagnosis/50-diskusage.py b/data/hooks/diagnosis/50-diskusage.py index 2c6fe387b..74b8eb4b9 100644 --- a/data/hooks/diagnosis/50-diskusage.py +++ b/data/hooks/diagnosis/50-diskusage.py @@ -8,6 +8,7 @@ class DiskUsageDiagnoser(Diagnoser): id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] cache_duration = 3600 * 24 + dependencies = [] def run(self): diff --git a/src/yunohost/diagnosis.py b/src/yunohost/diagnosis.py index e7aca585f..14b332fe3 100644 --- a/src/yunohost/diagnosis.py +++ b/src/yunohost/diagnosis.py @@ -137,12 +137,7 @@ class Diagnoser(): self.env = env self.args = args or {} self.cache_file = Diagnoser.cache_file(self.id_) - - descr_key = "diagnosis_description_" + self.id_ - self.description = m18n.n(descr_key) - # If no description available, fallback to id - if self.description == descr_key: - self.description = self.id_ + self.description = Diagnoser.get_description(self.id_) def cached_time_ago(self): @@ -159,9 +154,18 @@ class Diagnoser(): if not self.args.get("force", False) and self.cached_time_ago() < self.cache_duration: self.logger_debug("Cache still valid : %s" % self.cache_file) + # FIXME : i18n logger.info("(Cache still valid for %s diagnosis. Not re-diagnosing yet!)" % self.description) return 0, {} + for dependency in self.dependencies: + dep_report = Diagnoser.get_cached_report(dependency) + dep_errors = [item for item in dep_report["items"] if item["status"] == "ERROR"] + if dep_errors: + # FIXME : i18n + logger.error("Can't run diagnosis for %s while there are important issues related to %s." % (self.description, Diagnoser.get_description(dependency))) + return 1, {} + self.logger_debug("Running diagnostic for %s" % self.id_) items = list(self.run()) @@ -200,6 +204,13 @@ class Diagnoser(): Diagnoser.i18n(report) return report + @staticmethod + def get_description(id_): + key = "diagnosis_description_" + id_ + descr = m18n.n(key) + # If no description available, fallback to id + return descr if descr != key else id_ + @staticmethod def i18n(report): @@ -209,11 +220,7 @@ class Diagnoser(): # was generated ... e.g. if the diagnosing happened inside a cron job with locale EN # instead of FR used by the actual admin... - descr_key = "diagnosis_description_" + report["id"] - report["description"] = m18n.n(descr_key) - # If no description available, fallback to id - if report["description"] == descr_key: - report["description"] = report["id"] + report["description"] = Diagnoser.get_description(report["id"]) for item in report["items"]: summary_key, summary_args = item["summary"]