Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render variables in platform configuration files #99

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ci/test/magento/etc/nginx/server.example.conf
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
location /root {
root {{public_folder}};
}

location /example {}
3 changes: 3 additions & 0 deletions ci/test/run-general.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand Down
90 changes: 90 additions & 0 deletions src/Deployer/Task/PlatformConfiguration/NginxRenderTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Hypernode\Deploy\Deployer\Task\PlatformConfiguration;

use Deployer\Deployer;
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;
use function Deployer\get;
use function Deployer\task;
use function Deployer\writeln;

class NginxRenderTask extends TaskBase implements ConfigurableTaskInterface
{
use IncrementedTaskTrait;

/**
* @var Environment
*/
private $twig;

public function __construct(Environment $twig)
{
$this->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;
}
}
1 change: 1 addition & 0 deletions src/Deployer/Task/PlatformConfiguration/NginxTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading