-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreflight.php
More file actions
108 lines (93 loc) · 2.74 KB
/
preflight.php
File metadata and controls
108 lines (93 loc) · 2.74 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
#!/usr/bin/env php
<?php
/**
* @file
* Deployment preflight checks.
*
* @see https://github.com/City-of-Helsinki/drupal-helfi-platform/blob/main/documentation/openshift.md for documentation.
*/
declare(strict_types = 1);
/**
* Collect preflight messages.
*
* @param string $message
* The message.
*
* @return array
* The preflight error messages.
*/
function preflight_messages(string $message = '') : array {
static $messages = [];
if ($message) {
$messages[] = $message;
}
return $messages;
}
/**
* Mark preflight check as failed.
*
* @param string $message
* The message.
* @param string[]|int[] $variables
* The variable substitutions.
*/
function preflight_failed(string $message, string|int ...$variables) : void {
$message = sprintf('Preflight check failed: %s', sprintf($message, ...$variables));
preflight_messages($message);
}
/**
* Checks if given environment variable are set.
*
* @param array $items
* The environment variables to check.
*
* @return bool
* TRUE if environment variable is set.
*/
function environment_variable_isset(array $items) : bool {
foreach ($items as $item) {
if (getenv($item)) {
return TRUE;
}
}
return FALSE;
}
$class_loader = require_once './../vendor/autoload.php';
require_once './sites/default/settings.php'; // NOSONAR
$candidates = [
(getenv('APP_ENV') ?: 'local') . '.preflight.php',
'all.preflight.php',
];
$private_files_folder = getenv('DRUPAL_FILES_PRIVATE');
// Make sure private files folder is outside webroot unless explicitly
// configured so.
if (!$private_files_folder && is_dir('/var/www/html/public/sites/default/files/private')) {
preflight_failed('sites/default/files/private folder found.');
}
// Allow additional custom checks to be added.
// Create a 'all.preflight.php' file for checks that need to be run
// on all environments and '{env}.preflight.php' for environment-specific
// checks, for example 'testing.preflight.php'.
foreach ($candidates as $candidate) {
$fileName = sprintf('%s/%s', __DIR__, $candidate);
if (!file_exists($fileName)) {
continue;
}
require_once $fileName; // NOSONAR
}
if (isset($preflight_checks['environmentVariables'])) {
foreach ($preflight_checks['environmentVariables'] as $item) {
// Allow conditional environment variables. For example,
// ENV_VAR1|ENV_VAR2 will be treated as (ENV_VAR1 or ENV_VAR2) and
// will only fail if neither one is defined.
$parts = explode('|', $item);
if (!environment_variable_isset($parts)) {
preflight_failed('Environment variable %s is not set.', implode(', ', $parts));
}
}
}
// Fail deployment if any preflight check has failed.
if ($messages = preflight_messages()) {
echo implode(PHP_EOL, $messages);
exit(1); // NOSONAR
}