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

Commit

Permalink
rancher cli mode, and activate if resource-limit set
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Speckmaier committed Sep 25, 2018
1 parent 0e43e16 commit 2233a7d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 19 deletions.
52 changes: 37 additions & 15 deletions app/Blueprint/ResourceLimit/EventListener/ServiceWriteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@
use Rancherize\Blueprint\Infrastructure\Service\Events\ServiceWriterServicePreparedEvent;
use Rancherize\Blueprint\Infrastructure\Service\ExtraInformationNotFoundException;
use Rancherize\Blueprint\ResourceLimit\ExtraInformation\ExtraInformation as ResourceLimitExtraInformation;
use Rancherize\RancherAccess\RancherService;

/**
* Class ServiceWriteListener
* @package Rancherize\Blueprint\ResourceLimit\EventListener
*/
class ServiceWriteListener {
/**
* @var RancherService
*/
private $rancherService;

/**
* ServiceWriteListener constructor.
* @param RancherService $rancherService
*/

public function __construct( RancherService $rancherService ) {
$this->rancherService = $rancherService;
}

/**
* @param ServiceWriterServicePreparedEvent $event
Expand All @@ -22,15 +36,17 @@ public function writeService( ServiceWriterServicePreparedEvent $event ) {
$service = $event->getService();
try {
$extraInformation = $service->getExtraInformation( ResourceLimitExtraInformation::IDENTIFIER );
} catch ( ExtraInformationNotFoundException $e ) {
} catch (ExtraInformationNotFoundException $e) {
return;
}

if ( !$extraInformation instanceof ResourceLimitExtraInformation )
return;

$this->rancherService->setCliMode( true );

$rancherData = $this->addCpuReservation( $rancherData, $extraInformation );
$dockerData = $this->addCpuLImit( $dockerData, $extraInformation );
$dockerData = $this->addCpuLimit( $dockerData, $extraInformation );
$dockerData = $this->addMemoryLimit( $dockerData, $extraInformation );
$dockerData = $this->addMemoryReservation( $dockerData, $extraInformation );

Expand All @@ -43,13 +59,13 @@ public function writeService( ServiceWriterServicePreparedEvent $event ) {
* @param ResourceLimitExtraInformation $extraInformation
* @return mixed
*/
private function addCpuLImit( $dockerData, ResourceLimitExtraInformation $extraInformation ) {
private function addCpuLimit( $dockerData, ResourceLimitExtraInformation $extraInformation ) {

if ( $extraInformation->getCpuPeriod() === null || $extraInformation->getCpuQuota() === null )
return $dockerData;

$dockerData['cpu_period'] = $extraInformation->getCpuPeriod();
$dockerData['cpu_quota'] = $extraInformation->getCpuQuota();
$dockerData[ 'cpu_period' ] = $extraInformation->getCpuPeriod();
$dockerData[ 'cpu_quota' ] = $extraInformation->getCpuQuota();

return $dockerData;
}
Expand All @@ -66,25 +82,28 @@ private function addMemoryLimit( $dockerData, ResourceLimitExtraInformation $ext

$memory = $extraInformation->getMemoryLimit();
preg_match( '~(\d+)([gGmM]?)~', $memory, $matches );
$memory = (int)$matches[1];
$modifier = $matches[2];
switch ( $modifier ) {
$memory = (int)$matches[ 1 ];
$modifier = $matches[ 2 ];
switch ($modifier) {
case 'g':
/** @noinspection PhpMissingBreakStatementInspection */
case 'G':
$memory *= 1024;

case 'm':
/** @noinspection PhpMissingBreakStatementInspection */
case 'M':
$memory *= 1024;

case 'k':
/** @noinspection PhpMissingBreakStatementInspection */
case 'K':
$memory *= 1024;

default:
break;
}
$dockerData['mem_limit'] = $memory;
$dockerData[ 'mem_limit' ] = $memory;

return $dockerData;
}
Expand All @@ -99,37 +118,40 @@ private function addCpuReservation( array $rancherData, ResourceLimitExtraInform
if ( $extraInformation->getCpuReservation() === null )
return $rancherData;

$rancherData['milli_cpu_reservation'] = $extraInformation->getCpuReservation();
$rancherData[ 'milli_cpu_reservation' ] = $extraInformation->getCpuReservation();

return $rancherData;
}

private function addMemoryReservation( $dockerData, ResourceLimitExtraInformation $extraInformation ) {

if( $extraInformation->getMemoryReservation() === null)
if ( $extraInformation->getMemoryReservation() === null )
return $dockerData;

$memory = $extraInformation->getMemoryReservation();
preg_match( '~(\d+)([gGmM]?)~', $memory, $matches );
$memory = (int)$matches[1];
$modifier = $matches[2];
switch ( $modifier ) {
$memory = (int)$matches[ 1 ];
$modifier = $matches[ 2 ];
switch ($modifier) {
case 'g':
/** @noinspection PhpMissingBreakStatementInspection */
case 'G':
$memory *= 1024;

case 'm':
/** @noinspection PhpMissingBreakStatementInspection */
case 'M':
$memory *= 1024;

case 'k':
/** @noinspection PhpMissingBreakStatementInspection */
case 'K':
$memory *= 1024;

default:
break;
}
$dockerData['mem_reservation'] = $memory;
$dockerData[ 'mem_reservation' ] = $memory;

return $dockerData;
}
Expand Down
5 changes: 3 additions & 2 deletions app/Blueprint/ResourceLimit/ResourceLimitProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rancherize\Blueprint\ResourceLimit\Parser\Parser;
use Rancherize\Plugin\Provider;
use Rancherize\Plugin\ProviderTrait;
use Rancherize\RancherAccess\RancherService;
use Symfony\Component\EventDispatcher\EventDispatcher;

/**
Expand Down Expand Up @@ -95,8 +96,8 @@ public function register() {
return new Parser( $c[CpuLimitModeFactory::class], $c[MemLimitModeFactory::class] );
};

$this->container[ServiceWriteListener::class] = function () {
return new ServiceWriteListener();
$this->container[ServiceWriteListener::class] = function ($c) {
return new ServiceWriteListener( $c[RancherService::class] );
};
$this->container[MainServiceBuiltListener::class] = function ( $c ) {
return new MainServiceBuiltListener( $c[Parser::class] );
Expand Down
10 changes: 9 additions & 1 deletion app/RancherAccess/ArrayRancherAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Provides RancherAccount using an array with the keys Key -> `key` and Secret -> `secret`
*/
class ArrayRancherAccount implements RancherAccount {
class ArrayRancherAccount implements RancherAccount, RancherCliAccount {
/**
* @var array
*/
Expand Down Expand Up @@ -68,4 +68,12 @@ public function getRancherCompose() : string {
}


/**
* returns the name of the rancher exectuable, e.g. `rancher`
*
* @return string
*/
public function getCliVersion() {
return $this->get('rancher-cli', 'rancher');
}
}
17 changes: 17 additions & 0 deletions app/RancherAccess/RancherCliAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace Rancherize\RancherAccess;

/**
* Interface RancherCliAccount
* @package Rancherize\RancherAccess
*/
interface RancherCliAccount {


/**
* returns the name of the rancher exectuable, e.g. `rancher`
*
* @return string
*/
function getCliVersion();

}
25 changes: 24 additions & 1 deletion app/RancherAccess/RancherService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class RancherService {
*/
private $byNameService;

/**
* @var bool
*/
private $cliMode = false;

use ProcessTrait;


Expand Down Expand Up @@ -214,7 +219,11 @@ public function start(string $directory, string $stackName, array $serviceNames
$serviceNames = [$serviceNames];

$url = $this->getUrl();
$command = [ $this->account->getRancherCompose(), "-f", "$directory/docker-compose.yml", '-r', "$directory/rancher-compose.yml", '-p', $stackName, 'up', '-d' ];
$account = $this->account;
if ($this->cliMode && $account instanceof RancherCliAccount)
$command = [ $account->getCliVersion(), "-f", "$directory/docker-compose.yml", '--rancher-file', "$directory/rancher-compose.yml", '-s', $stackName, 'up', '-d' ];
else
$command = [ $account->getRancherCompose(), "-f", "$directory/docker-compose.yml", '-r', "$directory/rancher-compose.yml", '-p', $stackName, 'up', '-d' ];

if($upgrade)
$command = array_merge($command, ['--upgrade']);
Expand Down Expand Up @@ -551,4 +560,18 @@ public function create( $directory, $stackName, $serviceNames ) {
if( $ran->getExitCode() != 0 )
throw new CreateFailedException("Command ".$ran->getCommandLine());
}

/**
* @return bool
*/
public function isCliMode(): bool {
return $this->cliMode;
}

/**
* @param bool $cliMode
*/
public function setCliMode( bool $cliMode ) {
$this->cliMode = $cliMode;
}
}

0 comments on commit 2233a7d

Please sign in to comment.