99use Rancherize \Blueprint \Infrastructure \Dockerfile \Dockerfile ;
1010use Rancherize \Blueprint \Infrastructure \Infrastructure ;
1111use Rancherize \Blueprint \Infrastructure \Service \Service ;
12+ use Rancherize \Blueprint \Infrastructure \Service \Services \AppService ;
1213use Rancherize \Blueprint \Scheduler \SchedulerInitializer \SchedulerInitializer ;
14+ use Rancherize \Blueprint \Scheduler \SchedulerParser \SchedulerParser ;
15+ use Rancherize \Blueprint \TakesDockerAccount ;
1316use Rancherize \Blueprint \Validation \Exceptions \ValidationFailedException ;
1417use Rancherize \Blueprint \Validation \Traits \HasValidatorTrait ;
1518use Rancherize \Configuration \Configurable ;
1922use Rancherize \Configuration \Services \ConfigurableFallback ;
2023use Rancherize \Configuration \Services \ConfigurationFallback ;
2124use Rancherize \Configuration \Services \ConfigurationInitializer ;
25+ use Rancherize \Docker \DockerAccount ;
2226use Symfony \Component \Console \Input \InputInterface ;
2327use 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}
0 commit comments