Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 31b977e

Browse files
authored
Printing error output when runserver fails. (#1639)
* Printing error output when runserver fails. * Improving validation.
1 parent f0c6b7a commit 31b977e

File tree

11 files changed

+89
-30
lines changed

11 files changed

+89
-30
lines changed

src/Robo/Commands/Blt/DoctorCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function executeDoctorInsideVm() {
7474
$this->logger->error("The value of drupal_core_path in $drupal_vm_config_filepath contains an unresolved Ansible variable.");
7575
$this->logger->error("Do not use Ansible variable placeholders for drupal_core_path.");
7676
$this->logger->error("drupal_core_path is currently $drupal_vm_config_filepath. Please correct it.");
77-
throw new \Exception("Unparsable value in $drupal_vm_config_filepath.");
77+
throw new BltException("Unparsable value in $drupal_vm_config_filepath.");
7878
}
7979

8080
$this->say("Drupal VM was detected. Running blt doctor inside of VM...");

src/Robo/Commands/Deploy/DeployCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function getTagName($options) {
148148

149149
if (empty($tag_name)) {
150150
// @todo Validate tag name is valid. E.g., no spaces or special characters.
151-
throw new \Exception("You must enter a valid tag name.");
151+
throw new BltException("You must enter a valid tag name.");
152152
}
153153
else {
154154
$tag_name = $options['tag'];
@@ -245,7 +245,7 @@ protected function addGitRemotes() {
245245
// Add remotes and fetch upstream refs.
246246
$git_remotes = $this->getConfigValue('git.remotes');
247247
if (empty($git_remotes)) {
248-
throw new \Exception("git.remotes is empty. Please define at least one value for git.remotes in blt/project.yml.");
248+
throw new BltException("git.remotes is empty. Please define at least one value for git.remotes in blt/project.yml.");
249249
}
250250
foreach ($git_remotes as $remote_url) {
251251
$this->addGitRemote($remote_url);

src/Robo/Commands/Tests/BehatCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function initialize() {
7979
* @interactConfigureBehat
8080
* @validateMySqlAvailable
8181
* @validateDrupalIsInstalled
82+
* @validateSettingsFilesPresent
8283
* @validateBehatIsConfigured
8384
* @validateInsideVm
8485
* @launchWebServer
@@ -167,6 +168,7 @@ protected function launchSelenium() {
167168
->get('executor')
168169
->execute($this->getConfigValue('composer.bin') . "/selenium-server-standalone -port {$this->seleniumPort} -log {$this->seleniumLogFile} > /dev/null 2>&1")
169170
->background(TRUE)
171+
// @todo Print output when this command fails.
170172
->printOutput(TRUE)
171173
->dir($this->getConfigValue('repo.root'))
172174
->run();

src/Robo/Commands/Vm/VmCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ protected function requireDrupalVm() {
242242
}
243243
else {
244244
// @todo revert previous file chanages.
245-
throw new \Exception("Unable to install Drupal VM.");
245+
throw new BltException("Unable to install Drupal VM.");
246246
}
247247
}
248248

@@ -259,7 +259,7 @@ protected function checkRequirements() {
259259
$this->logger->error("Vagrant is not installed.");
260260
$this->say("Please install all dependencies for Drupal VM by following the Quickstart Guide:");
261261
$this->say("https://github.com/geerlingguy/drupal-vm#quick-start-guide");
262-
throw new \Exception("Drupal VM requirements are missing.");
262+
throw new BltException("Drupal VM requirements are missing.");
263263
}
264264
else {
265265
$this->installVagrantPlugin('vagrant-hostsupdater');

src/Robo/Common/Executor.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Acquia\Blt\Robo\Common;
44

55
use Acquia\Blt\Robo\Config\ConfigAwareTrait;
6+
use Acquia\Blt\Robo\Exceptions\BltException;
67
use GuzzleHttp\Client;
78
use Psr\Log\LoggerAwareInterface;
89
use Psr\Log\LoggerAwareTrait;
@@ -189,7 +190,7 @@ public function wait(callable $callable, array $args, $message = '') {
189190
usleep($checkEvery * 1000);
190191
}
191192

192-
throw new \Exception("Timed out");
193+
throw new BltException("Timed out.");
193194
}
194195

195196
/**

src/Robo/Config/DefaultConfig.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function getRepoRoot() {
4444
}
4545
}
4646

47-
throw new \Exception('Could not find repository root directory!');
47+
throw new BltException('Could not find repository root directory!');
4848
}
4949

5050
/**
@@ -66,7 +66,7 @@ protected function getBltRoot() {
6666
}
6767
}
6868

69-
throw new \Exception('Could not find the Drupal docroot directory');
69+
throw new BltException('Could not find the Drupal docroot directory');
7070
}
7171

7272
/**

src/Robo/Datastore/FileStore.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function getFileName($key, $writable = FALSE) {
116116
}
117117

118118
if (!$key) {
119-
throw new \Exception('Could not save data to a file because it is missing an ID');
119+
throw new BltException('Could not save data to a file because it is missing an ID');
120120
}
121121
return $this->directory . '/' . $key;
122122
}
@@ -142,13 +142,13 @@ protected function ensureDirectoryWritable() {
142142
// Reality check to prevent stomping on the local filesystem if there is
143143
// something wrong with the config.
144144
if (!$this->directory) {
145-
throw new \Exception('Could not save data to a file because the path setting is mis-configured.');
145+
throw new BltException('Could not save data to a file because the path setting is mis-configured.');
146146
}
147147

148148
$writable = is_dir($this->directory) || (!file_exists($this->directory) && @mkdir($this->directory, 0777, TRUE));
149149
$writable = $writable && is_writable($this->directory);
150150
if (!$writable) {
151-
throw new \Exception(
151+
throw new BltException(
152152
'Could not save data to a file because the path {path} cannot be written to.',
153153
['path' => $this->directory]
154154
);

src/Robo/Hooks/ValidateHook.php

+25-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Acquia\Blt\Robo\Common\IO;
66
use Acquia\Blt\Robo\Config\ConfigAwareTrait;
7+
use Acquia\Blt\Robo\Exceptions\BltException;
78
use Acquia\Blt\Robo\Inspector\InspectorAwareInterface;
89
use Acquia\Blt\Robo\Inspector\InspectorAwareTrait;
910
use Consolidation\AnnotatedCommand\CommandData;
@@ -38,7 +39,7 @@ class ValidateHook implements ConfigAwareInterface, LoggerAwareInterface, Inspec
3839
*/
3940
public function validateDocrootIsPresent(CommandData $commandData) {
4041
if (!$this->getInspector()->isDocrootPresent()) {
41-
throw new \Exception("Unable to find Drupal docroot.");
42+
throw new BltException("Unable to find Drupal docroot.");
4243
}
4344
}
4445

@@ -49,7 +50,7 @@ public function validateDocrootIsPresent(CommandData $commandData) {
4950
*/
5051
public function validateRepoRootIsPresent(CommandData $commandData) {
5152
if (empty($this->getInspector()->isRepoRootPresent())) {
52-
throw new \Exception("Unable to find repository root.");
53+
throw new BltException("Unable to find repository root.");
5354
}
5455
}
5556

@@ -63,7 +64,7 @@ public function validateDrupalIsInstalled(CommandData $commandData) {
6364
->isDrupalInstalled()
6465
) {
6566

66-
throw new \Exception("Drupal is not installed");
67+
throw new BltException("Drupal is not installed");
6768
}
6869
}
6970

@@ -73,14 +74,12 @@ public function validateDrupalIsInstalled(CommandData $commandData) {
7374
* @hook validate @validateSettingsFileIsValid
7475
*/
7576
public function validateSettingsFileIsValid(CommandData $commandData) {
76-
if (!$this->getInspector()
77-
->isDrupalSettingsFilePresent()
78-
) {
79-
throw new \Exception("Could not find settings.php for this site.");
77+
if (!$this->getInspector()->isDrupalSettingsFilePresent()) {
78+
throw new BltException("Could not find settings.php for this site.");
8079
}
8180

8281
if (!$this->getInspector()->isDrupalSettingsFileValid()) {
83-
throw new \Exception("BLT settings are not included in settings file.");
82+
throw new BltException("BLT settings are not included in settings file.");
8483
}
8584
}
8685

@@ -91,7 +90,7 @@ public function validateSettingsFileIsValid(CommandData $commandData) {
9190
*/
9291
public function validateBehatIsConfigured(CommandData $commandData) {
9392
if (!$this->getInspector()->isBehatConfigured()) {
94-
throw new \Exception("Behat is not configured properly. Please run `blt doctor` to diagnose the issue.");
93+
throw new BltException("Behat is not configured properly. Please run `blt doctor` to diagnose the issue.");
9594
}
9695
}
9796

@@ -103,8 +102,23 @@ public function validateBehatIsConfigured(CommandData $commandData) {
103102
public function validateMySqlAvailable() {
104103
if (!$this->getInspector()->isMySqlAvailable()) {
105104
// @todo Prompt to fix.
106-
throw new \Exception("MySql is not available. Please run `blt doctor` to diagnose the issue.");
105+
throw new BltException("MySql is not available. Please run `blt doctor` to diagnose the issue.");
106+
}
107+
}
108+
109+
/**
110+
* Validates that required settings files exist.
111+
*
112+
* @hook validate @validateSettingsFilesPresent
113+
*/
114+
public function validateSettingsFilesPresent() {
115+
if (!$this->getInspector()->isHashSaltPresent()) {
116+
throw new BltException("salt.txt is not present. Please run `blt setup:settings` to generate it.");
117+
}
118+
if (!$this->getInspector()->isDrupalLocalSettingsFilePresent()) {
119+
throw new BltException("Could not find settings.php for this site.");
107120
}
121+
// @todo Look for local.drushrc.php.
108122
}
109123

110124
/**
@@ -114,7 +128,7 @@ public function validateMySqlAvailable() {
114128
*/
115129
public function validateInsideVm() {
116130
if ($this->getInspector()->isDrupalVmLocallyInitialized() && !$this->getInspector()->isVmCli()) {
117-
throw new \Exception("You must run this command inside Drupal VM, or else do not use Drupal VM at all. Execute `vagrant ssh` and then execute the command, or else change drush.aliases.local in blt/project.local.yml.");
131+
throw new BltException("You must run this command inside Drupal VM, or else do not use Drupal VM at all. Execute `vagrant ssh` and then execute the command, or else change drush.aliases.local in blt/project.local.yml.");
118132
}
119133
}
120134

src/Robo/Hooks/WebServerHook.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use Acquia\Blt\Robo\Common\IO;
66
use Acquia\Blt\Robo\Config\ConfigAwareTrait;
7+
use Acquia\Blt\Robo\Exceptions\BltException;
78
use Acquia\Blt\Robo\Inspector\InspectorAwareInterface;
89
use Acquia\Blt\Robo\Inspector\InspectorAwareTrait;
910
use League\Container\ContainerAwareInterface;
1011
use League\Container\ContainerAwareTrait;
1112
use Psr\Log\LoggerAwareInterface;
1213
use Psr\Log\LoggerAwareTrait;
1314
use Robo\Contract\ConfigAwareInterface;
15+
use Symfony\Component\Filesystem\Filesystem;
1416

1517
/**
1618
*
@@ -35,8 +37,28 @@ public function launchWebServer() {
3537
$this->killWebServer();
3638
$this->say("Launching PHP's internal web server via drush.");
3739
$this->logger->info("Running server at $this->serverUrl...");
38-
$this->getContainer()->get('executor')->drush("runserver $this->serverUrl > /dev/null")->background(TRUE)->run();
39-
$this->getContainer()->get('executor')->waitForUrlAvailable($this->serverUrl);
40+
41+
$fs = new Filesystem();
42+
$fs->mkdir([$this->getConfigValue('repo.root') . '/tmp']);
43+
$log_file = $this->getConfigValue('repo.root') . '/tmp/runserver.log';
44+
45+
/** @var \Acquia\Blt\Robo\Common\Executor $executor */
46+
$executor = $this->getContainer()->get('executor');
47+
$result = $executor
48+
->drush("runserver $this->serverUrl")
49+
->background(TRUE)
50+
->run();
51+
52+
try {
53+
$executor->waitForUrlAvailable($this->serverUrl);
54+
}
55+
catch (\Exception $e) {
56+
if (!$result->wasSuccessful() && file_exists($log_file)) {
57+
$output = file_get_contents($log_file);
58+
unlink($log_file);
59+
throw new BltException($output);
60+
}
61+
}
4062
}
4163
}
4264

src/Robo/Inspector/Inspector.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Robo\Contract\BuilderAwareInterface;
1515
use Robo\Contract\ConfigAwareInterface;
1616
use Symfony\Component\Filesystem\Filesystem;
17+
use Robo\Contract\VerbosityThresholdInterface;
1718

1819
/**
1920
* Class Inspector.
@@ -47,7 +48,7 @@ class Inspector implements BuilderAwareInterface, ConfigAwareInterface, LoggerAw
4748
/**
4849
* @var array
4950
*/
50-
protected $drupalVmStatus = [];
51+
protected $drupalVmStatus = NULL;
5152

5253
/**
5354
* @var \Symfony\Component\Filesystem\Filesystem
@@ -138,6 +139,16 @@ public function isDrupalSettingsFilePresent() {
138139
return file_exists($this->getConfigValue('drupal.settings_file'));
139140
}
140141

142+
/**
143+
* Determines if salt.txt file exists.
144+
*
145+
* @return bool
146+
* TRUE if file exists.
147+
*/
148+
public function isHashSaltPresent() {
149+
return file_exists($this->getConfigValue('repo.root') . '/salt.txt');
150+
}
151+
141152
/**
142153
* Determines if Drupal local.settings.php file exists.
143154
*
@@ -267,7 +278,7 @@ public function isDrupalVmLocallyInitialized() {
267278
$status = $this->getDrupalVmStatus();
268279
$machine_name = $this->getConfigValue('project.machine_name');
269280
$initialized = !empty($status[$machine_name])
270-
&& file_exists($this->getConfigValue('repo.root') . '/box/config.yml');
281+
&& file_exists($this->getConfgValue('repo.root') . '/box/config.yml');
271282
$statement = $initialized ? "is" : "is not";
272283
$this->logger->debug("Drupal VM $statement initialized.");
273284

@@ -536,7 +547,7 @@ public function isSimpleSamlPhpInstalled() {
536547
* An array of status data.
537548
*/
538549
protected function getDrupalVmStatus() {
539-
if (empty($this->drupalVmStatus)) {
550+
if (is_null($this->drupalVmStatus)) {
540551
$this->setDrupalVmStatus();
541552
}
542553
return $this->drupalVmStatus;
@@ -547,12 +558,13 @@ protected function getDrupalVmStatus() {
547558
*/
548559
protected function setDrupalVmStatus() {
549560
$result = $this->executor->execute("vagrant status --machine-readable")
550-
->printOutput(FALSE)
551-
->printMetadata(FALSE)
552561
->interactive(FALSE)
562+
->printMetadata(TRUE)
563+
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERY_VERBOSE)
553564
->run();
554565
$output = $result->getMessage();
555566
if (!$result->wasSuccessful() || !$output) {
567+
$this->drupalVmStatus = [];
556568
return FALSE;
557569
}
558570
$lines = explode("\n", $output);

src/Robo/Wizards/SetupWizard.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ class SetupWizard extends Wizard {
1515
* Executes blt setup:settings command.
1616
*/
1717
public function wizardGenerateSettingsFiles() {
18+
$missing = FALSE;
1819
if (!$this->getInspector()->isDrupalLocalSettingsFilePresent()) {
1920
$this->logger->warning("<comment>{$this->getConfigValue('drupal.local_settings_file')}</comment> is missing.");
20-
$confirm = $this->confirm("Do you want to generate this required settings file?");
21+
$missing = TRUE;
22+
}
23+
elseif (!$this->getInspector()->isHashSaltPresent()) {
24+
$this->logger->warning("<comment>salt.txt</comment> is missing.");
25+
$missing = TRUE;
26+
}
27+
if ($missing) {
28+
$confirm = $this->confirm("Do you want to generate this required settings file(s)?");
2129
if ($confirm) {
2230
$bin = $this->getConfigValue('composer.bin');
2331
$this->executor

0 commit comments

Comments
 (0)