From d61705aacebd49be33314e0c4745b93445a96941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 3 Aug 2021 11:40:04 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20SWU:=20Fix=20first=20run=20updat?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It was actually not working at all any more. The new approach should perform better, cause less duplication and overall be cleaner. --- .../plugins/softwareupdate/__init__.py | 81 +++++--- .../static/js/softwareupdate.js | 178 ++++++++++-------- .../templates/softwareupdate_settings.jinja2 | 6 +- src/octoprint/server/util/sockjs.py | 3 +- 4 files changed, 157 insertions(+), 111 deletions(-) diff --git a/src/octoprint/plugins/softwareupdate/__init__.py b/src/octoprint/plugins/softwareupdate/__init__.py index 98727b3ff..0c332fd6c 100644 --- a/src/octoprint/plugins/softwareupdate/__init__.py +++ b/src/octoprint/plugins/softwareupdate/__init__.py @@ -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", diff --git a/src/octoprint/plugins/softwareupdate/static/js/softwareupdate.js b/src/octoprint/plugins/softwareupdate/static/js/softwareupdate.js index 11eee8226..f812219d7 100644 --- a/src/octoprint/plugins/softwareupdate/static/js/softwareupdate.js +++ b/src/octoprint/plugins/softwareupdate/static/js/softwareupdate.js @@ -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; diff --git a/src/octoprint/plugins/softwareupdate/templates/softwareupdate_settings.jinja2 b/src/octoprint/plugins/softwareupdate/templates/softwareupdate_settings.jinja2 index 0efe80cfd..a995fe21c 100644 --- a/src/octoprint/plugins/softwareupdate/templates/softwareupdate_settings.jinja2 +++ b/src/octoprint/plugins/softwareupdate/templates/softwareupdate_settings.jinja2 @@ -203,9 +203,9 @@