Created
June 23, 2017 11:01
-
-
Save alpipego/98f2f96c7497752ba0fa46bb376a119a to your computer and use it in GitHub Desktop.
Traverse a directory and add all 'unknown' virtual machines to vagrants index.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Traverse through folder and rebuild vagrant machine index | |
* | |
* @author Alexander Goller <[email protected]> | |
* @license MIT | |
*/ | |
// setup | |
define( 'MACHINE_INDEX', $_SERVER['HOME'] . '/.vagrant.d/data/machine-index/index' ); | |
define( 'SEARCH_PATH', $_SERVER['HOME'] . '/Sites' ); // possibly change this | |
// read the index | |
$index = (array) json_decode( file_get_contents( MACHINE_INDEX ) ); | |
$machineIndex = (array) $index['machines']; | |
$count = count( $machineIndex ); | |
$objects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( SEARCH_PATH ), RecursiveIteratorIterator::SELF_FIRST ); | |
/** | |
* @var string $name | |
* @var SplFileInfo $object | |
*/ | |
foreach ( $objects as $path => $object ) { | |
if ( file_exists( $path . '/Vagrantfile' ) ) { | |
echo '.'; | |
$parent = realpath( $path ); | |
} | |
if ( $object->getFilename() === '.vagrant' ) { | |
$vagrants = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path, RecursiveDirectoryIterator::SKIP_DOTS ), RecursiveIteratorIterator::SELF_FIRST ); | |
$j = 0; | |
/** | |
* @var string $vagrantName | |
* @var SplFileInfo $vagrant | |
*/ | |
foreach ( $vagrants as $vagrantName => $vagrant ) { | |
// get machine name | |
if ( $j === 1 ) { | |
$name = $vagrant->getFilename(); | |
} | |
$j ++; | |
if ( is_file( $vagrantName . '/index_uuid' ) ) { | |
echo '+'; | |
$id = file_get_contents( $vagrantName . '/index_uuid' ); | |
// if this machine is already registered, skip | |
if ( array_key_exists( $id, $machineIndex ) ) { | |
continue; | |
} | |
// get provider name | |
$provider = $vagrant->getFilename(); | |
// get machine state | |
$stateArr = []; | |
$state = ''; | |
exec( 'cd ' . $parent . ' && vagrant status', $stateArr ); | |
foreach ( $stateArr as $line ) { | |
if ( preg_match( "%$name\h+([a-z]+)\h\($provider\)%", $line, $matches ) ) { | |
$state = $matches[1]; | |
} | |
} | |
// add machine | |
$machineIndex[ $id ] = [ | |
'local_data_path' => $path, | |
'name' => $name, | |
'provider' => $provider, | |
'state' => $state, | |
'vagrantfile_name' => null, | |
'vagrantfile_path' => $parent, | |
'updated_at' => null, | |
]; | |
} | |
} | |
} | |
} | |
$index['machines'] = $machineIndex; | |
$added = count( $machineIndex ) - $count; | |
if ( file_put_contents( MACHINE_INDEX, json_encode( $index ) ) !== false ) { | |
echo "\n\nSuccesfully added $count VMs\n"; | |
exec( 'vagrant global-status' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment