From b42bd20311797f59feb9ff7476ed19e49d20f8e5 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Wed, 29 Aug 2018 01:34:15 +0000 Subject: [PATCH] First draft for diagnosis_run --- data/actionsmap/yunohost.yml | 2 +- locales/en.json | 1 + src/yunohost/diagnosis.py | 38 +++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index 4f849160f..aa85fdf70 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -1893,7 +1893,7 @@ diagnosis: help: Diagnosis categories to run (all by default) nargs: "*" --force: - help: Display additional information + help: Ignore the cached report even if it is still 'fresh' action: store_true -a: help: Serialized arguments for diagnosis scripts (e.g. "domain=domain.tld") diff --git a/locales/en.json b/locales/en.json index f681fc4ea..a91da4fe9 100644 --- a/locales/en.json +++ b/locales/en.json @@ -547,6 +547,7 @@ "user_update_failed": "Could not update user {user}: {error}", "user_updated": "User info changed", "users_available": "Available users:", + "unknown_categories": "The following categories are unknown : {categories}", "yunohost_already_installed": "YunoHost is already installed", "yunohost_ca_creation_failed": "Could not create certificate authority", "yunohost_ca_creation_success": "Local certification authority created.", diff --git a/src/yunohost/diagnosis.py b/src/yunohost/diagnosis.py index 0d312a7c1..10f09a576 100644 --- a/src/yunohost/diagnosis.py +++ b/src/yunohost/diagnosis.py @@ -24,14 +24,18 @@ Look for possible issues on the server """ +import errno + from moulinette import m18n from moulinette.core import MoulinetteError from moulinette.utils import log -from yunohost.hook import hook_list +from yunohost.hook import hook_list, hook_exec 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 } @@ -39,8 +43,36 @@ def diagnosis_list(): def diagnosis_report(categories=[], full=False): pass -def diagnosis_run(categories=[], force=False, args=""): - pass +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 ] + + # 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 ] + if unknown_categories: + raise MoulinetteError(m18n.n('unknown_categories', categories=", ".join(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 + + + # Call the hook ... + for category in categories: + logger.debug("Running diagnosis for %s ..." % category) + 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) + def diagnosis_ignore(category, args="", unignore=False): pass