2

Is it possible to start a Virtual Box VM from a web application?

I have a Virtual Machine with ubuntu installed, on Windows as host. I have an Angular (4) app, from which I want to start a VM. The point is, to start the Virtual Machine before the web application in Windows (both the Angular app and the VM run in Windows as host).

To start the VM from command line, I normally use something like:

C:/Program Files/Oracle/VirtualBox/VBoxManage.exe' startvm virtualMachineName

So I thought that using the same line in package json, would work:

"ng": "ng",
"start": "ng serve",
"startBackend": "'C:/Program Files/Oracle/VirtualBox/VBoxManage.exe' startvm virtualMachineName",
....
"build": "startBackend && ng build --prod",

I tried to scape the backslasches, because I'm on windows.

 "startBackend": "'C:\\Program Files\\Oracle\\VirtualBox\\VBoxManage.exe' startvm virtualMachineName",

but also didn't work.

Is this possible at all? Are there any other options?

I currently run something like

npm run electron-build

where I combined ng build with a script to start electron, as my app works in electron. Should I then start the VM directly with npm? How can I do that ?

The code provided by @Zlatko works perfectly, but now I have another question.

The node-virtualbox library works of course only for VirtualBox VM , but what about other type of VM, like VMware ?

So far, I tried node-vmrun, but I always get:

vm run not found or false

sie
  • 151
  • 1
  • 3
  • 19

1 Answers1

0

You could write a little script, say in your tools directory of the project and use virtualbox package.

Something like this:

const virtualbox = require('virtualbox');
virtualbox.start(process.env.VBOX_MACHINE_NAME, (err) => {
  if (err) {
    console.log('Error starting machine', err);
    process.exit(1);
  }
  process.exit();
}

Then add it to your scripts:

"startBackend": "node tools/start-vm.js",
"start": "npm run startBackend && ng build"

Notice the process.exit() and process.exit(1) - with the first, you exit cleanly and your ng build will run. With the second (give it a positive integer argument), you signal an error with the VM startup process so you don't run ng build.

Zlatko
  • 18,936
  • 14
  • 70
  • 123
  • Can I write this script directly in the electron main.js file ? – sie Jul 25 '18 at 08:05
  • Oh. Most likely, not sure about specifics of your system etc. Also you'd have to ensure that vbox is actually installed at whomever you send this electron to. – Zlatko Jul 25 '18 at 09:10
  • Do you know if there is something similare for VMware virtual machines ? – sie Jul 25 '18 at 11:45
  • Something like this? https://www.npmjs.com/package/vmrun But just search the npm, there's probably something specific if this doesn't cut it. – Zlatko Jul 25 '18 at 12:14
  • I guess that's a separate question, too much for comments. Also did you try searching their github issues for electron-related problems? – Zlatko Jul 26 '18 at 08:26