From 19c3e39a4d3dc4815b3c43779ab887ecec60ad0f Mon Sep 17 00:00:00 2001 From: Alexander Grooff Date: Thu, 1 Feb 2024 14:21:35 +0100 Subject: [PATCH 1/5] feat: render variables in Nginx --- ci/test/magento/etc/nginx/server.example.conf | 4 + .../PlatformConfiguration/NginxRenderTask.php | 86 +++++++++++++++++++ .../Task/PlatformConfiguration/NginxTask.php | 1 + 3 files changed, 91 insertions(+) create mode 100644 src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php diff --git a/ci/test/magento/etc/nginx/server.example.conf b/ci/test/magento/etc/nginx/server.example.conf index 8452009..df3146d 100644 --- a/ci/test/magento/etc/nginx/server.example.conf +++ b/ci/test/magento/etc/nginx/server.example.conf @@ -1 +1,5 @@ +location /root { + root {{public_folder}}; +} + location /example {} diff --git a/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php new file mode 100644 index 0000000..604aed0 --- /dev/null +++ b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php @@ -0,0 +1,86 @@ +twig = $twig; + } + + protected function getIncrementalNamePrefix(): string + { + return 'deploy:configuration:nginx:render:'; + } + + public function supports(TaskConfigurationInterface $config): bool + { + return $config instanceof NginxConfiguration; + } + + private function getAllDeployerVars(): array + { + if (!Context::has()) { + return Deployer::get()->config->ownValues(); + } else { + return Context::get()->getConfig()->ownValues(); + } + } + + /** + * Render variables in the given file + * @param string $file Template file to render + * @param array $variables Variables to render + * @return string Rendered file + */ + private function render(string $file, array $variables): string + { + // Get all present Deployer variables + $template = $this->twig->load($file); + return $template->render($variables); + } + + public function configureWithTaskConfig(TaskConfigurationInterface $config): ?Task + { + task('deploy:nginx:render', function () { + $variables = $this->getAllDeployerVars(); + // Render every file in nginx/config_path using twig + foreach (glob(get('nginx/config_path') . '/**') as $nginx_file) { + if (!is_file($nginx_file)) { + continue; + } + + $renderedContent = $this->render($nginx_file, $variables); + # Overwriting the template file shouldn't be a big deal right? + file_put_contents($nginx_file, $renderedContent); + } + }); + fail('deploy:nginx:prepare', 'deploy:nginx:cleanup'); + + return null; + } +} diff --git a/src/Deployer/Task/PlatformConfiguration/NginxTask.php b/src/Deployer/Task/PlatformConfiguration/NginxTask.php index 92c903e..61705d9 100644 --- a/src/Deployer/Task/PlatformConfiguration/NginxTask.php +++ b/src/Deployer/Task/PlatformConfiguration/NginxTask.php @@ -38,6 +38,7 @@ public function configureWithTaskConfig(TaskConfigurationInterface $config): ?Ta task('deploy:nginx', [ 'deploy:nginx:prepare', 'deploy:nginx:manage_vhost', + 'deploy:nginx:render', 'deploy:nginx:upload', 'deploy:nginx:sync', 'deploy:nginx:cleanup', From cfc406a7020d20c34ec464c334394d10ef9c795e Mon Sep 17 00:00:00 2001 From: Alexander Grooff Date: Thu, 1 Feb 2024 15:10:35 +0100 Subject: [PATCH 2/5] test: ensure templating works --- ci/test/run-general.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ci/test/run-general.sh b/ci/test/run-general.sh index b293098..e0eb694 100755 --- a/ci/test/run-general.sh +++ b/ci/test/run-general.sh @@ -151,6 +151,9 @@ $HN crontab -l -u app | grep "### BEGIN banaan1.store ###" $HN crontab -l -u app | grep "### END banaan1.store ###" $HN crontab -l -u app | sed -n -e '/### BEGIN banaan1.store ###/,/### END banaan1.store ###/ p' | grep "banaan" +# Verify platform configs are templated correctly +$HN grep "root /data/web/apps/banaan1.store/current/public;" /data/web/nginx/banaan1.store/server.example.conf || ($HN cat /data/web/nginx/banaan1.store/server.example.conf && exit 1) + ###################################### # REMOVE A NGINX LOCATION # # Create a new release but make sure # From b0dfd599d367a1ebe7726d28f4b116a6c60b607e Mon Sep 17 00:00:00 2001 From: Alexander Grooff Date: Thu, 1 Feb 2024 15:45:03 +0100 Subject: [PATCH 3/5] feat: render logging --- src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php index 604aed0..08a7183 100644 --- a/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php +++ b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php @@ -14,9 +14,8 @@ use function Deployer\fail; use function Deployer\get; -use function Deployer\run; -use function Deployer\set; use function Deployer\task; +use function Deployer\writeln; class NginxRenderTask extends TaskBase implements ConfigurableTaskInterface { @@ -75,6 +74,8 @@ public function configureWithTaskConfig(TaskConfigurationInterface $config): ?Ta } $renderedContent = $this->render($nginx_file, $variables); + writeln('Rendered contents for ' . $nginx_file . ': ' . $renderedContent); + # Overwriting the template file shouldn't be a big deal right? file_put_contents($nginx_file, $renderedContent); } From ac7713d7e0aaf4a260d9d7fd6c6d35bf8e6f04ca Mon Sep 17 00:00:00 2001 From: Alexander Grooff Date: Thu, 1 Feb 2024 16:11:15 +0100 Subject: [PATCH 4/5] fix: single wildcard --- src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php index 08a7183..2a8e078 100644 --- a/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php +++ b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php @@ -68,7 +68,7 @@ public function configureWithTaskConfig(TaskConfigurationInterface $config): ?Ta task('deploy:nginx:render', function () { $variables = $this->getAllDeployerVars(); // Render every file in nginx/config_path using twig - foreach (glob(get('nginx/config_path') . '/**') as $nginx_file) { + foreach (glob(get('nginx/config_path') . '/*') as $nginx_file) { if (!is_file($nginx_file)) { continue; } From d78f9d21b624a7f96b6daa8e76adf4d18b407441 Mon Sep 17 00:00:00 2001 From: Alexander Grooff Date: Thu, 1 Feb 2024 16:15:46 +0100 Subject: [PATCH 5/5] fix: recursive walk over dir --- .../PlatformConfiguration/NginxRenderTask.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php index 2a8e078..85c4c13 100644 --- a/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php +++ b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php @@ -2,14 +2,16 @@ namespace Hypernode\Deploy\Deployer\Task\PlatformConfiguration; -use Hypernode\Deploy\Deployer\Task\IncrementedTaskTrait; use Deployer\Deployer; -use Deployer\Task\Task; use Deployer\Task\Context; +use Deployer\Task\Task; use Hypernode\Deploy\Deployer\Task\ConfigurableTaskInterface; +use Hypernode\Deploy\Deployer\Task\IncrementedTaskTrait; use Hypernode\Deploy\Deployer\Task\TaskBase; use Hypernode\DeployConfiguration\PlatformConfiguration\NginxConfiguration; use Hypernode\DeployConfiguration\TaskConfigurationInterface; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; use Twig\Environment; use function Deployer\fail; @@ -68,16 +70,17 @@ public function configureWithTaskConfig(TaskConfigurationInterface $config): ?Ta task('deploy:nginx:render', function () { $variables = $this->getAllDeployerVars(); // Render every file in nginx/config_path using twig - foreach (glob(get('nginx/config_path') . '/*') as $nginx_file) { - if (!is_file($nginx_file)) { + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(get('nginx/config_path'))); + foreach ($iterator as $nginx_file) { + if (!$nginx_file->isFile()) { continue; } - $renderedContent = $this->render($nginx_file, $variables); - writeln('Rendered contents for ' . $nginx_file . ': ' . $renderedContent); + $renderedContent = $this->render($nginx_file->getPathname(), $variables); + writeln('Rendered contents for ' . $nginx_file->getPathname() . ': ' . $renderedContent); # Overwriting the template file shouldn't be a big deal right? - file_put_contents($nginx_file, $renderedContent); + file_put_contents($nginx_file->getPathname(), $renderedContent); } }); fail('deploy:nginx:prepare', 'deploy:nginx:cleanup');