✨ backup: Add first run wizard

Bypasses permissions in first run mode, allows
to restore existing backup.

Closes #3496
This commit is contained in:
Gina Häußge committed 2020-04-01 18:58:39 +02:00
1 parent ef7a247df3
commit 7e4c29befb
8 files changed
+60 -64

No files matched your search

+20 -4
View File
@@ -61,7 +61,8 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.TemplatePlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.BlueprintPlugin,
octoprint.plugin.StartupPlugin):
octoprint.plugin.StartupPlugin,
octoprint.plugin.WizardPlugin):
_pip_caller = None
@@ -99,7 +100,8 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
##~~ TemplatePlugin
def get_template_configs(self):
return [dict(type="settings", name=gettext("Backup & Restore"))]
return [dict(type="settings", name=gettext("Backup & Restore")),
dict(type="wizard", name=gettext("Restore Backup?"))]
##~~ BlueprintPlugin
@@ -210,9 +212,10 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
return NO_CONTENT
@octoprint.plugin.BlueprintPlugin.route("/restore", methods=["POST"])
@no_firstrun_access
@Permissions.PLUGIN_BACKUP_ACCESS.require(403)
def perform_restore(self):
if not Permissions.PLUGIN_BACKUP_ACCESS.can() and not self._settings.global_get(["server", "firstRun"]):
return flask.abort(403)
if not is_os_compatible(["!windows"]):
return flask.make_response("Invalid request, the restores are not supported on the underlying operating system", 400)
@@ -325,6 +328,14 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
def is_blueprint_protected(self):
return False
##~~ WizardPlugin
def is_wizard_required(self):
return self._settings.global_get(["server", "firstRun"])
def get_wizard_details(self):
return dict(required=self.is_wizard_required())
##~~ tornado hook
def route_hook(self, *args, **kwargs):
@@ -929,6 +940,9 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
os.remove(path)
# restart server
if not restart_command:
restart_command = configdata.get("server", dict()).get("commands", dict()).get("serverRestartCommand")
if restart_command:
import sarge
@@ -949,6 +963,8 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
else:
if callable(on_restore_done):
on_restore_done(path)
if callable(on_log_error):
on_log_error("No restart command configured. Please restart OctoPrint manually.")
return True
@@ -29,9 +29,10 @@ $(function() {
self.backupInProgress = ko.observable(false);
self.restoreSupported = ko.observable(true);
self.backupUploadButton = $("#settings-backup-upload");
self.backupUploadData = undefined;
self.backupUploadButton.fileupload({
self.backupUploadName = ko.observable();
var backupFileuploadOptions = {
dataType: "json",
maxNumberOfFiles: 1,
autoUpload: false,
@@ -49,8 +50,11 @@ $(function() {
self.backupUploadName(undefined);
self.backupUploadData = undefined;
}
});
self.backupUploadName = ko.observable();
};
$("#settings-backup-upload").fileupload(backupFileuploadOptions);
$("#wizard-backup-upload").fileupload(backupFileuploadOptions);
self.restoreInProgress = ko.observable(false);
self.restoreTitle = ko.observable();
self.restoreDialog = undefined;
@@ -284,6 +288,6 @@ $(function() {
OCTOPRINT_VIEWMODELS.push({
construct: BackupViewModel,
dependencies: ["loginStateViewModel", "settingsViewModel"],
elements: ["#settings_plugin_backup"]
elements: ["#settings_plugin_backup", "#wizard_plugin_backup"]
});
});
@@ -0,0 +1,26 @@
<h3>{{ _('Restore from a backup?') }}</h3>
{% trans %}<p>
If you would like to restore OctoPrint configuration from a backup now is the time.
</p>{% endtrans %}
<form class="form-horizontal">
<div class="control-group">
<label class="control-label">{{ _('Backup file') }}</label>
<div class="controls">
<div class="input-prepend">
<span class="btn fileinput-button">
<span>{{ _('Browse...') }}</span>
<input id="wizard-backup-upload" type="file" name="file" data-url="{{ url_for("plugin.backup.perform_restore") }}" accept=".zip">
</span>
<span class="add-on" data-bind="text: backupUploadName"></span>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-primary" data-bind="enable: !backupInProgress() && !restoreInProgress(), click: performRestoreFromUpload"><i class="fa fa-spinner fa-spin" data-bind="visible: restoreInProgress"></i> {{ _('Upload & restore') }}</button>
</div>
</div>
</form>
@@ -313,30 +313,6 @@ $(function() {
};
}
function CoreWizardBackupRestoreViewModel(parameters) {
var self = this;
self.required = false;
self.onWizardDetails = function(response) {
if (!self.required) return;
};
self.onWizardFinish = function() {
if (!self.required) return;
};
self.showRestoreFromBackup = function() {
$showRestorePluginDialogCallback = function () {
OctoPrint.coreui.viewmodels.wizardViewModel.closeDialog();
$("#settings_dialog_menu").hide()
OctoPrint.coreui.viewmodels.settingsViewModel.show("settings_plugin_backup");
}
OctoPrint.coreui.viewmodels.coreWizardAclViewModel._sendData({"ac": false}, $showRestorePluginDialogCallback);
};
}
OCTOPRINT_VIEWMODELS.push({
construct: CoreWizardAclViewModel,
dependencies: ["loginStateViewModel"],
@@ -361,9 +337,5 @@ $(function() {
construct: CoreWizardPrinterProfileViewModel,
dependencies: ["printerProfilesViewModel"],
elements: ["#wizard_plugin_corewizard_printerprofile"]
}, {
construct: CoreWizardBackupRestoreViewModel,
dependencies: ["settingsViewModel"],
elements: ["#wizard_plugin_corewizard_backuprestore"]
});
});
+2 -16
View File
@@ -107,7 +107,7 @@ class OnlineCheckSubwizard(object):
return dict(required=self._is_onlinecheck_wizard_required())
def _get_onlinecheck_wizard_name(self):
return gettext("Online connectivity check")
return gettext("Online Connectivity Check")
def _get_onlinecheck_additional_wizard_template_data(self):
return dict(mandatory=self._is_onlinecheck_wizard_required())
@@ -125,7 +125,7 @@ class PluginBlacklistSubwizard(object):
return dict(required=self._is_pluginblacklist_wizard_required())
def _get_pluginblacklist_wizard_name(self):
return gettext("Plugin blacklist")
return gettext("Plugin Blacklist")
def _get_pluginblacklist_additional_wizard_template_data(self):
return dict(mandatory=self._is_pluginblacklist_wizard_required())
@@ -146,20 +146,6 @@ class PrinterProfileSubwizard(object):
return gettext("Default Printer Profile")
# noinspection PyUnresolvedReferences,PyMethodMayBeStatic
class BackupRestoreSubwizard(object):
def _is_backuprestore_wizard_firstrunonly(self):
return True
def _is_backuprestore_wizard_required(self):
return True
def _get_backuprestore_wizard_details(self):
return dict(required=self._is_backuprestore_wizard_required())
def _get_backuprestore_wizard_name(self):
return gettext("Restore Backup")
Subwizards = type(to_native_str("Subwizards"),
tuple(cls for clsname, cls in inspect.getmembers(sys.modules[__name__], inspect.isclass)
if clsname.endswith("Subwizard")),
@@ -1,9 +0,0 @@
<h3>{{ _('Restore from a backup') }}</h3>
{% trans %}<p>
If you would like to restore OctoPrint configuration from a backup now is the time.
</p>{% endtrans %}
<div class="row-fluid">
<a href="#" class="btn btn-primary span6" data-bind="click: function() { showRestoreFromBackup() }">{{ _('Restore From Backup') }}</a>
</div>
+2 -1
View File
@@ -83,6 +83,7 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection,
_emit_permissions = {"connected": [],
"reauthRequired": [],
"plugin": lambda payload: [] if payload.get("plugin") == "backup" and settings().getBoolean(["server", "firstRun"]) else [Permissions.STATUS],
"*": [Permissions.STATUS]}
_unauthed_backlog_max = 100
@@ -419,7 +420,7 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection,
if permissions is None:
permissions = self._emit_permissions.get(type, self._emit_permissions["*"])
permissions = [x() if callable(x) else x for x in permissions]
permissions = permissions(payload) if callable(permissions) else [x for x in permissions]
if not self._user or not all(map(lambda p: self._user.has_permission(p), permissions)):
if not self._authed:
+1 -1
View File
@@ -325,7 +325,7 @@ default_settings = {
"plugin_pi_support"
],
"usersettings": ["access", "interface"],
"wizard": ["access"],
"wizard": ["plugin_backup", "plugin_corewizard_acl"],
"about": ["about", "plugin_pi_support", "supporters", "authors", "changelog", "license", "thirdparty", "plugin_pluginmanager"],
"generic": []
},