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/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 # diff --git a/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php new file mode 100644 index 0000000..85c4c13 --- /dev/null +++ b/src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php @@ -0,0 +1,90 @@ +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 + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(get('nginx/config_path'))); + foreach ($iterator as $nginx_file) { + if (!$nginx_file->isFile()) { + continue; + } + + $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->getPathname(), $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',