✨ PMGR: plugin data cleanup functions

Plugin data can now be cleaned up

  * on a plugin's uninstall through the plugin manager
  * manually through a new action button in the plugin manager

Implements #3299

Ideas for further improvement:

  * Management interface to show "orphaned" plugin data and delete
    on request.
This commit is contained in:
Gina Häußge committed 2019-11-01 13:03:47 +01:00
1 parent 6e6dd2c34e
commit b031092f07
5 files changed
+157 -38

No files matched your search

@@ -169,7 +169,6 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
notices_ttl=6*60,
pip_args=None,
pip_force_user=False,
confirm_uninstall=True,
confirm_disable=False,
dependency_links=False,
hidden=[],
@@ -270,6 +269,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
"uninstall": ["plugin"],
"enable": ["plugin"],
"disable": ["plugin"],
"cleanup": ["plugin"],
"refresh_repository": []
}
@@ -350,7 +350,15 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
return make_response("Unknown plugin: %s" % plugin_name, 404)
plugin = self._plugin_manager.plugins[plugin_name]
return self.command_uninstall(plugin)
return self.command_uninstall(plugin, cleanup=data.get("cleanup", False))
elif command == "cleanup":
plugin_name = data["plugin"]
if not plugin_name in self._plugin_manager.plugins:
return make_response("Unknown plugin: %s" % plugin_name, 404)
plugin = self._plugin_manager.plugins[plugin_name]
return self.command_cleanup(plugin)
elif command == "enable" or command == "disable":
plugin_name = data["plugin"]
@@ -531,7 +539,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
self._send_result_notification("install", result)
return jsonify(result)
def command_uninstall(self, plugin):
def command_uninstall(self, plugin, cleanup=False):
if plugin.key == "pluginmanager":
return make_response("Can't uninstall Plugin Manager", 403)
@@ -580,7 +588,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
self._logger.warn(u"Trying to uninstall plugin {plugin} but origin is unknown ({plugin.origin.type})".format(**locals()))
return make_response("Could not uninstall plugin, its origin is unknown")
needs_restart = self._plugin_manager.is_restart_needing_plugin(plugin)
needs_restart = self._plugin_manager.is_restart_needing_plugin(plugin) or cleanup
needs_refresh = plugin.implementation and isinstance(plugin.implementation, octoprint.plugin.ReloadNeedingPlugin)
needs_reconnect = self._plugin_manager.has_any_of_hooks(plugin, self._reconnect_hooks) and self._printer.is_operational()
@@ -620,6 +628,45 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
needs_reconnect=needs_reconnect,
plugin=self._to_external_plugin(plugin))
self._send_result_notification("uninstall", result)
self._logger.info("Plugin {} uninstalled".format(plugin.key))
if cleanup:
self.command_cleanup(plugin)
return jsonify(result)
def command_cleanup(self, plugin):
# delete plugin settings
self._settings.global_set(["plugins", plugin.key], None)
self._settings.save()
message = "Cleaned up settings of plugin {}".format(plugin.key)
self._log_stdout(message)
self._logger.info(message)
# delete plugin data folder
import os
import shutil
result_data = True
data_folder = os.path.join(self._settings.getBaseFolder("data"), plugin.key)
if os.path.exists(data_folder):
try:
shutil.rmtree(data_folder)
message = "Cleaned up data folder of plugin {}".format(plugin.key)
self._log_stdout(message)
self._logger.info(message)
except:
message = "Could not delete data folder of plugin {} at {}".format(plugin.key, data_folder)
self._log_stderr(message)
self._logger.exception(message)
result_data = False
result = dict(result=result_data,
needs_restart=True,
plugin=self._to_external_plugin(plugin))
self._send_result_notification("cleanup", result)
self._logger.info("Plugin {} cleaned up".format(plugin.key))
return jsonify(result)
def command_toggle(self, plugin, command):