✨ 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):
@@ -52,11 +52,25 @@
return this.base.simpleApiCommand("pluginmanager", "install", data, opts);
};
OctoPrintPluginManagerClient.prototype.uninstall = function(plugin, opts) {
OctoPrintPluginManagerClient.prototype.uninstall = function(plugin, cleanup, opts) {
// backwards compatibility to former argument list (plugin, opts)
if (arguments.length === 2 && typeof cleanup === "object") {
opts = cleanup;
cleanup = false;
}
var data = {
plugin: plugin,
cleanup: !!cleanup
};
return this.base.simpleApiCommand("pluginmanager", "uninstall", data, opts);
};
OctoPrintPluginManagerClient.prototype.cleanup = function(plugin, opts) {
var data = {
plugin: plugin
};
return this.base.simpleApiCommand("pluginmanager", "uninstall", data, opts);
return this.base.simpleApiCommand("pluginmanager", "cleanup", data, opts);
};
OctoPrintPluginManagerClient.prototype.enable = function(plugin, opts) {
@@ -185,6 +185,12 @@ $(function() {
&& !data.pending_uninstall;
};
self.enableCleanup = function(data) {
return self.enableManagement()
&& data.key !== 'pluginmanager'
&& !data.pending_uninstall;
};
self.enableRepoInstall = function(data) {
return self.enableManagement() && self.pipAvailable() && !self.safeMode() && !self.throttled() && self.online() && self.isCompatible(data);
};
@@ -565,14 +571,14 @@ $(function() {
}
if (data.bundled) return;
if (data.key == "pluginmanager") return;
if (data.key === "pluginmanager") return;
// defining actual uninstall logic as functor in order to handle
// the confirm/no-confirm logic without duplication of logic
var performUninstall = function() {
var performUninstall = function(cleanup) {
self._markWorking(gettext("Uninstalling plugin..."), _.sprintf(gettext("Uninstalling plugin \"%(name)s\""), {name: _.escape(data.name)}));
OctoPrint.plugins.pluginmanager.uninstall(data.key)
OctoPrint.plugins.pluginmanager.uninstall(data.key, cleanup)
.done(function() {
self.requestData();
})
@@ -589,19 +595,56 @@ $(function() {
});
};
if (self.settingsViewModel.settings.plugins.pluginmanager.confirm_uninstall()) {
// confirmation needed. Show confirmation dialog and call performUninstall if user clicks Yes
showConfirmationDialog({
message: _.sprintf(gettext("You are about to uninstall the plugin \"%(name)s\""), {name: _.escape(data.name)}),
cancel: gettext("Keep installed"),
proceed: gettext("Uninstall"),
onproceed: performUninstall,
nofade: true
});
} else {
// no confirmation needed, just go ahead and uninstall
performUninstall();
showConfirmationDialog({
message: _.sprintf(gettext("You are about to uninstall the plugin \"%(name)s\""), {name: _.escape(data.name)}),
cancel: gettext("Keep installed"),
proceed: [
gettext("Uninstall"),
gettext("Uninstall & clean up data")
],
onproceed: function(button) {
// buttons: 0=uninstall, 1=uninstall&cleanup
performUninstall(button === 1)
},
nofade: true
});
};
self.cleanupPlugin = function(data) {
if (!self.loginState.isAdmin()) {
return;
}
if (!self.enableUninstall(data)) {
return;
}
if (data.key === "pluginmanager") return;
var performCleanup = function() {
self._markWorking(gettext("Cleaning up plugin data..."), _.sprintf(gettext("Cleaning up data of plugin \"%(name)s\""), {name: _.escape(data.name)}));
OctoPrint.plugins.pluginmanager.cleanup(data.key)
.fail(function() {
new PNotify({
title: gettext("Something went wrong"),
text: gettext("Please consult octoprint.log for details"),
type: "error",
hide: false
});
})
.always(function() {
self._markDone();
})
};
showConfirmationDialog({
message: _.sprintf(gettext("You are about to cleanup the plugin data of \"%(name)s\". This operation cannot be reversed."), {name: _.escape(data.name)}),
cancel: gettext("Keep data"),
proceed: gettext("Cleanup data"),
onproceed: performCleanup,
nofade: true
});
};
self.refreshRepository = function() {
@@ -748,6 +791,10 @@ $(function() {
line = gettext("Disable <em>%(plugin)s</em>: %(result)s");
break;
}
case "cleanup": {
line = gettext("Cleanup <em>%(plugin)s</em>: %(result)s");
break;
}
default: {
return;
}
@@ -81,7 +81,7 @@
</div>
</td>
<td class="settings_plugin_plugin_manager_plugins_actions">
<a href="#" data-bind="css: $root.toggleButtonCss($data), attr: {title: $root.toggleButtonTitle($data)}, enable: $root.enableToggle($data), click: function() { $root.togglePlugin($data) }"></a>&nbsp;|&nbsp;<a href="#" class="fa fa-trash-o" title="{{ _('Uninstall Plugin')|edq }}" data-bind="css: {disabled: !$root.enableUninstall($data)}, enable: $root.enableUninstall($data), click: function() { $root.uninstallPlugin($data) }"></a>
<a href="#" data-bind="css: $root.toggleButtonCss($data), attr: {title: $root.toggleButtonTitle($data)}, enable: $root.enableToggle($data), click: function() { $root.togglePlugin($data) }"></a>&nbsp;|&nbsp;<a href="#" class="fa fa-eraser" title="{{ _('Cleanup Plugin Data')|edq }}" data-bind="css: {disabled: !$root.enableCleanup($data)}, enable: $root.enableCleanup($data), click: function() { $root.cleanupPlugin($data) }">&nbsp;|&nbsp;<a href="#" class="fa fa-trash-o" title="{{ _('Uninstall Plugin')|edq }}" data-bind="css: {disabled: !$root.enableUninstall($data)}, enable: $root.enableUninstall($data), click: function() { $root.uninstallPlugin($data) }"></a>
</td>
</tr>
</tbody>
@@ -260,13 +260,6 @@
<fieldset>
<legend>{{ _('Confirmations') }}</legend>
<div class="control-group" title="{{ _('Confirm before uninstalling a plugin')|edq }}">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: config_confirmUninstall"> {{ _('Confirm before uninstalling a plugin') }}
</label>
</div>
</div>
<div class="control-group" title="{{ _('Confirm before disabling a plugin')|edq }}">
<div class="controls">
<label class="checkbox">
+27 -9
View File
@@ -737,6 +737,8 @@ function showConfirmationDialog(msg, onacknowledge, options) {
var html = options.html;
var checkboxes = options.checkboxes;
var cancel = options.cancel || gettext("Cancel");
var proceed = options.proceed || gettext("Proceed");
var proceedClass = options.proceedClass || "danger";
@@ -764,18 +766,32 @@ function showConfirmationDialog(msg, onacknowledge, options) {
var cancelButton = $('<a href="javascript:void(0)" class="btn">' + cancel + '</a>')
.attr("data-dismiss", "modal")
.attr("aria-hidden", "true");
var proceedButton = $('<a href="javascript:void(0)" class="btn">' + proceed + '</a>')
.addClass("btn-" + proceedClass);
if (!_.isArray(proceed)) {
proceed = [proceed];
}
var proceedButtons = [];
_.each(proceed, function(text) {
proceedButtons.push($('<a href="javascript:void(0)" class="btn">' + text + '</a>')
.addClass("btn-" + proceedClass));
});
var modal = $('<div></div>')
.addClass('modal hide');
if (!nofade) {
modal.addClass('fade');
}
var buttons = $('<div></div>').addClass('modal-footer').append(cancelButton);
_.each(proceedButtons, function(button) {
buttons.append(button);
});
modal.addClass(dialogClass)
.append($('<div></div>').addClass('modal-header').append(modalHeader))
.append($('<div></div>').addClass('modal-body').append(modalBody))
.append($('<div></div>').addClass('modal-footer').append(cancelButton).append(proceedButton));
.append(buttons);
modal.on('hidden', function(event) {
if (onclose && _.isFunction(onclose)) {
onclose(event);
@@ -789,12 +805,14 @@ function showConfirmationDialog(msg, onacknowledge, options) {
}
modal.modal(modalOptions);
proceedButton.click(function(e) {
e.preventDefault();
if (onproceed && _.isFunction(onproceed)) {
onproceed(e);
}
modal.modal("hide");
_.each(proceedButtons, function(button, idx) {
button.click(function(e) {
e.preventDefault();
if (onproceed && _.isFunction(onproceed)) {
onproceed(idx, e);
}
modal.modal("hide");
})
});
cancelButton.click(function(e) {
if (oncancel && _.isFunction(oncancel)) {