-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
magento-team
merged 2 commits into
magento:develop
from
piotrkwiecinski:generate-varnish-vcl-console-command
Apr 19, 2017
+697
−6
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
app/code/Magento/PageCache/Api/VclTemplateLocatorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
275
app/code/Magento/PageCache/Console/Command/GenerateVclCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
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
14
app/code/Magento/PageCache/Exception/UnsupportedVarnishVersion.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo:
port
, notpost
. Also, I had a bit of problem understanding the phraseBackend 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.There was a problem hiding this comment.
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?