-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
131 lines (115 loc) · 3.61 KB
/
functions.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
<?php
namespace Deployer;
use Deployer\Exception\RunException;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @param $message
* @return void
*/
function debug($message): void
{
if (isVerbose()) {
writeln("<fg=yellow;options=bold>debug</> " . parse($message));
}
}
/**
* @return bool
*/
function isVerbose(): bool
{
return in_array(output()->getVerbosity(), [OutputInterface::VERBOSITY_VERBOSE, OutputInterface::VERBOSITY_VERY_VERBOSE, OutputInterface::VERBOSITY_DEBUG], true);
}
/**
* @return void
*/
function checkVerbosity(): void
{
if (!empty(getenv('DEPLOYER_CONFIG_VERBOSE'))) {
output()->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
}
}
/**
* Extend the deployer configuration with available environment variables (starting with "DEPLOYER_CONFIG_"):
*
* ENVIRONMENT_VARIABLE => deployer_configuration
* e.g.
* DEPLOYER_CONFIG_DATABASE_PASSWORD => database_password
*
* @return void
* @throws \Deployer\Exception\Exception
*/
function prepareDeployerConfiguration(): void {
$environmentVariables = getenv();
debug('Extending deployer configuration with environment variables');
foreach ($environmentVariables as $key => $value) {
if (str_starts_with($key, 'DEPLOYER_CONFIG_')) {
$configName = strtolower(str_replace('DEPLOYER_CONFIG_', '', $key));
if (has($configName)) {
debug("Attention: overwriting existing deployer configuration '$configName' with environment variable '$key' ");
}
set($configName, $value);
}
}
}
/**
* Check if a subcommand is available, e.g. "php bin/console", "ckeditor:install"
*
* @param string $command
* @param string $subcommand
* @return bool
* @throws \Deployer\Exception\Exception
* @throws \Deployer\Exception\RunException
* @throws \Deployer\Exception\TimeoutException
*/
function commandSupportSubcommand(string $command, string $subcommand): bool
{
$check = run("( $command list 2>&1 || $command --list) | grep -- $subcommand || true");
if (empty($check)) {
return false;
}
return str_contains($check, $subcommand);
}
/**
* Check if command exists locally
*
* @throws RunException
*/
function commandExistLocally(string $command): bool
{
return testLocally("hash $command 2>/dev/null");
}
/**
* Runs a remote command with the possibility to overwrite the default command options
*/
function runExtended(string $command, ?array $options = [], ?int $timeout = null, ?int $idle_timeout = null, ?string $secret = null, ?array $env = null, ?bool $real_time_output = null, ?bool $no_throw = null): string
{
return run(
$command,
$options,
$timeout ?? (int)get('run_timeout'),
$idle_timeout ?? (int)get('run_idle_timeout'),
$secret ?? (string)get('run_secret'),
$env ?? (array)get('run_env'),
$real_time_output ?? (bool)get('run_real_time_output'),
$no_throw ?? (bool)get('run_no_throw')
);
}
function getRecentDatabaseCacheDumpPath(): string
{
return get('dev_tr_db_dump_dir') . '/' . getRecentDatabaseCacheDumpFilename() . '.sql';
}
function getRecentDatabaseCacheDumpFilename(): string
{
return date('Y-m-d');
}
function recentDatabaseCacheDumpExists(): bool
{
return test('[ -f ' . getRecentDatabaseCacheDumpPath() . ' ]');
}
function cleanUpDatabaseCacheDumps(int $days = 7): void
{
$dbDumpDir = get('dev_tr_db_dump_dir');
run("mkdir -p $dbDumpDir");
// cleanup beforehand: delete all dump files with the above naming scheme older than 7 days
run("find $dbDumpDir -name '*.sql' -mtime +$days -delete");
}