11<?php namespace RancherizeBlueprintPhpCli \PhpCliBlueprint ;
22
3+ use Closure ;
34use Rancherize \Blueprint \Blueprint ;
5+ use Rancherize \Blueprint \Cron \CronService \CronService ;
6+ use Rancherize \Blueprint \Cron \Schedule \ScheduleParser ;
47use Rancherize \Blueprint \Flags \HasFlagsTrait ;
58use Rancherize \Blueprint \Infrastructure \Dockerfile \Dockerfile ;
69use Rancherize \Blueprint \Infrastructure \Infrastructure ;
10+ use Rancherize \Blueprint \Infrastructure \Service \Service ;
711use Rancherize \Blueprint \Scheduler \SchedulerInitializer \SchedulerInitializer ;
812use Rancherize \Blueprint \Validation \Exceptions \ValidationFailedException ;
913use Rancherize \Blueprint \Validation \Traits \HasValidatorTrait ;
@@ -148,6 +152,20 @@ public function build( Configuration $configuration, string $environment, string
148152 $ infrastructure ->setDockerfile ($ dockerfile );
149153
150154 // TODO: Implement build() method.
155+ $ service = $ this ->makeServerService ($ config , $ projectConfigurable );
156+ $ infrastructure ->addService ($ service );
157+
158+ /**
159+ * @var ScheduleParser $scheduleParser
160+ */
161+ $ scheduleParser = container ('schedule-parser ' );
162+ $ schedule = $ scheduleParser ->parseSchedule ($ config );
163+
164+ /**
165+ * @var CronService $cronService
166+ */
167+ $ cronService = container ('cron-service ' );
168+ $ cronService ->makeCron ($ service , $ schedule );
151169
152170 return $ infrastructure ;
153171 }
@@ -159,9 +177,14 @@ public function build( Configuration $configuration, string $environment, string
159177 protected function makeDockerfile (Configuration $ config ):Dockerfile {
160178 $ dockerfile = new Dockerfile ();
161179
162- $ baseimage = $ config ->has ('php ' )
163- ? 'php: ' . $ config ->get ('php ' , '7.0 ' ) . '-alpine '
164- : $ config ->get ('docker.base-image ' , 'php:7.0-alpine ' );
180+ /**
181+ * @IMPORTANT
182+ *
183+ * This is NOT the php version that will later run the image.
184+ * It is the base of the app DATA image.
185+ * The php image that will actually run the app is the one set for the ServerService with $serverService->setImage()
186+ */
187+ $ baseimage = $ config ->get ('docker.base-image ' , 'php:7.0-alpine ' );
165188 $ dockerfile ->setFrom ($ baseimage );
166189
167190 $ dockerfile ->addVolume ('/var/www/app ' );
@@ -196,7 +219,80 @@ protected function makeDockerfile(Configuration $config):Dockerfile {
196219
197220 $ dockerfile ->setCommand ('php ' .$ config ->get ('command ' , '-i ' ));
198221
222+
199223 return $ dockerfile ;
200224 }
201225
226+ /**
227+ * @param Configuration $config
228+ * @param Configuration $default
229+ * @return Service
230+ */
231+ protected function makeServerService (Configuration $ config , Configuration $ default ) : Service {
232+ $ serverService = new Service ();
233+ $ serverService ->setName ($ config ->get ('service-name ' ));
234+
235+ $ phpImage = $ config ->has ('php ' )
236+ ? 'php: ' . $ config ->get ('php ' , '7.0 ' ) . '-alpine '
237+ : $ config ->get ('docker.base-image ' , 'php:7.0-alpine ' );
238+ $ serverService ->setImage ($ config ->get ('docker.image ' , $ phpImage ));
239+
240+ if ( $ config ->get ('sync-user-into-container ' , false ) ) {
241+ $ serverService ->setEnvironmentVariable ('USER_ID ' , getmyuid ());
242+ $ serverService ->setEnvironmentVariable ('GROUP_ID ' , getmygid ());
243+ }
244+
245+ if ($ config ->get ('mount-workdir ' , false )) {
246+ $ mountSuffix = $ config ->get ('work-sub-directory ' , '' );
247+ $ targetSuffix = $ config ->get ('target-sub-directory ' , '' );
248+
249+ $ hostDirectory = getcwd () . $ mountSuffix ;
250+ $ containerDirectory = '/var/www/app ' . $ targetSuffix ;
251+ $ serverService ->addVolume ($ hostDirectory , $ containerDirectory );
252+ }
253+
254+ $ persistentDriver = $ config ->get ('docker.persistent-driver ' , 'pxd ' );
255+ $ persistentOptions = $ config ->get ('docker.persistent-options ' , [
256+ 'repl ' => '3 ' ,
257+ 'shared ' => 'true ' ,
258+ ]);
259+ foreach ( $ config ->get ('persistent-volumes ' , []) as $ volumeName => $ path ) {
260+ $ volume = new \Rancherize \Blueprint \Infrastructure \Service \Volume ();
261+ $ volume ->setDriver ($ persistentDriver );
262+ $ volume ->setOptions ($ persistentOptions );
263+ $ volume ->setExternalPath ($ volumeName );
264+ $ volume ->setInternalPath ($ path );
265+ $ serverService ->addVolume ( $ volume );
266+ }
267+
268+ $ this ->addAll ([$ default , $ config ], 'environment ' , function (string $ name , $ value ) use ($ serverService ) {
269+ $ serverService ->setEnvironmentVariable ($ name , $ value );
270+ });
271+
272+ $ this ->addAll ([$ default , $ config ], 'labels ' , function (string $ name , $ value ) use ($ serverService ) {
273+ $ serverService ->addLabel ($ name , $ value );
274+ });
275+
276+ if ($ config ->has ('external_links ' )) {
277+ foreach ($ config ->get ('external_links ' ) as $ name => $ value )
278+ $ serverService ->addExternalLink ($ value , $ name );
279+ }
280+
281+ return $ serverService ;
282+ }
283+
284+ /**
285+ * @param Configuration[] $configs
286+ * @param string $label
287+ * @param Closure $closure
288+ */
289+ private function addAll (array $ configs , string $ label , Closure $ closure ) {
290+ foreach ($ configs as $ c ) {
291+ if (!$ c ->has ($ label ))
292+ continue ;
293+
294+ foreach ($ c ->get ($ label ) as $ name => $ value )
295+ $ closure ($ name , $ value );
296+ }
297+ }
202298}
0 commit comments