-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenghistask.html
More file actions
267 lines (244 loc) · 11.8 KB
/
genghistask.html
File metadata and controls
267 lines (244 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<script type="text/javascript">
function suggestionOnClick(button, url, input) {
button.addClass('disabled');
$.getJSON(url, function(data) {
button.removeClass('disabled');
input.autocomplete({
source:data || [],
minLength:0,
close: function( event, ui ) {
input.autocomplete( "destroy" );
}
}).autocomplete("search","");
});
}
function treeListOnClick(button, url, input) {
button.addClass('disabled');
$.getJSON(url, function(data) {
//@TODO empty tree in case their is already a tree
const tree = treeList = $("<div>")
.css({height: "8em"})
.appendTo(input.parent())
.treeList({});
tree.treeList("data", data.children);
tree.on('treelistselect', function(event, item) {
if (typeof(item.children) == "undefined") {
const getName = (i) => i.depth===0 ? i.label : getName(i.parent) + "/" + i.label;
input.val(getName(item));
}
});
});
}
function refreshWorkspace(button, url, input) {
button.addClass('disabled');
$.post(url, function(data) {
//@TODO check request ERROR
button.removeClass('disabled');
$('.need-workspace-lookedup').show();
});
}
function displayLookup(input) {
if (input.val()=="_ADD_") {
$('.need-workspace-selected').hide();
$('.need-workspace-lookedup').hide();
} else {
$('.need-workspace-selected').show();
}
}
RED.nodes.registerType('genghistask',{
category: 'function',
color: '#a6bbcf',//@TODO same color as exec ?
defaults: {
name: {value:""},
workspace: {value:"", type:"genghistask-workspace"},
environment: {value:""},
script: {value:"",required:true}
},
inputs:1,
inputLabels: "env",
outputs:1,
outputLabels: ["exit code"],
icon: "file.png",
label: function() {
return this.name||this.script||"genghistask";
},
oneditprepare: function(test) {
const getUriParam = () => new URLSearchParams({
id: $('#node-input-workspace option[selected]').val(),
workspace: $('#node-input-workspace option[selected]').text(),
script: $('#node-input-script').val(),
nodeId: this.id
});
const autocompleteBehavior = {
"environment" : suggestionOnClick,
"script" : treeListOnClick,
"workspace": refreshWorkspace
};
Object.keys(autocompleteBehavior).map((autocompleteEntity)=>{
const button = $("#node-input-" + autocompleteEntity + "-lookup");
const input = $("#node-input-" + autocompleteEntity);
const url = `genghistask-${autocompleteEntity}?`;
const behavior = autocompleteBehavior[autocompleteEntity];
try {
input.autocomplete( "destroy" );
} catch(err) {
}
$(button).click(function() {return behavior(button, url + getUriParam(), input);});
});
const onChangeBehavior = {
"workspace": displayLookup
};
Object.keys(onChangeBehavior).map((autocompleteEntity)=>{
const input = $("#node-input-" + autocompleteEntity);
const behavior = onChangeBehavior[autocompleteEntity];
$(input).change(function() {return behavior(input);});
behavior(input);
});
var tabs = RED.tabs.create({
id: "node-config-genghistask-tabs",
onchange: function(tab) {
const url = `genghistask-tab?tab_id=` + tab.id.split("genghistask-tab-std").pop() +'&'+ getUriParam();
$("#node-config-genghistask-tabs-content").children().hide();
$("#" + tab.id).show();
$.get(url, function(data) {
if (tab.id == "genghistask-tab-stdlog" && data && data.split) {
const header = ['{"timestamp":"timestamp", "msg":"msg", "pid":"pid", "environment":"environment", "command":"command"}'];
content = '<table class="red-ui-info-table">' + (header.concat(data.split("\n")).filter(row=>row).map(row=> "<tr>" + Object.entries(JSON.parse(header[0])).map(([col])=>`<td>${JSON.parse(row)[col]||""}</td>`).join("") + "</tr>").join(""))+'</table>';
} else {
content = $('<pre></pre>').html(data)
}
$('#' + tab.id).html(content);
});
}
});
//TODO script preview in yet an other tab
const logAvailable = ["log", "out", "err", "shell"];
logAvailable.forEach(log=>tabs.addTab({
id: "genghistask-tab-std" + log,
label: this._("genghistask.std" + log)
}));
(function hackExistingTab() {
realone = $('#red-ui-tab-editor-tab-properties-link-button');
dummy = $('<a><i class="fa fa-cog"></i></a>');
dummy.click(function(){
realone.trigger('click');
$("#genghistask-main-form").show();
$("#genghistask-tabs-part").hide();
$('#red-ui-tab-editor-tab-properties span.red-ui-text-bidi-aware').text("Properties");
});
dummy.insertAfter(realone);
logAvailable.forEach(log=>{
a = $("<a>"+log+"</a>");
a.click(function(){
realone.trigger('click');
$("#genghistask-main-form").hide();
$("#genghistask-tabs-part").show();
tabs.activateTab("genghistask-tab-std" + log);
$('#red-ui-tab-editor-tab-properties span.red-ui-text-bidi-aware').text(log);
});
realone.parent().append(a);
});
realone.css('visibility','hidden');
$("#genghistask-tabs-part").hide();
$("#node-config-genghistask-tabs").hide();
})();
},
button: {
onclick: function () {
$.ajax({
url: "inject/" + this.id,
type: "POST",
success: function (resp) {
RED.notify(node._("inject.success"));
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 404) {
RED.notify(node._("common.notification.error", { message: node._("common.notification.errors.not-deployed") }), "error");
} else if (jqXHR.status == 500) {
RED.notify(node._("common.notification.error", { message: node._("inject.errors.failed") }), "error");
} else if (jqXHR.status == 0) {
RED.notify(node._("common.notification.error", { message: node._("common.notification.errors.no-response") }), "error");
} else {
RED.notify(node._("common.notification.error", { message: node._("common.notification.errors.unexpected", { status: jqXHR.status, message: textStatus }) }), "error");
}
}
});
}
},
});
</script>
<script type="text/html" data-template-name="genghistask">
<div id="genghistask-main-form">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<hr/>
<div class="form-row">
<label for="node-input-workspace"><i class="fa fa-tag"></i> Workspace</label>
<input type="text" id="node-input-workspace">
<a id="node-input-workspace-lookup" class="red-ui-button need-workspace-selected"><i id="node-input-workspace-lookup-icon" class="fa fa-refresh"></i></a>
</div>
<fieldset class="need-workspace-selected need-workspace-lookedup">
<div class="form-row">
<label for="node-input-environment"><i class="fa fa-tag"></i> Environment</label>
<input style="width:62%;" type="text" id="node-input-environment">
<a id="node-input-environment-lookup" class="red-ui-button"><i id="node-input-environment-lookup-icon" class="fa fa-search"></i></a>
</div>
<div class="form-row">
<label for="node-input-script"><i class="fa fa-tag"></i> Execute</label>
<input style="width:61%;" type="text" id="node-input-script">
<a id="node-input-script-lookup" class="red-ui-button"><i id="node-input-script-lookup-icon" class="fa fa-search"></i></a>
</div>
</fieldset>
</div>
<div id="genghistask-tabs-part">
<ul id="node-config-genghistask-tabs"></ul>
<div id="node-config-genghistask-tabs-content" style="min-height:150px;">
<div id="genghistask-tab-stdlog" style="display: none;">
</div>
<div id="genghistask-tab-stderr" style="display: none;">
</div>
<div id="genghistask-tab-stdout" style="display: none;">
</div>
<div id="genghistask-tab-stdshell" style="display: none;">
</div>
</div>
</div>
</script>
<script type="text/html" data-help-name="genghistask">
<p>Exec a command or run a script file from a workspace.</p>
<h3>Parameters</h3>
Specify what must be executed and if it must be done remotely.
<dl class="message-properties">
<dt>Workspace <span class="property-type"><a href="#" onclick="RED.sidebar.help.show(this.innerHTML); return false;">genghistask-workspace</a></span></dt>
<dd>A git repository.</dd>
<dt>Environment <span class="property-type">string</span></dt>
<dd>A hostname to execute the task remotely. Connection may be secured with <a href="#" onclick="RED.userSettings.show('genghistask-ssh-settings'); return false;">ssh</a></dd>
<dt>Script <span class="property-type">string</span></dt>
<dd>A bash command or the path towards a script.</dd>
<dt>Name (optional) <span class="property-type">string</span></dt>
<dd>Customize node name.</dd>
</dl>
<h3>Optional inputs</h3>
<p>All message properties will be used as environment variable.</p>
<p></p>
<dl class="message-properties">
<dt>foo
<span class="property-type">string</span>
</dt>
<dd>Inject a message with the properties FOO, in order to export the bash environement variable FOO=bar, . </dd>
</dl>
<h3>Outputs</h3>
The node will output only if the command exit with success.
<dl class="message-properties">
<dt>code <span class="property-type">string</span></dt>
<dd>The execution command return value.</dd>
<dt>...msg <span class="property-type">object</span></dt>
<dd>All fields of the input are also echo back in the output</dd>
</dl>
<h3>References</h3>
<ul>
<li><a>Docs</a> https://genghistask.github.io/</li>
</ul>
</script>