Skip to content

Instantly share code, notes, and snippets.

@m3m0r7
Created August 31, 2016 16:30
Show Gist options
  • Save m3m0r7/c7b8956501b8bb24377ce0ce2a2f10a0 to your computer and use it in GitHub Desktop.
Save m3m0r7/c7b8956501b8bb24377ce0ce2a2f10a0 to your computer and use it in GitHub Desktop.
<?php
date_default_timezone_set('Asia/Tokyo');
$result = `vagrant version`;
if (!preg_match('/([a-z0-9\.]+)\n/i', $result, $matches)) {
printf("Not installed vagrant\n");
return;
}
// vagrant version
$version = $matches[1];
// status
$status = `vagrant global-status`;
preg_match_all('/([a-z0-9]{7})\s+([a-z0-9]+)\s+([a-z0-9]+)\s+([a-z0-9]+)\s+([^\n]+)\n/i', $status, $matches);
printf(date('[Y-m-d H:i:s]') . " Vagrant version: %s\n", $version);
$stopped = 0;
foreach ($matches[0] as $i => $line) {
$runningDirectory = trim($matches[5][$i]);
$runningStatus = trim($matches[4][$i]);
// ディレクトリであるかどうか
if (is_dir($runningDirectory) && $runningStatus === 'running') {
printf(date('[Y-m-d H:i:s]') . " Stopping: %s\n", $runningDirectory);
exec('cd ' . $runningDirectory . ';vagrant halt 2>/dev/null', $output);
$stopped++;
}
}
printf(date('[Y-m-d H:i:s]') . " Stopped %d VMs\n", $stopped);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment