Untaring a file in nodeJS child process throws exception - node.js

I have a file bundle.tar.gz which I am trying to untar. The command tar xvfz bundle.tar.gz works on terminal.
However it fails when executed via node child process like so:
child_process.exec(`tar xvfz bundle.tar.gz && bash file.sh`,
{ cwd: path }, (err, stdout, stderr) => {
if (err && err.code !== 0) {
deferred.reject({message: err.message});
} else {
deferred.resolve('COMPLETED');
}
}
);
The error I'm getting:
message"=>"Command failed: /bin/sh -c tar xvfz bundle.tar.gz && bash file.sh ngzip: stdin: unexpected end of file\ntar: Unexpected EOF in archive\ntar: Unexpected EOF in archive\ntar: Error is not recoverable: exiting now\n
I dont have a clue why this is not happening :(

Related

execSync throws and error trying to run node

I am running some processes inside of an EC2 instance.
To run it I initiate it with an SSM command:
cd / && cd home/ec2-user && . .nvm/nvm.sh && cd ufo && npm run start
and inside of it, I have a method in app.ts which is initialized with ts-node app.ts
import { execSync } from 'node:child_process';
import { takeNextScheduledAudit } from './sqs-scheduler';
import { uploadResultsToBucket } from './s3-uploader';
import { AuditRunParams } from "./types";
import { sendAuditResults } from "./sendResults";
(async function conductor(): Promise<void> {
const nextAuditRunParams = await takeNextScheduledAudit();
if (!nextAuditRunParams) {
execSync("sudo shutdown -h now");
}
const { targetUrl, requesterId, endpoint } = nextAuditRunParams as AuditRunParams;
try {
execSync(`npx user-flow --url=${targetUrl} --open=false`);
const resultsUrl = await uploadResultsToBucket(targetUrl);
await sendAuditResults(requesterId, endpoint, resultsUrl);
} catch (error) {
console.log(error);
}
await conductor();
})();
If I log in manually and run npm run start the scripts works as intended but if I run it using the SSM command I get this output:
> start
> ts-node app.ts
Error: Command failed: npx user-flow --url=https://deep-blue.io/ --open=false
at checkExecSyncError (node:child_process:841:11)
at execSync (node:child_process:912:15)
at conductor (/home/ec2-user/ufo/app.ts:15:17)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
status: 243,
signal: null,
output: [ null, <Buffer >, <Buffer 0a> ],
pid: 2691,
stdout: <Buffer >,
stderr: <Buffer 0a>
}
and this error:
Error: Command failed: sudo shutdown -h now
at checkExecSyncError (node:child_process:841:11)
at execSync (node:child_process:912:15)
at conductor (/home/ec2-user/ufo/app.ts:10:17)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async conductor (/home/ec2-user/ufo/app.ts:21:5) {
status: null,
signal: 'SIGTERM',
output: [ null, Buffer(0) [Uint8Array] [], Buffer(0) [Uint8Array] [] ],
pid: 2705,
stdout: Buffer(0) [Uint8Array] [],
stderr: Buffer(0) [Uint8Array] []
}
failed to run commands: exit status 1
Moreover, if I run execSync("node -v && npx -v") it also throws an error.
Why can I run this script when i am logged in but if i run it via a SSM command it does not recognize node inside of node?
--- Edit - Added Info ---
When running execSync(node -v && npx -v,{shell: '/bin/bash'}) I get an error:
Error: Command failed: node -v && npx -v
When running execSync(ps -p $$ && echo $SHELL, {shell: '/bin/bash'}):
PID TTY TIME CMD
7817 ? 00:00:00 bash
/bin/bash
And when I loggin and do ps -p $$ && echo $SHELL I get:
PID TTY TIME CMD
6873 pts/0 00:00:00 bash
/bin/bash
By default, all of the child_process functions execute in the same environment as the process that launched them. I don't have an account handy to test with, but it's quite likely that SSM skips over a traditional shell and just executes certain runtimes directly.
You can use the exec options like this to set a particular shell in which to launch the process:
const output = execSync('echo "doing stuff"', {
shell: '/bin/bash',
})
console.log('***** output:', output.toString())
This is assuming the OS you're using for the EC2 instance has bash available. Most flavors of linux should, but for what you're doing there, /bin/sh is sufficient if not. To get a list of the available shells, you can run:
cat /etc/shells
## or possibly
sudo cat /etc/shells
EDIT: Since you say it works fine in a shell already, you have presumably already handled this, but user-flow would also have to be available. It's not a module from npmjs, so would need to already be present on the box as either a local dependency or a private repo to which the EC2 instance has access.

RSYNCWRAPPER: rsync exited with code 12

I'm using rsyncwrapper node module.
Here is my code:
rsync({
src: source_path,
dest: dest,
ssh: true,
recursive: true,
privateKey: keystring,
port: port,
args: ['--perms','--rsync-path="mkdir -p '+dest_path+' && rsync"']
},function (error,stdout,stderr,cmd) {
if ( error ) {
// failed
var data_response = {
'Status': 'Failed',
'Message': 'Rsync Failed=>'+error+stdout+stderr+cmd
};
logger.info("[Rsync] Response=>" + JSON.stringify(data_response));
console.log(error.message);
res.json(data_response);
} else {
console.log("Success");
var data_response = {
'Status': 'Success',
'Message': 'Rsync Success'
};
logger.info("[Rsync] Response=>" + JSON.stringify(data_response));
res.json(data_response);
}
});
However I get this error:
[2018-08-07 13:15:33.491] [INFO] JDWF-API - [Rsync] Response=>{"Status":"Failed","Message":"Rsync Failed=>Error: rsync exited with code 12WARNING : Unauthorized access to this system is forbidden and will be\nprosecuted by law. By accessing this system, you agree that your actions\nmay be monitored if unauthorized usage is suspected.\nmkdir: cannot create directory `/DATA/userWorkpace/devteam/7000003': Permission denied\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]\nrsync /DATA/files/gs/7000003/input// wf-sync#192.168.1.23:/DATA/userWorkpace/devteam/7000003/input// --rsh \"ssh -p 47365 -i /DATA/jdwf-api/secure/wf-sync.dat\" --recursive --perms --rsync-path=\"mkdir -p /DATA/userWorkpace/devteam/7000003/input// && rsync\""}
rsync exited with code 12
I have given 777 permissions to the folder. It doesnt fix it. Can someone help me out please.

Open Terminal failed with osascript after electron-packager

When I try to execute an osascript command under my Electron project, it works under dev mode(1), but after building it with electron-packager(2) and executing the binary file, an error appears.
It works with this execution command to debug:
# electron .
It fails with this compilation command to package the project:
# electron-packager . MyProject --platform=darwin --arch=x64 --version=1.3.5 --overwrite
This is the source code:
var childProcess = require('child_process');
var script = "/usr/bin/osascript -e 'tell application \"Terminal\"' -e 'set newTab to do script' -e 'end tell'";
childProcess.exec(script, function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
Console displays this error:
stdout:
stderr: 45:51: syntax error: Expected end of line but found “script”. (-2741)
exec error: Error: Command failed: /usr/bin/osascript -e 'tell application "Terminal"' -e 'set newTab to do script' -e 'end tell'
45:51: syntax error: Expected end of line but found “script”. (-2741)
BUT
The same source code works after packager if the script variable is equal to:
var script = "/usr/bin/osascript -e 'get volume settings'";
My Env
My node version: v4.5.0
My electron version: v1.3.5

How to execute forever command remotely without using full path?

I am trying to execute “forever” command remotely using powershell but I am getting error
'forever' is not recognized as an internal or external command
Here is what I am doing. I have the node js script "MyNodeScript.js" which executes the forever command. The script is on the WebServer "MyWebServer1".
Node.Js and Forever is installed on MyWebServer1 globally.
var exec = require('child_process').exec;
var _ = require('underscore');
var winston = require('winston');
var async = require('async');
var mycommand = 'forever -p ./logs --sourceDir ../wf_manager --workingDir ../wf_manager start -a --uid "ServiceApp" ServiceApp.js'
function start(callback) {
async.waterfall([
function (cb) {
executeCommand(mycommand, false, cb);
}
], function done(err) {
if (err) {
winston.error('failed to start all instances by forever:' + err);
} else {
winston.info('successfully started all instances by forever');
}
callback();
});
}
function executeCommand(command, skip, callback) {
async.waterfall([
function (cb) {
exec(command, cb);
}
], function done(err) {
if (err) {
if (skip) {
// skip the error
callback(null);
} else {
callback(err);
}
} else {
callback(null);
}
});
}
module.exports = {
executeCommand: executeCommand,
start: start
}
start(function(){});
On the same MyWebServer1 under same folder i have a powershell script "MyPowerShellScript.ps1" which call this node script. The powershell script has only one line
node D:\myfolder\maintenance\MyNodeScript.js
I can run this powershell script locally on MyWebServer1 and it works fine. But when i try to execute this powershell script as below from remote machine
invoke-command -computername MyWebServer1 -filepath \\MyWebServer1\MyFolder\maintenance\MyPowerShellScript.ps1
i am getting error
error: failed to start all instances by forever:Error: Command failed:
C:\Windows\system32\cmd.exe /s /c "forever -p ./logs --sourceDir
../wf_manager --workingDir ../wf_manager start -a --uid "ServiceApp"
ServiceApp.js"
+ CategoryInfo : NotSpecified: (error: failed t...ServiceApp.js":String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : MyWebServer1
'forever' is not recognized as an internal or external command, operable program or
batch file.
Note that i can execute the script remotely without any error if i update "MyNodeScript.js" and use full physical path for the forever command
var mycommand = 'C:\\Users\\admin\\AppData\\Roaming\\npm\\forever -p ./logs --sourceDir ../wf_manager --workingDir ../wf_manager start -a --uid "ServiceApp" ServiceApp.js'
However i would like to use just forever command. The path is already added as Environment variable on MyWebServer1
Try calling the ps1-file like this:
Invoke-Command -Scriptblock {powershell.exe -command {d:\blah\MyPowershellscript.ps1 } }
Via -filepath your specify a local scriptfile that should be executed remotely. As your script is present on the target system, you can omit that.

NodeJS dbus not working

I'm trying to get status from omxplayer with NodeJS via DBus, to do that i'm just trying to execuce shell script:
#!/bin/bash
#set -x
OMXPLAYER_DBUS_ADDR="/tmp/omxplayerdbus"
OMXPLAYER_DBUS_PID="/tmp/omxplayerdbus.pid"
export DBUS_SESSION_BUS_ADDRESS=`cat $OMXPLAYER_DBUS_ADDR`
export DBUS_SESSION_BUS_PID=`cat $OMXPLAYER_DBUS_PID`
[ -z "$DBUS_SESSION_BUS_ADDRESS" ] && { echo "Must have DBUS_SESSION_BUS_ADDRESS" >&2; exit 1; }
duration=`dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.omxplayer /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Duration`
[ $? -ne 0 ] && exit 1
duration="$(awk '{print $2}' <<< "$duration")"
position=`dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.omxplayer /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Position`
[ $? -ne 0 ] && exit 1
position="$(awk '{print $2}' <<< "$position")"
playstatus=`dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.omxplayer /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.PlaybackStatus`
[ $? -ne 0 ] && exit 1
playstatus="$(sed 's/^ *//;s/ *$//;' <<< "$playstatus")"
paused="true"
[ "$playstatus" == "Playing" ] && paused="false"
echo "Duration: $duration"
echo "Position: $position"
echo "Paused: $paused"
;;
with
var exec = require('child_process').exec;
exec('bash status.sh', function() {
console.log(arguments);
})
but it prints
{ '0':
{ [Error: Command failed: Failed to open connection to "session" message bus: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
] killed: false, code: 1, signal: null },
'1': '',
'2': 'Failed to open connection to "session" message bus: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.\n' }
When I execute that script directly in console it works. NodeJS is running on Raspberry Pi.
UPDATE:
I have also tried "node-dbus" and "dbus-native" modules, but none of them worked for me, but maybe I used them incorrectly? To execute
dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.omxplayer /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Duration
I used
dbus-native
var exec = require('child_process').exec;
exec('cat /tmp/omxplayerdbus', function(error, data, stderr) {
data = data.replace("\n",'');
var dbus = require('dbus-native');
var bus = dbus.sessionBus({
busAddress: data //unix:abstract=/tmp/dbus-7BuZanKhmv,guid=7fafe7baa2d38357478f04ff5429712a
});
bus.invoke({
path: '/org/mpris/MediaPlayer2',
destination: 'org.mpris.MediaPlayer2.omxplayer',
'interface': 'org.freedesktop.DBus.Properties.Position'
}, function(err, res) {
console.log(arguments);
});
//And this
var conn = dbus({
busAddress: data
});
conn.message({
path:'/org/mpris/MediaPlayer2',
destination: 'org.mpris.MediaPlayer2.omxplayer',
type: dbus.messageType.methodCall
});
conn.on('message', function(msg) { console.log(msg); }).on('error', function() {
console.log(arguments);
}).on('connect', function() {
console.log(arguments);
});
});
both of these methods throws me this error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:904:11)
at Object.afterWrite (net.js:720:19)
UPDATE 2
I'm now using "dbus-native" module and still keep getting "EPIPE" error. I have checked "Handshake.js" and there is everything alright, so I dumped stdin and stdout messages:
{stdin}AUTH EXTERNAL 30
{stdout}OK df028c4a159a4db39ccc41c0542b9e3b
{stdin}BEGIN
{stdin}lmo/org/freedesktop/DBussorg.freedesktop.DBussHellosorg.freedesktop.DBus
PuTTY{stdin}l5�o/org/mpris/MediaPlayer2sorg.freedesktop.DBus.PropertiessGets org.mpris.MediaPlayer2.omxplayegss org.mpris.MediaPlayer2.omxplayePosition
{stdout} - stdout message line
{stdin} - stdin message line
and then "EPIPE".
UPDATE 3
I have found out that "EPIPE" error is throwed right after first dbus "DATA" command, in this case it's
lmo/org/freedesktop/DBussorg.freedesktop.DBussHellosorg.freedesktop.DBus
PuTTY{stdin}l5�o/org/mpris/MediaPlayer2sorg.freedesktop.DBus.PropertiessGets org.mpris.MediaPlayer2.omxplayegss org.mpris.MediaPlayer2.omxplayePosition
i'm new in communications via dbus, but according to DBus protocol, messages should be sent DATA <data in hex encoding>, but dbus-native sends messages without DATA command name.
You are trying to read dbus object propery using properties api. Note that last parameter of dbus-send is interface.member, so the message you are sending would be
var bus = dbus.sessionBus({ busAddress: fs.readFileSync('/tmp/omxplayerdbus', 'ascii').trim()})
bus.invoke({
path: "/org/mpris/MediaPlayer2",
interface: "org.freedesktop.DBus.Properties",
member: "Get",
destination: "org.mpris.MediaPlayer2.omxplayer",
signature: "ss",
body: [
"org.mpris.MediaPlayer2.omxplayer",
"Position"
]
}, function(err, position) {
console.log(err, position);
});

Resources