From b6d3fa19e529fd6fc5b173757fca7aff0e293c85 Mon Sep 17 00:00:00 2001 From: Sven Speckmaier Date: Wed, 12 Jul 2017 17:47:02 +0200 Subject: [PATCH 1/7] extra nginx snippet information structure --- .../NginxSnippetExtraInformation.php | 35 ++++++++++++++ ...NginxSnippetInfrastructureEventHandler.php | 46 +++++++++++++++++++ .../NginxSnippetParser/NginxSnippetParser.php | 35 ++++++++++++++ .../NginxSnippets/NginxSnippetsProvider.php | 39 ++++++++++++++++ .../Webserver/WebserverBlueprint.php | 7 +++ app/Services/BuildService.php | 16 ++++++- .../InfrastructureBuiltEvent.php | 40 ++++++++++++++++ app/container.php | 2 +- 8 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 app/Blueprint/NginxSnippets/NginxSnippetExtraInformation/NginxSnippetExtraInformation.php create mode 100644 app/Blueprint/NginxSnippets/NginxSnippetInfrastructureEventHandler/NginxSnippetInfrastructureEventHandler.php create mode 100644 app/Blueprint/NginxSnippets/NginxSnippetParser/NginxSnippetParser.php create mode 100644 app/Blueprint/NginxSnippets/NginxSnippetsProvider.php create mode 100644 app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php diff --git a/app/Blueprint/NginxSnippets/NginxSnippetExtraInformation/NginxSnippetExtraInformation.php b/app/Blueprint/NginxSnippets/NginxSnippetExtraInformation/NginxSnippetExtraInformation.php new file mode 100644 index 0000000..03c229b --- /dev/null +++ b/app/Blueprint/NginxSnippets/NginxSnippetExtraInformation/NginxSnippetExtraInformation.php @@ -0,0 +1,35 @@ +snippets[$path] = $path; + } + + /** + * @return array + */ + public function getSnippets(): array { + return $this->snippets; + } +} \ No newline at end of file diff --git a/app/Blueprint/NginxSnippets/NginxSnippetInfrastructureEventHandler/NginxSnippetInfrastructureEventHandler.php b/app/Blueprint/NginxSnippets/NginxSnippetInfrastructureEventHandler/NginxSnippetInfrastructureEventHandler.php new file mode 100644 index 0000000..4839f57 --- /dev/null +++ b/app/Blueprint/NginxSnippets/NginxSnippetInfrastructureEventHandler/NginxSnippetInfrastructureEventHandler.php @@ -0,0 +1,46 @@ +getInfrastructure(); + + foreach($infrastructure->getServices() as $service) + $this->addService($service); + + $event->setInfrastructure($infrastructure); + } + + /** + * @param Service $service + */ + private function addService( Service $service ) { + try { + $information = $service->getExtraInformation(NginxSnippetExtraInformation::IDENTIFIER); + } catch(ExtraInformationNotFoundException $e) { + return; + } + + if( !$information instanceof NginxSnippetExtraInformation ) + return; + + $snippets = $information->getSnippets(); + if( empty($snippets) ) + return; + + foreach($snippets as $snippet) { + } + } +} \ No newline at end of file diff --git a/app/Blueprint/NginxSnippets/NginxSnippetParser/NginxSnippetParser.php b/app/Blueprint/NginxSnippets/NginxSnippetParser/NginxSnippetParser.php new file mode 100644 index 0000000..f757533 --- /dev/null +++ b/app/Blueprint/NginxSnippets/NginxSnippetParser/NginxSnippetParser.php @@ -0,0 +1,35 @@ +has('nginx.snippets') ) + return; + + if( !$configuration->get('nginx.enable', true) ) + return; + + $extraInformation = new NginxSnippetExtraInformation(); + $snippets = $configuration->get('nginx.snippets', []); + if( empty($snippets) ) + return; + + foreach( $snippets as $snippet ) + $extraInformation->addSnippet($snippet); + + $service->addExtraInformation( $extraInformation ); + } + +} \ No newline at end of file diff --git a/app/Blueprint/NginxSnippets/NginxSnippetsProvider.php b/app/Blueprint/NginxSnippets/NginxSnippetsProvider.php new file mode 100644 index 0000000..896b25a --- /dev/null +++ b/app/Blueprint/NginxSnippets/NginxSnippetsProvider.php @@ -0,0 +1,39 @@ +container['nginx-snippets-parser'] = function($c) { + return new NginxSnippetParser(); + }; + $this->container['nginx-infrastructure-built-listener'] = function($c) { + return new NginxSnippetInfrastructureEventHandler(); + }; + } + + /** + */ + public function boot() { + /** + * @var EventDispatcher $event + */ + $event = $this->container['event']; + $listener = $this->container['nginx-infrastructure-built-listener']; + $event->addListener(InfrastructureBuiltEvent::NAME, [$listener, 'infrastructureBuilt']); + } +} \ No newline at end of file diff --git a/app/Blueprint/Webserver/WebserverBlueprint.php b/app/Blueprint/Webserver/WebserverBlueprint.php index 643ab57..89f7f05 100644 --- a/app/Blueprint/Webserver/WebserverBlueprint.php +++ b/app/Blueprint/Webserver/WebserverBlueprint.php @@ -17,6 +17,7 @@ use Rancherize\Blueprint\Infrastructure\Service\Services\LaravelQueueWorker; use Rancherize\Blueprint\Infrastructure\Service\Services\PmaService; use Rancherize\Blueprint\Infrastructure\Service\Services\RedisService; +use Rancherize\Blueprint\NginxSnippets\NginxSnippetParser\NginxSnippetParser; use Rancherize\Blueprint\PublishUrls\PublishUrlsIniter\PublishUrlsInitializer; use Rancherize\Blueprint\PublishUrls\PublishUrlsParser\PublishUrlsParser; use Rancherize\Blueprint\Scheduler\SchedulerInitializer\SchedulerInitializer; @@ -215,6 +216,12 @@ public function build(Configuration $configuration, string $environment, string $externalServicesParser = container('external-service-parser'); $externalServicesParser->parse($config, $infrastructure); + /** + * @var NginxSnippetParser $nginxSnippetParser + */ + $nginxSnippetParser = container('nginx-snippets-parser'); + $nginxSnippetParser->parse( $serverService, $config ); + /** * @var CronParser $cronParser */ diff --git a/app/Services/BuildService.php b/app/Services/BuildService.php index a25896a..f1a0e09 100644 --- a/app/Services/BuildService.php +++ b/app/Services/BuildService.php @@ -5,7 +5,8 @@ use Rancherize\Configuration\Configuration; use Rancherize\Configuration\Traits\LoadsConfigurationTrait; use Rancherize\File\FileWriter; -use Symfony\Component\Console\Input\InputInterface; +use Rancherize\Services\BuildServiceEvent\InfrastructureBuiltEvent; +use Symfony\Component\EventDispatcher\EventDispatcher; /** * Class BuildService @@ -30,15 +31,22 @@ class BuildService { * @var InfrastructureWriter */ private $infrastructureWriter; + /** + * @var EventDispatcher + */ + private $eventDispatcher; /** * BuildService constructor. * @param ValidateService $validateService * @param InfrastructureWriter $infrastructureWriter + * @param EventDispatcher $eventDispatcher */ - public function __construct(ValidateService $validateService, InfrastructureWriter $infrastructureWriter) { + public function __construct(ValidateService $validateService, InfrastructureWriter $infrastructureWriter, + EventDispatcher $eventDispatcher) { $this->validateService = $validateService; $this->infrastructureWriter = $infrastructureWriter; + $this->eventDispatcher = $eventDispatcher; } /** @@ -55,6 +63,10 @@ public function build(Blueprint $blueprint, Configuration $configuration, string $this->validateService->validate($blueprint, $configuration, $environment); $infrastructure = $blueprint->build($configuration, $environment, $this->version); + $infrastructureBuiltEvent = new InfrastructureBuiltEvent($infrastructure); + $this->eventDispatcher->dispatch(InfrastructureBuiltEvent::NAME, $infrastructureBuiltEvent); + $infrastructure = $infrastructureBuiltEvent->getInfrastructure(); + $infrastructureWriter = $this->infrastructureWriter; $infrastructureWriter->setPath($directory); $infrastructureWriter->setSkipClear($skipClear); diff --git a/app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php b/app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php new file mode 100644 index 0000000..82a8ac0 --- /dev/null +++ b/app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php @@ -0,0 +1,40 @@ +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; + } + +} \ No newline at end of file diff --git a/app/container.php b/app/container.php index 8fcb8bb..de4d57f 100644 --- a/app/container.php +++ b/app/container.php @@ -90,7 +90,7 @@ }; $container['build-service'] = function($c) { - return new \Rancherize\Services\BuildService($c['validate-service'], $c['infrastructure-writer']); + return new \Rancherize\Services\BuildService($c['validate-service'], $c['infrastructure-writer'], $c['event']); }; $container['docker-service'] = function($c) { From 7405e379211abd51f4c320a4c558476c928149ee Mon Sep 17 00:00:00 2001 From: Sven Speckmaier Date: Thu, 13 Jul 2017 09:22:02 +0200 Subject: [PATCH 2/7] setting volume and adding copies for snippets --- ...NginxSnippetInfrastructureEventHandler.php | 39 +++++++------------ .../NginxSnippetService.php | 38 ++++++++++++++++++ .../NginxSnippets/NginxSnippetsProvider.php | 6 ++- 3 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 app/Blueprint/NginxSnippets/NginxSnippetService/NginxSnippetService.php diff --git a/app/Blueprint/NginxSnippets/NginxSnippetInfrastructureEventHandler/NginxSnippetInfrastructureEventHandler.php b/app/Blueprint/NginxSnippets/NginxSnippetInfrastructureEventHandler/NginxSnippetInfrastructureEventHandler.php index 4839f57..767f60c 100644 --- a/app/Blueprint/NginxSnippets/NginxSnippetInfrastructureEventHandler/NginxSnippetInfrastructureEventHandler.php +++ b/app/Blueprint/NginxSnippets/NginxSnippetInfrastructureEventHandler/NginxSnippetInfrastructureEventHandler.php @@ -1,8 +1,6 @@ snippetService = $snippetService; + } /** * @param InfrastructureBuiltEvent $event @@ -18,29 +28,8 @@ public function infrastructureBuilt( InfrastructureBuiltEvent $event ) { $infrastructure = $event->getInfrastructure(); foreach($infrastructure->getServices() as $service) - $this->addService($service); + $this->snippetService->addToInfrastructure($infrastructure, $service); $event->setInfrastructure($infrastructure); } - - /** - * @param Service $service - */ - private function addService( Service $service ) { - try { - $information = $service->getExtraInformation(NginxSnippetExtraInformation::IDENTIFIER); - } catch(ExtraInformationNotFoundException $e) { - return; - } - - if( !$information instanceof NginxSnippetExtraInformation ) - return; - - $snippets = $information->getSnippets(); - if( empty($snippets) ) - return; - - foreach($snippets as $snippet) { - } - } } \ No newline at end of file diff --git a/app/Blueprint/NginxSnippets/NginxSnippetService/NginxSnippetService.php b/app/Blueprint/NginxSnippets/NginxSnippetService/NginxSnippetService.php new file mode 100644 index 0000000..b27ad55 --- /dev/null +++ b/app/Blueprint/NginxSnippets/NginxSnippetService/NginxSnippetService.php @@ -0,0 +1,38 @@ +getDockerfile(); + + try { + $information = $service->getExtraInformation(NginxSnippetExtraInformation::IDENTIFIER); + } catch(ExtraInformationNotFoundException $e) { + return; + } + + if( !$information instanceof NginxSnippetExtraInformation ) + return; + + $snippets = $information->getSnippets(); + if( empty($snippets) ) + return; + + $dockerfile->addVolume('/etc/nginx/server.d'); + foreach($snippets as $snippet) + $dockerfile->copy($snippet, '/etc/nginx/server.d/'); + } +} \ No newline at end of file diff --git a/app/Blueprint/NginxSnippets/NginxSnippetsProvider.php b/app/Blueprint/NginxSnippets/NginxSnippetsProvider.php index 896b25a..ce1b223 100644 --- a/app/Blueprint/NginxSnippets/NginxSnippetsProvider.php +++ b/app/Blueprint/NginxSnippets/NginxSnippetsProvider.php @@ -2,6 +2,7 @@ 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; @@ -21,8 +22,11 @@ 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(); + return new NginxSnippetInfrastructureEventHandler($c['nginx-snippet-service']); }; } From cd2fce859615184d0e313fc2a8b7623e0ff5f71c Mon Sep 17 00:00:00 2001 From: Sven Speckmaier Date: Thu, 13 Jul 2017 09:32:08 +0200 Subject: [PATCH 3/7] added snippets provider to the plugin list --- app/lists/plugins.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/lists/plugins.php b/app/lists/plugins.php index 5be0325..cab09f2 100644 --- a/app/lists/plugins.php +++ b/app/lists/plugins.php @@ -6,4 +6,5 @@ 'Rancherize\Blueprint\Scheduler\SchedulerProvider', 'Rancherize\Blueprint\ExternalService\ExternalServiceProvider', 'Rancherize\Blueprint\Cron\CronProvider', + 'Rancherize\Blueprint\NginxSnippets\NginxSnippetsProvider', ]; From af619019089f6961e877f2b3a9bb580c7da36b4e Mon Sep 17 00:00:00 2001 From: Sven Speckmaier Date: Thu, 13 Jul 2017 09:50:31 +0200 Subject: [PATCH 4/7] added missing extends Event --- app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php b/app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php index 82a8ac0..0bc2849 100644 --- a/app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php +++ b/app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php @@ -1,12 +1,13 @@ Date: Thu, 13 Jul 2017 10:20:12 +0200 Subject: [PATCH 5/7] bounced up nginx image versions to 1.3.0 --- .../Infrastructure/Service/Services/NginxService.php | 2 +- app/Blueprint/Webserver/WebserverBlueprint.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Blueprint/Infrastructure/Service/Services/NginxService.php b/app/Blueprint/Infrastructure/Service/Services/NginxService.php index e9b7c11..32c92ee 100644 --- a/app/Blueprint/Infrastructure/Service/Services/NginxService.php +++ b/app/Blueprint/Infrastructure/Service/Services/NginxService.php @@ -11,7 +11,7 @@ class NginxService extends Service { * NginxService constructor. */ public function __construct() { - $this->setImage('ipunktbs/nginx:1.9.7-7-1.2.0'); + $this->setImage('ipunktbs/nginx:1.10.2-7-1.3.0'); } } \ No newline at end of file diff --git a/app/Blueprint/Webserver/WebserverBlueprint.php b/app/Blueprint/Webserver/WebserverBlueprint.php index 89f7f05..a94c5c3 100644 --- a/app/Blueprint/Webserver/WebserverBlueprint.php +++ b/app/Blueprint/Webserver/WebserverBlueprint.php @@ -303,9 +303,9 @@ private function addAll(array $configs, string $label, Closure $closure) { protected function makeServerService(Configuration $config, Configuration $default) : Service { $serverService = new Service(); $serverService->setName($config->get('service-name')); - $serverService->setImage($config->get('docker.image', 'ipunktbs/nginx:1.9.7-7-1.2.11')); + $serverService->setImage($config->get('docker.image', 'ipunktbs/nginx:1.10.2-7-1.3.0')); if( $config->get('debug-image', false) ) - $serverService->setImage($config->get('docker.image', 'ipunktbs/nginx-debug:debug-1.2.11')); + $serverService->setImage($config->get('docker.image', 'ipunktbs/nginx-debug:debug-1.3.0')); if( $config->get('sync-user-into-container', false) ) { $serverService->setEnvironmentVariable('USER_ID', getmyuid()); From 454428bc063b6cd38294f0c07a3386e857307bb4 Mon Sep 17 00:00:00 2001 From: Sven Speckmaier Date: Thu, 13 Jul 2017 10:43:00 +0200 Subject: [PATCH 6/7] added documentation --- app/Blueprint/NginxSnippets/README.md | 3 +++ app/Blueprint/Webserver/README.md | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 app/Blueprint/NginxSnippets/README.md diff --git a/app/Blueprint/NginxSnippets/README.md b/app/Blueprint/NginxSnippets/README.md new file mode 100644 index 0000000..5f25937 --- /dev/null +++ b/app/Blueprint/NginxSnippets/README.md @@ -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 diff --git a/app/Blueprint/Webserver/README.md b/app/Blueprint/Webserver/README.md index 8f7bf72..80040fd 100644 --- a/app/Blueprint/Webserver/README.md +++ b/app/Blueprint/Webserver/README.md @@ -42,6 +42,8 @@ This blueprint creates infrastructures to support apps using php7. |`work-sub-directory` | '' | Appended to `.` / `getcwd()` as source directory for mounting / copying to the image | |`target-sub-directory` | '' | Appended to `/var/www/app` as target directory for mounting / copying to the image | |`extra-files` | [] | A list of pathes relative to the project root. All files will be added to /opt/custom/ | +|`nginx.enable` | true | Can be set to `false` to disable adding the config snippets without removing their configuration | +|`nginx.snippets` | [] | List of files relative to the project root to be added to /etc/nginx/server.d. File ending with `.conf` will be included by the default nginx server config | #### Additional services From fd934fe2592e5147060ef00d21f90066bcabbc60 Mon Sep 17 00:00:00 2001 From: Sven Speckmaier Date: Thu, 13 Jul 2017 10:57:07 +0200 Subject: [PATCH 7/7] added mount-workdir support for nginx snippets --- .../NginxSnippetExtraInformation.php | 22 ++++++++++++ .../NginxSnippetParser/NginxSnippetParser.php | 3 ++ .../NginxSnippetService.php | 35 +++++++++++++++++-- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/app/Blueprint/NginxSnippets/NginxSnippetExtraInformation/NginxSnippetExtraInformation.php b/app/Blueprint/NginxSnippets/NginxSnippetExtraInformation/NginxSnippetExtraInformation.php index 03c229b..54139c0 100644 --- a/app/Blueprint/NginxSnippets/NginxSnippetExtraInformation/NginxSnippetExtraInformation.php +++ b/app/Blueprint/NginxSnippets/NginxSnippetExtraInformation/NginxSnippetExtraInformation.php @@ -10,8 +10,16 @@ class NginxSnippetExtraInformation implements ServiceExtraInformation { const IDENTIFIER = 'nginx-snippet'; + /** + * @var array + */ protected $snippets = []; + /** + * @var bool + */ + protected $mountWorkdir = false; + /** * @return mixed */ @@ -32,4 +40,18 @@ public function addSnippet( $path ) { 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; + } } \ No newline at end of file diff --git a/app/Blueprint/NginxSnippets/NginxSnippetParser/NginxSnippetParser.php b/app/Blueprint/NginxSnippets/NginxSnippetParser/NginxSnippetParser.php index f757533..171692f 100644 --- a/app/Blueprint/NginxSnippets/NginxSnippetParser/NginxSnippetParser.php +++ b/app/Blueprint/NginxSnippets/NginxSnippetParser/NginxSnippetParser.php @@ -26,6 +26,9 @@ public function parse( Service $service, Configuration $configuration ) { if( empty($snippets) ) return; + if( $configuration->get('mount-workdir', false) ) + $extraInformation->setMountWorkdir(true); + foreach( $snippets as $snippet ) $extraInformation->addSnippet($snippet); diff --git a/app/Blueprint/NginxSnippets/NginxSnippetService/NginxSnippetService.php b/app/Blueprint/NginxSnippets/NginxSnippetService/NginxSnippetService.php index b27ad55..4efefb9 100644 --- a/app/Blueprint/NginxSnippets/NginxSnippetService/NginxSnippetService.php +++ b/app/Blueprint/NginxSnippets/NginxSnippetService/NginxSnippetService.php @@ -1,5 +1,6 @@ addVolume('/etc/nginx/server.d'); - foreach($snippets as $snippet) - $dockerfile->copy($snippet, '/etc/nginx/server.d/'); + 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 ); + + } } } \ No newline at end of file