This 'args' things sounds like a big YAGNI after all

This commit is contained in:
Alexandre Aubin 2019-07-20 19:02:11 +02:00
parent 24f9d475b8
commit d2bbb5a2b3
6 changed files with 54 additions and 85 deletions

View file

@ -1898,9 +1898,6 @@ diagnosis:
--force: --force:
help: Ignore the cached report even if it is still 'fresh' help: Ignore the cached report even if it is still 'fresh'
action: store_true action: store_true
-a:
help: Serialized arguments for diagnosis scripts (e.g. "domain=domain.tld")
full: --args
ignore: ignore:
action_help: Configure some diagnosis results to be ignored action_help: Configure some diagnosis results to be ignored

View file

@ -15,70 +15,65 @@ class IPDiagnoser(Diagnoser):
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
cache_duration = 60 cache_duration = 60
def validate_args(self, args):
if "version" not in args.keys():
return {"versions": [4, 6]}
else:
assert str(args["version"]) in ["4", "6"], "Invalid version, should be 4 or 6."
return {"versions": [int(args["version"])]}
def run(self): def run(self):
versions = self.args["versions"] #
# IPv4 Diagnosis
#
if 4 in versions: # If we can't ping, there's not much else we can do
if not self.can_ping_outside(4):
ipv4 = None
# If we do ping, check that we can resolv domain name
else:
can_resolve_dns = self.can_resolve_dns()
# And if we do, then we can fetch the public ip
if can_resolve_dns:
ipv4 = self.get_public_ip(4)
# If we can't ping, there's not much else we can do # In every case, we can check that resolvconf seems to be okay
if not self.can_ping_outside(4): # (symlink managed by resolvconf service + pointing to dnsmasq)
ipv4 = None good_resolvconf = self.resolvconf_is_symlink() and self.resolvconf_points_to_localhost()
# If we do ping, check that we can resolv domain name
else:
can_resolve_dns = self.can_resolve_dns()
# And if we do, then we can fetch the public ip
if can_resolve_dns:
ipv4 = self.get_public_ip(4)
# In every case, we can check that resolvconf seems to be okay # If we can't resolve domain names at all, that's a pretty big issue ...
# (symlink managed by resolvconf service + pointing to dnsmasq) # If it turns out that at the same time, resolvconf is bad, that's probably
good_resolvconf = self.resolvconf_is_symlink() and self.resolvconf_points_to_localhost() # 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", {}))
# 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", {}))
else:
# Well, maybe we could report a "success", "dns resolution is working", idk if it's worth it
pass
# If we can't resolve domain names at all, that's a pretty big issue ... # And finally, we actually report the ipv4 connectivity stuff
# If it turns out that at the same time, resolvconf is bad, that's probably yield dict(meta={"version": 4},
# the cause of this, so we use a different message in that case data=ipv4,
if not can_resolve_dns: status="SUCCESS" if ipv4 else "ERROR",
yield dict(meta={"name": "dnsresolution"}, summary=("diagnosis_ip_connected_ipv4", {}) if ipv4
status="ERROR", else ("diagnosis_ip_no_ipv4", {}))
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", {}))
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}, # IPv6 Diagnosis
data=ipv4, #
status="SUCCESS" if ipv4 else "ERROR",
summary=("diagnosis_ip_connected_ipv4", {}) if ipv4
else ("diagnosis_ip_no_ipv4", {}))
if 6 in versions: if not self.can_ping_outside(4):
ipv6 = None
else:
ipv6 = self.get_public_ip(6)
if not self.can_ping_outside(4): yield dict(meta={"version": 6},
ipv6 = None data=ipv6,
else: status="SUCCESS" if ipv6 else "WARNING",
ipv6 = self.get_public_ip(6) 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): def can_ping_outside(self, protocol=4):

View file

@ -14,14 +14,6 @@ class DNSRecordsDiagnoser(Diagnoser):
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] 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}
else:
assert args["domain"] in all_domains, "Unknown domain"
return {"domains": [args["domain"]]}
def run(self): def run(self):
resolvers = read_file("/etc/resolv.dnsmasq.conf").split("\n") resolvers = read_file("/etc/resolv.dnsmasq.conf").split("\n")
@ -32,7 +24,8 @@ class DNSRecordsDiagnoser(Diagnoser):
self.resolver = ipv4_resolvers[0] self.resolver = ipv4_resolvers[0]
main_domain = _get_maindomain() main_domain = _get_maindomain()
for domain in self.args["domains"]: all_domains = domain_list()["domains"]
for domain in all_domains:
self.logger_debug("Diagnosing DNS conf for %s" % domain) 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 yield report

View file

@ -21,10 +21,6 @@ class ServicesDiagnoser(Diagnoser):
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
cache_duration = 300 cache_duration = 300
def validate_args(self, args):
# TODO / FIXME Ugh do we really need this arg system
return {}
def run(self): def run(self):
all_result = service_status() all_result = service_status()

View file

@ -9,10 +9,6 @@ class DiskUsageDiagnoser(Diagnoser):
id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1]
cache_duration = 3600 * 24 cache_duration = 3600 * 24
def validate_args(self, args):
# TODO / FIXME Ugh do we really need this arg system
return {}
def run(self): def run(self):
disk_partitions = psutil.disk_partitions() disk_partitions = psutil.disk_partitions()

View file

@ -84,7 +84,7 @@ def diagnosis_show(categories=[], issues=False, full=False):
return {"reports": all_reports} return {"reports": all_reports}
def diagnosis_run(categories=[], force=False, args=None): def diagnosis_run(categories=[], force=False):
# Get all the categories # Get all the categories
all_categories = _list_diagnosis_categories() all_categories = _list_diagnosis_categories()
@ -98,13 +98,6 @@ def diagnosis_run(categories=[], force=False, args=None):
if unknown_categories: if unknown_categories:
raise YunohostError('unknown_categories', categories=", ".join(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("&")}
else:
args = {}
args["force"] = force
issues = [] issues = []
# Call the hook ... # Call the hook ...
diagnosed_categories = [] diagnosed_categories = []
@ -113,7 +106,7 @@ def diagnosis_run(categories=[], force=False, args=None):
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: try:
code, report = hook_exec(path, args=args, env=None) code, report = hook_exec(path, args={"force": force}, env=None)
except Exception as e: 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: else:
@ -143,7 +136,6 @@ class Diagnoser():
self.logger_debug, self.logger_warning, self.logger_info = loggers self.logger_debug, self.logger_warning, self.logger_info = loggers
self.env = env self.env = env
self.args = args or {} self.args = args or {}
self.args.update(self.validate_args(self.args))
self.cache_file = Diagnoser.cache_file(self.id_) self.cache_file = Diagnoser.cache_file(self.id_)
descr_key = "diagnosis_description_" + self.id_ descr_key = "diagnosis_description_" + self.id_