public function determineBridgedInterface()
{
// what are we doing?
$log = usingLog()->startAction("determine bridged network interface for Vagrant VM");
try {
// 1. try to load Vagrant settings from storyplayer.json
// e.g.: "moduleSettings":{"vagrant":{"bridgedIface":"eth0"}}
$vagrantSettings = fromStoryplayer()->getModuleSetting('vagrant');
if (!empty($vagrantSettings->bridgedIface)) {
$log->endAction('Returning configured ' . $vagrantSettings->bridgedIface . ' interface');
return $vagrantSettings->bridgedIface;
}
} catch (E5xx_ActionFailed $e) {
// ignore errors as this setting may not exist
}
// 2. check if VirtualBox (VBoxManage) is installed
$command = 'which VBoxManage';
$commandRunner = new CommandRunner();
$result = $commandRunner->runSilently($command);
if ($result->returnCode !== 0) {
// VBoxManage is not installed, we are probably using another provider
// like OpenStack that do not require this setting
$log->endAction('VBoxManage is not installed: returning default eth0 interface');
return 'eth0';
}
// 3. VBoxManage can actually tell us what we need to know
$command = 'VBoxManage list bridgedifs';
$commandRunner = new CommandRunner();
$result = $commandRunner->runSilently($command);
if ($result->returnCode != 0) {
$log->endAction('unable to get list of bridgable network interfaces from VBoxManage :(');
throw new E5xx_ActionFailed(__METHOD__);
}
// now we just need to make sense of it all
$lines = explode("\n", $result->output);
$iface = null;
foreach ($lines as $line) {
$matches = [];
if (preg_match("|Name:[\\s]+(.*)|", $line, $matches)) {
$iface = $matches[1];
} else {
if ($iface !== null && preg_match("|IPAddress:[\\s]+(.*)|", $line, $matches)) {
// our network interface contains an IPAddress - it is likely
// to be one that works
if ($matches[1] != '0.0.0.0') {
$log->endAction($iface);
return $iface;
}
}
}
}
// if we get here, then we haven't found a network interface to use
$log->endAction("no bridgeable network interface found :(");
throw new E5xx_ActionFailed(__METHOD__);
}