-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_disk_space.php
More file actions
66 lines (48 loc) 路 1.97 KB
/
Copy pathcheck_disk_space.php
File metadata and controls
66 lines (48 loc) 路 1.97 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
<?php
declare(strict_types=1);
namespace Deployer;
use Deployer\Exception\RunException;
task('requirements:check:disk_space', function (): void {
if (!get('requirements_check_disk_space_enabled')) {
return;
}
$warnPercent = (int) get('requirements_disk_space_warn_percent');
$failPercent = (int) get('requirements_disk_space_fail_percent');
checkDiskSpaceAtPath('Disk space: webspace', get('requirements_disk_space_webspace_path'), $warnPercent, $failPercent);
$credentials = resolveDatabaseCredentials();
if (null === $credentials) {
addRequirementRow('Disk space: database', REQUIREMENT_SKIP, 'No database credentials available');
return;
}
if (!in_array($credentials['host'], ['127.0.0.1', 'localhost'], true)) {
addRequirementRow(
'Disk space: database',
REQUIREMENT_SKIP,
"Database host ({$credentials['host']}) not reachable from the deploy target"
);
return;
}
$datadir = detectDatabaseDatadir($credentials);
if (null === $datadir) {
addRequirementRow('Disk space: database', REQUIREMENT_SKIP, 'Could not determine database data directory');
return;
}
checkDiskSpaceAtPath('Disk space: database', $datadir, $warnPercent, $failPercent);
})->hidden();
/**
* @param array{user: string, password: string, host: string, port: int} $credentials
*/
function detectDatabaseDatadir(array $credentials): ?string
{
try {
$output = trim(runMysqlQuery($credentials, "SHOW VARIABLES LIKE 'datadir'"));
} catch (RunException) {
return null;
}
// Output format: "datadir\t/var/lib/mysql/", possibly preceded by mysql client warning lines
// on stderr (merged into stdout by runMysqlQuery()), so only the last line is relevant.
$lines = explode("\n", $output);
$columns = explode("\t", trim((string) end($lines)));
$path = $columns[1] ?? null;
return ('' !== $path && null !== $path) ? $path : null;
}