node-red/packages/node_modules/@node-red/editor-client/src/js/ui/deploy.js

511 lines
21 KiB
JavaScript
Raw Normal View History

2015-03-15 23:54:55 +01:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
2015-03-15 23:54:55 +01:00
*
* 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.
**/
2015-07-01 00:42:03 +02:00
2015-03-15 23:54:55 +01:00
RED.deploy = (function() {
2015-07-01 00:42:03 +02:00
2015-03-15 23:54:55 +01:00
var deploymentTypes = {
2015-04-02 00:24:47 +02:00
"full":{img:"red/images/deploy-full-o.png"},
"nodes":{img:"red/images/deploy-nodes-o.png"},
"flows":{img:"red/images/deploy-flows-o.png"}
2015-03-15 23:54:55 +01:00
}
2015-07-01 00:42:03 +02:00
var ignoreDeployWarnings = {
unknown: false,
unusedConfig: false,
invalid: false
}
2015-07-01 00:42:03 +02:00
2015-03-15 23:54:55 +01:00
var deploymentType = "full";
2015-07-01 00:42:03 +02:00
var deployInflight = false;
2016-12-20 20:42:38 +01:00
var currentDiff = null;
2015-03-15 23:54:55 +01:00
function changeDeploymentType(type) {
deploymentType = type;
2016-10-20 14:11:12 +02:00
$("#btn-deploy-icon").attr("src",deploymentTypes[type].img);
2015-03-15 23:54:55 +01:00
}
2015-07-01 00:42:03 +02:00
2015-04-10 16:13:40 +02:00
/**
* 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.png"
*/
function init(options) {
options = options || {};
var type = options.type || "default";
2015-07-01 00:42:03 +02:00
2015-04-10 16:13:40 +02:00
if (type == "default") {
$('<li><span class="deploy-button-group button-group">'+
2016-10-20 14:11:12 +02:00
'<a id="btn-deploy" class="deploy-button disabled" href="#">'+
'<span class="deploy-button-content">'+
'<img id="btn-deploy-icon" src="red/images/deploy-full-o.png"> '+
'<span>'+RED._("deploy.deploy")+'</span>'+
'</span>'+
'<span class="deploy-button-spinner hide">'+
'<img src="red/images/spin.svg"/>'+
'</span>'+
'</a>'+
'<a id="btn-deploy-options" class="deploy-button" href="#"><i class="fa fa-caret-down"></i></a>'+
2019-05-01 00:38:54 +02:00
'</span></li>').prependTo(".red-ui-header-toolbar");
2015-04-10 16:13:40 +02:00
RED.menu.init({id:"btn-deploy-options",
options: [
2015-05-22 01:40:27 +02:00
{id:"deploymenu-item-full",toggle:"deploy-type",icon:"red/images/deploy-full.png",label:RED._("deploy.full"),sublabel:RED._("deploy.fullDesc"),selected: true, onselect:function(s) { if(s){changeDeploymentType("full")}}},
{id:"deploymenu-item-flow",toggle:"deploy-type",icon:"red/images/deploy-flows.png",label:RED._("deploy.modifiedFlows"),sublabel:RED._("deploy.modifiedFlowsDesc"), onselect:function(s) {if(s){changeDeploymentType("flows")}}},
{id:"deploymenu-item-node",toggle:"deploy-type",icon:"red/images/deploy-nodes.png",label:RED._("deploy.modifiedNodes"),sublabel:RED._("deploy.modifiedNodesDesc"),onselect:function(s) { if(s){changeDeploymentType("nodes")}}},
null,
{id:"deploymenu-item-reload", icon:"red/images/deploy-reload.png",label:RED._("deploy.restartFlows"),sublabel:RED._("deploy.restartFlowsDesc"),onselect:"core:restart-flows"},
2015-04-10 16:13:40 +02:00
]
});
} else if (type == "simple") {
2015-05-22 01:40:27 +02:00
var label = options.label || RED._("deploy.deploy");
2015-04-10 16:13:40 +02:00
var icon = 'red/images/deploy-full-o.png';
if (options.hasOwnProperty('icon')) {
icon = options.icon;
}
2015-07-01 00:42:03 +02:00
2015-04-10 16:13:40 +02:00
$('<li><span class="deploy-button-group button-group">'+
2015-04-13 22:50:40 +02:00
'<a id="btn-deploy" class="deploy-button disabled" href="#">'+
2016-10-20 14:11:12 +02:00
'<span class="deploy-button-content">'+
(icon?'<img id="btn-deploy-icon" src="'+icon+'"> ':'')+
'<span>'+label+'</span>'+
'</span>'+
'<span class="deploy-button-spinner hide">'+
'<img src="red/images/spin.svg"/>'+
'</span>'+
'</a>'+
2019-05-01 00:38:54 +02:00
'</span></li>').prependTo(".red-ui-header-toolbar");
2015-04-10 16:13:40 +02:00
}
2015-07-01 00:42:03 +02:00
$('#btn-deploy').on("click", function(event) {
event.preventDefault();
save();
});
2015-07-01 00:42:03 +02:00
2017-01-11 15:33:40 +01:00
RED.actions.add("core:deploy-flows",save);
RED.actions.add("core:restart-flows",restart);
2017-01-11 15:33:40 +01:00
2015-03-17 16:38:31 +01:00
RED.events.on('nodes:change',function(state) {
2015-03-15 23:54:55 +01:00
if (state.dirty) {
window.onbeforeunload = function() {
2015-07-01 00:42:03 +02:00
return RED._("deploy.confirm.undeployedChanges");
}
2015-03-15 23:54:55 +01:00
$("#btn-deploy").removeClass("disabled");
} else {
window.onbeforeunload = null;
2015-03-15 23:54:55 +01:00
$("#btn-deploy").addClass("disabled");
}
});
var activeNotifyMessage;
RED.comms.subscribe("notification/runtime-deploy",function(topic,msg) {
if (!activeNotifyMessage) {
var currentRev = RED.nodes.version();
if (currentRev === null || deployInflight || currentRev === msg.revision) {
return;
}
var message = $('<p>').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;
}
}
]
});
}
});
2015-03-15 23:54:55 +01:00
}
2015-03-17 16:38:31 +01:00
2015-09-25 23:33:54 +02:00
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);
2015-09-25 23:33:54 +02:00
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 = $('<div>');
$('<p data-i18n="deploy.confirm.conflict"></p>').appendTo(message);
2019-05-03 21:22:46 +02:00
var conflictCheck = $('<div class="red-ui-deploy-dialog-confirm-conflict-row">'+
'<img src="red/images/spin.svg"/><div data-i18n="deploy.confirm.conflictChecking"></div>'+
'</div>').appendTo(message);
2019-05-03 21:22:46 +02:00
var conflictAutoMerge = $('<div class="red-ui-deploy-dialog-confirm-conflict-row">'+
'<i style="color: #3a3;" class="fa fa-check"></i><div data-i18n="deploy.confirm.conflictAutoMerge"></div>'+
'</div>').hide().appendTo(message);
2019-05-03 21:22:46 +02:00
var conflictManualMerge = $('<div class="red-ui-deploy-dialog-confirm-conflict-row">'+
'<i style="color: #999;" class="fa fa-exclamation"></i><div data-i18n="deploy.confirm.conflictManualMerge"></div>'+
'</div>').hide().appendTo(message);
message.i18n();
currentDiff = null;
var buttons = [
{
text: RED._("common.label.cancel"),
click: function() {
conflictNotification.close();
}
},
{
2019-05-03 21:22:46 +02:00
id: "red-ui-deploy-dialog-confirm-deploy-review",
text: RED._("deploy.confirm.button.review"),
class: "primary disabled",
click: function() {
2019-05-03 21:22:46 +02:00
if (!$("#red-ui-deploy-dialog-confirm-deploy-review").hasClass('disabled')) {
RED.diff.showRemoteDiff();
conflictNotification.close();
}
}
},
{
2019-05-03 21:22:46 +02:00
id: "red-ui-deploy-dialog-confirm-deploy-merge",
text: RED._("deploy.confirm.button.merge"),
class: "primary disabled",
click: function() {
2019-05-03 21:22:46 +02:00
if (!$("#red-ui-deploy-dialog-confirm-deploy-merge").hasClass('disabled')) {
RED.diff.mergeDiff(currentDiff);
conflictNotification.close();
}
}
}
];
if (activeDeploy) {
buttons.push({
2019-05-03 21:22:46 +02:00
id: "red-ui-deploy-dialog-confirm-deploy-overwrite",
text: RED._("deploy.confirm.button.overwrite"),
class: "primary",
click: function() {
save(true,activeDeploy);
conflictNotification.close();
}
})
}
var conflictNotification = RED.notify(message,{
modal: true,
fixed: true,
width: 600,
buttons: buttons
});
var now = Date.now();
RED.diff.getRemoteDiff(function(diff) {
var ellapsed = Math.max(1000 - (Date.now()-now), 0);
currentDiff = diff;
setTimeout(function() {
conflictCheck.hide();
var d = Object.keys(diff.conflicts);
if (d.length === 0) {
conflictAutoMerge.show();
2019-05-03 21:22:46 +02:00
$("#red-ui-deploy-dialog-confirm-deploy-merge").removeClass('disabled')
} else {
conflictManualMerge.show();
}
2019-05-03 21:22:46 +02:00
$("#red-ui-deploy-dialog-confirm-deploy-review").removeClass('disabled')
},ellapsed);
})
}
function cropList(list) {
if (list.length > 5) {
var remainder = list.length - 5;
list = list.slice(0,5);
list.push(RED._("deploy.confirm.plusNMore",{count:remainder}));
}
return list;
}
function sanitize(html) {
return html.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")
}
function restart() {
var startTime = Date.now();
$(".deploy-button-content").css('opacity',0);
$(".deploy-button-spinner").show();
var deployWasEnabled = !$("#btn-deploy").hasClass("disabled");
$("#btn-deploy").addClass("disabled");
deployInflight = true;
2019-05-01 00:38:54 +02:00
$("#red-ui-header-shade").show();
$("#editor-shade").show();
2019-04-29 23:38:14 +02:00
$("#red-ui-palette-shade").show();
2019-04-30 23:56:39 +02:00
$("#red-ui-sidebar-shade").show();
$.ajax({
url:"flows",
type: "POST",
headers: {
"Node-RED-Deployment-Type":"reload"
}
}).done(function(data,textStatus,xhr) {
if (deployWasEnabled) {
$("#btn-deploy").removeClass("disabled");
}
RED.notify('<p>'+RED._("deploy.successfulRestart")+'</p>',"success");
}).fail(function(xhr,textStatus,err) {
if (deployWasEnabled) {
$("#btn-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() {
deployInflight = false;
var delta = Math.max(0,300-(Date.now()-startTime));
setTimeout(function() {
$(".deploy-button-content").css('opacity',1);
$(".deploy-button-spinner").hide();
2019-05-01 00:38:54 +02:00
$("#red-ui-header-shade").hide();
$("#editor-shade").hide();
2019-04-29 23:38:14 +02:00
$("#red-ui-palette-shade").hide();
2019-04-30 23:56:39 +02:00
$("#red-ui-sidebar-shade").hide();
},delta);
});
}
function save(skipValidation,force) {
2016-10-20 14:11:12 +02:00
if (!$("#btn-deploy").hasClass("disabled")) {
2018-01-12 22:00:11 +01:00
if (!RED.user.hasPermission("flows.write")) {
RED.notify(RED._("user.errors.deploy"),"error");
return;
}
if (!skipValidation) {
var hasUnknown = false;
var hasInvalid = false;
var hasUnusedConfig = false;
2015-07-01 00:42:03 +02:00
2015-03-15 23:54:55 +01:00
var unknownNodes = [];
2015-09-25 23:33:54 +02:00
var invalidNodes = [];
2015-03-15 23:54:55 +01:00
RED.nodes.eachNode(function(node) {
hasInvalid = hasInvalid || !node.valid;
2015-09-25 23:33:54 +02:00
if (!node.valid) {
invalidNodes.push(getNodeInfo(node));
}
2015-03-15 23:54:55 +01:00
if (node.type === "unknown") {
if (unknownNodes.indexOf(node.name) == -1) {
unknownNodes.push(node.name);
}
}
});
hasUnknown = unknownNodes.length > 0;
2015-07-01 00:42:03 +02:00
2015-09-25 23:33:54 +02:00
var unusedConfigNodes = [];
RED.nodes.eachConfig(function(node) {
if ((node._def.hasUsers !== false) && (node.users.length === 0)) {
2015-09-25 23:33:54 +02:00
unusedConfigNodes.push(getNodeInfo(node));
hasUnusedConfig = true;
2015-03-15 23:54:55 +01:00
}
});
2015-07-01 00:42:03 +02:00
var showWarning = false;
var notificationMessage;
var notificationButtons = [];
var notification;
if (hasUnknown && !ignoreDeployWarnings.unknown) {
showWarning = true;
notificationMessage = "<p>"+RED._('deploy.confirm.unknown')+"</p>"+
2019-05-03 21:22:46 +02:00
'<ul class="red-ui-deploy-dialog-confirm-list"><li>'+cropList(unknownNodes).map(function(n) { return sanitize(n) }).join("</li><li>")+"</li></ul><p>"+
RED._('deploy.confirm.confirm')+
"</p>";
notificationButtons= [
{
2019-05-03 21:22:46 +02:00
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;
2015-09-25 23:33:54 +02:00
invalidNodes.sort(sortNodeInfo);
notificationMessage = "<p>"+RED._('deploy.confirm.improperlyConfigured')+"</p>"+
2019-05-03 21:22:46 +02:00
'<ul class="red-ui-deploy-dialog-confirm-list"><li>'+cropList(invalidNodes.map(function(A) { return sanitize( (A.tab?"["+A.tab+"] ":"")+A.label+" ("+A.type+")")})).join("</li><li>")+"</li></ul><p>"+
RED._('deploy.confirm.confirm')+
"</p>";
notificationButtons= [
{
2019-05-03 21:22:46 +02:00
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
});
2015-03-15 23:54:55 +01:00
return;
}
}
2015-07-01 00:42:03 +02:00
2015-03-15 23:54:55 +01:00
var nns = RED.nodes.createCompleteNodeSet();
var startTime = Date.now();
2016-10-20 14:11:12 +02:00
$(".deploy-button-content").css('opacity',0);
$(".deploy-button-spinner").show();
$("#btn-deploy").addClass("disabled");
var data = {flows:nns};
if (!force) {
data.rev = RED.nodes.version();
}
2015-03-15 23:54:55 +01:00
deployInflight = true;
2019-05-01 00:38:54 +02:00
$("#red-ui-header-shade").show();
$("#editor-shade").show();
2019-04-29 23:38:14 +02:00
$("#red-ui-palette-shade").show();
2019-04-30 23:56:39 +02:00
$("#red-ui-sidebar-shade").show();
2015-03-15 23:54:55 +01:00
$.ajax({
url:"flows",
type: "POST",
data: JSON.stringify(data),
2015-03-15 23:54:55 +01:00
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);
2016-12-06 23:37:21 +01:00
RED.nodes.originalFlow(nns);
if (hasUnusedConfig) {
2016-01-13 00:03:33 +01:00
RED.notify(
'<p>'+RED._("deploy.successfulDeploy")+'</p>'+
'<p>'+RED._("deploy.unusedConfigNodes")+' <a href="#" onclick="RED.sidebar.config.show(true); return false;">'+RED._("deploy.unusedConfigNodesLink")+'</a></p>',"success",false,6000);
} else {
RED.notify('<p>'+RED._("deploy.successfulDeploy")+'</p>',"success");
}
2015-03-15 23:54:55 +01:00
RED.nodes.eachNode(function(node) {
if (node.changed) {
node.dirty = true;
node.changed = false;
}
if (node.moved) {
node.dirty = true;
node.moved = false;
}
2015-03-15 23:54:55 +01:00
if(node.credentials) {
delete node.credentials;
}
});
RED.nodes.eachConfig(function (confNode) {
confNode.changed = false;
2015-03-15 23:54:55 +01:00
if (confNode.credentials) {
delete confNode.credentials;
}
});
RED.nodes.eachSubflow(function(subflow) {
subflow.changed = false;
});
RED.nodes.eachWorkspace(function(ws) {
ws.changed = false;
});
2015-03-15 23:54:55 +01:00
// Once deployed, cannot undo back to a clean state
RED.history.markAllDirty();
RED.view.redraw();
RED.events.emit("deploy");
2015-03-15 23:54:55 +01:00
}).fail(function(xhr,textStatus,err) {
RED.nodes.dirty(true);
2016-10-20 14:11:12 +02:00
$("#btn-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");
2015-03-15 23:54:55 +01:00
} else {
RED.notify(RED._("deploy.deployFailed",{message:RED._("deploy.errors.noResponse")}),"error");
2015-03-15 23:54:55 +01:00
}
}).always(function() {
deployInflight = false;
var delta = Math.max(0,300-(Date.now()-startTime));
setTimeout(function() {
$(".deploy-button-content").css('opacity',1);
$(".deploy-button-spinner").hide();
2019-05-01 00:38:54 +02:00
$("#red-ui-header-shade").hide();
$("#editor-shade").hide();
2019-04-29 23:38:14 +02:00
$("#red-ui-palette-shade").hide();
2019-04-30 23:56:39 +02:00
$("#red-ui-sidebar-shade").hide();
},delta);
2015-03-15 23:54:55 +01:00
});
}
}
return {
2017-09-20 11:30:07 +02:00
init: init,
setDeployInflight: function(state) {
deployInflight = state;
}
2015-03-15 23:54:55 +01:00
}
})();