44use Rancherize \Blueprint \Flags \HasFlagsTrait ;
55use Rancherize \Blueprint \Infrastructure \Dockerfile \Dockerfile ;
66use Rancherize \Blueprint \Infrastructure \Infrastructure ;
7+ use Rancherize \Blueprint \Scheduler \SchedulerInitializer \SchedulerInitializer ;
78use Rancherize \Blueprint \Validation \Exceptions \ValidationFailedException ;
9+ use Rancherize \Blueprint \Validation \Traits \HasValidatorTrait ;
810use Rancherize \Configuration \Configurable ;
911use Rancherize \Configuration \Configuration ;
1012use Rancherize \Configuration \PrefixConfigurableDecorator ;
13+ use Rancherize \Configuration \PrefixConfigurationDecorator ;
1114use Rancherize \Configuration \Services \ConfigurableFallback ;
15+ use Rancherize \Configuration \Services \ConfigurationFallback ;
1216use Rancherize \Configuration \Services \ConfigurationInitializer ;
1317use Symfony \Component \Console \Input \InputInterface ;
1418use Symfony \Component \Console \Output \OutputInterface ;
@@ -24,6 +28,8 @@ class PhpCliBlueprint implements Blueprint {
2428 */
2529 use HasFlagsTrait;
2630
31+ use HasValidatorTrait;
32+
2733 /**
2834 * Fill the configurable with all possible options with explanatory default options set
2935 *
@@ -68,21 +74,43 @@ public function init( Configurable $configurable, string $environment, InputInte
6874 */
6975 $ initializer = new ConfigurationInitializer ($ output );
7076
77+
78+
7179 /**
7280 *
7381 */
7482 if ( $ this ->getFlag ('dev ' , false ) ) {
7583 /**
7684 * Provide defaults for local development environment
7785 */
78-
79- return ;
80- }
81-
82- /**
83- * Provide defaults for rancher environments
84- */
85- }
86+ $ initializer ->init ($ fallbackConfigurable , 'mount-workdir ' , true );
87+ } else {
88+ $ initializer ->init ($ fallbackConfigurable , 'rancher.stack ' , 'Project ' );
89+
90+ $ schedulerInitializer = new SchedulerInitializer ($ initializer );
91+ $ schedulerInitializer ->init ($ fallbackConfigurable , $ projectConfigurable );
92+ }
93+
94+ /**
95+ * Provide defaults for rancher environments
96+ */
97+ $ initializer ->init ($ fallbackConfigurable , 'external_links ' , []);
98+
99+ $ initializer ->init ($ fallbackConfigurable , 'docker.repository ' , 'repo/name ' , $ projectConfigurable );
100+ $ initializer ->init ($ fallbackConfigurable , 'docker.version-prefix ' , '' , $ projectConfigurable );
101+ $ initializer ->init ($ fallbackConfigurable , 'service-name ' , 'Project ' , $ projectConfigurable );
102+ $ initializer ->init ($ fallbackConfigurable , 'docker.base-image ' , 'php:7.0-alpine ' , $ projectConfigurable );
103+ $ initializer ->init ($ fallbackConfigurable , 'environment ' , ["EXAMPLE " => 'value ' ]);
104+
105+ /**
106+ * Blueprint specific entries
107+ */
108+ $ initializer ->init ($ fallbackConfigurable , 'php ' , '7.0 ' , $ projectConfigurable );
109+ // $initializer->init($fallbackConfigurable, 'extensions', []);
110+ $ initializer ->init ($ fallbackConfigurable , 'add-composer ' , false , $ projectConfigurable );
111+ $ initializer ->init ($ fallbackConfigurable , 'command ' , "-i " , $ projectConfigurable );
112+
113+ }
86114
87115 /**
88116 * Ensure that the given environment has at least the minimal configuration options set to start and deploy this
@@ -93,12 +121,14 @@ public function init( Configurable $configurable, string $environment, InputInte
93121 * @throws ValidationFailedException
94122 */
95123 public function validate ( Configuration $ configurable , string $ environment ) {
96- // TODO: Implement validate() method.
97124
98- /**
99- * Validation config.
100- * Throw ValidationFailedExceptions to indicate errors
101- */
125+ $ projectConfigurable = new PrefixConfigurationDecorator ($ configurable , "project.default. " );
126+ $ environmentConfigurable = new PrefixConfigurationDecorator ($ configurable , "project.environments. $ environment. " );
127+ $ config = new ConfigurationFallback ($ environmentConfigurable , $ projectConfigurable );
128+
129+ $ this ->getValidator ()->validate ($ config , [
130+ 'service-name ' => 'required ' ,
131+ ]);
102132 }
103133
104134 /**
@@ -110,11 +140,63 @@ public function validate( Configuration $configurable, string $environment ) {
110140 public function build ( Configuration $ configuration , string $ environment , string $ version = null ): Infrastructure {
111141 $ infrastructure = new Infrastructure ();
112142
113- $ dockerfile = new Dockerfile ();
143+ $ projectConfigurable = new PrefixConfigurationDecorator ($ configuration , "project.default. " );
144+ $ environmentConfigurable = new PrefixConfigurationDecorator ($ configuration , "project.environments. $ environment. " );
145+ $ config = new ConfigurationFallback ($ environmentConfigurable , $ projectConfigurable );
146+
147+ $ dockerfile = $ this ->makeDockerfile ($ config );
114148 $ infrastructure ->setDockerfile ($ dockerfile );
115149
116150 // TODO: Implement build() method.
117151
118152 return $ infrastructure ;
119153 }
154+
155+ /**
156+ * @param $config
157+ * @return Dockerfile
158+ */
159+ protected function makeDockerfile (Configuration $ config ):Dockerfile {
160+ $ dockerfile = new Dockerfile ();
161+
162+ $ baseimage = $ config ->has ('php ' )
163+ ? 'php: ' . $ config ->get ('php ' , '7.0 ' ) . '-alpine '
164+ : $ config ->get ('docker.base-image ' , 'php:7.0-alpine ' );
165+ $ dockerfile ->setFrom ($ baseimage );
166+
167+ $ dockerfile ->addVolume ('/var/www/app ' );
168+
169+ $ dockerfile ->setWorkdir ('/var/www/app ' );
170+
171+ $ copySuffix = $ config ->get ('work-sub-directory ' , '' );
172+ $ targetSuffix = $ config ->get ('target-sub-directory ' , '' );
173+ $ dockerfile ->copy ('. ' .$ copySuffix , '/var/www/app ' .$ targetSuffix );
174+
175+ if ($ config ->get ('add-composer ' , false )) {
176+ $ dockerfile ->run ('php -r "copy( \'https://getcomposer.org/installer \', \'composer-setup.php \');" \
177+ && php -r "if (hash_file( \'SHA384 \', \'composer-setup.php \') === \'669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410 \') { echo \'Installer verified \'; } else { echo \'Installer corrupt \'; unlink( \'composer-setup.php \'); } echo PHP_EOL;" \
178+ && php composer-setup.php \
179+ php -r "unlink( \'composer-setup.php \');" \
180+ && cd /var/www/app && ./composer.phar install && rm composer.phar ' );
181+ }
182+
183+ $ additionalFiles = $ config ->get ('add-files ' );
184+ if ( is_array ($ additionalFiles ) ) {
185+ foreach ($ additionalFiles as $ file => $ path ) {
186+ $ dockerfile ->copy ($ file , $ path );
187+ }
188+ }
189+ $ additionalVolumes = $ config ->get ('add-volumes ' );
190+ if ( is_array ($ additionalVolumes ) ) {
191+ foreach ($ additionalFiles as $ path ) {
192+ $ dockerfile ->addVolume ($ path );
193+ }
194+ }
195+ $ dockerfile ->run ('rm -Rf /var/www/app/.rancherize ' );
196+
197+ $ dockerfile ->setCommand ('php ' .$ config ->get ('command ' , '-i ' ));
198+
199+ return $ dockerfile ;
200+ }
201+
120202}
0 commit comments