Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running exec in start callback always fails as VM isn't ready #9

Closed
chiefy opened this issue Apr 14, 2014 · 3 comments
Closed

Running exec in start callback always fails as VM isn't ready #9

chiefy opened this issue Apr 14, 2014 · 3 comments

Comments

@chiefy
Copy link
Contributor

chiefy commented Apr 14, 2014

I am having an issue, and I know the reason why it's happening, but I am unsure if there is a way to solve it.

When I issue a virtualbox.start(vm_name, cb) the callback is getting executed before the VM has fully booted. When I try to execute a command in said VM, it fails as it is not ready for commands. I've "solved" this issue by adding a setTimeout but it feels very hacky. Is there any way to long-poll the VM and query if it's actually ready to accept commands?

Thanks!

@michaelsanford
Copy link
Member

Hi @chiefy ,

virtualbox.start() actually spawns vboxmanage on the host and starts the named virtual machine in headless mode.

The vboxmanage tool will return once the virtual machine has successfully started, which is not the same thing as fully booted. So, your callback gets executed at that point (when vboxmanage returns).

How do you define "booted"? On a linux machine, runlevel 3? What about Windows Server? How does vboxmanage know? What about other operating systems, or systems that might not have guest-utils installed.

You can test it yourself with the Virtualbox GUI open, and running the commandline vboxmanage -nologo startvm "Name" (optionally --type headless) and see when you get a command prompt back versus what state the machine is in.

My workaround

Even checking the VMState property (from vboxmanage showvminfo --machinereadable "Name") will only show you "poweroff" / "running" / "paused" states, which is not useful.

The most reliable way I've found to ensure that your virtual machine is actually completely operational before doing something with it is to actually ping a real service on your virtual machine and check the response.

In one of my deployments, I needed to make sure that a node API was running, so I just asked the API if it was alive.

You could probably modify your callback to start a setInterval that checks for such a response every couple of seconds, and when it receives it, kill the interval and do whatever the callback was supposed to do in the first place.

@chiefy
Copy link
Contributor Author

chiefy commented Apr 18, 2014

@michaelsanford dude - thanks for such a great explanation. I was going to look at what Vagrant does, but that totally makes sense. Thank so much for the reply!

@chiefy chiefy closed this as completed Apr 18, 2014
@michaelsanford
Copy link
Member

My pleasure, @chiefy ! Your question is surely to be asked by others, so I felt it ought to be thoroughly addressed. Your question even prompted me to start writing a little article on node + vm management patterns.

I've been working extensively with node and virtualbox recently, so found a lot of interesting pitfalls and workarounds. I hope to test and merge a big update soon — 2bf68fc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants