-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLagoon.php
More file actions
109 lines (88 loc) · 3.08 KB
/
Lagoon.php
File metadata and controls
109 lines (88 loc) · 3.08 KB
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
<?php
declare(strict_types=1);
namespace DrevOps\EnvironmentDetector\Providers;
use DrevOps\EnvironmentDetector\Environment;
/**
* Lagoon provider.
*
* Detects the Lagoon environment type.
*
* @package DrevOps\EnvironmentDetector\Providers
*/
class Lagoon extends AbstractProvider {
/**
* {@inheritdoc}
*/
public const ID = 'lagoon';
/**
* {@inheritdoc}
*/
public const LABEL = 'Lagoon';
/**
* {@inheritdoc}
*/
protected function envPrefixes(): array {
return ['LAGOON_'];
}
/**
* {@inheritdoc}
*/
public function active(): bool {
return getenv('LAGOON_KUBERNETES') !== FALSE;
}
/**
* {@inheritdoc}
*/
public function type(): ?string {
$type = NULL;
// Environment is marked as 'production'.
if (getenv('LAGOON_ENVIRONMENT_TYPE') == 'production') {
$type = Environment::PRODUCTION;
}
elseif (getenv('LAGOON_ENVIRONMENT_TYPE') == 'development') {
$type = Environment::DEVELOPMENT;
// Try to identify production environment using a branch name for
// the cases when the Lagoon environment is not marked as 'production'
// yet. Note that `ENVIRONMENT_PRODUCTION_BRANCH` is a custom variable
// that should be set in the Lagoon project settings.
if (!empty(getenv('LAGOON_GIT_BRANCH')) && !empty(getenv('ENVIRONMENT_PRODUCTION_BRANCH')) && getenv('LAGOON_GIT_BRANCH') === getenv('ENVIRONMENT_PRODUCTION_BRANCH')) {
$type = Environment::PRODUCTION;
}
// `main` or `master` is a Stage if another branch is used for production.
elseif (getenv('LAGOON_GIT_BRANCH') == 'main' || getenv('LAGOON_GIT_BRANCH') == 'master') {
$type = Environment::STAGE;
}
// Release and hotfix branches are considered Stage.
elseif (!empty(getenv('LAGOON_GIT_BRANCH')) && (str_starts_with((string) getenv('LAGOON_GIT_BRANCH'), 'release/') || str_starts_with((string) getenv('LAGOON_GIT_BRANCH'), 'hotfix/'))) {
$type = Environment::STAGE;
}
}
return $type;
}
/**
* Applies Drupal context.
*/
public function contextualizeDrupal(): void {
global $settings;
// Lagoon reverse proxy settings.
$settings['reverse_proxy'] = TRUE;
// Reverse proxy settings.
$settings['reverse_proxy_header'] = 'HTTP_TRUE_CLIENT_IP';
// Cache prefix.
if (getenv('LAGOON_PROJECT') && (getenv('LAGOON_GIT_SAFE_BRANCH') || getenv('ENVIRONMENT_PRODUCTION_BRANCH'))) {
$settings['cache_prefix'] = getenv('LAGOON_PROJECT') . '_' . (getenv('LAGOON_GIT_SAFE_BRANCH') ?: getenv('ENVIRONMENT_PRODUCTION_BRANCH'));
}
// URL when accessed from PHP processes in Lagoon.
$settings['trusted_host_patterns'][] = '^nginx\-php$';
// Public Lagoon URL.
$settings['trusted_host_patterns'][] = '^.+\.au\.amazee\.io$';
// Lagoon routes.
$routes = $this->data()['LAGOON_ROUTES'] ?? [];
if (!empty($routes) && is_string($routes)) {
$patterns = str_replace(['.', 'https://', 'http://', ','], [
'\.', '', '', '|',
], $routes);
$settings['trusted_host_patterns'][] = '^' . $patterns . '$';
}
}
}