Skip to content

Commit

Permalink
Improving detection of DVM state. (#1601)
Browse files Browse the repository at this point in the history
  • Loading branch information
grasmash authored Jun 5, 2017
1 parent 6516354 commit caf84c2
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 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 @@ -256,14 +261,11 @@ public function isDrupalVmBooted() {
return FALSE;
}

$result = $this->executor->execute("vagrant status")
->printOutput(FALSE)
->printMetadata(FALSE)
->interactive(FALSE)
->run();
$output = $result->getMessage();
$status = $this->getDrupalVmStatus();
$machine_name = $this->getConfigValue('project.machine_name');
$booted = !empty($status[$machine_name]['state'])
&& $status[$machine_name]['state'] == 'running';

$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 caf84c2

Please sign in to comment.