mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Make the PEP8 gods less angry
This commit is contained in:
parent
5f4450ab87
commit
aed53786f2
3 changed files with 51 additions and 58 deletions
|
@ -3,13 +3,13 @@
|
|||
import os
|
||||
import random
|
||||
|
||||
from moulinette import m18n
|
||||
from moulinette.utils.network import download_text
|
||||
from moulinette.utils.process import check_output
|
||||
from moulinette.utils.filesystem import read_file
|
||||
|
||||
from yunohost.diagnosis import Diagnoser
|
||||
|
||||
|
||||
class IPDiagnoser(Diagnoser):
|
||||
|
||||
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
|
||||
|
@ -17,10 +17,10 @@ class IPDiagnoser(Diagnoser):
|
|||
|
||||
def validate_args(self, args):
|
||||
if "version" not in args.keys():
|
||||
return { "versions" : [4, 6] }
|
||||
return {"versions": [4, 6]}
|
||||
else:
|
||||
assert str(args["version"]) in ["4", "6"], "Invalid version, should be 4 or 6."
|
||||
return { "versions" : [int(args["version"])] }
|
||||
return {"versions": [int(args["version"])]}
|
||||
|
||||
def run(self):
|
||||
|
||||
|
@ -46,26 +46,26 @@ class IPDiagnoser(Diagnoser):
|
|||
# If it turns out that at the same time, resolvconf is bad, that's probably
|
||||
# the cause of this, so we use a different message in that case
|
||||
if not can_resolve_dns:
|
||||
yield dict(meta = {"name": "dnsresolution"},
|
||||
status = "ERROR",
|
||||
summary = ("diagnosis_ip_broken_dnsresolution", {}) if good_resolvconf
|
||||
else ("diagnosis_ip_broken_resolvconf", {}))
|
||||
yield dict(meta={"name": "dnsresolution"},
|
||||
status="ERROR",
|
||||
summary=("diagnosis_ip_broken_dnsresolution", {}) if good_resolvconf
|
||||
else ("diagnosis_ip_broken_resolvconf", {}))
|
||||
# Otherwise, if the resolv conf is bad but we were able to resolve domain name,
|
||||
# still warn that we're using a weird resolv conf ...
|
||||
elif not good_resolvconf:
|
||||
yield dict(meta = {"name": "dnsresolution"},
|
||||
status = "WARNING",
|
||||
summary = ("diagnosis_ip_weird_resolvconf", {}))
|
||||
yield dict(meta={"name": "dnsresolution"},
|
||||
status="WARNING",
|
||||
summary=("diagnosis_ip_weird_resolvconf", {}))
|
||||
else:
|
||||
# Well, maybe we could report a "success", "dns resolution is working", idk if it's worth it
|
||||
pass
|
||||
|
||||
# And finally, we actually report the ipv4 connectivity stuff
|
||||
yield dict(meta = {"version": 4},
|
||||
data = ipv4,
|
||||
status = "SUCCESS" if ipv4 else "ERROR",
|
||||
summary = ("diagnosis_ip_connected_ipv4", {}) if ipv4 \
|
||||
else ("diagnosis_ip_no_ipv4", {}))
|
||||
yield dict(meta={"version": 4},
|
||||
data=ipv4,
|
||||
status="SUCCESS" if ipv4 else "ERROR",
|
||||
summary=("diagnosis_ip_connected_ipv4", {}) if ipv4
|
||||
else ("diagnosis_ip_no_ipv4", {}))
|
||||
|
||||
if 6 in versions:
|
||||
|
||||
|
@ -74,12 +74,11 @@ class IPDiagnoser(Diagnoser):
|
|||
else:
|
||||
ipv6 = self.get_public_ip(6)
|
||||
|
||||
yield dict(meta = {"version": 6},
|
||||
data = ipv6,
|
||||
status = "SUCCESS" if ipv6 else "WARNING",
|
||||
summary = ("diagnosis_ip_connected_ipv6", {}) if ipv6 \
|
||||
else ("diagnosis_ip_no_ipv6", {}))
|
||||
|
||||
yield dict(meta={"version": 6},
|
||||
data=ipv6,
|
||||
status="SUCCESS" if ipv6 else "WARNING",
|
||||
summary=("diagnosis_ip_connected_ipv6", {}) if ipv6
|
||||
else ("diagnosis_ip_no_ipv6", {}))
|
||||
|
||||
def can_ping_outside(self, protocol=4):
|
||||
|
||||
|
@ -113,11 +112,9 @@ class IPDiagnoser(Diagnoser):
|
|||
random.shuffle(resolvers)
|
||||
return any(ping(protocol, resolver) for resolver in resolvers[:5])
|
||||
|
||||
|
||||
def can_resolve_dns(self):
|
||||
return os.system("dig +short ip.yunohost.org >/dev/null 2>/dev/null") == 0
|
||||
|
||||
|
||||
def resolvconf_is_symlink(self):
|
||||
return os.path.realpath("/etc/resolv.conf") == "/run/resolvconf/resolv.conf"
|
||||
|
||||
|
@ -126,7 +123,6 @@ class IPDiagnoser(Diagnoser):
|
|||
resolvers = [r.split(" ")[1] for r in read_file(file_).split("\n") if r.startswith("nameserver")]
|
||||
return resolvers == ["127.0.0.1"]
|
||||
|
||||
|
||||
def get_public_ip(self, protocol=4):
|
||||
|
||||
# FIXME - TODO : here we assume that DNS resolution for ip.yunohost.org is working
|
||||
|
@ -149,4 +145,3 @@ class IPDiagnoser(Diagnoser):
|
|||
|
||||
def main(args, env, loggers):
|
||||
return IPDiagnoser(args, env, loggers).diagnose()
|
||||
|
||||
|
|
|
@ -2,25 +2,25 @@
|
|||
|
||||
import os
|
||||
|
||||
from moulinette.utils.network import download_text
|
||||
from moulinette.utils.process import check_output
|
||||
from moulinette.utils.filesystem import read_file
|
||||
|
||||
from yunohost.diagnosis import Diagnoser
|
||||
from yunohost.domain import domain_list, _build_dns_conf, _get_maindomain
|
||||
|
||||
|
||||
class DNSRecordsDiagnoser(Diagnoser):
|
||||
|
||||
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
|
||||
cache_duration = 3600*24
|
||||
cache_duration = 3600 * 24
|
||||
|
||||
def validate_args(self, args):
|
||||
all_domains = domain_list()["domains"]
|
||||
if "domain" not in args.keys():
|
||||
return { "domains" : all_domains }
|
||||
return {"domains": all_domains}
|
||||
else:
|
||||
assert args["domain"] in all_domains, "Unknown domain"
|
||||
return { "domains" : [ args["domain"] ] }
|
||||
return {"domains": [args["domain"]]}
|
||||
|
||||
def run(self):
|
||||
|
||||
|
@ -34,7 +34,7 @@ class DNSRecordsDiagnoser(Diagnoser):
|
|||
|
||||
for domain in self.args["domains"]:
|
||||
self.logger_debug("Diagnosing DNS conf for %s" % domain)
|
||||
for report in self.check_domain(domain, domain==main_domain):
|
||||
for report in self.check_domain(domain, domain == main_domain):
|
||||
yield report
|
||||
|
||||
def check_domain(self, domain, is_main_domain):
|
||||
|
@ -44,13 +44,13 @@ class DNSRecordsDiagnoser(Diagnoser):
|
|||
# Here if there are no AAAA record, we should add something to expect "no" AAAA record
|
||||
# to properly diagnose situations where people have a AAAA record but no IPv6
|
||||
|
||||
for category, records in expected_configuration.items():
|
||||
for category, records in expected_configuration.items():
|
||||
|
||||
discrepancies = []
|
||||
|
||||
for r in records:
|
||||
current_value = self.get_current_record(domain, r["name"], r["type"]) or "None"
|
||||
expected_value = r["value"] if r["value"] != "@" else domain+"."
|
||||
expected_value = r["value"] if r["value"] != "@" else domain + "."
|
||||
|
||||
if current_value == "None":
|
||||
discrepancies.append(("diagnosis_dns_missing_record", (r["type"], r["name"], expected_value)))
|
||||
|
@ -64,16 +64,15 @@ class DNSRecordsDiagnoser(Diagnoser):
|
|||
status = "SUCCESS"
|
||||
summary = ("diagnosis_dns_good_conf", {"domain": domain, "category": category})
|
||||
|
||||
output = dict(meta = {"domain": domain, "category": category},
|
||||
status = status,
|
||||
summary = summary)
|
||||
output = dict(meta={"domain": domain, "category": category},
|
||||
status=status,
|
||||
summary=summary)
|
||||
|
||||
if discrepancies:
|
||||
output["details"] = discrepancies
|
||||
|
||||
yield output
|
||||
|
||||
|
||||
def get_current_record(self, domain, name, type_):
|
||||
if name == "@":
|
||||
command = "dig +short @%s %s %s" % (self.resolver, type_, domain)
|
||||
|
@ -83,12 +82,10 @@ class DNSRecordsDiagnoser(Diagnoser):
|
|||
# e.g. no internet connectivity (dependency mechanism to good result from 'ip' diagosis ?)
|
||||
# or the resolver is unavailable for some reason
|
||||
output = check_output(command).strip()
|
||||
output = output.replace("\;",";")
|
||||
if output.startswith('"') and output.endswith('"'):
|
||||
output = '"' + ' '.join(output.replace('"',' ').split()) + '"'
|
||||
output = '"' + ' '.join(output.replace('"', ' ').split()) + '"'
|
||||
return output
|
||||
|
||||
|
||||
def main(args, env, loggers):
|
||||
return DNSRecordsDiagnoser(args, env, loggers).diagnose()
|
||||
|
||||
|
|
|
@ -38,21 +38,23 @@ logger = log.getActionLogger('yunohost.diagnosis')
|
|||
|
||||
DIAGNOSIS_CACHE = "/var/cache/yunohost/diagnosis/"
|
||||
|
||||
|
||||
def diagnosis_list():
|
||||
all_categories_names = [ h for h, _ in _list_diagnosis_categories() ]
|
||||
return { "categories": all_categories_names }
|
||||
all_categories_names = [h for h, _ in _list_diagnosis_categories()]
|
||||
return {"categories": all_categories_names}
|
||||
|
||||
|
||||
def diagnosis_show(categories=[], issues=False, full=False):
|
||||
|
||||
# Get all the categories
|
||||
all_categories = _list_diagnosis_categories()
|
||||
all_categories_names = [ category for category, _ in all_categories ]
|
||||
all_categories_names = [category for category, _ in all_categories]
|
||||
|
||||
# Check the requested category makes sense
|
||||
if categories == []:
|
||||
categories = all_categories_names
|
||||
else:
|
||||
unknown_categories = [ c for c in categories if c not in all_categories_names ]
|
||||
unknown_categories = [c for c in categories if c not in all_categories_names]
|
||||
if unknown_categories:
|
||||
raise YunohostError('unknown_categories', categories=", ".join(categories))
|
||||
|
||||
|
@ -62,7 +64,7 @@ def diagnosis_show(categories=[], issues=False, full=False):
|
|||
try:
|
||||
report = Diagnoser.get_cached_report(category)
|
||||
except Exception as e:
|
||||
logger.error("Failed to fetch diagnosis result for category '%s' : %s" % (category, str(e))) # FIXME : i18n
|
||||
logger.error("Failed to fetch diagnosis result for category '%s' : %s" % (category, str(e))) # FIXME : i18n
|
||||
else:
|
||||
if not full:
|
||||
del report["timestamp"]
|
||||
|
@ -72,33 +74,33 @@ def diagnosis_show(categories=[], issues=False, full=False):
|
|||
if "data" in item:
|
||||
del item["data"]
|
||||
if issues:
|
||||
report["items"] = [ item for item in report["items"] if item["status"] != "SUCCESS" ]
|
||||
report["items"] = [item for item in report["items"] if item["status"] != "SUCCESS"]
|
||||
# Ignore this category if no issue was found
|
||||
if not report["items"]:
|
||||
continue
|
||||
|
||||
all_reports.append(report)
|
||||
|
||||
|
||||
return {"reports": all_reports}
|
||||
|
||||
|
||||
def diagnosis_run(categories=[], force=False, args=None):
|
||||
|
||||
# Get all the categories
|
||||
all_categories = _list_diagnosis_categories()
|
||||
all_categories_names = [ category for category, _ in all_categories ]
|
||||
all_categories_names = [category for category, _ in all_categories]
|
||||
|
||||
# Check the requested category makes sense
|
||||
if categories == []:
|
||||
categories = all_categories_names
|
||||
else:
|
||||
unknown_categories = [ c for c in categories if c not in all_categories_names ]
|
||||
unknown_categories = [c for c in categories if c not in all_categories_names]
|
||||
if unknown_categories:
|
||||
raise YunohostError('unknown_categories', categories=", ".join(unknown_categories))
|
||||
|
||||
# Transform "arg1=val1&arg2=val2" to { "arg1": "val1", "arg2": "val2" }
|
||||
if args is not None:
|
||||
args = { arg.split("=")[0]: arg.split("=")[1] for arg in args.split("&") }
|
||||
args = {arg.split("=")[0]: arg.split("=")[1] for arg in args.split("&")}
|
||||
else:
|
||||
args = {}
|
||||
args["force"] = force
|
||||
|
@ -108,12 +110,12 @@ def diagnosis_run(categories=[], force=False, args=None):
|
|||
diagnosed_categories = []
|
||||
for category in categories:
|
||||
logger.debug("Running diagnosis for %s ..." % category)
|
||||
path = [p for n, p in all_categories if n == category ][0]
|
||||
path = [p for n, p in all_categories if n == category][0]
|
||||
|
||||
try:
|
||||
code, report = hook_exec(path, args=args, env=None)
|
||||
except Exception as e:
|
||||
logger.error("Diagnosis failed for category '%s' : %s" % (category, str(e)), exc_info=True) # FIXME : i18n
|
||||
logger.error("Diagnosis failed for category '%s' : %s" % (category, str(e)), exc_info=True) # FIXME : i18n
|
||||
else:
|
||||
diagnosed_categories.append(category)
|
||||
if report != {}:
|
||||
|
@ -127,6 +129,7 @@ def diagnosis_run(categories=[], force=False, args=None):
|
|||
|
||||
return
|
||||
|
||||
|
||||
def diagnosis_ignore(category, args="", unignore=False):
|
||||
pass
|
||||
|
||||
|
@ -149,7 +152,6 @@ class Diagnoser():
|
|||
if self.description == descr_key:
|
||||
self.description = self.id_
|
||||
|
||||
|
||||
def cached_time_ago(self):
|
||||
|
||||
if not os.path.exists(self.cache_file):
|
||||
|
@ -172,10 +174,9 @@ class Diagnoser():
|
|||
|
||||
items = list(self.run())
|
||||
|
||||
new_report = { "id": self.id_,
|
||||
"cached_for": self.cache_duration,
|
||||
"items": items
|
||||
}
|
||||
new_report = {"id": self.id_,
|
||||
"cached_for": self.cache_duration,
|
||||
"items": items}
|
||||
|
||||
# TODO / FIXME : should handle the case where we only did a partial diagnosis
|
||||
self.logger_debug("Updating cache %s" % self.cache_file)
|
||||
|
@ -227,13 +228,13 @@ class Diagnoser():
|
|||
item["summary"] = m18n.n(summary_key, **summary_args)
|
||||
|
||||
if "details" in item:
|
||||
item["details"] = [ m18n.n(key, *values) for key, values in item["details"] ]
|
||||
item["details"] = [m18n.n(key, *values) for key, values in item["details"]]
|
||||
|
||||
|
||||
def _list_diagnosis_categories():
|
||||
hooks_raw = hook_list("diagnosis", list_by="priority", show_info=True)["hooks"]
|
||||
hooks = []
|
||||
for _, some_hooks in sorted(hooks_raw.items(), key=lambda h:int(h[0])):
|
||||
for _, some_hooks in sorted(hooks_raw.items(), key=lambda h: int(h[0])):
|
||||
for name, info in some_hooks.items():
|
||||
hooks.append((name, info["path"]))
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue