From 22d428078e0e132dd109181abcf4398e3da4add8 Mon Sep 17 00:00:00 2001 From: Fabian Siegel Date: Thu, 23 Jul 2020 18:03:39 +0200 Subject: [PATCH 1/2] pluploader/pluploader#9 - added safe-mode command Signed-off-by: Fabian Siegel --- pluploader/upmapi.py | 26 ++++++++++++++++++ scripts/pluploader.py | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/pluploader/upmapi.py b/pluploader/upmapi.py index cf991d5..6d1863f 100644 --- a/pluploader/upmapi.py +++ b/pluploader/upmapi.py @@ -195,3 +195,29 @@ def module_status(previous_request: dict) -> typing.Tuple[int, int, typing.List[ else: disabled_modules.append(module) return all_modules, enabled_modules, disabled_modules + + +def get_safemode(base_url: furl) -> bool: + request_url = base_url.copy() + request_url.add(path=UPM_API_ENDPOINT) + request_url.add(path="safe-mode") + response = requests.get(request_url.url) + return response.json().get("enabled") + + +def enable_disable_safemode(base_url: furl, enable: bool, keepState: bool = False) -> bool: + headers = {"Content-Type": "application/vnd.atl.plugins.safe.mode.flag+json"} + data = { + "enabled": enable, + "links": {}, + } + + request_url = base_url.copy() + request_url.add(path=UPM_API_ENDPOINT) + request_url.add(path="safe-mode") + request_url.add(query_params={"keepState": "true" if keepState else "false"}) + response = requests.put(request_url.url, + headers=headers, + json=data) + response_json = response.json() + return "subCode" not in response_json and response_json["enabled"] == enable diff --git a/scripts/pluploader.py b/scripts/pluploader.py index bf8cf63..7088229 100755 --- a/scripts/pluploader.py +++ b/scripts/pluploader.py @@ -96,6 +96,13 @@ def main(): uninstall_parser = commandparser.add_parser("uninstall") uninstall_parser.add_argument("plugin", nargs='?', default=None) + safemode_parser = commandparser.add_parser("safe-mode") + safemode_subparser = safemode_parser.add_subparsers(dest="subcommand") + safemode_status_parser = safemode_subparser.add_parser("status") + safemode_enable_parser = safemode_subparser.add_parser("enable") + safemode_disable_parser = safemode_subparser.add_parser("disable") + safemode_disable_parser.add_argument("--keep-state", default=False, action="store_true") + commandparser.add_parser("install") args = p.parse_args() @@ -115,6 +122,13 @@ def main(): uninstall_plugin(base_url, args) elif (args.command == "info"): plugin_info(base_url, args) + elif args.command == "safe-mode": + if args.subcommand == "status" or args.subcommand is None: + safemode_status(base_url, args) + elif args.subcommand == "enable": + safemode_enable(base_url, args) + elif args.subcommand == "disable": + safemode_disable(base_url, args) else: install(base_url, args) @@ -296,5 +310,53 @@ def install(base_url, args): logging.error("An error occured while uploading plugin") sys.exit(1) + +def safemode_status(base_url, args): + try: + safemode_st = f"{Fore.YELLOW}enabled{Fore.RESET}" if upm.get_safemode( + base_url) else f"{Fore.GREEN}disabled{Fore.RESET}" + logging.info(f"Safe-mode is currently {safemode_st}") + except requests.exceptions.ConnectionError: + logging.error("Could not connect to host - check your base-url") + sys.exit(1) + except Exception as e: + logging.error(f"An error occured - check your credentials") + logging.error(f"{e}") + sys.exit(1) + + +def safemode_enable(base_url, args): + try: + success = upm.enable_disable_safemode(base_url, True) + if success: + logging.info(f"Safe-mode is now {Fore.GREEN}enabled{Fore.RESET}") + else: + logging.error(f"Could not enable safe-mode - is safe-mode already enabled?") + except requests.exceptions.ConnectionError: + logging.error("Could not connect to host - check your base-url") + sys.exit(1) + except Exception as e: + logging.error(f"An error occured - check your credentials") + logging.error(f"{e}") + sys.exit(1) + + +def safemode_disable(base_url, args): + try: + success = upm.enable_disable_safemode(base_url, False, args.keep_state) + if success: + logging.info( + f"Safe-mode is now {Fore.GREEN}disabled{Fore.RESET}, all plugins {'got restored' if not args.keep_state else 'stayed disabled'}.") + else: + logging.error(f"Could not enable safe-mode - is safe-mode already disabled?") + except requests.exceptions.ConnectionError: + logging.error("Could not connect to host - check your base-url") + sys.exit(1) + except Exception as e: + logging.error(f"An error occured - check your credentials") + logging.error(f"{e}") + sys.exit(1) + + if __name__ == "__main__": main() From 8924e68df971fb4ffd82f0e679be554bac6e44b7 Mon Sep 17 00:00:00 2001 From: Fabian Siegel Date: Tue, 28 Jul 2020 14:17:25 +0200 Subject: [PATCH 2/2] Fixed typo --- scripts/pluploader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pluploader.py b/scripts/pluploader.py index 7088229..ec0ad1f 100755 --- a/scripts/pluploader.py +++ b/scripts/pluploader.py @@ -348,7 +348,7 @@ def safemode_disable(base_url, args): logging.info( f"Safe-mode is now {Fore.GREEN}disabled{Fore.RESET}, all plugins {'got restored' if not args.keep_state else 'stayed disabled'}.") else: - logging.error(f"Could not enable safe-mode - is safe-mode already disabled?") + logging.error(f"Could not disable safe-mode - is safe-mode already disabled?") except requests.exceptions.ConnectionError: logging.error("Could not connect to host - check your base-url") sys.exit(1)