Compare commits

..

2 Commits

Author SHA1 Message Date
Ferdinand Thiessen e9fbce0da7 [cli] Added install command to install the database and all plugins
Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
2022-07-31 16:33:55 +02:00
Ferdinand Thiessen 47b838804b [plugins][cli] Fix initial migration file + Make sure plugin permissions are installed
Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
2022-07-31 16:33:09 +02:00
2 changed files with 19 additions and 16 deletions

View File

@ -18,27 +18,28 @@ def install_plugin_command(ctx, plugin, all):
"""Install one or more plugins""" """Install one or more plugins"""
if not all and len(plugin) == 0: if not all and len(plugin) == 0:
ctx.fail("At least one plugin must be specified, or use `--all` flag.") ctx.fail("At least one plugin must be specified, or use `--all` flag.")
if all: if all:
plugins = current_app.config["FG_PLUGINS"].values() plugins = current_app.config["FG_PLUGINS"]
else: else:
try: try:
plugins = [current_app.config["FG_PLUGINS"][p] for p in plugin] plugins = {plugin_name: current_app.config["FG_PLUGINS"][plugin_name] for plugin_name in plugin}
except KeyError as e: except KeyError as e:
ctx.fail(f"Invalid plugin ID, could not find >{e.args[0]}<") ctx.fail(f"Invalid plugin name, could not find >{e.args[0]}<")
for p in plugins:
name = p.id.split(".")[-1] for name, plugin in plugins.items():
click.echo(f"Installing {name}{'.'*(20-len(name))}", nl=False) click.echo(f"Installing {name}{'.'*(20-len(name))}", nl=False)
# Install permissions # Install permissions
if p.permissions: if plugin.permissions:
cur_perm = set(x.name for x in Permission.query.filter(Permission.name.in_(p.permissions)).all()) cur_perm = set(x.name for x in Permission.query.filter(Permission.name.in_(plugin.permissions)).all())
all_perm = set(p.permissions) all_perm = set(plugin.permissions)
add = all_perm - cur_perm add = all_perm - cur_perm
if add: if add:
db.session.bulk_save_objects([Permission(name=x) for x in all_perm]) db.session.bulk_save_objects([Permission(name=x) for x in all_perm])
db.session.commit() db.session.commit()
# Custom installation steps # Custom installation steps
p.install() plugin.install()
click.secho(" ok", fg="green") click.secho(" ok", fg="green")
@ -61,14 +62,16 @@ def uninstall(ctx: click.Context, plugin):
if len(plugin) == 0: if len(plugin) == 0:
ctx.fail("At least one plugin must be specified") ctx.fail("At least one plugin must be specified")
try: try:
plugins = [current_app.config["FG_PLUGINS"][p] for p in plugin] plugins = {plugin_name: current_app.config["FG_PLUGINS"][plugin_name] for plugin_name in plugin}
except KeyError as e: except KeyError as e:
ctx.fail(f"Invalid plugin ID, could not find >{e.args[0]}<") ctx.fail(f"Invalid plugin ID, could not find >{e.args[0]}<")
if ( if (
click.prompt( click.prompt(
"You are going to uninstall:\n\n" "You are going to uninstall:\n\n"
f"\t{', '.join([p.id.split('.')[-1] for p in plugins])}\n\n" f"\t{', '.join([plugin_name for plugin_name in plugins.keys()])}\n\n"
"Are you sure?", "Are you sure?",
default="n", default="n",
show_choices=True, show_choices=True,
@ -77,10 +80,9 @@ def uninstall(ctx: click.Context, plugin):
!= "y" != "y"
): ):
ctx.exit() ctx.exit()
for p in plugins: for name, plugin in plugins.items():
name = p.id.split(".")[-1]
click.echo(f"Uninstalling {name}{'.'*(20-len(name))}", nl=False) click.echo(f"Uninstalling {name}{'.'*(20-len(name))}", nl=False)
p.uninstall() plugin.uninstall()
click.secho(" ok", fg="green") click.secho(" ok", fg="green")
@ -95,7 +97,7 @@ def ls(enabled, no_header):
return p.version return p.version
plugins = entry_points(group="flaschengeist.plugins") plugins = entry_points(group="flaschengeist.plugins")
enabled_plugins = [key for key, value in config.items() if "enabled" in value] + [config["FLASCHENGEIST"]["auth"]] enabled_plugins = [key for key, value in config.items() if "enabled" in value and value["enabled"]] + [config["FLASCHENGEIST"]["auth"]]
loaded_plugins = current_app.config["FG_PLUGINS"].keys() loaded_plugins = current_app.config["FG_PLUGINS"].keys()
if not no_header: if not no_header:

View File

@ -126,7 +126,8 @@ class Plugin:
def install(self): def install(self):
"""Installation routine """Installation routine
Is always called with Flask application context Is always called with Flask application context,
it is called after the plugin permissions are installed.
""" """
pass pass