-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_setup.php
More file actions
147 lines (125 loc) · 4.86 KB
/
Copy pathfeature_setup.php
File metadata and controls
147 lines (125 loc) · 4.86 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
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
<?php
namespace Deployer;
use MoveElevator\DeployerTools\Database\DbUtility;
require_once('feature_init.php');
require_once('url_shortener.php');
task('feature:setup', function () {
// Without a feature we are deploying the base instance and must not touch any
// feature scaffolding — renderRemoteTemplates() would overwrite its .env.
if (!featureRequested()) {
return;
}
checkVerbosity();
// extend deploy path / public url
$feature = initFeature();
if (!checkFeatureBranchExists()) {
info("setup feature branch <fg=magenta;options=bold>$feature</>");
set('feature_setup', true);
DbUtility::getDatabaseManager()->create();
renderRemoteTemplates();
} else {
set('feature_setup', false);
}
})
->select('type=feature-branch-deployment')
->once()
->desc('Setup a feature branch')
;
/**
* Checks if a feature branch already exists regarding the database and the server path
*
* @throws \Deployer\Exception\Exception
* @throws \Deployer\Exception\RunException
* @throws \Deployer\Exception\TimeoutException
*/
function checkFeatureBranchExists(): bool
{
$path = get('deploy_path');
return (DbUtility::getDatabaseManager()->exists() && test("[[ -d $path ]]"));
}
/**
* Render additional remote files by given templates to ensure the operability of feature branches
*
* Configure the registration of templates like this:
*
* set('feature_templates', [
* __DIR__ . '/.deployment/deployer/templates/.env.dist' => '/shared/.env'
* ]);
*
* Within the template file (e.g. .env.dist) are arguments to replace available:
* {{DEPLOYER_CONFIG_DATABASE_USER}}
*
* @return void
* @throws \Deployer\Exception\Exception
* @throws \Deployer\Exception\RunException
* @throws \Deployer\Exception\TimeoutException
*/
function renderRemoteTemplates(): void
{
debug('Rendering remote template');
$databaseName = DbUtility::getDatabaseManager()->getDatabaseName();
// the normalized name, matching the instance directory and url segment
$feature = get('feature');
$templates = get('feature_templates');
if (!$templates) {
$templates = [];
}
// ToDo: simplify!
// get additional arguments from environment variables
$environmentVariables = getenv();
$additionalTemplateVariables = [];
foreach ($environmentVariables as $key => $value) {
if (str_starts_with($key, 'DEPLOYER_CONFIG_')) {
debug('additional template variable: ' . $key);
$additionalTemplateVariables[$key] = $value;
}
}
$featurePath = isUrlShortener() ? "$feature/" :$feature . '/current/' . get('web_path') ;
// preparing default arguments for templates and extend by additional template variables
$arguments = array_merge([
'DEPLOYER_CONFIG_DATABASE_HOST' => get('database_host'),
'DEPLOYER_CONFIG_DATABASE_PORT' => get('database_port'),
'DEPLOYER_CONFIG_DATABASE_USER' => get('database_user'),
'DEPLOYER_CONFIG_DATABASE_PASSWORD' => get('database_password'),
'DEPLOYER_CONFIG_DATABASE_NAME' => has('database_name') ? get('database_name') : $databaseName,
'DEPLOYER_CONFIG_FEATURE_NAME' => (string)$feature,
'DEPLOYER_CONFIG_FEATURE_URL' => get('public_urls')[0],
'DEPLOYER_CONFIG_FEATURE_PATH' => $featurePath,
'DEPLOYER_CONFIG_ENCRYPTION_KEY' => bin2hex(random_bytes(48)),
],
$additionalTemplateVariables)
;
// iterate through predefined templates
foreach ($templates as $template => $target) {
debug('Uploading template: ' . $template . '...');
uploadTemplate($template, $target, $arguments);
}
}
/**
* Extend a given local template with the provided arguments and uploads them to a remote host
*
* @param $localTemplate
* @param $remoteTarget
* @param $arguments
* @return void
* @throws \Deployer\Exception\Exception
* @throws \Deployer\Exception\RunException
* @throws \Deployer\Exception\TimeoutException
*/
function uploadTemplate($localTemplate, $remoteTarget, $arguments): void {
debug('Uploading template: ' . $localTemplate . '...');
$templateContent = file_get_contents($localTemplate);
// replace all {{variables}} with arguments
foreach ($arguments as $argument => $replacement) {
$templateContent = str_replace('{{' .$argument . '}}', (string)$replacement, $templateContent);
}
// generate temporary local file for file upload to remote
$temporaryFileName = ".deployer." . preg_replace('/[^A-Za-z0-9\-]/', '', $remoteTarget) . ".tmp";
file_put_contents($temporaryFileName, $templateContent);
$path = get('deploy_path') . $remoteTarget;
// create path to template destination if not exists
runExtended("mkdir -p " . pathinfo($path)['dirname']);
// upload template to remote
upload($temporaryFileName,get('deploy_path') . $remoteTarget);
unlink($temporaryFileName);
}