/** * Copyright JS Foundation and other contributors, http://js.foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ RED.deploy = (function() { var deploymentTypes = { "full":{img:"red/images/deploy-full-o.svg"}, "nodes":{img:"red/images/deploy-nodes-o.svg"}, "flows":{img:"red/images/deploy-flows-o.svg"} } var ignoreDeployWarnings = { unknown: false, unusedConfig: false, invalid: false } var deploymentType = "full"; var deployInflight = false; var currentDiff = null; function changeDeploymentType(type) { deploymentType = type; $("#red-ui-header-button-deploy-icon").attr("src",deploymentTypes[type].img); } /** * options: * type: "default" - Button with drop-down options - no further customisation available * type: "simple" - Button without dropdown. Customisations: * label: the text to display - default: "Deploy" * icon : the icon to use. Null removes the icon. default: "red/images/deploy-full-o.svg" */ function init(options) { options = options || {}; var type = options.type || "default"; if (type == "default") { $('
').text(RED._('deploy.confirm.backgroundUpdate')); activeNotifyMessage = RED.notify(message,{ modal: true, fixed: true, buttons: [ { text: RED._('deploy.confirm.button.ignore'), click: function() { activeNotifyMessage.close(); activeNotifyMessage = null; } }, { text: RED._('deploy.confirm.button.review'), class: "primary", click: function() { activeNotifyMessage.close(); var nns = RED.nodes.createCompleteNodeSet(); resolveConflict(nns,false); activeNotifyMessage = null; } } ] }); } }); } function getNodeInfo(node) { var tabLabel = ""; if (node.z) { var tab = RED.nodes.workspace(node.z); if (!tab) { tab = RED.nodes.subflow(node.z); tabLabel = tab.name; } else { tabLabel = tab.label; } } var label = RED.utils.getNodeLabel(node,node.id); return {tab:tabLabel,type:node.type,label:label}; } function sortNodeInfo(A,B) { if (A.tab < B.tab) { return -1;} if (A.tab > B.tab) { return 1;} if (A.type < B.type) { return -1;} if (A.type > B.type) { return 1;} if (A.name < B.name) { return -1;} if (A.name > B.name) { return 1;} return 0; } function resolveConflict(currentNodes, activeDeploy) { var message = $('
'+RED._("deploy.successfulRestart")+'
',"success"); }).fail(function(xhr,textStatus,err) { if (deployWasEnabled) { $("#red-ui-header-button-deploy").removeClass("disabled"); } if (xhr.status === 401) { RED.notify(RED._("deploy.deployFailed",{message:RED._("user.notAuthorized")}),"error"); } else if (xhr.status === 409) { resolveConflict(nns, true); } else if (xhr.responseText) { RED.notify(RED._("deploy.deployFailed",{message:xhr.responseText}),"error"); } else { RED.notify(RED._("deploy.deployFailed",{message:RED._("deploy.errors.noResponse")}),"error"); } }).always(function() { var delta = Math.max(0,300-(Date.now()-startTime)); setTimeout(function() { deployButtonClearBusy(); deployInflight = false; },delta); }); } function save(skipValidation, force) { if ($("#red-ui-header-button-deploy").hasClass("disabled")) { return; //deploy is disabled } if ($("#red-ui-header-shade").is(":visible")) { return; //deploy is shaded } if (!RED.user.hasPermission("flows.write")) { RED.notify(RED._("user.errors.deploy"), "error"); return; } let hasUnusedConfig = false; if (!skipValidation) { let hasUnknown = false; let hasInvalid = false; const unknownNodes = []; const invalidNodes = []; RED.nodes.eachConfig(function (node) { if (node.valid === undefined) { RED.editor.validateNode(node); } if (!node.valid && !node.d) { invalidNodes.push(getNodeInfo(node)); } if (node.type === "unknown") { if (unknownNodes.indexOf(node.name) == -1) { unknownNodes.push(node.name); } } }); RED.nodes.eachNode(function (node) { if (!node.valid && !node.d) { invalidNodes.push(getNodeInfo(node)); } if (node.type === "unknown") { if (unknownNodes.indexOf(node.name) == -1) { unknownNodes.push(node.name); } } }); hasUnknown = unknownNodes.length > 0; hasInvalid = invalidNodes.length > 0; const unusedConfigNodes = []; RED.nodes.eachConfig(function (node) { if ((node._def.hasUsers !== false) && (node.users.length === 0)) { unusedConfigNodes.push(getNodeInfo(node)); hasUnusedConfig = true; } }); let showWarning = false; let notificationMessage; let notificationButtons = []; let notification; if (hasUnknown && !ignoreDeployWarnings.unknown) { showWarning = true; notificationMessage = "" + RED._('deploy.confirm.unknown') + "
" + '" + RED._('deploy.confirm.confirm') + "
"; notificationButtons = [ { text: RED._("deploy.unknownNodesButton"), class: "pull-left", click: function() { notification.close(); RED.actions.invoke("core:search","type:unknown "); } }, { id: "red-ui-deploy-dialog-confirm-deploy-deploy", text: RED._("deploy.confirm.button.confirm"), class: "primary", click: function () { save(true); notification.close(); } } ]; } else if (hasInvalid && !ignoreDeployWarnings.invalid) { showWarning = true; invalidNodes.sort(sortNodeInfo); notificationMessage = "" + RED._('deploy.confirm.improperlyConfigured') + "
" + '" + RED._('deploy.confirm.confirm') + "
"; notificationButtons = [ { text: RED._("deploy.invalidNodesButton"), class: "pull-left", click: function() { notification.close(); RED.actions.invoke("core:search","is:invalid "); } }, { id: "red-ui-deploy-dialog-confirm-deploy-deploy", text: RED._("deploy.confirm.button.confirm"), class: "primary", click: function () { save(true); notification.close(); } } ]; } if (showWarning) { notificationButtons.unshift( { text: RED._("common.label.cancel"), click: function () { notification.close(); } } ); notification = RED.notify(notificationMessage, { modal: true, fixed: true, buttons: notificationButtons }); return; } } const nns = RED.nodes.createCompleteNodeSet(); const startTime = Date.now(); deployButtonSetBusy(); const data = { flows: nns }; if (!force) { data.rev = RED.nodes.version(); } deployInflight = true; shadeShow(); $.ajax({ url: "flows", type: "POST", data: JSON.stringify(data), contentType: "application/json; charset=utf-8", headers: { "Node-RED-Deployment-Type": deploymentType } }).done(function (data, textStatus, xhr) { RED.nodes.dirty(false); RED.nodes.version(data.rev); RED.nodes.originalFlow(nns); if (hasUnusedConfig) { let notification; const opts = { type: "success", fixed: false, timeout: 6000, buttons: [ { text: RED._("deploy.unusedConfigNodesButton"), class: "pull-left", click: function() { notification.close(); RED.actions.invoke("core:search","is:config is:unused "); } }, { text: RED._("common.label.close"), class: "primary", click: function () { save(true); notification.close(); } } ] } notification = RED.notify( '' + RED._("deploy.successfulDeploy") + '
' + '' + RED._("deploy.unusedConfigNodes") + '
', opts); } else { RED.notify('' + RED._("deploy.successfulDeploy") + '
', "success"); } RED.nodes.eachNode(function (node) { const flow = node.z && (RED.nodes.workspace(node.z) || RED.nodes.subflow(node.z) || null); const isLocked = flow ? flow.locked : false; if (flow && isLocked) { flow.locked = false; } if (node.changed) { node.dirty = true; node.changed = false; } if (node.moved) { node.dirty = true; node.moved = false; } if (node.credentials) { delete node.credentials; } if (flow && isLocked) { flow.locked = isLocked; } }); RED.nodes.eachConfig(function (confNode) { confNode.changed = false; if (confNode.credentials) { delete confNode.credentials; } }); RED.nodes.eachSubflow(function (subflow) { subflow.changed = false; }); RED.nodes.eachWorkspace(function (ws) { ws.changed = false; }); // Once deployed, cannot undo back to a clean state RED.history.markAllDirty(); RED.view.redraw(); RED.events.emit("deploy"); }).fail(function (xhr, textStatus, err) { RED.nodes.dirty(true); $("#red-ui-header-button-deploy").removeClass("disabled"); if (xhr.status === 401) { RED.notify(RED._("deploy.deployFailed", { message: RED._("user.notAuthorized") }), "error"); } else if (xhr.status === 409) { resolveConflict(nns, true); } else if (xhr.responseText) { RED.notify(RED._("deploy.deployFailed", { message: xhr.responseText }), "error"); } else { RED.notify(RED._("deploy.deployFailed", { message: RED._("deploy.errors.noResponse") }), "error"); } }).always(function () { const delta = Math.max(0, 300 - (Date.now() - startTime)); setTimeout(function () { deployInflight = false; deployButtonClearBusy() shadeHide() }, delta); }); } return { init: init, setDeployInflight: function(state) { deployInflight = state; } } })();