Add diagnoser example for ip

This commit is contained in:
Alexandre Aubin 2018-08-29 23:48:16 +00:00
parent b42bd20311
commit 7fb694dbcc
2 changed files with 69 additions and 1 deletions

View file

@ -0,0 +1,55 @@
#!/usr/bin/env python
from moulinette import m18n
from moulinette.utils.network import download_text
from yunohost.diagnosis import Diagnoser
class IPDiagnoser(Diagnoser):
def validate_args(self, args):
if "version" not in args.keys():
return { "versions" : [4, 6] }
else:
if str(args["version"]) not in ["4", "6"]:
raise MoulinetteError(1, "Invalid version, should be 4 or 6.")
return { "versions" : [int(args["version"])] }
def run(self):
versions = self.args["versions"]
if 4 in versions:
ipv4 = self.get_public_ip(4)
yield dict(meta = {"version": "4"},
result = ipv4,
report = ("SUCCESS", m18n.n("diagnosis_network_connected_ipv4")) if ipv4 \
else ("ERROR", m18n.n("diagnosis_network_no_ipv4")))
if 6 in versions:
ipv6 = self.get_public_ip(6)
yield dict(meta = {"version": "6"},
result = ipv6,
report = ("SUCCESS", m18n.n("diagnosis_network_connected_ipv6")) if ipv6 \
else ("WARNING", m18n.n("diagnosis_network_no_ipv6")))
def get_public_ip(self, protocol=4):
if protocol == 4:
url = 'https://ip.yunohost.org'
elif protocol == 6:
url = 'https://ip6.yunohost.org'
else:
raise ValueError("invalid protocol version")
try:
return download_text(url, timeout=30).strip()
except Exception as e:
self.logger_debug("Could not get public IPv%s : %s" % (str(protocol), str(e)))
return None
def main(args, env, loggers):
return IPDiagnoser(args, env, loggers).report()

View file

@ -71,7 +71,7 @@ def diagnosis_run(categories=[], force=False, args=None):
path = [p for n, p in all_categories if n == category ][0]
# TODO : get the return value and do something with it
hook_exec(path, args=args, env=None)
return {"report": hook_exec(path, args=args, env=None) }
def diagnosis_ignore(category, args="", unignore=False):
@ -79,6 +79,19 @@ def diagnosis_ignore(category, args="", unignore=False):
############################################################
class Diagnoser():
def __init__(self, args, env, loggers):
self.logger_debug, self.logger_warning, self.logger_info = loggers
self.env = env
self.args = self.validate_args(args)
def report(self):
# TODO : implement some caching mecanism in there
return list(self.run())
def _list_diagnosis_categories():
hooks_raw = hook_list("diagnosis", list_by="priority", show_info=True)["hooks"]
hooks = []