1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SamIT\Yii2\PhpFpm; |
5
|
|
|
|
6
|
|
|
use yii\base\InvalidConfigException; |
7
|
|
|
use yii\base\UnknownPropertyException; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
use yii\helpers\Console; |
10
|
|
|
use yii\mutex\Mutex; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Module |
14
|
|
|
* @package SamIT\Yii2\PhpFpm |
15
|
|
|
* @property-write string[] $additionalExtensions |
16
|
|
|
* @property-write string|int[] $additionalPoolConfig |
17
|
|
|
* @property-write string[] $additionalPhpConfig |
18
|
|
|
* @property-write string[] $additionalFpmConfig |
19
|
|
|
*/ |
20
|
|
|
class Module extends \yii\base\Module |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The variables will be written to /runtime/env.json as JSON, where your application can read them. |
24
|
|
|
* @var string[] List of required environment variables. If one is missing the container will exit. |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
public $environmentVariables = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array Pool directives |
31
|
|
|
* @see http://php.net/manual/en/install.fpm.configuration.php |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
public $poolConfig = [ |
35
|
|
|
'user' => 'nobody', |
36
|
|
|
'group' => 'nobody', |
37
|
|
|
'listen' => 9000, |
38
|
|
|
'pm' => 'dynamic', |
39
|
|
|
'pm.max_children' => 40, |
40
|
|
|
'pm.start_servers' => 3, |
41
|
|
|
'pm.min_spare_servers' => 1, |
42
|
|
|
'pm.max_spare_servers' => 3, |
43
|
|
|
'access.log' => '/proc/self/fd/2', |
44
|
|
|
'clear_env' => 'yes', |
45
|
|
|
'catch_workers_output' => 'yes' |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array PHP configuration, supplied via php_admin_value in fpm config. |
50
|
|
|
*/ |
51
|
|
|
public $phpConfig = [ |
52
|
|
|
'upload_max_filesize' => '20M', |
53
|
|
|
'post_max_size' => '25M' |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array Global directives |
58
|
|
|
* @see http://php.net/manual/en/install.fpm.configuration.php |
59
|
|
|
* |
60
|
|
|
*/ |
61
|
|
|
public $fpmConfig = [ |
62
|
|
|
'error_log' => '/proc/self/fd/2', |
63
|
|
|
'daemonize' => 'no', |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* List of php extensions to install |
68
|
|
|
*/ |
69
|
|
|
public $extensions = [ |
70
|
|
|
'ctype', |
71
|
|
|
'gd', |
72
|
|
|
'iconv', |
73
|
|
|
'intl', |
74
|
|
|
'json', |
75
|
|
|
'mbstring', |
76
|
|
|
'session', |
77
|
|
|
'pdo_mysql', |
78
|
|
|
'session', |
79
|
|
|
'curl' |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var string The name of the created image. |
84
|
|
|
*/ |
85
|
|
|
public $image; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var string The tag of the created image. |
89
|
|
|
*/ |
90
|
|
|
public $tag = 'latest'; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var bool wheter to push successful builds. |
94
|
|
|
*/ |
95
|
|
|
public $push = false; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var string Location of composer.json / composer.lock |
99
|
|
|
*/ |
100
|
|
|
public $composerFilePath = '@app/../'; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var string[] List of console commands that are executed upon container launch. |
104
|
|
|
*/ |
105
|
|
|
public $initializationCommands = []; |
106
|
|
|
/** |
107
|
|
|
* @return string A PHP-FPM config file. |
108
|
|
|
*/ |
109
|
1 |
|
protected function createFpmConfig() |
110
|
|
|
{ |
111
|
1 |
|
$config = []; |
112
|
|
|
// Add global directives. |
113
|
1 |
|
$config[] = '[global]'; |
114
|
1 |
|
foreach ($this->fpmConfig as $key => $value) { |
115
|
1 |
|
$config[] = "$key = $value"; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Add pool directives. |
119
|
1 |
|
$poolConfig = $this->poolConfig; |
120
|
1 |
|
foreach ($this->phpConfig as $key => $value) { |
121
|
1 |
|
$poolConfig["php_admin_value[$key]"] = $value; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
$config[] = '[www]'; |
125
|
1 |
|
foreach ($poolConfig as $key => $value) { |
126
|
1 |
|
$config[] = "$key = $value"; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return \implode("\n", $config); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string A shell script that checks for existence of (non-empty) variables and runs php-fpm. |
134
|
|
|
*/ |
135
|
1 |
|
protected function createEntrypoint(): string |
136
|
|
|
{ |
137
|
|
|
// Get the route. |
138
|
1 |
|
$route = "{$this->getUniqueId()}/migrate/up"; |
139
|
1 |
|
$script = "/project/{$this->getConsoleEntryScript()}"; |
140
|
1 |
|
$result = []; |
141
|
1 |
|
$result[] = '#!/bin/sh'; |
142
|
|
|
// Check for variables. |
143
|
1 |
|
foreach ($this->environmentVariables as $name) { |
144
|
|
|
$result[] = \strtr('if [ -z "${name}" ]; then echo "Variable \${name} is required."; exit 1; fi', [ |
145
|
|
|
'{name}' => $name |
146
|
|
|
]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Check if runtime directory is writable. |
150
|
1 |
|
$result[] = <<<SH |
151
|
|
|
su nobody -s /bin/touch /runtime/testfile && rm /runtime/testfile; |
152
|
|
|
if [ $? -ne 0 ]; then |
153
|
|
|
echo Runtime directory is not writable; |
154
|
|
|
exit 1 |
155
|
|
|
fi |
156
|
|
|
SH; |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
|
160
|
|
|
// Check if runtime is a tmpfs. |
161
|
1 |
|
$message = Console::ansiFormat('/runtime should really be a tmpfs.', [Console::FG_RED]); |
162
|
1 |
|
$result[] = <<<SH |
163
|
1 |
|
grep 'tmpfs /runtime' /proc/mounts; |
164
|
|
|
if [ $? -ne 0 ]; then |
165
|
1 |
|
echo $message; |
166
|
|
|
fi |
167
|
|
|
SH; |
168
|
1 |
|
$result[] = <<<SH |
169
|
|
|
su nobody -s /bin/touch /runtime/env.json |
170
|
|
|
jq -n env > /runtime/env.json |
171
|
|
|
if [ $? -ne 0 ]; then |
172
|
|
|
echo "failed to store env in /runtime/env.json"; |
173
|
|
|
exit 1 |
174
|
|
|
fi |
175
|
|
|
SH; |
176
|
|
|
|
177
|
|
|
|
178
|
1 |
|
foreach ($this->initializationCommands as $route) { |
179
|
|
|
$result[] = "$script $route --interactive=0 || exit"; |
180
|
|
|
} |
181
|
1 |
|
$result[] = 'exec php-fpm7 --force-stderr --fpm-config /php-fpm.conf'; |
182
|
1 |
|
return \implode("\n", $result); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $version This is stored in the VERSION environment variable. |
187
|
|
|
* @throws InvalidConfigException |
188
|
|
|
* @return Context |
189
|
|
|
*/ |
190
|
1 |
|
public function createBuildContext(string $version): \SamIT\Yii2\PhpFpm\helpers\Context |
191
|
|
|
{ |
192
|
1 |
|
$root = \Yii::getAlias('@app'); |
193
|
1 |
|
if (!\is_string($root)) { |
194
|
|
|
throw new \Exception('Alias @app must be defined.'); |
195
|
|
|
} |
196
|
|
|
|
197
|
1 |
|
$context = new \SamIT\Yii2\PhpFpm\helpers\Context(); |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* BEGIN COMPOSER |
201
|
|
|
*/ |
202
|
1 |
|
$context->command('FROM composer'); |
203
|
1 |
|
$context->addFile('/build/composer.json', \Yii::getAlias($this->composerFilePath) .'/composer.json'); |
204
|
|
|
|
205
|
1 |
|
if (\file_exists(\Yii::getAlias($this->composerFilePath) . '/composer.lock')) { |
206
|
1 |
|
$context->addFile('/build/composer.lock', \Yii::getAlias($this->composerFilePath) . '/composer.lock'); |
207
|
|
|
} |
208
|
|
|
|
209
|
1 |
|
$context->run('composer global require hirak/prestissimo'); |
210
|
|
|
|
211
|
1 |
|
$context->run('cd /build && composer install --no-dev --no-autoloader --ignore-platform-reqs --prefer-dist'); |
212
|
|
|
|
213
|
|
|
// Add the actual source code. |
214
|
1 |
|
$context->addFile('/build/' . \basename($root), $root); |
215
|
1 |
|
$context->run('cd /build && composer dumpautoload -o --no-dev'); |
216
|
|
|
/** |
217
|
|
|
* END COMPOSER |
218
|
|
|
*/ |
219
|
|
|
|
220
|
1 |
|
$context->from('php:7.4-fpm-alpine'); |
221
|
1 |
|
$context->addUrl("/usr/local/bin/", "https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions"); |
222
|
1 |
|
$context->run("chmod +x /usr/local/bin/install-php-extensions"); |
223
|
1 |
|
$context->run('install-php-extensions ' . implode(' ', $this->extensions)); |
224
|
1 |
|
$context->run('mkdir /runtime && chown nobody:nobody /runtime'); |
225
|
1 |
|
$context->volume('/runtime'); |
226
|
1 |
|
$context->copyFromLayer("/project", "0", "/build"); |
227
|
|
|
|
228
|
1 |
|
$context->add('/entrypoint.sh', $this->createEntrypoint()); |
229
|
|
|
|
230
|
1 |
|
$context->run('chmod +x /entrypoint.sh'); |
231
|
|
|
|
232
|
1 |
|
$context->add('/php-fpm.conf', $this->createFpmConfig()); |
233
|
|
|
|
234
|
1 |
|
$context->run("php-fpm --force-stderr --fpm-config /php-fpm.conf -t"); |
235
|
|
|
|
236
|
1 |
|
$context->entrypoint(["/entrypoint.sh"]); |
237
|
|
|
|
238
|
1 |
|
$context->env('VERSION', $version); |
239
|
|
|
// Test if we can run a console command. |
240
|
1 |
|
if (\stripos($this->getConsoleEntryScript(), 'codecept') === false) { |
241
|
|
|
$script = "[ -f /project/{$this->getConsoleEntryScript()} ]"; |
242
|
|
|
$context->run($script); |
243
|
|
|
} |
244
|
1 |
|
return $context; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @throws InvalidConfigException in case the app is not configured as expected |
249
|
|
|
* @return string the relative path of the (console) entry script with respect to the project (not app) root. |
250
|
|
|
*/ |
251
|
1 |
|
public function getConsoleEntryScript(): string |
252
|
|
|
{ |
253
|
1 |
|
$full = \array_slice(\debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), -1)[0]['file']; |
254
|
1 |
|
$relative = \strtr($full, [\dirname(\Yii::getAlias('@app')) => '']); |
255
|
1 |
|
if ($relative === $full) { |
256
|
|
|
throw new InvalidConfigException("The console entry script must be located inside the @app directory."); |
257
|
|
|
} |
258
|
1 |
|
return \ltrim($relative, '/'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
1 |
|
public function __set($name, $value): void |
263
|
|
|
{ |
264
|
1 |
|
if (\strncmp($name, 'additional', 10) === 0) { |
265
|
1 |
|
$this->add(\lcfirst(\substr($name, 10)), $value); |
266
|
|
|
} else { |
267
|
|
|
parent::__set($name, $value); |
268
|
|
|
} |
269
|
1 |
|
} |
270
|
|
|
|
271
|
1 |
|
private function add($name, array $value): void |
272
|
|
|
{ |
273
|
1 |
|
if (!\property_exists($this, $name)) { |
274
|
|
|
throw new UnknownPropertyException("Unknown property $name"); |
275
|
|
|
} |
276
|
1 |
|
$this->$name = ArrayHelper::merge($this->$name, $value); |
277
|
1 |
|
} |
278
|
|
|
} |
279
|
|
|
|