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 #39 from ipunkt/php-advanced-config
Browse files Browse the repository at this point in the history
Php advanced config
  • Loading branch information
Sven Speckmaier authored Sep 14, 2017
2 parents 377ddd8 + 5b563a3 commit d4939c7
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm;

/**
* Interface DefaultTimezone
* @package Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm
*
* Possibly implemented by a PhpVersion.
* Sets the default timezone used by php
*/
interface DefaultTimezone {

const DEFAULT_TIMEZONE = 'default';

/**
* Set the default php timezone
*
* @param $defaultTimezone
* @return $this
*/
function setDefaultTimezone( $defaultTimezone );

}
16 changes: 16 additions & 0 deletions app/Blueprint/Infrastructure/Service/Maker/PhpFpm/MemoryLimit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm;

/**
* Interface MemoryLimit
* @package Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm
*/
interface MemoryLimit {

const DEFAULT_MEMORY_LIMIT = 'default';

/**
* @return $this
*/
function setMemoryLimit($limit);

}
30 changes: 30 additions & 0 deletions app/Blueprint/Infrastructure/Service/Maker/PhpFpm/PhpFpmMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use Rancherize\Blueprint\Infrastructure\Infrastructure;
use Rancherize\Blueprint\Infrastructure\Service\Service;
use Rancherize\Configuration\Configuration;
use Rancherize\Configuration\PrefixConfigurationDecorator;

/**
* Class PhpFpmMaker
Expand Down Expand Up @@ -105,12 +106,41 @@ protected function getPhpVersion( Configuration $config ): PhpVersion {

$phpVersionString = $config->get( 'php', '7.0' );

$advancedConfig = is_array($phpVersionString);
if( $advancedConfig )
$phpVersionString = $config->get('php.version', '7.0');

if ( !array_key_exists( $phpVersionString, $this->phpVersions ) )
throw new PhpVersionNotAvailableException( $phpVersionString );

$phpVersion = $this->phpVersions[$phpVersionString];

$this->setAppSource( $phpVersion );
$this->setConfig($phpVersion, $config);

return $phpVersion;
}

/**
* @param PhpVersion $phpVersion
* @param $config
*/
private function setConfig( PhpVersion $phpVersion, Configuration $config ) {

if( !is_array($config->get('php') ) )
return;

$phpConfig = new PrefixConfigurationDecorator($config, 'php.');
if( $phpVersion instanceof MemoryLimit && $phpConfig->has('memory-limit') )
$phpVersion->setMemoryLimit( $phpConfig->get('memory-limit') );

if( $phpVersion instanceof PostLimit && $phpConfig->has('post-limit') )
$phpVersion->setPostLimit( $phpConfig->get('post-limit') );

if( $phpVersion instanceof UploadFileLimit && $phpConfig->has('upload-file-limit') )
$phpVersion->setUploadFileLimit( $phpConfig->get('upload-file-limit') );

if( $phpVersion instanceof DefaultTimezone && $phpConfig->has('default-timezone') )
$phpVersion->setDefaultTimezone( $phpConfig->get('default-timezone') );
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\PhpVersions;
use Rancherize\Blueprint\Infrastructure\Infrastructure;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\DefaultTimezone;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\MemoryLimit;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\PhpVersion;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\PostLimit;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\UploadFileLimit;
use Rancherize\Blueprint\Infrastructure\Service\Service;
use Rancherize\Configuration\Configuration;

/**
* Class PHP53
* @package Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\PhpVersions
*/
class PHP53 implements PhpVersion {
class PHP53 implements PhpVersion, MemoryLimit, PostLimit, UploadFileLimit, DefaultTimezone {

const PHP_IMAGE = 'ipunktbs/php-fpm:53-1.0.7';
const PHP_IMAGE = 'ipunktbs/php-fpm:53-1.2.0';

/**
* @var string|Service
*/
protected $appTarget;

/**
* @var string
*/
protected $memoryLimit = self::DEFAULT_MEMORY_LIMIT;

/**
* @var string
*/
protected $postLimit = self::DEFAULT_POST_LIMIT;

/**
* @var string
*/
protected $uploadFileLimit = self::DEFAULT_UPLOAD_FILE_LIMIT;

/**
* @var string
*/
protected $defaultTimezone = self::DEFAULT_TIMEZONE;

/**
* @param Configuration $config
* @param Service $mainService
Expand All @@ -33,6 +57,19 @@ public function make(Configuration $config, Service $mainService, Infrastructure
$phpFpmService->setImage( self::PHP_IMAGE );
$phpFpmService->setRestart(Service::RESTART_UNLESS_STOPPED);

$memoryLimit = $this->memoryLimit;
if( $memoryLimit !== self::DEFAULT_MEMORY_LIMIT )
$phpFpmService->setEnvironmentVariable('PHP_MEMORY_LIMIT', $memoryLimit);
$postLimit = $this->postLimit;
if( $postLimit !== self::DEFAULT_POST_LIMIT )
$phpFpmService->setEnvironmentVariable('PHP_POST_MAX_SIZE', $postLimit);
$uploadFileLimit = $this->uploadFileLimit;
if( $uploadFileLimit !== self::DEFAULT_UPLOAD_FILE_LIMIT )
$phpFpmService->setEnvironmentVariable('PHP_UPLOAD_MAX_FILESIZE', $uploadFileLimit);
$defaultTimezone = $this->defaultTimezone;
if( $defaultTimezone !== self::DEFAULT_TIMEZONE)
$phpFpmService->setEnvironmentVariable('DEFAULT_TIMEZONE', $defaultTimezone);

$this->addAppSource($phpFpmService);

/**
Expand All @@ -41,6 +78,11 @@ public function make(Configuration $config, Service $mainService, Infrastructure
foreach( $mainService->getEnvironmentVariables() as $name => $value )
$phpFpmService->setEnvironmentVariable($name, $value);

/**
* Copy links from the main service so databases etc are available
*/
$phpFpmService->addLinksFrom($mainService);

$mainService->addSidekick($phpFpmService);
$mainService->addVolumeFrom($phpFpmService);
$infrastructure->addService($phpFpmService);
Expand Down Expand Up @@ -111,4 +153,39 @@ public function makeCommand( $commandName, $command, Service $mainService ) {
$mainService->addSidekick($phpCommandService);
return $phpCommandService;
}

/**
* @return $this
*/
public function setMemoryLimit( $limit ) {
$this->memoryLimit = $limit;
return $this;
}

/**
* @return $this
*/
public function setPostLimit( $limit ) {
$this->postLimit = $limit;
return $this;
}

/**
* @return $this
*/
public function setUploadFileLimit( $limit ) {
$this->uploadFileLimit = $limit;
return $this;
}

/**
* Set the default php timezone
*
* @param $defaultTimezone
* @return $this
*/
public function setDefaultTimezone( $defaultTimezone ) {
$this->defaultTimezone = $defaultTimezone;
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\PhpVersions;
use Rancherize\Blueprint\Infrastructure\Infrastructure;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\MemoryLimit;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\PhpVersion;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\PostLimit;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\UploadFileLimit;
use Rancherize\Blueprint\Infrastructure\Service\Service;
use Rancherize\Configuration\Configuration;

/**
* Class PHP70
* @package Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\PhpVersions
*/
class PHP70 implements PhpVersion {
class PHP70 implements PhpVersion, MemoryLimit, PostLimit, UploadFileLimit {

public function make(Configuration $config, Service $mainService, Infrastructure $infrastructure) {
/**
* @var string
*/
private $memoryLimit = self::DEFAULT_MEMORY_LIMIT;

/**
* @var string
*/
private $uploadFileLimit = self::DEFAULT_UPLOAD_FILE_LIMIT;

/**
* @var string
*/
private $postLimit = self::DEFAULT_POST_LIMIT;

public function make( Configuration $config, Service $mainService, Infrastructure $infrastructure) {
/**
* PHP7.0 is started by default, no fpm service needs to be added
*/

$memoryLimit = $this->memoryLimit;
if( $memoryLimit !== self::DEFAULT_MEMORY_LIMIT)
$mainService->setEnvironmentVariable('PHP_MEMORY_LIMIT', $memoryLimit);

$postLimit = $this->postLimit;
if( $postLimit !== self::DEFAULT_POST_LIMIT)
$mainService->setEnvironmentVariable('PHP_POST_MAX_SIZE', $postLimit);

$uploadFileLimit = $this->uploadFileLimit;
if( $uploadFileLimit !== self::DEFAULT_UPLOAD_FILE_LIMIT)
$mainService->setEnvironmentVariable('PHP_UPLOAD_MAX_FILESIZE', $uploadFileLimit);
}

/**
Expand Down Expand Up @@ -55,4 +85,28 @@ public function setAppService(Service $appService) {
public function makeCommand( $commandName, $command, Service $mainService) {
die('Error: PHP Commands not Yet implemented for PHP7');
}

/**
* @return $this
*/
public function setMemoryLimit( $limit ) {
$this->memoryLimit = $limit;
return $this;
}

/**
* @return $this
*/
public function setPostLimit( $limit ) {
$this->postLimit = $limit;
return $this;
}

/**
* @return $this
*/
public function setUploadFileLimit( $limit ) {
$this->uploadFileLimit = $limit;
return $this;
}
}
15 changes: 15 additions & 0 deletions app/Blueprint/Infrastructure/Service/Maker/PhpFpm/PostLimit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm;

/**
* Interface PostLimit
* @package Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm
*/
interface PostLimit {

const DEFAULT_POST_LIMIT = 'default';

/**
* @return $this
*/
function setPostLimit($limit);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm;

/**
* Interface UploadFileLimit
* @package Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm
*/
interface UploadFileLimit {

const DEFAULT_UPLOAD_FILE_LIMIT = 'default';

/**
* @return $this
*/
function setUploadFileLimit($limit);

}
7 changes: 6 additions & 1 deletion app/Blueprint/Webserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ This blueprint creates infrastructures to support apps using php7.

| Option | Defaults to | Explanation |
| ------- |:-----------:| ------------ |
|`php`| `7.0` | Add php fpm to the nginx. The default version `7.0` does not start an extra service. Other fpm versions are run in their own container and will be a sidekick inside the nginx service. Available Versions: `7.0`, `5.3` |
|`php`| `7.0` | Legacy version of php.version |
|`php.version`| `7.0` | Add php fpm to the nginx. The default version `7.0` does not start an extra service. Other fpm versions are run in their own container and will be a sidekick inside the nginx service. Available Versions: `7.0`, `5.3` |
|`php.memory-limit`| `1024M` | |
|`php.post-limit`| `8M` | PHP memory limit option |
|`php.upload-file-limit`| `2M` | PHP post_max_size option |
|`php.default-timezone`| `UTC` | PHP date.timezone option |
|`queues`| [] | Add Laravel Queue Worker, providing their name and connection in `name` and `connection`. Example: `"queues":[{"connection": "redis","name": "default"}],` |
|`add-redis`| false | Add a Redis server and link it to the main app, providing its name and port in `REDIS_HOST` and `REDIS_PORT` |

Expand Down
4 changes: 3 additions & 1 deletion app/Blueprint/Webserver/WebserverBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ public function init(Configurable $configurable, string $environment, InputInter
$cronInitializer->init( $fallbackConfigurable, $initializer );
}

$initializer->init($fallbackConfigurable, 'php', "7.0");
$initializer->init($fallbackConfigurable, 'php', [
'version' => "7.0"
]);
$initializer->init($fallbackConfigurable, 'queues', []);
$initializer->init($fallbackConfigurable, 'docker.repository', 'repo/name', $projectConfigurable);
$initializer->init($fallbackConfigurable, 'docker.version-prefix', '', $projectConfigurable);
Expand Down

0 comments on commit d4939c7

Please sign in to comment.