This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ipunkt/nginx-snippets
Nginx snippets
- Loading branch information
Showing
13 changed files
with
312 additions
and
6 deletions.
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
57 changes: 57 additions & 0 deletions
57
app/Blueprint/NginxSnippets/NginxSnippetExtraInformation/NginxSnippetExtraInformation.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,57 @@ | ||
<?php namespace Rancherize\Blueprint\NginxSnippets\NginxSnippetExtraInformation; | ||
|
||
use Rancherize\Blueprint\Infrastructure\Service\ServiceExtraInformation; | ||
|
||
/** | ||
* Interface NginxSnippetExtraInformation | ||
* @package Rancherize\Blueprint\NginxSnippets\NginxSnippetExtraInformation | ||
*/ | ||
class NginxSnippetExtraInformation implements ServiceExtraInformation { | ||
|
||
const IDENTIFIER = 'nginx-snippet'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $snippets = []; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $mountWorkdir = false; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getIdentifier() { | ||
return self::IDENTIFIER; | ||
} | ||
|
||
/** | ||
* @param $path | ||
*/ | ||
public function addSnippet( $path ) { | ||
$this->snippets[$path] = $path; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getSnippets(): array { | ||
return $this->snippets; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isMountWorkdir(): bool { | ||
return $this->mountWorkdir; | ||
} | ||
|
||
/** | ||
* @param bool $mountWorkdir | ||
*/ | ||
public function setMountWorkdir( bool $mountWorkdir ) { | ||
$this->mountWorkdir = $mountWorkdir; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...nippets/NginxSnippetInfrastructureEventHandler/NginxSnippetInfrastructureEventHandler.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,35 @@ | ||
<?php namespace Rancherize\Blueprint\NginxSnippets\NginxSnippetInfrastructureEventHandler; | ||
|
||
use Rancherize\Blueprint\NginxSnippets\NginxSnippetService\NginxSnippetService; | ||
use Rancherize\Services\BuildServiceEvent\InfrastructureBuiltEvent; | ||
|
||
/** | ||
* Class NginxSnippetInfrastructureEventHandler | ||
* @package Rancherize\Blueprint\NginxSnippets\NginxSnippetInfrastructureEventHandler | ||
*/ | ||
class NginxSnippetInfrastructureEventHandler { | ||
/** | ||
* @var NginxSnippetService | ||
*/ | ||
private $snippetService; | ||
|
||
/** | ||
* NginxSnippetInfrastructureEventHandler constructor. | ||
* @param NginxSnippetService $snippetService | ||
*/ | ||
public function __construct( NginxSnippetService $snippetService) { | ||
$this->snippetService = $snippetService; | ||
} | ||
|
||
/** | ||
* @param InfrastructureBuiltEvent $event | ||
*/ | ||
public function infrastructureBuilt( InfrastructureBuiltEvent $event ) { | ||
$infrastructure = $event->getInfrastructure(); | ||
|
||
foreach($infrastructure->getServices() as $service) | ||
$this->snippetService->addToInfrastructure($infrastructure, $service); | ||
|
||
$event->setInfrastructure($infrastructure); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
app/Blueprint/NginxSnippets/NginxSnippetParser/NginxSnippetParser.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,38 @@ | ||
<?php namespace Rancherize\Blueprint\NginxSnippets\NginxSnippetParser; | ||
|
||
use Rancherize\Blueprint\Infrastructure\Service\Service; | ||
use Rancherize\Blueprint\NginxSnippets\NginxSnippetExtraInformation\NginxSnippetExtraInformation; | ||
use Rancherize\Configuration\Configuration; | ||
|
||
/** | ||
* Class NginxSnippetParser | ||
* @package Rancherize\Blueprint\NginxSnippets\NginxSnippetParser | ||
*/ | ||
class NginxSnippetParser { | ||
|
||
/** | ||
* @param Service $service | ||
* @param Configuration $configuration | ||
*/ | ||
public function parse( Service $service, Configuration $configuration ) { | ||
if( !$configuration->has('nginx.snippets') ) | ||
return; | ||
|
||
if( !$configuration->get('nginx.enable', true) ) | ||
return; | ||
|
||
$extraInformation = new NginxSnippetExtraInformation(); | ||
$snippets = $configuration->get('nginx.snippets', []); | ||
if( empty($snippets) ) | ||
return; | ||
|
||
if( $configuration->get('mount-workdir', false) ) | ||
$extraInformation->setMountWorkdir(true); | ||
|
||
foreach( $snippets as $snippet ) | ||
$extraInformation->addSnippet($snippet); | ||
|
||
$service->addExtraInformation( $extraInformation ); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
app/Blueprint/NginxSnippets/NginxSnippetService/NginxSnippetService.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,67 @@ | ||
<?php namespace Rancherize\Blueprint\NginxSnippets\NginxSnippetService; | ||
|
||
use Rancherize\Blueprint\Infrastructure\Dockerfile\Dockerfile; | ||
use Rancherize\Blueprint\Infrastructure\Infrastructure; | ||
use Rancherize\Blueprint\Infrastructure\Service\ExtraInformationNotFoundException; | ||
use Rancherize\Blueprint\Infrastructure\Service\Service; | ||
use Rancherize\Blueprint\NginxSnippets\NginxSnippetExtraInformation\NginxSnippetExtraInformation; | ||
|
||
/** | ||
* Class NginxSnippetService | ||
* @package Rancherize\Blueprint\NginxSnippets\NginxSnippetService | ||
*/ | ||
class NginxSnippetService { | ||
|
||
/** | ||
* @param Infrastructure $infrastructure | ||
* @param Service $service | ||
*/ | ||
public function addToInfrastructure( Infrastructure $infrastructure, Service $service ) { | ||
$dockerfile = $infrastructure->getDockerfile(); | ||
|
||
try { | ||
$information = $service->getExtraInformation(NginxSnippetExtraInformation::IDENTIFIER); | ||
} catch(ExtraInformationNotFoundException $e) { | ||
return; | ||
} | ||
|
||
if( !$information instanceof NginxSnippetExtraInformation ) | ||
return; | ||
|
||
$snippets = $information->getSnippets(); | ||
if( empty($snippets) ) | ||
return; | ||
|
||
if( $information->isMountWorkdir() ) { | ||
|
||
$this->addSnippetsToService( $service, $snippets ); | ||
|
||
return; | ||
} | ||
|
||
$this->addSnippetsToImage( $dockerfile, $snippets ); | ||
|
||
} | ||
|
||
/** | ||
* @param $dockerfile | ||
* @param $snippets | ||
*/ | ||
protected function addSnippetsToImage( Dockerfile $dockerfile, $snippets ) { | ||
$dockerfile->addVolume( '/etc/nginx/server.d' ); | ||
foreach ( $snippets as $snippet ) | ||
$dockerfile->copy( $snippet, '/etc/nginx/server.d/' ); | ||
} | ||
|
||
/** | ||
* @param Service $service | ||
* @param $snippets | ||
*/ | ||
protected function addSnippetsToService( Service $service, $snippets ) { | ||
foreach ( $snippets as $snippet ) { | ||
$filename = basename( $snippet ); | ||
$service->addVolume( getcwd() . DIRECTORY_SEPARATOR . $snippet, '/etc/nginx/server.d/' . $filename ); | ||
|
||
} | ||
} | ||
} |
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,43 @@ | ||
<?php namespace Rancherize\Blueprint\NginxSnippets; | ||
|
||
use Rancherize\Blueprint\NginxSnippets\NginxSnippetInfrastructureEventHandler\NginxSnippetInfrastructureEventHandler; | ||
use Rancherize\Blueprint\NginxSnippets\NginxSnippetParser\NginxSnippetParser; | ||
use Rancherize\Blueprint\NginxSnippets\NginxSnippetService\NginxSnippetService; | ||
use Rancherize\Plugin\Provider; | ||
use Rancherize\Plugin\ProviderTrait; | ||
use Rancherize\Services\BuildServiceEvent\InfrastructureBuiltEvent; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
|
||
/** | ||
* Class NginxSnippetsProvider | ||
* @package Rancherize\Blueprint\NginxSnippets | ||
*/ | ||
class NginxSnippetsProvider implements Provider { | ||
|
||
use ProviderTrait; | ||
|
||
/** | ||
*/ | ||
public function register() { | ||
$this->container['nginx-snippets-parser'] = function($c) { | ||
return new NginxSnippetParser(); | ||
}; | ||
$this->container['nginx-snippet-service'] = function($c) { | ||
return new NginxSnippetService(); | ||
}; | ||
$this->container['nginx-infrastructure-built-listener'] = function($c) { | ||
return new NginxSnippetInfrastructureEventHandler($c['nginx-snippet-service']); | ||
}; | ||
} | ||
|
||
/** | ||
*/ | ||
public function boot() { | ||
/** | ||
* @var EventDispatcher $event | ||
*/ | ||
$event = $this->container['event']; | ||
$listener = $this->container['nginx-infrastructure-built-listener']; | ||
$event->addListener(InfrastructureBuiltEvent::NAME, [$listener, 'infrastructureBuilt']); | ||
} | ||
} |
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,3 @@ | ||
# NginxSnippets | ||
Parses `nginx.enable` and `nginx.snippets` via NginxSnippetParser and adds information to a service. | ||
Makes `/etc/nginx/server.d` a volume in the built app container and copies all files given in `nginx.snippets` into there |
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
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
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
41 changes: 41 additions & 0 deletions
41
app/Services/BuildServiceEvent/InfrastructureBuiltEvent.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,41 @@ | ||
<?php namespace Rancherize\Services\BuildServiceEvent; | ||
|
||
use Rancherize\Blueprint\Infrastructure\Infrastructure; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
/** | ||
* Class InfrastructureBuiltEvent | ||
* @package Rancherize\Services\BuildServiceEvent | ||
*/ | ||
class InfrastructureBuiltEvent extends Event { | ||
|
||
const NAME = 'build-service.infrastructure-built'; | ||
|
||
/** | ||
* InfrastructureBuiltEvent constructor. | ||
* @param Infrastructure $infrastructure | ||
*/ | ||
public function __construct( Infrastructure $infrastructure) { | ||
$this->infrastructure = $infrastructure; | ||
} | ||
|
||
/** | ||
* @var Infrastructure | ||
*/ | ||
protected $infrastructure; | ||
|
||
/** | ||
* @return Infrastructure | ||
*/ | ||
public function getInfrastructure(): Infrastructure { | ||
return $this->infrastructure; | ||
} | ||
|
||
/** | ||
* @param Infrastructure $infrastructure | ||
*/ | ||
public function setInfrastructure( Infrastructure $infrastructure ) { | ||
$this->infrastructure = $infrastructure; | ||
} | ||
|
||
} |
Oops, something went wrong.