unable to run nodejs child_process on amazon ec2 instance - node.js

I am running nodejs child_process function spawn() to do mongoexport. I have passed all the necessary fields to the command and it is working fine on my local machine. Below is the function code
userDetailsChild = spawn('mongoexport', ['--username',username,'--
password',password,'--db',db,'--collection','users','--type', 'csv',
'--fields', 'userId,firstName','--
out','/home/bitnami/apps/webapp/dist/server/prod/public/user-
details.csv']);
userDetailsChild.on('exit', function (code: any) {
if (code != 0) {
userDetailsChild.kill();
callback(new Error(), null);
} else {
console.log('userDetailsChild process closed with code ' + code);
userDetailsChild.kill();
}
});
When I try to run the same code with server credentials on amazon WordPress ec2 instance on my server machine it fails with no error message.
Somewhere I have seen path variables set as process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];
But didnt worked in my case.
Any help will be appreciated!!

While I do not know why the command is failing, I do know that you appear to be ignoring the more than likely helpful information coming back to you via stderr. ;)

Related

Liquibase lib file has console.log which prints the DB credentials. Please find the line#47 of the attached liquibase lib file. how to stop it?

const LiquibaseTS = require('node-liquibase').Liquibase;
const POSTGRESQL_DEFAULT_CONFIG = require('node-liquibase').POSTGRESQL_DEFAULT_CONFIG;
const myConfig = {
...POSTGRESQL_DEFAULT_CONFIG,
changeLogFile: './changelog.xml',
url: 'jdbc:postgresql://localhost:5432/node_liquibase_testing',
username: 'postgres',
password: 'postgres123',
logLevel: 'info'
}
const instTs = new LiquibaseTS(myConfig);
instTs.update();
I've the above code in my index.js file.
I get the below console output when the line instTs.update(); get executed,
Running /Users/path1/path2/sampleapp/node_modules/node-liquibase/dist/liquibase/liquibase --changeLogFile="./changelog.xml" --url="jdbc:postgresql://localhost" --username="Postgres" --password="postgres123" --classpath="/Users/path1/path2/sampleapp/node_modules/node-liquibase/dist/drivers/postgresql-42.2.8.jar" --logLevel="info" update ...
when I debug the node-liquibase library I found that this console print is due to the line#47 of the file /Users/path1path2/sampleapp/node_modules/node-liquibase/dist/node-liquibase.cjs.development.js
the code snippet in the node-liquibase.cjs.development.js file is,
CommandHandler.spawnChildProcess = function spawnChildProcess(commandString) {
console.log("Running " + commandString + "...");
return new Promise(function (resolve, reject) {
child_process.exec(commandString, function (error, stdout, stderr) {
console.log('\n', stdout);
if (error) {
console.error('\n', stderr); // error.stderr = stderr;
return reject(error);
}
resolve(stdout);
});
});
};
return CommandHandler;
}();
as per that file, the line# 47 is very first line of the function
console.log("Running " + commandString + "...");
This code is printing the DB details/credentials in the console. Also I noticed that liquibase prints the DB details/credentials on the console along with the error for the changeset error scenario.
What I need?
I want to stop printing the DB credentials on the console during normal run.
During error scenario, I want only the liquibase error printed on the console. it should not print the error with db credentials on the console.
index.js code instTs.update(); is not throwing any exception during error scenario. How can we find/distinguish the error scenario. eg: changeset to insert a value where the table doesn't exist.
Any help greatly appreciated.
Since it prints out the configuration, your only option is probably to move the sensitive information to another spot liquibase can read configuration from.
If you make a liquibase.properties file with your password in it, I don't think that will show in the logs. I've not used the node-liquibase frontend much, so I don't know if you need to explicitly add a liquibasePropertiesFile setting to your config.
Alternately, if you use a newer version of liquibase (probably newer than what node-liquibase ships with by default, but there is a way to override that with the liquibase argument) you can set the password and other fields with environment variables as well, and those will not be logged.

Mac spawn startosinstall process with node.js

I'm trying to spawn the startosinstall binary to begin a macOS upgrade. However, I'm seeing startosinstall binary run as root but then a sub process osinstallersetupd gets invoked but seems to run under my standard user account and not root.
Ultimately it breaks the automation because I get hit with an administrative authentication prompt.
auth
Showing the processes: grep
Is this likely an Apple bug/limitation or could it be related to the way of invoking the first binary?
const spawn = require("child_process").spawn;
child = spawn("/Applications/Install macOS High Sierra.app/Contents/Resources/startosinstall",
[
"--applicationpath",
"/Applications/Install macOS High Sierra.app",
"--agreetolicense"
],
{
//maybe try some config options?
}
);
child.stderr.on("data", (data) => {
console.log(data.toString() + "\n");
});
child.stdout.on("data", (data) => {
console.log(data.toString() + "\n");
});
child.on("exit", (code, signal) => {
console.log("child process exited: " + code + "\n");
});
For what it's worth I'm logged in as root and running the script above.
Any help with this is greatly appreciated!
I believe I've found the issue. I am using parts of a script that helps in facilitating upgrades and found that when using the FileVault authenticated reboots you create and load a plist.
macOSUpgrade: https://github.com/kc9wwh/macOSUpgrade/blob/master/macOSUpgrade.sh#L278
However, it seems I was not actually loading the plist and that caused an issue.
tl;dr I wasn't loading the plist: https://github.com/kc9wwh/macOSUpgrade/blob/master/macOSUpgrade.sh#L337

Node js app running inside container opens user's web browser

I need an option to open user's default web browser from node js app which is running inside docker container. I need to know that to implement OAuth flow.
I know that I can do it by opening new tab on the client side, but I don't have this option for other reasons.
I'm not familiar with docker container, but for just straight nodejs, this works for me:
// start browser
if (opSys == "Win64")
command = ("start http://localhost:8000/init"); // Win64
else if (opSys == "MacOS")
command = ("open http://localhost:8000/init"); // MacOS
else
command = ("xdg-open http://localhost:8000/init"); // Linux
exec (command, function (error, stdout, stderr)
{
if (error)
{
console.log ("command: ", command);
console.log ("error: ", stderr);
}
});

Node.js open Chrome Ubuntu 16.04 LTS

This is a similar question to here. However I am using Ubuntu and the previous question's accepted answer does not seem relevant.
I am using node to call a shell script that in turn calls chrome. A terminal opens and echo's the url but chrome browser does not open. I have also tried /usr/bin/google-chrome after discovering it from which command as well as google-chrome-stable to no avail. Why doesn't chrome launch on Ubuntu with node.js child process? Im running desktop version 16.04 LTS. If I run this shell script on the terminal without node it runs great.
JS:
var exec = require('child_process').exec,
child;
child = exec('gnome-terminal -x '+__dirname+'/ss.sh http://www.google.com',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
SHELL (ss.sh)
#!/bin/bash
echo $1
google-chrome $1 --start-maximized
OUTPUT:
Edit: I just tried running this on another box running Ubuntu 14.04 and receive the error: failed to create /home/user/.pki/nssdb directory. The plot thickens.
JAVA: If I run this with almost the same code in Java it works perfectly:
public static void main(String[] args) {
try {
String url = args[0];
ProcessBuilder pb = new ProcessBuilder("/home/user/ss.sh", url);
pb.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The answer to this was file permission and it came from #paulsm4 comment. He references #A.B. answer from here.
As soon as he posts his comment as an answer I will accept and delete this.

NodeJS not spawning child process except in tests

I have the following NodeJS code:
var spawn = require('child_process').spawn;
var Unzipper = {
unzip: function(src, dest, callback) {
var self = this;
if (!fs.existsSync(dest)) {
fs.mkdir(dest);
}
var unzip = spawn('unzip', [ src, '-d', dest ]);
unzip.stdout.on('data', function (data) {
self.stdout(data);
});
unzip.stderr.on('data', function (data) {
self.stderr(data);
callback({message: "There was an error executing an unzip process"});
});
unzip.on('close', function() {
callback();
});
}
};
I have a NodeUnit test that executes successfully. Using phpStorm to debug the test the var unzip is assigned correctly
However if I run the same code as part of a web service, the spawn call doesn't return properly and the server crashes on trying to attach an on handler to the nonexistent stdout property of the unzip var.
I've tried running the program outside of phpStorm, however it crashes on the command line as well for the same reason. I'm suspecting it's a permissions issue that the tests don't have to deal with. A web server spawning processes could cause chaos in a production environment, therefore some extra permissions might be needed, but I haven't been able to find (or I've missed) documentation to support my hypothesis.
I'm running v0.10.3 on OSX Snow Leopard (via MacPorts).
Why can't I spawn the child process correctly?
UPDATES
For #jonathan-wiepert
I'm using Prototypical inheritance so when I create an "instance" of Unzipper I set stdout and stderr ie:
var unzipper = Unzipper.spawn({
stdout: function(data) { util.puts(data); },
stderr: function(data) { util.puts(data); }
});
This is similar to the concept of "constructor injection". As for your other points, thanks for the tips.
The error I'm getting is:
project/src/Unzipper.js:15
unzip.stdout.on('data', function (data) {
^
TypeError: Cannot call method 'on' of undefined
As per my debugging screenshots, the object that is returned from the spawn call is different under different circumstances. My test passes (it checks that a ZIP can be unzipped correctly) so the problem occurs when running this code as a web service.
The problem was that the spawn method created on the Object prototype (see this article on Protypical inheritance) was causing the child_process.spawn function to be replaced, so the wrong function was being called.
I saved child_process.spawn into a property on the Unzipper "class" before it gets clobbered and use that property instead.

Resources