1 | <?php |
||
23 | class Module extends \yii\base\Module |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var bool Whether the container should attempt to run migrations on launch. |
||
28 | */ |
||
29 | public $runMigrations = false; |
||
30 | |||
31 | /** |
||
32 | * @var bool whether migrations should acquire a lock. |
||
33 | * It must be configured in the 'mutex' component of this module or the application |
||
34 | * Note that this mutex must be shared between all instances of your application. |
||
35 | * Consider using something like redis or mysql mutex. |
||
36 | */ |
||
37 | public $migrationsUseMutex = true; |
||
38 | |||
39 | /** |
||
40 | * The variables will be written to /runtime/env.json as JSON, where your application can read them. |
||
41 | * @var string[] List of required environment variables. If one is missing the container will exit. |
||
42 | * |
||
43 | */ |
||
44 | public $environmentVariables = []; |
||
45 | |||
46 | /** |
||
47 | * @var array Pool directives |
||
48 | * @see http://php.net/manual/en/install.fpm.configuration.php |
||
49 | * |
||
50 | */ |
||
51 | public $poolConfig = [ |
||
52 | 'user' => 'nobody', |
||
53 | 'group' => 'nobody', |
||
54 | 'listen' => 9000, |
||
55 | 'pm' => 'dynamic', |
||
56 | 'pm.max_children' => 40, |
||
57 | 'pm.start_servers' => 3, |
||
58 | 'pm.min_spare_servers' => 1, |
||
59 | 'pm.max_spare_servers' => 3, |
||
60 | 'access.log' => '/proc/self/fd/2', |
||
61 | 'clear_env' => 'yes', |
||
62 | 'catch_workers_output' => 'yes' |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * @var array PHP configuration, supplied via php_admin_value in fpm config. |
||
67 | */ |
||
68 | public $phpConfig = [ |
||
69 | 'upload_max_filesize' => '20M', |
||
70 | 'post_max_size' => '25M' |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * @var array Global directives |
||
75 | * @see http://php.net/manual/en/install.fpm.configuration.php |
||
76 | * |
||
77 | */ |
||
78 | public $fpmConfig = [ |
||
79 | 'error_log' => '/proc/self/fd/2', |
||
80 | 'daemonize' => 'no', |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * List of OS packages to install |
||
85 | */ |
||
86 | public $packages = [ |
||
87 | 'php7', |
||
88 | 'php7-fpm', |
||
89 | 'tini', |
||
90 | 'ca-certificates', |
||
91 | /** |
||
92 | * @see https://stedolan.github.io/jq/ |
||
93 | * This is used for converting the env to JSON. |
||
94 | */ |
||
95 | 'jq' |
||
96 | ]; |
||
97 | |||
98 | /** |
||
99 | * List of php extensions to install |
||
100 | */ |
||
101 | public $extensions = [ |
||
102 | 'ctype', |
||
103 | 'gd', |
||
104 | 'iconv', |
||
105 | 'intl', |
||
106 | 'json', |
||
107 | 'mbstring', |
||
108 | 'session', |
||
109 | 'pdo_mysql', |
||
110 | 'session', |
||
111 | 'curl' |
||
112 | ]; |
||
113 | |||
114 | /** |
||
115 | * @var string The name of the created image. |
||
116 | */ |
||
117 | public $image; |
||
118 | |||
119 | /** |
||
120 | * @var string The tag of the created image. |
||
121 | */ |
||
122 | public $tag = 'latest'; |
||
123 | |||
124 | /** |
||
125 | * @var bool wheter to push successful builds. |
||
126 | */ |
||
127 | public $push = false; |
||
128 | |||
129 | /** |
||
130 | * @var string Location of composer.json / composer.lock |
||
131 | */ |
||
132 | public $composerFilePath = '@app/../'; |
||
133 | /** |
||
134 | * @return string A PHP-FPM config file. |
||
135 | */ |
||
136 | 3 | protected function createFpmConfig() |
|
158 | |||
159 | /** |
||
160 | * @return string A shell script that checks for existence of (non-empty) variables and runs php-fpm. |
||
161 | */ |
||
162 | 3 | protected function createEntrypoint(): string |
|
222 | |||
223 | 3 | public function createBuildContext(): Context |
|
224 | { |
||
225 | 3 | $builder = new ContextBuilder(); |
|
226 | |||
227 | /** |
||
228 | * BEGIN COMPOSER |
||
229 | */ |
||
230 | 3 | $builder->from('composer'); |
|
231 | 3 | $builder->addFile('/build/composer.json', \Yii::getAlias($this->composerFilePath) .'/composer.json'); |
|
232 | 3 | if (\file_exists(\Yii::getAlias($this->composerFilePath) . '/composer.lock')) { |
|
233 | 3 | $builder->addFile('/build/composer.lock', \Yii::getAlias($this->composerFilePath) . '/composer.lock'); |
|
234 | } |
||
235 | |||
236 | 3 | $builder->run('cd /build && composer install --no-dev --no-autoloader --ignore-platform-reqs --prefer-dist && rm -rf /root/.composer'); |
|
237 | |||
238 | |||
239 | // Add the actual source code. |
||
240 | 3 | $root = \Yii::getAlias('@app'); |
|
241 | 3 | if (!\is_string($root)) { |
|
242 | throw new \Exception('Alias @app must be defined.'); |
||
243 | } |
||
244 | |||
245 | 3 | $builder->addFile('/build/' . \basename($root), $root); |
|
246 | 3 | $builder->run('cd /build && composer dumpautoload -o'); |
|
247 | /** |
||
248 | * END COMPOSER |
||
249 | */ |
||
250 | |||
251 | |||
252 | 3 | $builder->from('alpine:edge'); |
|
253 | 3 | $packages = $this->packages; |
|
254 | |||
255 | 3 | foreach ($this->extensions as $extension) { |
|
256 | $packages[] = "php7-$extension"; |
||
257 | } |
||
258 | 3 | $builder->run('apk add --update --no-cache ' . \implode(' ', $packages)); |
|
259 | 3 | $builder->run('mkdir /runtime && chown nobody:nobody /runtime'); |
|
260 | 3 | $builder->volume('/runtime'); |
|
261 | 3 | $builder->copy('--from=0 /build', '/project'); |
|
262 | 3 | $builder->add('/entrypoint.sh', $this->createEntrypoint()); |
|
263 | 3 | $builder->run('chmod +x /entrypoint.sh'); |
|
264 | 3 | $builder->add('/php-fpm.conf', $this->createFpmConfig()); |
|
265 | 3 | $builder->run("php-fpm7 --force-stderr --fpm-config /php-fpm.conf -t"); |
|
266 | 3 | $builder->entrypoint('["/sbin/tini", "--", "/entrypoint.sh"]'); |
|
267 | |||
268 | // Test if we can run a console command. |
||
269 | 3 | if (\stripos($this->getConsoleEntryScript(), 'codecept') === false) { |
|
270 | $script = "[ -f /project/{$this->getConsoleEntryScript()} ]"; |
||
271 | $builder->run($script); |
||
272 | } |
||
273 | 3 | return $builder->getContext(); |
|
274 | } |
||
275 | |||
276 | 3 | public function getLock(int $timeout = 0) |
|
277 | { |
||
278 | 3 | if ($this->has('mutex')) { |
|
279 | 1 | $mutex = $this->get('mutex'); |
|
280 | 1 | if ($mutex instanceof Mutex |
|
281 | 1 | && $mutex->acquire(__CLASS__, $timeout) |
|
282 | ) { |
||
283 | \register_shutdown_function(function() use ($mutex): void { |
||
284 | $mutex->release(__CLASS__); |
||
285 | 1 | }); |
|
286 | 1 | return true; |
|
287 | } |
||
288 | } |
||
289 | 3 | return false; |
|
290 | } |
||
291 | |||
292 | /** |
||
293 | * @throws InvalidConfigException in case the app is not configured as expected |
||
294 | * @return string the relative path of the (console) entry script with respect to the project (not app) root. |
||
295 | */ |
||
296 | 3 | public function getConsoleEntryScript(): string |
|
305 | |||
306 | |||
307 | 1 | public function __set($name, $value): void |
|
315 | |||
316 | 1 | private function add($name, array $value): void |
|
323 | } |
||
324 |