forked from acquia/blt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInspector.php
581 lines (513 loc) · 15.6 KB
/
Inspector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
<?php
namespace Acquia\Blt\Robo\Inspector;
use Acquia\Blt\Robo\Config\YamlConfigProcessor;
use Robo\Config\YamlConfigLoader;
use Acquia\Blt\Robo\Common\Executor;
use Acquia\Blt\Robo\Common\IO;
use Acquia\Blt\Robo\Config\BltConfig;
use Acquia\Blt\Robo\Config\ConfigAwareTrait;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Robo\Common\BuilderAwareTrait;
use Robo\Contract\BuilderAwareInterface;
use Robo\Contract\ConfigAwareInterface;
use Symfony\Component\Filesystem\Filesystem;
use Robo\Contract\VerbosityThresholdInterface;
/**
* Class Inspector.
*
* @package Acquia\Blt\Robo\Common
*/
class Inspector implements BuilderAwareInterface, ConfigAwareInterface, LoggerAwareInterface {
use BuilderAwareTrait;
use ConfigAwareTrait;
use LoggerAwareTrait;
use IO;
/**
* Process executor.
*
* @var \Acquia\Blt\Robo\Common\Executor
*/
protected $executor;
/**
* @var null
*/
protected $isDrupalInstalled = NULL;
/**
* @var null
*/
protected $isMySqlAvailable = NULL;
/**
* @var array
*/
protected $drupalVmStatus = NULL;
/**
* @var \Symfony\Component\Filesystem\Filesystem
*/
protected $fs;
/**
* The constructor.
*
* @param \Acquia\Blt\Robo\Common\Executor $executor
* Process executor.
*/
public function __construct(Executor $executor) {
$this->executor = $executor;
$this->fs = new Filesystem();
}
/**
* @return \Symfony\Component\Filesystem\Filesystem
*/
public function getFs() {
return $this->fs;
}
/**
* @param \Symfony\Component\Filesystem\Filesystem $fs
*/
public function setFs($fs) {
$this->fs = $fs;
}
/**
*
*/
public function clearState() {
$this->isDrupalInstalled = NULL;
$this->isMySqlAvailable = NULL;
$this->drupalVmStatus = [];
}
/**
* Determines if the repository root directory exists.
*
* @return bool
* TRUE if file exists.
*/
public function isRepoRootPresent() {
return file_exists($this->getConfigValue('repo.root'));
}
/**
* Determines if the Drupal docroot directory exists.
*
* @return bool
* TRUE if file exists.
*/
public function isDocrootPresent() {
return file_exists($this->getConfigValue('docroot'));
}
/**
* Determines if BLT configuration file exists, typically project.yml.
*
* @return bool
* TRUE if file exists.
*/
public function isBltConfigFilePresent() {
return file_exists($this->getConfigValue('blt.config-files.project'));
}
/**
* Determines if BLT configuration file exists, typically project.local.yml.
*
* @return bool
* TRUE if file exists.
*/
public function isBltLocalConfigFilePresent() {
return file_exists($this->getConfigValue('blt.config-files.local'));
}
/**
* Determines if Drupal settings.php file exists.
*
* @return bool
* TRUE if file exists.
*/
public function isDrupalSettingsFilePresent() {
return file_exists($this->getConfigValue('drupal.settings_file'));
}
/**
* Determines if salt.txt file exists.
*
* @return bool
* TRUE if file exists.
*/
public function isHashSaltPresent() {
return file_exists($this->getConfigValue('repo.root') . '/salt.txt');
}
/**
* Determines if Drupal local.settings.php file exists.
*
* @return bool
* TRUE if file exists.
*/
public function isDrupalLocalSettingsFilePresent() {
return file_exists($this->getConfigValue('drupal.local_settings_file'));
}
/**
* Determines if Drupal settings.php contains required BLT includes.
*
* @return bool
* TRUE if settings.php is valid for BLT usage.
*/
public function isDrupalSettingsFileValid() {
$settings_file_contents = file_get_contents($this->getConfigValue('drupal.settings_file'));
if (!strstr($settings_file_contents,
'/../vendor/acquia/blt/settings/blt.settings.php')
) {
return FALSE;
}
return TRUE;
}
/**
* Checks that Drupal is installed, caches result.
*
* This method caches its result in $this->drupalIsInstalled.
*
* @return bool
* TRUE if Drupal is installed.
*/
public function isDrupalInstalled() {
// This will only run once per command. If Drupal is installed mid-command,
// this value needs to be changed.
if (is_null($this->isDrupalInstalled)) {
$this->isDrupalInstalled = $this->getDrupalInstalled();
}
return $this->isDrupalInstalled;
}
/**
* Determines if Drupal is installed.
*
* This method does not cache its result.
*
* @return bool
* TRUE if Drupal is installed.
*/
protected function getDrupalInstalled() {
$this->logger->debug("Verifying that Drupal is installed...");
$result = $this->executor->drush("sqlq \"SHOW TABLES LIKE 'config'\"")->run();
$output = trim($result->getMessage());
$installed = $result->wasSuccessful() && $output == 'config';
return $installed;
}
/**
* Gets the result of `drush status`.
*
* @return array
* The result of `drush status`.
*/
public function getDrushStatus() {
$status_info = json_decode($this->executor->drush('status --format=json --show-passwords')->run()->getMessage(), TRUE);
return $status_info;
}
/**
* Determines if MySQL is available, caches result.
*
* This method caches its result in $this->mySqlAvailable.
*
* @return bool
* TRUE if MySQL is available.
*/
public function isMySqlAvailable() {
if (is_null($this->isMySqlAvailable)) {
$this->isMySqlAvailable = $this->getMySqlAvailable();
}
return $this->isMySqlAvailable;
}
/**
* Determines if MySQL is available. Uses MySQL credentials from Drush.
*
* This method does not cache its result.
*
* @return bool
* TRUE if MySQL is available.
*/
public function getMySqlAvailable() {
$this->logger->debug("Verifying that MySQL is available...");
/** @var \Robo\Result $result */
$result = $this->executor->drush("sqlq \"SHOW DATABASES\"")
->run();
return $result->wasSuccessful();
}
/**
* Determines if Drupal VM configuration exists in the project.
*
* @return bool
* TRUE if Drupal VM configuration exists.
*/
public function isDrupalVmConfigPresent() {
return file_exists($this->getConfigValue('repo.root') . '/Vagrantfile');
}
/**
* Determines if Drupal VM is initialized for the local machine.
*
* I.E., whether Drupal VM is the default LAMP stack for BLT on local machine.
*
* @return bool
* TRUE if Drupal VM is initialized for the local machine.
*/
public function isDrupalVmLocallyInitialized() {
$status = $this->getDrupalVmStatus();
$machine_name = $this->getConfigValue('project.machine_name');
$initialized = !empty($status[$machine_name])
&& file_exists($this->getConfgValue('repo.root') . '/box/config.yml');
$statement = $initialized ? "is" : "is not";
$this->logger->debug("Drupal VM $statement initialized.");
return $initialized;
}
/**
* Determines if Drupal VM is booted.
*
* @return bool
* TRUE if Drupal VM is booted.
*/
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';
$statement = $booted ? "is" : "is not";
$this->logger->debug("Drupal VM $statement booted.");
return $booted;
}
/**
* Determines if the current PHP process is being executed inside VM.
*
* @return bool
* TRUE if current PHP process is being executed inside of VM.
*/
public function isVmCli() {
return $_SERVER['USER'] == 'vagrant';
}
/**
* Checks to see if a given vagrant plugin is installed.
*
* You can check to see if vagrant is installed with commandExists('vagrant').
*
* @param string $plugin
* The plugin name.
*
* @return bool
* TRUE if the plugin is installed.
*/
public function isVagrantPluginInstalled($plugin) {
$installed = (bool) $this->executor->execute("vagrant plugin list | grep '$plugin'")
->interactive(FALSE)
->silent(TRUE)
->run()
->getMessage();
return $installed;
}
/**
* Checks to see if BLT alias is installed on CLI.
*
* @return bool
* TRUE if BLT alias is installed.
*/
public function isBltAliasInstalled() {
$cli_config_file = $this->getCliConfigFile();
if (!is_null($cli_config_file) && file_exists($cli_config_file)) {
$contents = file_get_contents($cli_config_file);
if (strstr($contents, 'function blt')) {
return TRUE;
}
}
return FALSE;
}
/**
* Determines the CLI config file.
*
* @return null|string
* Returns file path or NULL if none was found.
*/
public function getCliConfigFile() {
$file = NULL;
$user = posix_getpwuid(posix_getuid());
$home_dir = $user['dir'];
if (!empty($_ENV['SHELL']) && strstr($_ENV['SHELL'], 'zsh')) {
$file = $home_dir . '/.zshrc';
}
elseif (file_exists($home_dir . '/.bash_profile')) {
$file = $home_dir . '/.bash_profile';
}
elseif (file_exists($home_dir . '/.bashrc')) {
$file = $home_dir . '/.bashrc';
}
elseif (file_exists($home_dir . '/.profile')) {
$file = $home_dir . '/.profile';
}
return $file;
}
/**
* Checks if a given command exists on the system.
*
* @param string $command
* The command binary only. E.g., "drush" or "php".
*
* @return bool
* TRUE if the command exists, otherwise FALSE.
*/
public function commandExists($command) {
exec("command -v $command >/dev/null 2>&1", $output, $exit_code);
return $exit_code == 0;
}
/**
* Verifies that installed minimum git version is met.
*
* @param string $minimum_version
* The minimum git version that is required.
*
* @return bool
* TRUE if minimum version is satisfied.
*/
public function isGitMinimumVersionSatisfied($minimum_version) {
exec("git --version | cut -d' ' -f3", $output, $exit_code);
if (version_compare($output[0], $minimum_version, '>=')) {
return TRUE;
}
return FALSE;
}
/**
* Gets the local behat configuration defined in local.yml.
*
* @return \Acquia\Blt\Robo\Config\BltConfig
* The local Behat configuration.
*/
public function getLocalBehatConfig() {
$behat_local_config_file = $this->getConfigValue('repo.root') . '/tests/behat/local.yml';
$behat_local_config = new BltConfig();
$loader = new YamlConfigLoader();
$processor = new YamlConfigProcessor();
$processor->extend($loader->load($behat_local_config_file));
$behat_local_config->import($processor->export());
return $behat_local_config;
}
/**
* Returns an array of required Behat files, as defined by Behat config.
*
* For instance, this will return the Drupal root dir, Behat features dir,
* and bootstrap dir on the local file system. All of these files are
* required for behat to function properly.
*
* @return array
* An array of required Behat configuration files.
*/
public function getBehatConfigFiles() {
$behat_local_config = $this->getLocalBehatConfig();
return [
$behat_local_config->get('local.extensions.Drupal\DrupalExtension.drupal.drupal_root'),
$behat_local_config->get('local.suites.default.paths.features'),
$behat_local_config->get('local.suites.default.paths.bootstrap'),
];
}
/**
* Determines if required Behat files exist.
*
* @return bool
* TRUE if all required Behat files exist.
*/
public function areBehatConfigFilesPresent() {
return $this->filesExist($this->getBehatConfigFiles());
}
/**
* Determines if all file in a given array exist.
*
* @return bool
* TRUE if all files exist.
*/
public function filesExist($files) {
foreach ($files as $file) {
if (!file_exists($file)) {
$this->logger->warning("Required file $file does not exist.");
return FALSE;
}
}
return TRUE;
}
/**
* Determines if Behat is properly configured on the local machine.
*
* This will ensure that required Behat file exists, and that require
* configuration values are properly defined.
*
* @return bool
* TRUE is Behat is properly configured on the local machine.
*/
public function isBehatConfigured() {
// Verify that URIs required for Drupal and Behat are configured correctly.
$local_behat_config = $this->getLocalBehatConfig();
if ($this->getConfigValue('project.local.uri') != $local_behat_config->get('local.extensions.Behat\MinkExtension.base_url')) {
$this->logger->warning('project.local.uri in project.yml does not match local.extensions.Behat\MinkExtension.base_url in local.yml.');
$this->logger->warning('project.local.uri = ' . $this->getConfigValue('project.local.uri'));
$this->logger->warning('local.extensions.Behat\MinkExtension.base_url = ' . $local_behat_config->get('local.extensions.Behat\MinkExtension.base_url'));
return FALSE;
}
// Verify that URIs required for an ad-hoc PHP internal server are
// configured correctly.
if ($this->getConfigValue('behat.run-server')) {
if ($this->getConfigValue('behat.server.url') != $this->getConfigValue('project.local.uri')) {
$this->logger->warning("behat.run-server is enabled, but the server URL does not match Drupal's base URL.");
$this->logger->warning('project.local.uri = ' . $this->getConfigValue('project.local.uri'));
$this->logger->warning('behat.server.url = ' . $this->getConfigValue('behat.server.url'));
$this->logger->warning('local.extensions.Behat\MinkExtension.base_url = ' . $local_behat_config->get('local.extensions.Behat\MinkExtension.base_url'));
return FALSE;
}
}
// Verify that required Behat files are present.
if (!$this->areBehatConfigFilesPresent()) {
return FALSE;
}
return TRUE;
}
/**
* Determines if the PhantomJS binary is present.
*
* @return bool
* TRUE if the PhantomJS binary is present.
*/
public function isPhantomJsBinaryPresent() {
return file_exists("{$this->getConfigValue('composer.bin')}/phantomjs");
}
/**
* Checks if simplesamlphp has already been setup by BLT.
*
* @return bool
* TRUE if the simplesamlphp config key exists and is true.
*/
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 (is_null($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")
->interactive(FALSE)
->printMetadata(TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERY_VERBOSE)
->run();
$output = $result->getMessage();
if (!$result->wasSuccessful() || !$output) {
$this->drupalVmStatus = [];
return FALSE;
}
$lines = explode("\n", $output);
foreach ($lines as $line) {
$parsed_line = explode(',', $line);
if (count($parsed_line) < 4) {
continue;
}
list($timestamp, $target, $type, $data) = $parsed_line;
$this->drupalVmStatus[$target][$type] = $data;
}
}
}