2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2015-03-11 18:43:42 +01:00
|
|
|
* Copyright 2013,2015 IBM Corp.
|
2013-09-05 16:02:48 +02: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.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-06-07 23:00:03 +02:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var exec = require('child_process').exec;
|
2015-01-07 22:12:01 +01:00
|
|
|
var isUtf8 = require('is-utf8');
|
2014-06-07 23:00:03 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function ExecNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2015-03-26 16:01:55 +01:00
|
|
|
this.cmd = (n.command || "").trim();
|
2015-04-24 16:11:59 +02:00
|
|
|
if (n.addpay === undefined) { n.addpay = true; }
|
2015-03-11 18:43:42 +01:00
|
|
|
this.addpay = n.addpay;
|
2015-03-26 16:01:55 +01:00
|
|
|
this.append = (n.append || "").trim();
|
2014-05-04 00:32:04 +02:00
|
|
|
this.useSpawn = n.useSpawn;
|
2015-11-25 00:09:44 +01:00
|
|
|
this.activeProcesses = {};
|
2014-06-07 23:00:03 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
|
|
|
this.on("input", function(msg) {
|
2015-10-22 20:02:22 +02:00
|
|
|
node.status({fill:"blue",shape:"dot",text:" "});
|
2014-09-08 22:10:06 +02:00
|
|
|
if (this.useSpawn === true) {
|
|
|
|
// make the extra args into an array
|
|
|
|
// then prepend with the msg.payload
|
2015-10-05 22:17:24 +02:00
|
|
|
|
2015-11-25 00:09:44 +01:00
|
|
|
var arg = node.cmd;
|
|
|
|
if (node.addpay) {
|
|
|
|
arg += " "+msg.payload;
|
|
|
|
}
|
|
|
|
arg += " "+node.append;
|
2015-10-05 22:17:24 +02:00
|
|
|
// slice whole line by spaces (trying to honour quotes);
|
|
|
|
arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g);
|
|
|
|
var cmd = arg.shift();
|
|
|
|
if (RED.settings.verbose) { node.log(cmd+" ["+arg+"]"); }
|
|
|
|
if (cmd.indexOf(" ") == -1) {
|
|
|
|
var ex = spawn(cmd,arg);
|
2015-11-25 00:09:44 +01:00
|
|
|
node.activeProcesses[ex.pid] = ex;
|
2014-09-08 22:10:06 +02:00
|
|
|
ex.stdout.on('data', function (data) {
|
|
|
|
//console.log('[exec] stdout: ' + data);
|
2015-01-07 22:12:01 +01:00
|
|
|
if (isUtf8(data)) { msg.payload = data.toString(); }
|
|
|
|
else { msg.payload = data; }
|
2014-09-08 22:10:06 +02:00
|
|
|
node.send([msg,null,null]);
|
|
|
|
});
|
|
|
|
ex.stderr.on('data', function (data) {
|
|
|
|
//console.log('[exec] stderr: ' + data);
|
2015-01-07 22:12:01 +01:00
|
|
|
if (isUtf8(data)) { msg.payload = data.toString(); }
|
|
|
|
else { msg.payload = new Buffer(data); }
|
2014-09-08 22:10:06 +02:00
|
|
|
node.send([null,msg,null]);
|
|
|
|
});
|
|
|
|
ex.on('close', function (code) {
|
|
|
|
//console.log('[exec] result: ' + code);
|
2015-11-25 00:09:44 +01:00
|
|
|
delete node.activeProcesses[ex.pid];
|
2014-09-08 22:10:06 +02:00
|
|
|
msg.payload = code;
|
2016-04-10 13:21:07 +02:00
|
|
|
if (code === 0) { node.status({}); }
|
|
|
|
else if (code < 0) { node.status({fill:"red",shape:"dot",text:"rc: "+code}); }
|
|
|
|
else { node.status({fill:"yellow",shape:"dot",text:"rc: "+code}); }
|
2014-09-08 22:10:06 +02:00
|
|
|
node.send([null,null,msg]);
|
|
|
|
});
|
|
|
|
ex.on('error', function (code) {
|
2015-11-25 00:09:44 +01:00
|
|
|
delete node.activeProcesses[ex.pid];
|
2015-03-16 14:58:01 +01:00
|
|
|
node.error(code,msg);
|
2014-04-07 00:25:40 +02:00
|
|
|
});
|
|
|
|
}
|
2015-05-10 22:47:22 +02:00
|
|
|
else { node.error(RED._("exec.spawnerr")); }
|
2014-09-08 22:10:06 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-03-11 18:43:42 +01:00
|
|
|
var cl = node.cmd;
|
2015-07-08 16:06:08 +02:00
|
|
|
if ((node.addpay === true) && ((msg.payload.toString() || "").trim() !== "")) { cl += " "+msg.payload; }
|
2015-03-11 18:43:42 +01:00
|
|
|
if (node.append.trim() !== "") { cl += " "+node.append; }
|
2014-10-25 18:50:55 +02:00
|
|
|
if (RED.settings.verbose) { node.log(cl); }
|
2015-01-08 13:35:42 +01:00
|
|
|
var child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) {
|
|
|
|
msg.payload = new Buffer(stdout,"binary");
|
2015-04-24 16:11:59 +02:00
|
|
|
try {
|
|
|
|
if (isUtf8(msg.payload)) { msg.payload = msg.payload.toString(); }
|
|
|
|
} catch(e) {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.log(RED._("exec.badstdout"));
|
2015-04-24 16:11:59 +02:00
|
|
|
}
|
2014-09-08 22:10:06 +02:00
|
|
|
var msg2 = {payload:stderr};
|
|
|
|
var msg3 = null;
|
|
|
|
//console.log('[exec] stdout: ' + stdout);
|
|
|
|
//console.log('[exec] stderr: ' + stderr);
|
|
|
|
if (error !== null) {
|
|
|
|
msg3 = {payload:error};
|
|
|
|
//console.log('[exec] error: ' + error);
|
|
|
|
}
|
|
|
|
node.status({});
|
|
|
|
node.send([msg,msg2,msg3]);
|
2015-11-25 00:09:44 +01:00
|
|
|
delete node.activeProcesses[child.pid];
|
2014-09-08 22:10:06 +02:00
|
|
|
});
|
2015-11-25 00:09:44 +01:00
|
|
|
child.on('error',function(){})
|
|
|
|
node.activeProcesses[child.pid] = child;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.on('close',function() {
|
|
|
|
for (var pid in node.activeProcesses) {
|
|
|
|
if (node.activeProcesses.hasOwnProperty(pid)) {
|
|
|
|
node.activeProcesses[pid].kill();
|
|
|
|
}
|
2013-10-20 21:37:40 +02:00
|
|
|
}
|
2015-11-25 00:09:44 +01:00
|
|
|
node.activeProcesses = {};
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("exec",ExecNode);
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|