Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from ipunkt/nginx-snippets
Browse files Browse the repository at this point in the history
Nginx snippets
  • Loading branch information
Sven Speckmaier authored Jul 13, 2017
2 parents 27658c0 + fd934fe commit e99592a
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}
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;
}
}
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);
}
}
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 );
}

}
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 );

}
}
}
43 changes: 43 additions & 0 deletions app/Blueprint/NginxSnippets/NginxSnippetsProvider.php
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']);
}
}
3 changes: 3 additions & 0 deletions app/Blueprint/NginxSnippets/README.md
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
2 changes: 2 additions & 0 deletions app/Blueprint/Webserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions app/Blueprint/Webserver/WebserverBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -296,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());
Expand Down
16 changes: 14 additions & 2 deletions app/Services/BuildService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand Down
41 changes: 41 additions & 0 deletions app/Services/BuildServiceEvent/InfrastructureBuiltEvent.php
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;
}

}
Loading

0 comments on commit e99592a

Please sign in to comment.