-
Notifications
You must be signed in to change notification settings - Fork 394
/
EnvironmentDetector.php
398 lines (351 loc) · 9.53 KB
/
EnvironmentDetector.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
<?php
namespace Acquia\Blt\Robo\Common;
use drupol\phposinfo\Enum\FamilyName;
use drupol\phposinfo\OsInfo;
/**
* Attempts to detect various properties about the current hosting environment.
*
* @package Acquia\Blt\Robo\Common
*/
class EnvironmentDetector {
/**
* Is AH env.
*/
public static function isAhEnv() {
return (bool) self::getAhEnv();
}
/**
* Check if this is an ACSF env.
*
* Roughly duplicates the detection logic implemented by the ACSF module.
*
* @param mixed $ah_group
* The Acquia Hosting site / group name (e.g. my_subscription).
* @param mixed $ah_env
* The Acquia Hosting environment name (e.g. 01dev).
*
* @return bool
* TRUE if this is an ACSF environment, FALSE otherwise.
*
* @see https://git.drupalcode.org/project/acsf/blob/8.x-2.62/acsf_init/lib/sites/default/acsf.settings.php#L14
*/
public static function isAcsfEnv($ah_group = NULL, $ah_env = NULL) {
if (is_null($ah_group)) {
$ah_group = self::getAhGroup();
}
if (is_null($ah_env)) {
$ah_env = self::getAhEnv();
}
if (empty($ah_group) || empty($ah_env)) {
return FALSE;
}
return file_exists("/mnt/files/$ah_group.$ah_env/files-private/sites.json");
}
/**
* Is AH prod.
*/
public static function isAhProdEnv() {
$ah_env = self::getAhEnv();
// ACE prod is 'prod'; ACSF can be '01live', '02live', ...
return $ah_env == 'prod' || preg_match('/^\d*live$/', $ah_env);
}
/**
* Is AH stage.
*/
public static function isAhStageEnv() {
$ah_env = self::getAhEnv();
// ACE staging is 'test' or 'stg'; ACSF is '01test', '02test', ...
return preg_match('/^\d*test$/', $ah_env) || $ah_env == 'stg';
}
/**
* Is AH dev.
*/
public static function isAhDevEnv() {
// ACE dev is 'dev', 'dev1', ...; ACSF dev is '01dev', '02dev', ...
return (preg_match('/^\d*dev\d*$/', self::getAhEnv()));
}
/**
* Is AH ODE.
*/
public static function isAhOdeEnv($ah_env = NULL) {
if (is_null($ah_env)) {
$ah_env = self::getAhEnv();
}
// CDEs (formerly 'ODEs') can be 'ode1', 'ode2', ...
return (preg_match('/^ode\d*$/', $ah_env));
}
/**
* Is AH devcloud.
*/
public static function isAhDevCloud() {
return (!empty($_SERVER['HTTP_HOST']) && strstr($_SERVER['HTTP_HOST'], 'devcloud'));
}
/**
* Get AH group.
*/
public static function getAhGroup() {
return getenv('AH_SITE_GROUP');
}
/**
* Get AH env.
*/
public static function getAhEnv() {
return getenv('AH_SITE_ENVIRONMENT');
}
/**
* Get CI env name.
*
* In the case of multiple environment detectors declaring a CI env name, the
* first one wins.
*/
public static function getCiEnv() {
$results = self::getSubclassResults(__FUNCTION__);
if ($results) {
return current($results);
}
$mapping = [
'TRAVIS' => 'travis',
'PIPELINE_ENV' => 'pipelines',
'PROBO_ENVIRONMENT' => 'probo',
'GITLAB_CI' => 'gitlab',
];
foreach ($mapping as $env_var => $ci_name) {
if (getenv($env_var)) {
return $ci_name;
}
}
return FALSE;
}
/**
* Is CI.
*/
public static function isCiEnv() {
return self::getCiEnv() || getenv('CI');
}
/**
* Get the settings file include for the current CI environment.
*
* This may be provided by BLT, or via a Composer package that has provided
* its own environment detector. In the case of multiple detectors providing a
* settings file, the first one wins.
*
* @return string
* Settings file full path and filename.
*/
public static function getCiSettingsFile() {
$results = array_filter(self::getSubclassResults(__FUNCTION__));
if ($results) {
return current($results);
}
return sprintf("%s/vendor/acquia/blt/settings/%s.settings.php", dirname(DRUPAL_ROOT), self::getCiEnv());
}
/**
* Is Pantheon.
*/
public static function isPantheonEnv() {
return (bool) getenv('PANTHEON_ENVIRONMENT');
}
/**
* Get Pantheon.
*/
public static function getPantheonEnv() {
return getenv('PANTHEON_ENVIRONMENT');
}
/**
* Is Pantheon.
*/
public static function isPantheonDevEnv() {
return self::getPantheonEnv() == 'dev';
}
/**
* Is Pantheon.
*/
public static function isPantheonStageEnv() {
return self::getPantheonEnv() == 'test';
}
/**
* Is Pantheon.
*/
public static function isPantheonProdEnv() {
return self::getPantheonEnv() == 'live';
}
/**
* Is local.
*/
public static function isLocalEnv() {
$results = self::getSubclassResults(__FUNCTION__);
if ($results) {
return TRUE;
}
return !self::isAhEnv() && !self::isPantheonEnv() && !self::isCiEnv();
}
/**
* Is dev.
*/
public static function isDevEnv() {
$results = self::getSubclassResults(__FUNCTION__);
if ($results) {
return TRUE;
}
return self::isAhDevEnv() || self::isPantheonDevEnv();
}
/**
* Is stage.
*/
public static function isStageEnv() {
$results = self::getSubclassResults(__FUNCTION__);
if ($results) {
return TRUE;
}
return self::isAhStageEnv() || self::isPantheonStageEnv();
}
/**
* Is prod.
*/
public static function isProdEnv() {
$results = self::getSubclassResults(__FUNCTION__);
if ($results) {
return TRUE;
}
return self::isAhProdEnv() || self::isPantheonProdEnv();
}
/**
* Get AH files.
*/
public static function getAhFilesRoot() {
return '/mnt/files/' . self::getAhGroup() . '.' . self::getAhEnv();
}
/**
* Is ACSF.
*/
public static function isAcsfInited() {
return file_exists(DRUPAL_ROOT . "/sites/g");
}
/**
* Get a standardized name of the current OS.
*
* @return string
* Name of the OS family.
*/
public static function getPlatform() {
// phpcs:ignore
return OsInfo::family();
}
/**
* Get a unique ID for the current running environment.
*
* Should conform to UUIDs generated by sebhildebrandt/systeminformation.
*
* @see https://github.com/sebhildebrandt/systeminformation/blob/master/lib/osinfo.js
*
* @return string|null
* The machine UUID.
*/
public static function getMachineUuid() {
switch (self::getPlatform()) {
case FamilyName::LINUX:
return shell_exec('( cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname ) | head -n 1 || :');
case FamilyName::DARWIN:
$output = shell_exec('ioreg -rd1 -c IOPlatformExpertDevice | grep IOPlatformUUID');
$parts = explode('=', str_replace('"', '', $output));
return strtolower(trim($parts[1]));
case FamilyName::WINDOWS:
return shell_exec('%windir%\\System32\\reg query "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography" /v MachineGuid');
default:
return NULL;
}
}
/**
* OS name.
*
* @return string
* The OS name.
*/
public static function getOsName() {
return OsInfo::os();
}
/**
* OS version.
*
* @return string
* The OS version.
*/
public static function getOsVersion() {
return OsInfo::version();
}
/**
* OS is Darwin.
*/
public static function isDarwin() {
return OsInfo::isApple();
}
/**
* Get ACSF db.
*
* @return string|null
* ACSF db name.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
public static function getAcsfDbName() {
return isset($GLOBALS['gardens_site_settings']) && self::isAcsfEnv() ? $GLOBALS['gardens_site_settings']['conf']['acsf_db_name'] : NULL;
}
/**
* Find the repo root.
*
* This isn't as trivial as it sounds, since a simple relative path
* (__DIR__ . '/../../../../../../') won't work if this package is symlinked
* using a Composer path repository, and this file can be invoked from both
* web requests and BLT CLI calls.
*
* @return string
* The repo root as an absolute path.
*/
public static function getRepoRoot() {
if (defined('DRUPAL_ROOT')) {
// This is a web or Drush request.
return DRUPAL_ROOT . '/..';
}
else {
// This is a BLT CLI call. Get the $repo_root that was set in
// bin/blt-robo.php.
// phpcs:ignore
global $repo_root;
return $repo_root;
}
}
/**
* Call a given function in all EnvironmentDetector subclasses.
*
* Composer packages can provide their own version of an EnvironmentDetector
* that inherits from this one. This allows for detection of new types of
* environments not hardcoded in this class.
*
* @param string $functionName
* The function name to call in a subclass.
*
* @return array
* Results from each subclass function call (omits any null / false results)
*
* @throws \ReflectionException
*/
private static function getSubclassResults($functionName) {
static $detectors;
if (!isset($detectors)) {
$autoloader = require self::getRepoRoot() . '/vendor/autoload.php';
$classMap = $autoloader->getClassMap();
$detectors = array_filter($classMap, function ($classPath) {
return strpos($classPath, 'Blt/Plugin/EnvironmentDetector') !== FALSE;
});
}
$results = [];
foreach ($detectors as $detector => $classPath) {
// Only call this method if it's been overridden by the child class.
$detectorReflector = new \ReflectionMethod($detector, $functionName);
if ($detectorReflector->getDeclaringClass()->getName() === $detector) {
$results[] = call_user_func([$detector, $functionName]);
}
}
return array_filter($results);
}
}