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 #7 from ipunkt/scheduling
Scheduling
- Loading branch information
Showing
11 changed files
with
491 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
app/Blueprint/Scheduler/EventListener/SchedulerServiceWriterListener.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,47 @@ | ||
<?php namespace Rancherize\Blueprint\Scheduler\EventListener; | ||
|
||
use Rancherize\Blueprint\Infrastructure\Service\Events\ServiceWriterServicePreparedEvent; | ||
use Rancherize\Blueprint\Infrastructure\Service\ExtraInformationNotFoundException; | ||
use Rancherize\Blueprint\Scheduler\SchedulerExtraInformation\SchedulerExtraInformation; | ||
use Rancherize\Blueprint\Scheduler\SchedulerYamlWriter\SchedulerYamlWriter; | ||
|
||
/** | ||
* Class HealthcheckServiceWriterListener | ||
* @package Rancherize\Blueprint\Healthcheck\EventListener | ||
*/ | ||
class SchedulerServiceWriterListener { | ||
/** | ||
* @var SchedulerYamlWriter | ||
*/ | ||
private $yamlWriter; | ||
|
||
/** | ||
* PublishUrlsServiceWriterListener constructor. | ||
* @param SchedulerYamlWriter $yamlWriter | ||
*/ | ||
public function __construct( SchedulerYamlWriter $yamlWriter ) { | ||
$this->yamlWriter = $yamlWriter; | ||
} | ||
|
||
/** | ||
* @param ServiceWriterServicePreparedEvent $event | ||
*/ | ||
public function servicePrepared( ServiceWriterServicePreparedEvent $event ) { | ||
$dockerContent = $event->getDockerContent(); | ||
|
||
$fileVersion = $event->getFileVersion(); | ||
$service = $event->getService(); | ||
try { | ||
$extraInformation = $service->getExtraInformation(SchedulerExtraInformation::IDENTIFIER ); | ||
} catch(ExtraInformationNotFoundException $e) { | ||
return; | ||
} | ||
|
||
if( !$extraInformation instanceof SchedulerExtraInformation ) | ||
return; | ||
|
||
$this->yamlWriter->write( $fileVersion, $extraInformation, $dockerContent ); | ||
|
||
$event->setDockerContent($dockerContent); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
app/Blueprint/Scheduler/SchedulerExtraInformation/SchedulerExtraInformation.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,134 @@ | ||
<?php namespace Rancherize\Blueprint\Scheduler\SchedulerExtraInformation; | ||
|
||
use Rancherize\Blueprint\Infrastructure\Service\ServiceExtraInformation; | ||
|
||
/** | ||
* Class SchedulerExtraInformation | ||
* @package Rancherize\Blueprint\Scheduler | ||
*/ | ||
class SchedulerExtraInformation implements ServiceExtraInformation { | ||
|
||
const IDENTIFIER = 'scheduler'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $scheduler; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $allowSameHost; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $requireTags = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $forbidTags = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $shouldHaveTags = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $shouldNotTags = []; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getIdentifier() { | ||
return self::IDENTIFIER; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getScheduler() { | ||
return $this->scheduler; | ||
} | ||
|
||
/** | ||
* @param string $scheduler | ||
*/ | ||
public function setScheduler( $scheduler ) { | ||
$this->scheduler = $scheduler; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isAllowSameHost() { | ||
return $this->allowSameHost; | ||
} | ||
|
||
/** | ||
* @param bool $allowSameHost | ||
*/ | ||
public function setAllowSameHost( $allowSameHost ) { | ||
$this->allowSameHost = $allowSameHost; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getRequireTags() { | ||
return $this->requireTags; | ||
} | ||
|
||
/** | ||
* @param array $requireTags | ||
*/ | ||
public function setRequireTags( array $requireTags ) { | ||
$this->requireTags = $requireTags; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getForbidTags() { | ||
return $this->forbidTags; | ||
} | ||
|
||
/** | ||
* @param array $forbidTags | ||
*/ | ||
public function setForbidTags( array $forbidTags ) { | ||
$this->forbidTags = $forbidTags; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getShouldHaveTags(): array { | ||
return $this->shouldHaveTags; | ||
} | ||
|
||
/** | ||
* @param array $shouldHaveTags | ||
*/ | ||
public function setShouldHaveTags( array $shouldHaveTags ) { | ||
$this->shouldHaveTags = $shouldHaveTags; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getShouldNotTags(): array { | ||
return $this->shouldNotTags; | ||
} | ||
|
||
/** | ||
* @param array $shouldNotTags | ||
*/ | ||
public function setShouldNotTags( array $shouldNotTags ) { | ||
$this->shouldNotTags = $shouldNotTags; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
app/Blueprint/Scheduler/SchedulerInitializer/SchedulerInitializer.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,40 @@ | ||
<?php namespace Rancherize\Blueprint\Scheduler\SchedulerInitializer; | ||
|
||
use Rancherize\Configuration\Configurable; | ||
use Rancherize\Configuration\PrefixConfigurableDecorator; | ||
use Rancherize\Configuration\Services\ConfigurationInitializer; | ||
|
||
/** | ||
* Class SchedulerInitializer | ||
* @package Rancherize\Blueprint\Scheduler\SchedulerInitializer | ||
*/ | ||
class SchedulerInitializer { | ||
/** | ||
* @var ConfigurationInitializer | ||
*/ | ||
private $initializer; | ||
|
||
/** | ||
* SchedulerInitializer constructor. | ||
* @param ConfigurationInitializer $initializer | ||
*/ | ||
public function __construct(ConfigurationInitializer $initializer) { | ||
$this->initializer = $initializer; | ||
} | ||
|
||
/** | ||
* @param Configurable $environmentSetter | ||
* @param Configurable|null $projectSetter | ||
*/ | ||
public function init( Configurable $environmentSetter, Configurable $projectSetter = null ) { | ||
|
||
if($projectSetter === null) | ||
$projectSetter = $environmentSetter; | ||
|
||
$schedulerEnvironmentConfigurable = new PrefixConfigurableDecorator($environmentSetter, 'scheduler.'); | ||
$schedulerProjectConfigurable = new PrefixConfigurableDecorator($projectSetter, 'scheduler.'); | ||
|
||
$this->initializer->init($schedulerEnvironmentConfigurable, 'enable', false, $schedulerProjectConfigurable); | ||
$this->initializer->init($schedulerEnvironmentConfigurable, 'tags', ['apps' => 'true']); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/Blueprint/Scheduler/SchedulerParser/SchedulerParser.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,46 @@ | ||
<?php namespace Rancherize\Blueprint\Scheduler\SchedulerParser; | ||
|
||
use Rancherize\Blueprint\Infrastructure\Service\Service; | ||
use Rancherize\Blueprint\Scheduler\SchedulerExtraInformation\SchedulerExtraInformation; | ||
use Rancherize\Configuration\Configuration; | ||
use Rancherize\Configuration\PrefixConfigurationDecorator; | ||
|
||
/** | ||
* Class SchedulerParser | ||
* @package Rancherize\Blueprint\Scheduler | ||
*/ | ||
class SchedulerParser { | ||
|
||
/** | ||
* @param Service $service | ||
* @param Configuration $configuration | ||
*/ | ||
public function parse( Service $service, Configuration $configuration ) { | ||
$schedulerInformation = new SchedulerExtraInformation(); | ||
$schedulerConfig = new PrefixConfigurationDecorator($configuration, 'scheduler.'); | ||
|
||
if( !$configuration->has('scheduler') ) | ||
return; | ||
|
||
if( !$schedulerConfig->get('enable', true) ) | ||
return; | ||
|
||
$requiredTags = $schedulerConfig->get('tags', []); | ||
if( !is_array($requiredTags) ) | ||
$requiredTags = []; | ||
$schedulerInformation->setRequireTags($requiredTags); | ||
|
||
$forbiddenTags = $schedulerConfig->get('forbid-tags', []); | ||
if( !is_array($forbiddenTags) ) | ||
$forbiddenTags = []; | ||
$schedulerInformation->setForbidTags($forbiddenTags); | ||
|
||
$allowSameHost = $schedulerConfig->get('same-host', false); | ||
$schedulerInformation->setAllowSameHost($allowSameHost); | ||
|
||
$scheduler = $schedulerConfig->get('scheduler', 'rancher'); | ||
$schedulerInformation->setScheduler($scheduler); | ||
|
||
$service->addExtraInformation( $schedulerInformation ); | ||
} | ||
} |
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,53 @@ | ||
<?php namespace Rancherize\Blueprint\Scheduler; | ||
|
||
use Rancherize\Blueprint\Infrastructure\Service\Events\ServiceWriterServicePreparedEvent; | ||
use Rancherize\Blueprint\Scheduler\EventListener\SchedulerServiceWriterListener; | ||
use Rancherize\Blueprint\Scheduler\SchedulerParser\SchedulerParser; | ||
use Rancherize\Blueprint\Scheduler\SchedulerYamlWriter\Rancher\RancherTagService; | ||
use Rancherize\Blueprint\Scheduler\SchedulerYamlWriter\Rancher\V2\V2RancherSchedulerYamlWriter; | ||
use Rancherize\Blueprint\Scheduler\SchedulerYamlWriter\SchedulerYamlWriter; | ||
use Rancherize\Plugin\Provider; | ||
use Rancherize\Plugin\ProviderTrait; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
|
||
/** | ||
* Class SchedulerProvider | ||
* @package Rancherize\Blueprint\Scheduler | ||
*/ | ||
class SchedulerProvider implements Provider { | ||
|
||
use ProviderTrait; | ||
|
||
|
||
/** | ||
*/ | ||
public function register() { | ||
$this->container['scheduler-parser'] = function($c) { | ||
return new SchedulerParser(); | ||
}; | ||
$this->container['scheduler-yaml-writer'] = function($c) { | ||
return new SchedulerYamlWriter(); | ||
}; | ||
$this->container['scheduler-service-writer-listener'] = function($c) { | ||
return new SchedulerServiceWriterListener($c['scheduler-yaml-writer']); | ||
}; | ||
|
||
$this->container['rancher-tag-service'] = function($c) { | ||
return new RancherTagService(); | ||
}; | ||
$this->container['v2-rancher-scheduler-yaml-writer'] = function($c) { | ||
return new V2RancherSchedulerYamlWriter( $c['rancher-tag-service'] ); | ||
}; | ||
} | ||
|
||
/** | ||
*/ | ||
public function boot() { | ||
/** | ||
* @var EventDispatcher $event | ||
*/ | ||
$event = $this->container['event']; | ||
$listener = $this->container['scheduler-service-writer-listener']; | ||
$event->addListener(ServiceWriterServicePreparedEvent::NAME, [$listener, 'servicePrepared']); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/Blueprint/Scheduler/SchedulerYamlWriter/Rancher/RancherTagService.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,29 @@ | ||
<?php namespace Rancherize\Blueprint\Scheduler\SchedulerYamlWriter\Rancher; | ||
|
||
/** | ||
* Class RancherTagService | ||
* @package Rancherize\Blueprint\Scheduler\SchedulerYamlWriter\Rancher | ||
* | ||
* Coverts arrays to "$key=$value" / "$value=true" which then can be imploded for rancher scheduling rules | ||
*/ | ||
class RancherTagService { | ||
|
||
/** | ||
* @param array $tags | ||
*/ | ||
public function makeTags( array $keyValueTags ) { | ||
|
||
$tags = []; | ||
foreach($keyValueTags as $tag => $value) { | ||
if( is_numeric($tag) ) { | ||
$tag = $value; | ||
$value = 'true'; | ||
} | ||
|
||
$tags[] = "$tag=$value"; | ||
|
||
} | ||
|
||
return $tags; | ||
} | ||
} |
Oops, something went wrong.