Skip to content

Commit

Permalink
Merge pull request #10 from livelyapps/9-safe-mode
Browse files Browse the repository at this point in the history
pluploader/pluploader#9 - added safe-mode command
  • Loading branch information
Fabian Siegel authored Jul 28, 2020
2 parents 99a139e + 52bba7e commit 63527d8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pluploader/upmapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
61 changes: 61 additions & 0 deletions scripts/pluploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,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()
Expand All @@ -122,6 +129,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)

Expand Down Expand Up @@ -308,6 +322,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 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)
except Exception as e:
logging.error(f"An error occured - check your credentials")
logging.error(f"{e}")
sys.exit(1)


if __name__ == "__main__":
main()

0 comments on commit 63527d8

Please sign in to comment.