Skip to content

Commit a882af9

Browse files
authored
Merge pull request #27 from section-io/upgrade-varnish-support
Extension supports Varnish 5+
2 parents af3187d + 28cb5bc commit a882af9

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

Console/Command/UpdateVclCommand.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,26 @@ class UpdateVclCommand extends Command
2323
protected $state;
2424
/** @var \Magento\PageCache\Model\Config\PageCache $pageCacheConfig */
2525
protected $pageCacheConfig;
26+
/** @var \Sectionio\Metrics\Helper\Data $helper */
27+
protected $helper;
2628

2729
/**
2830
* @param \Sectionio\Metrics\Helper\Aperture $aperture
2931
* @param \Sectionio\Metrics\Helper\State $state
32+
* @param \Sectionio\Metrics\Helper\Data $helper
3033
* @param \Magento\PageCache\Model\Config $pageCacheConfig
3134
*/
3235
public function __construct(
3336
\Sectionio\Metrics\Helper\Aperture $aperture,
3437
\Sectionio\Metrics\Helper\State $state,
38+
\Sectionio\Metrics\Helper\Data $helper,
3539
\Magento\PageCache\Model\Config $pageCacheConfig
3640
) {
3741
parent::__construct();
3842
$this->aperture = $aperture;
3943
$this->state = $state;
4044
$this->pageCacheConfig = $pageCacheConfig;
45+
$this->helper = $helper;
4146
}
4247

4348
/**
@@ -60,6 +65,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
6065
$application_id = $this->state->getApplicationId();
6166
$environment_name = $this->state->getEnvironmentName();
6267
$proxy_name = $this->state->getProxyName();
68+
/** @var string $proxy_image*/
69+
$proxy_version = $this->helper->getProxyVersion($account_id, $application_id);
6370

6471
if (!$account_id) {
6572
throw new \Exception('account_id has not been set, please run sectionio:setup');
@@ -77,8 +84,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
7784
throw new \Exception('proxy_name has not been set, please run sectionio:setup');
7885
}
7986

80-
/** Extract the generated Varnish 4 VCL code */
81-
$vcl = $this->pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH);
87+
$major_release = $this->helper->getMajorRelease($proxy_version);
88+
89+
90+
/** Extract the generated VCL code appropriate for their version*/
91+
$vcl = $this->helper->getCorrectVCL($this->pageCacheConfig, $major_release);
92+
8293
$result = $this->aperture->updateProxyConfiguration($account_id, $application_id, $environment_name, $proxy_name, $vcl, 'MagentoTurpentine');
8394

8495
if ($result['http_code'] == 200) {

Controller/Adminhtml/Report/Save.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,14 @@ public function updateVarnishConfiguration()
202202
$application_id = $this->state->getApplicationId();
203203
/** @var string $environment_name */
204204
$environment_name = $this->state->getEnvironmentName();
205+
/** @var string $proxy_image*/
206+
$proxy_version = $this->helper->getProxyVersion($account_id, $application_id);
207+
208+
$major_release = $this->helper->getMajorRelease($proxy_version);
209+
210+
/** Extract the generated VCL code appropriate for their version*/
211+
$vcl = $this->helper->getCorrectVCL($this->pageCacheConfig, $major_release);
205212

206-
/** Extract the generated Varnish 4 VCL code */
207-
$vcl = $this->pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH);
208213
$result = $this->aperture->updateProxyConfiguration($account_id, $application_id, $environment_name, 'varnish', $vcl, 'MagentoTurpentine');
209214

210215
if ($result['http_code'] == 200) {

Helper/Data.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,63 @@ public function clearDefaultApplication()
580580
$applicationFactory->save();
581581
}
582582
}
583+
584+
/**
585+
* get complete proxy image version for a given environment from aperture.
586+
*
587+
* @param int $account_id
588+
* @param int $application_id
589+
*
590+
* @return string
591+
*/
592+
public function getProxyVersion($account_id, $application_id)
593+
{
594+
/**generateApertureUrl takes an associative array */
595+
$parameters = array("accountId"=>$account_id, "applicationId"=>$application_id, "environmentName"=>"Production");
596+
597+
/** build the account url */
598+
$partial_url = $this->generateApertureUrl($parameters);
599+
$url = $partial_url . "/stack";
600+
601+
$curl_response = $this->performCurl($url);
602+
/** return response as an associative array */
603+
$response = json_decode($curl_response['body_content'], true);
604+
605+
/** find element with name=varnish, grab the image */
606+
$image;
607+
foreach($response as $proxy){
608+
if ($proxy['name'] == 'varnish') {
609+
$image = $proxy['image'];
610+
}
611+
}
612+
/** returns only the version ie "5.2.1" */
613+
return explode(":", $image)[1];
614+
}
615+
616+
/**
617+
* Takes a full version like "5.2.1" and returns the major release (5)
618+
* @param string $full_release
619+
*
620+
* @return string
621+
*/
622+
public function getMajorRelease($full_release)
623+
{
624+
return explode(".", $full_release)[0];
625+
}
626+
627+
/**
628+
* Takes in the pageCacheConfig object and a major release and returns the correct vcl
629+
* @param object $pageCacheConfig
630+
* @param string $full_release
631+
*
632+
* @return string
633+
*/
634+
public function getCorrectVCL($pageCacheConfig, $major_release)
635+
{
636+
if ($major_release == "4") {
637+
return $pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH);
638+
} else {
639+
return $pageCacheConfig->getVclFile(\Magento\PageCache\Model\Config::VARNISH_5_CONFIGURATION_PATH);
640+
}
641+
}
583642
}

0 commit comments

Comments
 (0)