$(function() { function SoftwareUpdateViewModel(parameters) { var self = this; self.loginState = parameters[0]; self.printerState = parameters[1]; self.settings = parameters[2]; self.access = parameters[3]; // optional self.piSupport = parameters[4]; // might be null! self.popup = undefined; self.updateInProgress = false; self.waitingForRestart = false; self.restartTimeout = undefined; self.currentlyBeingUpdated = []; self.working = ko.observable(false); self.workingTitle = ko.observable(); self.workingDialog = undefined; self.workingOutput = undefined; self.loglines = ko.observableArray([]); self.checking = ko.observable(false); self.octoprintReleasedVersion = ko.observable(); self.octoprintUnconfigured = ko.pureComputed(function() { return self.settings.settings.plugins.softwareupdate.octoprint_type() === "git_commit" && self.error_checkoutFolder(); }); self.octoprintUnreleased = ko.pureComputed(function() { return self.settings.settings.plugins.softwareupdate.octoprint_type() === "github_release" && !self.octoprintReleasedVersion(); }); self.environmentSupported = ko.observable(true); self.environmentVersions = ko.observableArray([]); self.cacheTimestamp = ko.observable(); self.cacheTimestampText = ko.pureComputed(function() { return formatDate(self.cacheTimestamp()); }); self.config_cacheTtl = ko.observable(); self.config_notifyUsers = ko.observable(); self.config_trackedBranch = ko.observable(); self.config_checkoutFolder = ko.observable(); self.config_pipTarget = ko.observable(); self.config_checkType = ko.observable(); self.config_releaseChannel = ko.observable(); self.config_pipEnableCheck = ko.observable(); self.error_checkoutFolder = ko.pureComputed(function() { return self.config_checkType() === "git_commit" && (!self.config_checkoutFolder() || self.config_checkoutFolder().trim() === ''); }); self.enableUpdate = ko.pureComputed(function() { return !self.updateInProgress && self.environmentSupported() && !self.printerState.isPrinting() && !self.throttled(); }); self.enable_configSave = ko.pureComputed(function() { return self.config_checkType() === "github_release" || self.config_checkType() === "github_commit" || (self.config_checkType() === "git_commit" && !self.error_checkoutFolder()); }); self.configurationDialog = undefined; self._updateClicked = false; self.config_availableCheckTypes = ko.observableArray([]); self.config_availableReleaseChannels = ko.observableArray([]); self.reloadOverlay = $("#reloadui_overlay"); self.versions = new ItemListHelper( "plugin.softwareupdate.versions", { "name": function(a, b) { // sorts ascending, puts octoprint first if (a.key.toLocaleLowerCase() === "octoprint") return -1; if (b.key.toLocaleLowerCase() === "octoprint") return 1; if (a.displayName.toLocaleLowerCase() < b.displayName.toLocaleLowerCase()) return -1; if (a.displayName.toLocaleLowerCase() > b.displayName.toLocaleLowerCase()) return 1; return 0; } }, {}, "name", [], [], 0 ); self.availableAndPossible = ko.pureComputed(function() { return _.filter(self.versions.items(), function(info) { return info.updateAvailable && info.updatePossible; }); }); self.throttled = ko.pureComputed(function() { return self.piSupport && self.piSupport.currentIssue() && !self.settings.settings.plugins.pluginmanager.ignore_throttled(); }); self.onUserPermissionsChanged = self.onUserLoggedIn = self.onUserLoggedOut = function() { if (self.loginState.hasPermission(self.access.permissions.PLUGIN_SOFTWAREUPDATE_CHECK)) { self.performCheck(); } else { self._closePopup(); } }; self._showPopup = function(options, eventListeners, singleButtonNotify) { singleButtonNotify = singleButtonNotify || false; self._closePopup(); if (singleButtonNotify) { self.popup = PNotify.singleButtonNotify(options); } else { self.popup = new PNotify(options); } if (eventListeners) { var popupObj = self.popup.get(); _.each(eventListeners, function(value, key) { popupObj.on(key, value); }) } }; self._updatePopup = function(options) { if (self.popup === undefined) { self._showPopup(options); } else { self.popup.update(options); } }; self._closePopup = function() { if (self.popup !== undefined) { self.popup.remove(); } }; self.showPluginSettings = function() { self._copyConfig(); self.configurationDialog.modal(); }; self.savePluginSettings = function(viewModel, event) { var target = $(event.target); target.prepend(' '); var data = { plugins: { softwareupdate: { cache_ttl: parseInt(self.config_cacheTtl()), notify_users: self.config_notifyUsers(), octoprint_type: self.config_checkType(), octoprint_release_channel: self.config_releaseChannel(), octoprint_checkout_folder: self.config_checkoutFolder(), octoprint_tracked_branch: self.config_trackedBranch(), octoprint_pip_target: self.config_pipTarget(), pip_enable_check: self.config_pipEnableCheck() } } }; self.settings.saveData(data, { success: function() { self.configurationDialog.modal("hide"); self._copyConfig(); self.performCheck(); }, complete: function() { $("i.fa-spinner", target).remove(); }, sending: true }); }; self._copyConfig = function() { var availableCheckTypes = [{"key": "github_release", "name": gettext("Release")}, {"key": "github_commit", "name": gettext("Github Commit")}, {"key": "git_commit", "name": gettext("Local checkout")}]; self.config_availableCheckTypes(availableCheckTypes); var availableReleaseChannels = []; _.each(self.settings.settings.plugins.softwareupdate.octoprint_branch_mappings(), function(mapping) { availableReleaseChannels.push({"key": mapping.branch(), "name": gettext(mapping.name() || mapping.branch())}); }); self.config_availableReleaseChannels(availableReleaseChannels); self.config_cacheTtl(self.settings.settings.plugins.softwareupdate.cache_ttl()); self.config_notifyUsers(self.settings.settings.plugins.softwareupdate.notify_users()); self.config_checkType(self.settings.settings.plugins.softwareupdate.octoprint_type()); self.config_releaseChannel(self.settings.settings.plugins.softwareupdate.octoprint_release_channel()); self.config_checkoutFolder(self.settings.settings.plugins.softwareupdate.octoprint_checkout_folder()); self.config_trackedBranch(self.settings.settings.plugins.softwareupdate.octoprint_tracked_branch()); self.config_pipTarget(self.settings.settings.plugins.softwareupdate.octoprint_pip_target()); self.config_pipEnableCheck(self.settings.settings.plugins.softwareupdate.pip_enable_check()); }; self._copyConfigBack = function() { self.settings.settings.plugins.softwareupdate.octoprint_checkout_folder(self.config_checkoutFolder()); self.settings.settings.plugins.softwareupdate.octoprint_type(self.config_checkType()); }; 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); versions.push(value); }); self.versions.updateItems(versions); var octoprint = data.information["octoprint"]; self.octoprintReleasedVersion(!octoprint || octoprint.released_version); self.environmentSupported(data.environment.supported); self.environmentVersions(data.environment.versions); if (data.status === "inProgress") { self._markWorking(gettext("Updating..."), gettext("Updating, please wait.")); return; } if (!self.loginState.hasPermission(self.access.permissions.PLUGIN_SOFTWAREUPDATE_UPDATE) && !self.settings.settings.plugins.softwareupdate.notify_users()) return; if (data.status === "updateAvailable" || data.status === "updatePossible") { var text = "
" + gettext("This version of the Python environment is not supported for direct updates.") + "
"; } else { text += "" + gettext("Those components marked with can be updated directly.") + "
"; } if (!self.loginState.hasPermission(self.access.permissions.PLUGIN_SOFTWAREUPDATE_UPDATE)) { text += "" + gettext("To have updates applied, get in touch with an administrator of this OctoPrint instance.") + "
"; } text += "" + gettext("This will update the following components and restart the server:") + "
"; html += "" + gettext("Be sure to read through any linked release notes, especially those for OctoPrint since they might contain important information you need to know before upgrading.") + "
" + "" + gettext("This action may disrupt any ongoing print jobs.") + "
" + "" + gettext("Depending on your printer's controller and general setup, restarting OctoPrint may cause your printer to be reset.") + "
" + "" + gettext("Are you sure you want to proceed?") + "
"; showConfirmationDialog({ title: gettext("Are you sure you want to update now?"), html: html, proceed: gettext("Proceed"), onproceed: function() { self.performUpdate((force === true), _.map(items, function(info) { return info.key })); }, onclose: function() { self._updateClicked = false; } }); }; self._showWorkingDialog = function(title) { if (!self.loginState.hasPermission(self.access.permissions.PLUGIN_SOFTWAREUPDATE_CHECK)) { return; } self.working(true); self.workingTitle(title); self.workingDialog.modal({keyboard: false, backdrop: "static", show: true}); }; self._markWorking = function(title, line, stream) { if (stream === undefined) { stream = "message"; } self.loglines.removeAll(); self.loglines.push({line: line, stream: stream}); self._showWorkingDialog(title); }; self._markDone = function(line, stream) { if (stream === undefined) { stream = "message"; } self.working(false); self.loglines.push({line: "", stream: stream}); self.loglines.push({line: line, stream: stream}); self._scrollWorkingOutputToEnd(); }; self._scrollWorkingOutputToEnd = function() { self.workingOutput.scrollTop(self.workingOutput[0].scrollHeight - self.workingOutput.height()); }; self.onBeforeWizardTabChange = function(next, current) { if (next && next === "#wizard_plugin_softwareupdate") { // switching to the plugin wizard tab self._copyConfig(); } else if (current && current === "#wizard_plugin_softwareupdate") { // switching away from the plugin wizard tab self._copyConfigBack(); } return true; }; self.onAfterWizardFinish = function() { // we might have changed our config, so we need to refresh our check data from the server self.performCheck(); }; self.onStartup = function() { self.workingDialog = $("#settings_plugin_softwareupdate_workingdialog"); self.workingOutput = $("#settings_plugin_softwareupdate_workingdialog_output"); self.configurationDialog = $("#settings_plugin_softwareupdate_configurationdialog"); }; self.onServerDisconnect = function() { if (self.restartTimeout !== undefined) { clearTimeout(self.restartTimeout); } return true; }; self.onEventConnectivityChanged = function(payload) { if (!payload || !payload.new) return; self.performCheck(); }; self.onDataUpdaterReconnect = function() { if (self.waitingForRestart) { self.waitingForRestart = false; self.updateInProgress = false; if (!self.reloadOverlay.is(":visible")) { self.reloadOverlay.show(); } } }; self.onDataUpdaterPluginMessage = function(plugin, data) { if (plugin !== "softwareupdate") { return; } var messageType = data.type; var messageData = data.data; var options = undefined; var restartType = undefined; var title = undefined; var text = undefined; switch(messageType) { // make sure we are marked as working if we get any of the in-progress messages case "loglines": case "updating": case "restarting": case "restart_manually": case "restart_failed": case "success": case "error": { if (!self.working()) { self._markWorking(gettext("Updating..."), gettext("Updating, please wait.")); } break; } } switch (messageType) { case "loglines": { _.each(messageData.loglines, function(line) { self.loglines.push(self._preprocessLine(line)); }); self._scrollWorkingOutputToEnd(); break; } case "updating": { text = _.sprintf(gettext("Now updating %(name)s to %(version)s"), {name: _.escape(messageData.name), version: _.escape(messageData.version)}); self.loglines.push({line: "", stream: "separator"}); self.loglines.push({line: _.repeat("+", text.length), stream: "separator"}); self.loglines.push({line: text, stream: "message"}); self.loglines.push({line: _.repeat("+", text.length), stream: "separator"}); self._scrollWorkingOutputToEnd(); self._updatePopup({ text: text, hide: false, buttons: { sticker: false } }); break; } case "restarting": { title = gettext("Update successful, restarting!"); text = gettext("The update finished successfully and the server will now be restarted."); options = { title: title, text: text, type: "success", hide: false, buttons: { sticker: false } }; self.loglines.push({line: text, stream: "message"}); self._scrollWorkingOutputToEnd(); self.waitingForRestart = true; self.restartTimeout = setTimeout(function() { title = gettext("Restart failed"); text = gettext("The server apparently did not restart by itself, you'll have to do it manually. Please consult octoprint.log on what went wrong."); self._showPopup({ title: title, text: text, type: "error", hide: false, buttons: { sticker: false } }); self.waitingForRestart = false; self._markDone(text, "message_error"); }, 60000); break; } case "restart_manually": { restartType = messageData.restart_type; text = gettext("The update finished successfully, please restart OctoPrint now."); if (restartType === "environment") { text = gettext("The update finished successfully, please reboot the server now."); } title = gettext("Update successful, restart required!"); options = { title: title, text: text, type: "success", hide: false, buttons: { sticker: false } }; self.updateInProgress = false; self._markDone(text); break; } case "restart_failed": { restartType = messageData.restart_type; text = gettext("Restarting OctoPrint failed, please restart it manually. You might also want to consult octoprint.log on what went wrong here."); if (restartType === "environment") { text = gettext("Rebooting the server failed, please reboot it manually. You might also want to consult octoprint.log on what went wrong here."); } title = gettext("Restart failed"); options = { title: title, test: text, type: "error", hide: false, buttons: { sticker: false } }; self.waitingForRestart = false; self.updateInProgress = false; self._markDone(text, "message_error"); break; } case "success": { title = gettext("Update successful!"); text = gettext("The update finished successfully."); options = { title: title, text: text, type: "success", hide: false, buttons: { sticker: false } }; self.updateInProgress = false; self._markDone(text); break; } case "error": { title = gettext("Update failed!"); text = gettext("The update did not finish successfully. Please consultoctoprint.log and plugin_softwareupdate_console.log for details.");
self._showPopup({
title: title,
text: text,
type: "error",
hide: false,
buttons: {
sticker: false
}
});
self.updateInProgress = false;
self._markDone(text, "message_error");
break;
}
case "update_versions": {
self.performCheck();
break;
}
}
if (options !== undefined) {
self._showPopup(options);
}
};
self._forcedStdoutPatterns = ["You are using pip version .*?, however version .*? is available\.",
"You should consider upgrading via the '.*?' command\.",
"'.*?' does not exist -- can't clean it"];
self._forcedStdoutLine = new RegExp(self._forcedStdoutPatterns.join("|"));
self._preprocessLine = function(line) {
if (line.stream === "stderr" && line.line.match(self._forcedStdoutLine)) {
line.stream = "stdout";
}
return line;
}
}
OCTOPRINT_VIEWMODELS.push({
construct: SoftwareUpdateViewModel,
dependencies: ["loginStateViewModel", "printerStateViewModel", "settingsViewModel", "accessViewModel", "piSupportViewModel"],
optional: ["piSupportViewModel"],
elements: ["#settings_plugin_softwareupdate", "#softwareupdate_confirmation_dialog", "#wizard_plugin_softwareupdate"]
});
});