From 66ca69f060498fd8a805ace0ca6e0c93aa4b669e Mon Sep 17 00:00:00 2001 From: Mustafa Baser Date: Wed, 12 Jan 2022 14:23:48 +0300 Subject: [PATCH] feat: enable/disable admin-ui frontend plugins (ref: #150) --- static/scripts/admin_ui_plugin.py | 87 +++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 static/scripts/admin_ui_plugin.py diff --git a/static/scripts/admin_ui_plugin.py b/static/scripts/admin_ui_plugin.py new file mode 100644 index 00000000000..52046748f69 --- /dev/null +++ b/static/scripts/admin_ui_plugin.py @@ -0,0 +1,87 @@ +import os +import time +import glob +import json +import shutil +from collections import OrderedDict + +mydir = os.getcwd() +dirs = glob.glob('/opt/jans/jans-setup/output/gluu-admin-ui*') +run_cmd = '/bin/su node -c "PATH=$PATH:/opt/jre/bin:/opt/node/bin {}"' + +for d in dirs: + if os.path.exists(os.path.join(d, '.env')): + uid_admin_dir = d + break +else: + print("Admin UI installation directory not found.") + sys.exit() + +os.chdir(uid_admin_dir) +plugin_json_fn = os.path.join(uid_admin_dir, 'plugins.config.json') + +def read_plugins(): + with open(plugin_json_fn) as f: + plugins = json.load(f, object_pairs_hook=OrderedDict) + return plugins + +plugins = read_plugins() + +def print_plugins(): + print("Available Plugins") + for i, p in enumerate(plugins): + e = '\033[92m*\033[0m' if p.get('enabled') else ' ' + print('{} {} {}'.format(e, i+1, p.get('title') or p.get('key'))) + print() + + +def exec_command(cmd): + print("\033[1mExecuting {}\033[0m".format(cmd)) + os.system(run_cmd.format(cmd)) + + +def build_copy(): + exec_command('npm run build:prod') + admin_dir = '/var/www/html/admin' + if os.path.exists(admin_dir): + os.rename(admin_dir, admin_dir + '.' + time.ctime()) + + print("Copying admin ui files to apache directory") + shutil.copytree(os.path.join(uid_admin_dir, 'dist'), admin_dir) + + +while True: + print_plugins() + user_input = input('Enable/Disable/Add/Finish/Quit [\033[92me\033[0m/d/a/f/q]: ') + if user_input: + choice = user_input.lower()[0] + if choice == 'q': + print("Exiting without modification") + break + + elif choice == 'f': + build_copy() + break + + elif choice in ('e', 'd'): + pactive = True if choice == 'e' else False + choice_str = {'e': 'enable', 'd':'disable'}[choice] + plugin_number = input('Enter plugin number to {} :'.format(choice_str)) + if plugin_number.isdigit() and int(plugin_number) <= len(plugins): + pn = int(plugin_number) - 1 + for i, p in enumerate(plugins): + if i == pn: + exec_command('npm run plugin:{} {}'.format(choice_str, p['key'])) + plugins = read_plugins() + break + elif choice == 'a': + plugin_fn = input('Enter path of plugin: ') + if plugin_fn.lower().endswith('.zip'): + exec_command('npm run plugin:install {}'.format(plugin_fn)) + plugins = read_plugins() + else: + print("Can't find \033[31m{}\033[0m".format(plugin_metadata_fn)) + print() + +with open(plugin_json_fn, 'w') as w: + json.dump(plugins, w, indent=2)