Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
E.Azer Koçulu committed Jan 3, 2012
0 parents commit a65986a
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
51 changes: 51 additions & 0 deletions README.md
@@ -0,0 +1,51 @@
# node-virtualbox
A JavaScript library to interact with Virtualbox.

# Installation

```bash
$ npm install virtualbox
```

# Examples

## Starting and Running a program

```javascript
var virtualbox = require('virtualbox');

virtualbox.start("Win32", function(error){

if(error) throw error;

console.log('VM "w7" has been successfully started');

virtualbox.exec({ vm: "win32", cmd: "C:\Program Files\Internet Explorer\iexplore.exe", params: "http://google.com" }, function(error){

if(error) throw error;

console.log('Running Internet Explorer...');

});

});
```

# Available Methods

* start
* stop
* exec
* reset
* pause
* resume

# Troubleshooting

* Make sure that Guest account is enabled on the VM.
* This library starts VMs headlessly by default. If you're having with executing a command, start the VM with GUI and run observe the screen after executing same command.
* For the "Concurrent guest process limit is reached" error message, only solution I know is to reset the VM.

# Example Uses

* [Lowkick](http://github.com/azer/lowkick)
8 changes: 8 additions & 0 deletions lib/logging.js
@@ -0,0 +1,8 @@
var log4js = require('log4js'),
logger = log4js.getLogger('VirtualBox');

logger.setLevel('INFO');

module.exports = logger;


73 changes: 73 additions & 0 deletions lib/virtualbox.js
@@ -0,0 +1,73 @@
var exec = require('child_process').exec,
logging = require('./logging'),
undefined = function(){}();

function pause(vmname, callback){
logging.info('Pausing VM "%s"', vmname);
exec('vboxmanage controlvm "'+vmname+'" pause', function(error, stdout, stderr){
callback(error);
});
}

function reset(vmname, callback){
logging.info('Resetting VM "%s"', vmname);
exec('vboxmanage controlvm "'+vmname+'" reset', function(error, stdout, stderr){
callback(error);
});
}

function resume(vmname, callback){
logging.info('Resuming VM "%s"', vmname);
exec('vboxmanage controlvm "'+vmname+'" resume', function(error, stdout, stderr){
callback(error);
});
}

function start(vmname, callback){
logging.info('Starting VM "%s"', vmname);
exec('vboxmanage -nologo startvm "'+vmname+'" --type headless', function(error, stdout, stderr){

if(error && /VBOX_E_INVALID_OBJECT_STATE/.test(error.message)){
error = undefined;
}

callback(error);
});
}

function stop(vmname, callback){
logging.info('Stopping VM "%s"', vmname);
exec('vboxmanage controlvm "'+vmname+'" savestate', function(error, stdout, stderr){
callback(error);
});
}

function vmExec(options, callback){
var vm = options.vm || options.name || options.vmname || options.title,
path = options.path || options.cmd || options.command || options.exec || options.execute || options.run,
params = options.params || options.parameters || options.args;

Array.isArray(params) && ( params = params.join(" ") );

params == undefined && ( params = "" );

path = path.replace(/\\/g, '\\\\');

var cmd = 'VBoxManage guestcontrol '+vm+' execute --image "cmd.exe" --username Guest -- "/c" "'+path+'" "'+params+'"';

logging.info('Executing command "%s" on VM "%s"', cmd, vm);

exec(cmd, function(error, stdout, stderr){
callback(error);
});
}


module.exports = {
'exec': vmExec,
'pause': pause,
'reset': reset,
'resume': resume,
'start': start,
'stop': stop
};
13 changes: 13 additions & 0 deletions package.json
@@ -0,0 +1,13 @@
{
"name":"virtualbox",
"version":"0.0.1",
"description":"A library to interact with VirtualBox.",
"author":"Azer Koculu <azer@kodfabrik.com>",
"keywords":["virtualbox","vboxmanage"],
"directories":{ "lib": "./lib" },
"main":"./lib/virtualbox",
"repository": { "type":"git", "url" : "http://github.com/azer/node-virtualbox.git" },
"dependencies":{
"log4js": "0.x"
}
}
13 changes: 13 additions & 0 deletions test/exec.js
@@ -0,0 +1,13 @@
var virtualbox = require('../lib/virtualbox'),
args = process.argv.slice(2),
vm = args[0],
path = "C:\\Program Files\\Internet Explorer\\iexplore.exe";


virtualbox.start(vm, function(){

virtualbox.exec({ 'vm': vm, 'path': path, 'params': [args[1] || 'google.com'] }, function(error){
if(error) throw error;
});

});
6 changes: 6 additions & 0 deletions test/pause.js
@@ -0,0 +1,6 @@
var virtualbox = require('../lib/virtualbox'),
args = process.argv.slice(2);

virtualbox.pause(args[0], function(error){
if(error) throw error;
});
6 changes: 6 additions & 0 deletions test/resume.js
@@ -0,0 +1,6 @@
var virtualbox = require('../lib/virtualbox'),
args = process.argv.slice(2);

virtualbox.resume(args[0], function(error){
if(error) throw error;
});
6 changes: 6 additions & 0 deletions test/start.js
@@ -0,0 +1,6 @@
var virtualbox = require('../lib/virtualbox'),
args = process.argv.slice(2);

virtualbox.start(args[0], function(error){
if(error) throw error;
});
6 changes: 6 additions & 0 deletions test/stop.js
@@ -0,0 +1,6 @@
var virtualbox = require('../lib/virtualbox'),
args = process.argv.slice(2);

virtualbox.stop(args[0], function(error){
if(error) throw error;
});

0 comments on commit a65986a

Please sign in to comment.