I'm in the process of building a web application that (without getting into the exact application of these technologies) will allow users to create repositories and share them with one another.
I am in the initial design phase, and wanted to know what the best way to go about executing terminal commands from the interface would be. Ideally, users would be able to click a button and I would initialize a new git repository for them.
Note: During the design process, I will be hosting the site on my Amazon EC2 Instance that has git installed.
In summary, you need to run git from a Node.js application. "Running" git is actually spawning the git process, and this is something you can do natively.
// Spawn a git process.
const spawn = require('child_process').spawn;
const git = spawn('git', ['init']);
// Hook into the close event. See the manual for other events.
git.on('close', (code) => {
// You can check the return code here to see if an error occured.
console.log('git init finished with return code ' + code);
});
Related
Some gcloud commands don't have API or client library support (for example - this one).
In these cases, is there a simple way to run gcloud commands from a nodejs application?
The gcloud endpoints service commands for IAM policy are difficult for me to check quickly but, if IIRC (and if this is similar to gcloud projects commands for IAM policy), it's not that there's no API, but that there's no single API call.
What you can always do with gcloud is append --log-http to see what happens beneath the covers. With IAM policy mutations (off-top-of-head), you get the policy, mutate it, and then apply the changes back using the etag the GET gave you. The backend checks the policy's state (the etag is like a hash of the policy) and, if it's unchanged, you can make the change.
If this is what's happening here, you should be able to repro the functionality in NodeJS using the existing (!) APIs and, if you're using API Client Libraries (rather than Cloud Client libraries), the functionality will be available.
Apart from the complexity involved in shelling out to gcloud, you'll need to also authenticate it and then you'll need to (un)marshal data to the shell and manage errors. Ergo, it's messy and generally discouraged.
In node.js ,we have child_process module. As the name suggests the child_process provides function like spawn or exec that creates new child process that executes shell command like independent process. spawn is a function that takes the main command as
first argument and other command line options as an array values in place of second parameter.
So with respect to link that you share, you might end-up writing something like this :
const { spawn } = require("child_process");
const listening = spawn('gcloud', ['endpoints', 'services', 'blah', '--option','someValue']);
listening.stdout.on("data", data => {
console.log(`stdout: ${data}`);
});
listening.stderr.on("data", data => {
console.log(`stderr: ${data}`);
});
listening.on('error', (error) => {
console.log(`error: ${error.message}`);
});
References :
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
I'm not sure this directly answers your question but there is an npm package that can help you run unix commands from within the app.
Check out shell.js
I'm developing an electron application which downloads software. For users who target "Program Files" however, the installation needs to run with administrator permissions.
I'm creating a child process in which the installer runs using child_process.fork(), and am depending on the IPC connection for the ability to send and receive messages.
Unfortunately however, I can't find any way to elevate this process. Some libraries (such as node-windows) use child_process.exec() under the hood, but this doesn't create the IPC connection.
What is the best way to go about this?
The simplest option is to run the whole app as administrator.
You can force (or, politically correct,remind) user to run as admin.
E.g. in electron-builder with "requestedExecutionLevel": "requireAdministrator"
If you want to elevate only child process, you can either make this child process smart enough to ask for elevation, or use an 'elevator', extra program which will ask for the elevation.
Node-windows does that with VBS script
electron-react-boileplate does that with pre-compiled program elevate
Also node-powershell supports executing commands, if necessary, with elevation (basic powershell).
As for IPC, what are you after? child_process.exec buffers the output, while child_proces.spawn gives it to you in a stream-like manner (see child process)
You just need to provide a callback with the correct arguments.
Example from child process:
const { exec, spawn } = require('child_process');
exec('my.bat', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
I am trying to create an app using node, in which users can schedule certain cron jobs ,the setting for these job I am fetching from the users and saving it to mongoDB .I need my app to be such that users can start stop these jobs whenever they want . I have a class created which is something like this
Class Croncreator {
constructor () {Creates the cron job}
startCron ()
stropCron()}
Now all this is working ,but I cannot wrap my head around how do I manage this in a multi user environment?
Do I create an instance of this class in my express route for "/api/savecronjob" ,if yes then how do I manage the start stop feature .Considering the fact that 1 user may be creating multiple jobs a time and switching them on off when ever he wants.
I solved the problem by using a NPM package called cron-job-manager.
Read more about it NPM LINK
The documentation is pretty self explanatory .
I basically created a function to create the cron jobs with a identifier,and created two routes to start and stop these jobs.
Versions
I'm using Meteor 1.0.3 and node 0.10.35 on a small Ubuntu Server 14.04 LTS (HVM), SSD Volume Type - ami-3d50120d EC2 instance.
Context
I know how to do server side debugging on my development box, just $ meteor debug and open another browser pointing to the url it produces -- works great.
But now, I'm getting a server error on my EC2 instance I'm not getting in development. So I'd like to set up a remote debug session sever side.
Also, I deployed to the EC2 instance using the Meteor-up package (mup).
EDIT
In an effort to provide more background (and context) around my issue I'm adding the following:
What I'm trying to do is, on my EC2 instance, create a new pdf in a location such as:
application-name/server/.files/user/user-name/pdf-file.pdf
On my OSX development box, the process works fine.
When I deploy to EC2, and try out this process, it doesn't work. The directory:
/user-name/
for the user is never created for some reason.
I'd like to debug in order to figure out why I can't create the directory.
The code to create the directory that works on my development box is like so:
server.js
Meteor.methods({
checkUserFileDir: function () {
var fs = Npm.require('fs');
var dir = process.env.PWD + '/server/.files/users/' + this.userId + '/';
try {
fs.mkdirSync(dir);
} catch (e) {
if (e.code != 'EEXIST') throw e;
}
}
});
I ssh'd into the EC2 instance to make sure the path
/server/.files/user/
exists, because this portion of the path is neccessary in order for the above code to work correctly. I checked the path after the code should have ran, and the
/user-name/
portion of the path is not being created.
Question
How can I debug remote server side code in a easy way on my EC2 instance, like I do on my local development box?
Kadira.io supports remote errors/exceptions tracking. It allows you to see the stacktrace on server side exceptions in the context of your meteor methods.
See https://kadira.io/error-tracking.html for more detail.
It seems in my case, since I'm using Meteor-up (mup), I can not debug per-say, but get access to the remote EC2 instance server console and errors by using command $ mup logs -f on my development box.
This effectively solves my issue with being blind on the server side remote instance.
It still falls short of actual debugging remotely, which speeds up the process of finding errors and performance bottlenecks, but it's all we have for now.
For someone who still searching:
#zodern added server-side debugging of meteor apps to great meteor-up tool:
https://github.com/zodern/meteor-up/pull/976
Do mup meteor debug in deployment dir and you will be almost set, just follow the text.
I need to write a node.js program that will somehow get triggered anytime a developer checks in code into svn. This will update a file in the working directory. The developers work on Mac OS X and Windows. The program needs to run on both machines.
Is there a way I can somehow listen to svn client's commit?
Are there any sdk for svn that allows plugin/extension?
Will watching .svn hidden directory (that svn creates for its own use) for changes do it? if so, how can I know by looking at this directory that a file was committed?
At first I thought hooks might be the way to go but hooks are run on the machine that host's svn and they are mostly for admin tasks such as sending email alerts or kicking off builds
First you need to understand that there is no way to know whether an update has occurred on the server without connecting to the server. Hence you cannot do it simply by looking at the local folders because that is not how svn works.
A workaround to achieve what you want would be the following. On the server, write a "post-commit" hook that takes a counter from a text file, increments it, and writes it back. Deposit this text file somewhere where your clients can download it. I'm going to assume this will be on "http://www.example.com/commit-id.txt". Then, on the clients, use a shell script that monitors this text file for changes and executes the desired action. Using windows powershell, for instance, this could work as follows. For Mac you need to use a different shell but I'm sure this script could be easily ported.
function get-current-commit-id {
trap [Exception] {
# Make sure the script doesn't freak out when server is down or connection
$commitid
return
}
# The rand.next() ensures by brute force that the text file is not cached
[int] $clnt.DownloadString("http://www.example.com/commit-id.txt?q="+$rand.next())
}
$clnt = new-object System.Net.WebClient
$commitid = get-current-commit-id
while( 1 ){
$commitidnew = get-current-commit-id
if( $commitidnew -ne $rsid ){
Write-Output "Commit occured on server"
### execute desired action here
### perhaps you'll also want to run "svn up"
$commitid = $rsidnew
}
### check for new update every 10 seconds
sleep 10
}