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

[WIP] Varnish Vcl generator command #9286

Merged
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
21 changes: 21 additions & 0 deletions app/code/Magento/PageCache/Api/VclGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\PageCache\Api;

/**
* @api
*/
interface VclGeneratorInterface
{
/**
* Return generated varnish.vcl configuration file
*
* @param int $version
* @return string
*/
public function generateVcl($version);
}
26 changes: 26 additions & 0 deletions app/code/Magento/PageCache/Api/VclTemplateLocatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\PageCache\Api;

use Magento\PageCache\Exception\UnsupportedVarnishVersion;

/**
* Vcl template locator interface
*
* @api
*/
interface VclTemplateLocatorInterface
{
/**
* Get Varnish Vcl template
*
* @param int $version
* @return string
* @throws UnsupportedVarnishVersion
*/
public function getTemplate($version);
}
275 changes: 275 additions & 0 deletions app/code/Magento/PageCache/Console/Command/GenerateVclCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\PageCache\Console\Command;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Console\Cli;
use Magento\Framework\Filesystem\DriverPool;
use Magento\Framework\Filesystem\File\WriteFactory;
use Magento\Framework\HTTP\PhpEnvironment\Request;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\PageCache\Api\VclGeneratorInterfaceFactory;
use Magento\PageCache\Model\Config;
use Magento\PageCache\Model\Varnish\VclTemplateLocator;
use Magento\Store\Model\ScopeInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateVclCommand extends Command
{
/**
* Access list option name
*/
const ACCESS_LIST_OPTION = 'access-list';

/**
* Backend host option name
*/
const BACKEND_HOST_OPTION = 'backend-host';

/**
* Backend port option name
*/
const BACKEND_PORT_OPTION = 'backend-port';

/**
* Varnish version option name
*/
const EXPORT_VERSION_OPTION = 'export-version';

/**
* Grace period option name
*/
const GRACE_PERIOD_OPTION = 'grace-period';

/**
* Output file option name
*/
const OUTPUT_FILE_OPTION = 'output-file';

/**
* @var \Magento\Framework\Filesystem\Directory\WriteFactory
*/
private $writeFactory;

/**
* @var VclGeneratorInterfaceFactory
*/
private $vclGeneratorFactory;

/**
* @var array
*/
private $inputToVclMap = [
self::ACCESS_LIST_OPTION => 'accessList',
self::BACKEND_PORT_OPTION => 'backendPort',
self::BACKEND_HOST_OPTION => 'backendHost',
self::GRACE_PERIOD_OPTION => 'gracePeriod',
];

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var Json
*/
private $serializer;

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('varnish:vcl:generate')
->setDescription('Generates Varnish VCL and echos it to the command line')
->setDefinition($this->getOptionList());
}

/**
* @param VclGeneratorInterfaceFactory $vclGeneratorFactory
* @param WriteFactory $writeFactory
* @param ScopeConfigInterface $scopeConfig
* @param Json $serializer
*/
public function __construct(
VclGeneratorInterfaceFactory $vclGeneratorFactory,
WriteFactory $writeFactory,
ScopeConfigInterface $scopeConfig,
Json $serializer
) {
parent::__construct();
$this->writeFactory = $writeFactory;
$this->vclGeneratorFactory = $vclGeneratorFactory;
$this->scopeConfig = $scopeConfig;
$this->serializer = $serializer;
}

/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$errors = $this->validate($input);
if ($errors) {
foreach ($errors as $error) {
$output->writeln('<error>'.$error.'</error>');

return Cli::RETURN_FAILURE;
}
}

try {
$outputFile = $input->getOption(self::OUTPUT_FILE_OPTION);
$varnishVersion = $input->getOption(self::EXPORT_VERSION_OPTION);
$vclParameters = array_merge($this->inputToVclParameters($input), [
'sslOffloadedHeader' => $this->getSslOffloadedHeader(),
'designExceptions' => $this->getDesignExceptions(),
]);
$vclGenerator = $this->vclGeneratorFactory->create($vclParameters);
$vcl = $vclGenerator->generateVcl($varnishVersion);

if ($outputFile) {
$writer = $this->writeFactory->create($outputFile, DriverPool::FILE, 'w+');
$writer->write($vcl);
$writer->close();
} else {
$output->writeln($vcl);
}

return Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$output->writeln('<error>'.$e->getMessage().'</error>');
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln($e->getTraceAsString());
}

return Cli::RETURN_FAILURE;
}
}

/**
* Get list of options for the command
*
* @return InputOption[]
*/
private function getOptionList()
{
return [
new InputOption(
self::ACCESS_LIST_OPTION,
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'IPs access list that can purge Varnish',
['localhost']
),
new InputOption(
self::BACKEND_HOST_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Backend host for configuration',
'localhost'
),
new InputOption(
self::BACKEND_PORT_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Backend post for configuration',
Copy link
Contributor

@adragus-inviqa adragus-inviqa Apr 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: port, not post. Also, I had a bit of problem understanding the phrase Backend port for configuration. Can we be more explicit? I know what it does, but I just think some people will need more info about it. Don't be terse on documentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. Do you have suggestions how to better call it?

8080
),
new InputOption(
self::EXPORT_VERSION_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Varnish configuration version to export',
VclTemplateLocator::VARNISH_SUPPORTED_VERSION_4
),
new InputOption(
self::GRACE_PERIOD_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Grace period in seconds',
300
),
new InputOption(
self::OUTPUT_FILE_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Save output to target file'
),
];
}

/**
* @param InputInterface $input
* @return array
*/
private function inputToVclParameters(InputInterface $input)
{
$parameters = [];

foreach ($this->inputToVclMap as $inputKey => $vclKey) {
$parameters[$vclKey] = $input->getOption($inputKey);
}

return $parameters;
}

/**
* Input validation
*
* @param InputInterface $input
* @return array
*/
private function validate(InputInterface $input)
{
$errors = [];

if ($input->hasOption(self::BACKEND_PORT_OPTION)
&& ($input->getOption(self::BACKEND_PORT_OPTION) < 0
|| $input->getOption(self::BACKEND_PORT_OPTION) > 65535)
) {
$errors[] = 'Invalid backend port value';
}

if ($input->hasOption(self::GRACE_PERIOD_OPTION)
&& $input->getOption(self::GRACE_PERIOD_OPTION) < 0
) {
$errors[] = 'Grace period can\'t be lower than 0';
}

return $errors;
}

/**
* Get ssl Offloaded header
*
* @return mixed
*/
private function getSslOffloadedHeader()
{
return $this->scopeConfig->getValue(Request::XML_PATH_OFFLOADER_HEADER);
}

/**
* Get design exceptions
*
* @return array
*/
private function getDesignExceptions()
{
$expressions = $this->scopeConfig->getValue(
Config::XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX,
ScopeInterface::SCOPE_STORE
);

return $expressions ? $this->serializer->unserialize($expressions) : [];
}
}
14 changes: 14 additions & 0 deletions app/code/Magento/PageCache/Exception/UnsupportedVarnishVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\PageCache\Exception;

use Magento\Framework\Exception\LocalizedException;

class UnsupportedVarnishVersion extends LocalizedException
{

}
Loading