Skip to content

Commit

Permalink
Improving detection of DVM state.
Browse files Browse the repository at this point in the history
  • Loading branch information
grasmash committed Jun 5, 2017
1 parent d4feab1 commit f2115b3
Showing 1 changed file with 51 additions and 13 deletions.
64 changes: 51 additions & 13 deletions src/Robo/Inspector/Inspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Inspector implements BuilderAwareInterface, ConfigAwareInterface, LoggerAw
*/
protected $isMySqlAvailable = NULL;

/**
* @var array
*/
protected $drupalVmStatus = [];

/**
* The constructor.
*
Expand All @@ -56,6 +61,7 @@ public function __construct(Executor $executor) {
public function clearState() {
$this->isDrupalInstalled = NULL;
$this->isMySqlAvailable = NULL;
$this->drupalVmStatus = [];
}

/**
Expand Down Expand Up @@ -234,11 +240,10 @@ public function isDrupalVmConfigPresent() {
* TRUE if Drupal VM is initialized for the local machine.
*/
public function isDrupalVmLocallyInitialized() {
// We assume that if the local drush alias is ${project.machine_name.local},
// rather than self, then Drupal VM is being used locally.
$drush_local_alias = $this->getConfigValue('drush.aliases.local');
$expected_vm_alias = $this->getConfigValue('project.machine_name') . '.local';
$initialized = ($drush_local_alias == $expected_vm_alias) && file_exists($this->getConfigValue('repo.root') . '/box/config.yml');
$status = $this->getDrupalVmStatus();
$machine_name = $this->getConfigValue('project.machine_name');
$initialized = !empty($status[$machine_name])
&& file_exists($this->getConfigValue('repo.root') . '/box/config.yml');
$statement = $initialized ? "is" : "is not";
$this->logger->debug("Drupal VM $statement initialized.");

Expand All @@ -255,15 +260,12 @@ public function isDrupalVmBooted() {
if (!$this->commandExists('vagrant')) {
return FALSE;
}

$status = $this->getDrupalVmStatus();
$machine_name = $this->getConfigValue('project.machine_name');
$booted = !empty($status[$machine_name]['state'])
&& $status[$machine_name]['state'] == 'running';

$result = $this->executor->execute("vagrant status")
->printOutput(FALSE)
->printMetadata(FALSE)
->interactive(FALSE)
->run();
$output = $result->getMessage();

$booted = strstr($output, "running");
$statement = $booted ? "is" : "is not";
$this->logger->debug("Drupal VM $statement booted.");

Expand Down Expand Up @@ -504,4 +506,40 @@ public function isSimpleSamlPhpInstalled() {
return $this->getConfig()->has('simplesamlphp') && $this->getConfigValue('simplesamlphp');
}

/**
* Gets the value of $this->drupalVmStatus. Sets it if empty.
*
* @return array
* An array of status data.
*/
protected function getDrupalVmStatus() {
if (empty($this->drupalVmStatus)) {
$this->setDrupalVmStatus();
}
return $this->drupalVmStatus;
}

/**
* Sets $this->drupalVmStatus by executing `vagrant status`.
*/
protected function setDrupalVmStatus() {
$result = $this->executor->execute("vagrant status --machine-readable")
->printOutput(FALSE)
->printMetadata(FALSE)
->interactive(FALSE)
->run();
$output = $result->getOutputData();
if (!$result->wasSuccessful() || !$output) {
return FALSE;
}
$lines = explode("\n", $output);
foreach ($lines as $line) {
if (count($line) < 4) {
continue;
}
list($timestamp, $target, $type, $data) = explode(',', $line);
$this->drupalVmStatus[$target][$type] = $data;
}
}

}

0 comments on commit f2115b3

Please sign in to comment.