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

Commit eb65a6c

Browse files
author
Sven Speckmaier
committed
added scheduler, app sidekick for volumes_from
1 parent 31f7935 commit eb65a6c

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

plugin/PhpCliBlueprint/PhpCliBlueprint.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
use Rancherize\Blueprint\Infrastructure\Dockerfile\Dockerfile;
1010
use Rancherize\Blueprint\Infrastructure\Infrastructure;
1111
use Rancherize\Blueprint\Infrastructure\Service\Service;
12+
use Rancherize\Blueprint\Infrastructure\Service\Services\AppService;
1213
use Rancherize\Blueprint\Scheduler\SchedulerInitializer\SchedulerInitializer;
14+
use Rancherize\Blueprint\Scheduler\SchedulerParser\SchedulerParser;
15+
use Rancherize\Blueprint\TakesDockerAccount;
1316
use Rancherize\Blueprint\Validation\Exceptions\ValidationFailedException;
1417
use Rancherize\Blueprint\Validation\Traits\HasValidatorTrait;
1518
use Rancherize\Configuration\Configurable;
@@ -19,14 +22,15 @@
1922
use Rancherize\Configuration\Services\ConfigurableFallback;
2023
use Rancherize\Configuration\Services\ConfigurationFallback;
2124
use Rancherize\Configuration\Services\ConfigurationInitializer;
25+
use Rancherize\Docker\DockerAccount;
2226
use Symfony\Component\Console\Input\InputInterface;
2327
use Symfony\Component\Console\Output\OutputInterface;
2428

2529
/**
2630
* Class PhpCliBlueprint
2731
* @package RancherizeBlueprintPhpCli\PhpCliBlueprint
2832
*/
29-
class PhpCliBlueprint implements Blueprint {
33+
class PhpCliBlueprint implements Blueprint, TakesDockerAccount {
3034

3135
private $targetDirectory = '/var/cli/app';
3236

@@ -37,6 +41,11 @@ class PhpCliBlueprint implements Blueprint {
3741

3842
use HasValidatorTrait;
3943

44+
/**
45+
* @var SchedulerParser
46+
*/
47+
protected $rancherSchedulerParser;
48+
4049
/**
4150
* Fill the configurable with all possible options with explanatory default options set
4251
*
@@ -157,6 +166,7 @@ public function build( Configuration $configuration, string $environment, string
157166
// TODO: Implement build() method.
158167
$service = $this->makeServerService($config, $projectConfigurable);
159168
$infrastructure->addService($service);
169+
$this->addAppContainer($version, $config, $service, $infrastructure);
160170

161171
$this->parseCronSchedule( $config, $service );
162172

@@ -242,6 +252,7 @@ protected function makeServerService(Configuration $config, Configuration $defau
242252
$serverService->addVolume($hostDirectory, $containerDirectory);
243253
}
244254

255+
245256
$command = 'php ' . $config->get( 'command', '-i' );
246257
$serverService->setCommand($command);
247258
$serverService->setRestart( Service::RESTART_START_ONCE );
@@ -274,6 +285,8 @@ protected function makeServerService(Configuration $config, Configuration $defau
274285
$serverService->addExternalLink($value, $name);
275286
}
276287

288+
$this->rancherSchedulerParser->parse($serverService, $config);
289+
277290
return $serverService;
278291
}
279292

@@ -313,4 +326,60 @@ protected function parseCronSchedule( $config, $service ) {
313326
$cronService = container( 'cron-service' );
314327
$cronService->makeCron( $service, $schedule );
315328
}
329+
330+
/**
331+
* @param string $version
332+
* @param Configuration $config
333+
* @param Service $serverService
334+
* @param Infrastructure $infrastructure
335+
*/
336+
protected function addAppContainer($version, Configuration $config, Service $serverService, Infrastructure $infrastructure) {
337+
if (!$config->get('use-app-container', true))
338+
return;
339+
340+
$imageName = $config->get('docker.repository') . ':' . $config->get('docker.version-prefix') . $version;
341+
$imageNameWithServer = $this->applyServer($imageName);
342+
343+
$appService = new AppService($imageNameWithServer);
344+
$appService->setName($config->get('service-name') . 'App');
345+
346+
$serverService->addSidekick($appService);
347+
$serverService->addVolumeFrom($appService);
348+
$infrastructure->addService($appService);
349+
}
350+
351+
/**
352+
* @var DockerAccount
353+
*/
354+
protected $dockerAccount = null;
355+
356+
protected function applyServer(string $imageName) {
357+
if( $this->dockerAccount === null)
358+
return $imageName;
359+
360+
$server = $this->dockerAccount->getServer();
361+
if( empty($server) )
362+
return $imageName;
363+
364+
$serverHost = parse_url($server, PHP_URL_HOST);
365+
$imageNameWithServer = $serverHost.'/'.$imageName;
366+
367+
return $imageNameWithServer;
368+
}
369+
370+
/**
371+
* @param DockerAccount $dockerAccount
372+
* @return $this
373+
*/
374+
public function setDockerAccount( DockerAccount $dockerAccount ) {
375+
$this->dockerAccount = $dockerAccount;
376+
return $this;
377+
}
378+
379+
/**
380+
* @param SchedulerParser $rancherSchedulerParser
381+
*/
382+
public function setRancherSchedulerParser( SchedulerParser $rancherSchedulerParser ) {
383+
$this->rancherSchedulerParser = $rancherSchedulerParser;
384+
}
316385
}

plugin/RancherBlueprintPhpCliProvider.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace RancherizeBlueprintPhpCli;
22

3+
use Pimple\Container;
34
use Rancherize\Blueprint\Factory\BlueprintFactory;
45
use Rancherize\Plugin\Provider;
56
use Rancherize\Plugin\ProviderTrait;
@@ -26,6 +27,12 @@ public function boot() {
2627
*/
2728
$blueprintFactory = container('blueprint-factory');
2829

29-
$blueprintFactory->add('php-cli', PhpCliBlueprint::class);
30+
$blueprintFactory->add('php-cli', function(Container $c) {
31+
$blueprint = new PhpCliBlueprint;
32+
33+
$blueprint->setRancherSchedulerParser( $c['scheduler-parser'] );
34+
35+
return $blueprint;
36+
});
3037
}
3138
}

0 commit comments

Comments
 (0)