🐛 SWU: Fix first run update

It was actually not working at all any more. The new approach
should perform better, cause less duplication and overall
be cleaner.
This commit is contained in:
Gina Häußge committed 2021-08-03 11:40:39 +02:00
1 parent 4d2e7f1ddd
commit d61705aace
4 files changed
+157 -111

No files matched your search

@@ -70,6 +70,8 @@ class SoftwareUpdatePlugin(
OCTOPRINT_RESTART_TYPES = ("pip", "single_file_plugin")
VALID_RESTART_TYPES = ("octoprint", "environment")
DATA_FORMAT_VERSION = "v4"
CHECK_OVERLAY_KEY = "softwareupdate_check_overlay"
@@ -638,15 +640,45 @@ class SoftwareUpdatePlugin(
self._save_update_log()
def _is_octoprint_outdated_and_can_update(self):
information, update_available, update_possible = self.get_current_versions()
if not update_available or not update_possible or "octoprint" not in information:
checks = self._get_configured_checks()
check = checks.get("octoprint", None)
if not check:
return False
octoprint_information = information["octoprint"]
return (
octoprint_information["updateAvailable"]
and octoprint_information["updatePossible"]
check = self._populated_check("octoprint", check)
credentials = self._settings.get(["credentials"], merged=True)
_, update_available, update_possible, _, _ = self._get_current_version(
"octoprint", check, credentials=credentials
)
if not update_available or not update_possible:
return False
restart_type = self._get_restart_type(check)
restart_command = self._get_restart_command(restart_type)
return restart_command is not None
def _get_restart_type(self, check):
if check.get("restart") in self.VALID_RESTART_TYPES:
return check["restart"]
elif "pip" in check or check.get("method") in self.OCTOPRINT_RESTART_TYPES:
return "octoprint"
else:
target_restart_type = None
return target_restart_type
def _get_restart_command(self, restart_type):
if restart_type == "octoprint":
return self._settings.global_get(
["server", "commands", "serverRestartCommand"]
)
elif restart_type == "environment":
return self._settings.global_get(
["server", "commands", "systemRestartCommand"]
)
else:
return None
# ~~ SettingsPlugin API
@@ -1100,6 +1132,8 @@ class SoftwareUpdatePlugin(
)(view)()
@octoprint.plugin.BlueprintPlugin.route("/check", methods=["GET"])
@no_firstrun_access
@Permissions.PLUGIN_SOFTWAREUPDATE_CHECK.require(403)
def check_for_update(self):
if (
not Permissions.PLUGIN_SOFTWAREUPDATE_CHECK.can()
@@ -1443,7 +1477,11 @@ class SoftwareUpdatePlugin(
def _is_wizard_update_required(self):
firstrun = self._settings.global_get(["server", "firstRun"])
return firstrun and self._is_octoprint_outdated_and_can_update()
return (
firstrun
and self._is_octoprint_outdated_and_can_update()
and not self._is_settings_wizard_required()
)
def _is_settings_wizard_required(self):
checks = self._get_configured_checks()
@@ -1457,6 +1495,14 @@ class SoftwareUpdatePlugin(
def get_wizard_version(self):
return 1
def get_wizard_details(self):
result = {}
if self._is_wizard_update_required():
information, _, _ = self.get_current_versions()
data = information.get("octoprint", {})
result["update"] = data
return result
##~~ EventHandlerPlugin API
def on_event(self, event, payload):
@@ -1900,15 +1946,7 @@ class SoftwareUpdatePlugin(
if target_result is not None:
target_results[target] = target_result
if "restart" in check:
target_restart_type = check["restart"]
elif (
"pip" in check
or check.get("method") in self.OCTOPRINT_RESTART_TYPES
):
target_restart_type = "octoprint"
else:
target_restart_type = None
target_restart_type = self._get_restart_type(check)
# if our update requires a restart we have to determine which type
if restart_type is None or (
@@ -1939,16 +1977,7 @@ class SoftwareUpdatePlugin(
# one of our updates requires a restart of either type "octoprint" or "environment". Let's see if
# we can actually perform that
restart_command = None
if restart_type == "octoprint":
restart_command = self._settings.global_get(
["server", "commands", "serverRestartCommand"]
)
elif restart_type == "environment":
restart_command = self._settings.global_get(
["server", "commands", "systemRestartCommand"]
)
restart_command = self._get_restart_command(restart_type)
if restart_command:
self._send_client_message(
"restarting",
@@ -306,74 +306,79 @@ $(function () {
);
};
self._enrichInformation = function (key, information) {
information["key"] = key;
if (
!information.hasOwnProperty("displayName") ||
information.displayName === ""
) {
information.displayName = information.key;
}
if (
!information.hasOwnProperty("displayVersion") ||
information.displayVersion === ""
) {
information.displayVersion = information.information.local.name;
}
if (
!information.hasOwnProperty("releaseNotes") ||
information.releaseNotes === ""
) {
information.releaseNotes = undefined;
}
var fullNameTemplate = gettext("%(name)s: %(version)s");
information.fullNameLocal = _.sprintf(fullNameTemplate, {
name: _.escape(information.displayName),
version: _.escape(information.displayVersion)
});
var fullNameRemoteVars = {
name: _.escape(information.displayName),
version: gettext("unknown")
};
if (
information.hasOwnProperty("information") &&
information.information.hasOwnProperty("remote") &&
information.information.remote.hasOwnProperty("name")
) {
fullNameRemoteVars.version = _.escape(
information.information.remote.name
);
}
information.fullNameRemote = _.sprintf(fullNameTemplate, fullNameRemoteVars);
if (information.releaseChannels && information.releaseChannels.current) {
information.releaseChannels.current = ko.observable(
information.releaseChannels.current
);
information.releaseChannels.current.subscribe(function (selected) {
var patch = {};
patch[key] = {channel: selected};
OctoPrint.plugins.softwareupdate.configure(patch).done(function () {
self.performCheck(false, false, false, [key]);
});
});
}
information.toggleDisabled = function () {
var patch = {};
patch[key] = {disabled: !information.disabled};
OctoPrint.plugins.softwareupdate.configure(patch).done(function () {
self.performCheck(false, false, false, [key]);
});
};
return information;
};
self.fromCheckResponse = function (data, ignoreSeen, showIfNothingNew) {
self.cacheTimestamp(data.timestamp);
var versions = [];
_.each(data.information, function (value, key) {
value["key"] = key;
if (!value.hasOwnProperty("displayName") || value.displayName === "") {
value.displayName = value.key;
}
if (
!value.hasOwnProperty("displayVersion") ||
value.displayVersion === ""
) {
value.displayVersion = value.information.local.name;
}
if (!value.hasOwnProperty("releaseNotes") || value.releaseNotes === "") {
value.releaseNotes = undefined;
}
var fullNameTemplate = gettext("%(name)s: %(version)s");
value.fullNameLocal = _.sprintf(fullNameTemplate, {
name: _.escape(value.displayName),
version: _.escape(value.displayVersion)
});
var fullNameRemoteVars = {
name: _.escape(value.displayName),
version: gettext("unknown")
};
if (
value.hasOwnProperty("information") &&
value.information.hasOwnProperty("remote") &&
value.information.remote.hasOwnProperty("name")
) {
fullNameRemoteVars.version = _.escape(value.information.remote.name);
}
value.fullNameRemote = _.sprintf(fullNameTemplate, fullNameRemoteVars);
if (value.releaseChannels && value.releaseChannels.current) {
value.releaseChannels.current = ko.observable(
value.releaseChannels.current
);
value.releaseChannels.current.subscribe(function (selected) {
var patch = {};
patch[key] = {channel: selected};
OctoPrint.plugins.softwareupdate
.configure(patch)
.done(function () {
self.performCheck(false, false, false, [key]);
});
});
}
value.toggleDisabled = function () {
var patch = {};
patch[key] = {disabled: !value.disabled};
OctoPrint.plugins.softwareupdate.configure(patch).done(function () {
self.performCheck(false, false, false, [key]);
});
};
if (key === "octoprint") {
self.octoprintData.item = value;
self.octoprintData.current(value.information.local.name);
self.octoprintData.available(value.information.remote.name);
}
self._enrichInformation(key, value);
versions.push(value);
});
self.versions.updateItems(versions);
@@ -683,14 +688,7 @@ $(function () {
};
self.performUpdate = function (force, items) {
if (
!(
self.loginState.hasPermission(
self.access.permissions.PLUGIN_SOFTWAREUPDATE_UPDATE
) || CONFIG_FIRST_RUN
)
)
return;
if (!self.updateAccess()) return;
if (self.printerState.isPrinting()) return;
self.updateInProgress = true;
@@ -747,15 +745,16 @@ $(function () {
return self.update(force, self.availableAndPossibleAndEnabled());
};
self.updateAccess = function () {
return (
self.loginState.hasPermission(
self.access.permissions.PLUGIN_SOFTWAREUPDATE_UPDATE
) || CONFIG_FIRST_RUN
);
};
self.update = function (force, items) {
if (
self.updateInProgress ||
!(
self.loginState.hasPermission(
self.access.permissions.PLUGIN_SOFTWAREUPDATE_UPDATE
) || CONFIG_FIRST_RUN
)
) {
if (self.updateInProgress || !self.updateAccess()) {
self._updateClicked = false;
return;
}
@@ -851,8 +850,10 @@ $(function () {
self._showWorkingDialog = function (title) {
if (
!self.loginState.hasPermission(
self.access.permissions.PLUGIN_SOFTWAREUPDATE_CHECK
!(
self.loginState.hasPermission(
self.access.permissions.PLUGIN_SOFTWAREUPDATE_CHECK
) || CONFIG_FIRST_RUN
)
) {
return;
@@ -929,6 +930,21 @@ $(function () {
self.performCheck();
};
self.onWizardDetails = function (data) {
if (
data.softwareupdate &&
data.softwareupdate.details &&
data.softwareupdate.details.update
) {
var value = data.softwareupdate.details.update;
self._enrichInformation("octoprint", value);
self.octoprintData.item = value;
self.octoprintData.current(value.information.local.name);
self.octoprintData.available(value.information.remote.name);
}
};
self.onDataUpdaterReconnect = function () {
if (self.waitingForRestart) {
self.waitingForRestart = false;
@@ -203,9 +203,9 @@
<h3 data-bind="text: workingTitle"></h3>
</div>
<div class="modal-body">
<pre id="settings_plugin_softwareupdate_workingdialog_output" class="pre-scrollable pre-output" style="height: 170px; display: none" data-bind="visible: loginState.isAdmin, foreach: loglines"><span data-bind="text: line, css: {stdout: stream == 'stdout', stderr: stream == 'stderr', call: stream == 'call', message: stream == 'message'}"></span></pre>
<p style="display: none" data-bind="visible: !loginState.isAdmin() && $root.working()"><i class="fas fa-spinner fa-spin"></i> {{ _('Update in progress, please wait.') }}</p>
<p style="display: none" data-bind="visible: !loginState.isAdmin() && !$root.working()">{{ _('Update done, please close this window.') }}</p>
<pre id="settings_plugin_softwareupdate_workingdialog_output" class="pre-scrollable pre-output" style="height: 170px; display: none" data-bind="visible: $root.updateAccess(), foreach: loglines"><span data-bind="text: line, css: {stdout: stream == 'stdout', stderr: stream == 'stderr', call: stream == 'call', message: stream == 'message'}"></span></pre>
<p style="display: none" data-bind="visible: !$root.updateAccess() && $root.working()"><i class="fas fa-spinner fa-spin"></i> {{ _('Update in progress, please wait.') }}</p>
<p style="display: none" data-bind="visible: !$root.updateAccess() && !$root.working()">{{ _('Update done, please close this window.') }}</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" data-bind="enable: !$root.working()" aria-hidden="true">{{ _('Close') }}</button>
+2 -1
View File
@@ -99,11 +99,12 @@ class PrinterStateConnection(
"*": [],
}
# TODO: Permissions should be overridable from plugins, this special case stuff here is a hack
_emit_permissions = {
"connected": [],
"reauthRequired": [],
"plugin": lambda payload: []
if payload.get("plugin") == "backup"
if payload.get("plugin") in ("backup", "softwareupdate")
and settings().getBoolean(["server", "firstRun"])
else [Permissions.STATUS],
"*": [Permissions.STATUS],