1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SamIT\Yii2\PhpFpm; |
5
|
|
|
|
6
|
|
|
use Docker\Context\Context; |
7
|
|
|
use Docker\Context\ContextBuilder; |
8
|
|
|
use Docker\Context\ContextInterface; |
9
|
|
|
use yii\helpers\FileHelper; |
10
|
|
|
|
11
|
|
|
class Module extends \yii\base\Module |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var bool Whether the container should attempt to run migrations on launch. |
16
|
|
|
*/ |
17
|
|
|
public $runMigrations = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The variables will be populated via the pool config. |
21
|
|
|
* @var string[] List of required environment variables. If one is missing the container will exit. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
public $environmentVariables = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array Pool directives |
28
|
|
|
* @see http://php.net/manual/en/install.fpm.configuration.php |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
public $poolConfig = [ |
32
|
|
|
'user' => 'nobody', |
33
|
|
|
'group' => 'nobody', |
34
|
|
|
'listen' => 9000, |
35
|
|
|
'pm' => 'dynamic', |
36
|
|
|
'pm.max_children' => 40, |
37
|
|
|
'pm.start_servers' => 3, |
38
|
|
|
'pm.min_spare_servers' => 1, |
39
|
|
|
'pm.max_spare_servers' => 3, |
40
|
|
|
'access.log' => '/proc/self/fd/2', |
41
|
|
|
'clear_env' => 'yes', |
42
|
|
|
|
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array PHP configuration, supplied via php_admin_value in fpm config. |
47
|
|
|
*/ |
48
|
|
|
public $phpConfig = [ |
49
|
|
|
'upload_max_filesize' => '20M', |
50
|
|
|
'post_max_size' => '25M' |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array Global directives |
55
|
|
|
* @see http://php.net/manual/en/install.fpm.configuration.php |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
|
public $fpmConfig = [ |
59
|
|
|
'error_log' => '/proc/self/fd/2', |
60
|
|
|
'daemonize' => 'no', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
public $extensions = [ |
64
|
|
|
'ctype', |
65
|
|
|
'gd', |
66
|
|
|
'iconv', |
67
|
|
|
'intl', |
68
|
|
|
'json', |
69
|
|
|
'mbstring', |
70
|
|
|
'session', |
71
|
|
|
'pdo_mysql', |
72
|
|
|
'session', |
73
|
|
|
'curl' |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var Name and optionally tag of the image. |
78
|
|
|
* |
79
|
|
|
*/ |
80
|
|
|
public $image; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var string Location of composer.json / composer.lock |
84
|
|
|
*/ |
85
|
|
|
public $composerFilePath = '@app/../'; |
86
|
|
|
/** |
87
|
|
|
* @return string A PHP-FPM config file. |
88
|
|
|
*/ |
89
|
1 |
|
protected function createFpmConfig() |
90
|
|
|
{ |
91
|
1 |
|
$config = []; |
92
|
|
|
// Add global directives. |
93
|
1 |
|
if (!empty($this->fpmConfig)) { |
94
|
1 |
|
$config[] = '[global]'; |
95
|
1 |
|
foreach ($this->fpmConfig as $key => $value) { |
96
|
1 |
|
$config[] = "$key = $value"; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Add pool directives. |
101
|
1 |
|
$poolConfig = $this->poolConfig; |
102
|
1 |
|
foreach($this->phpConfig as $key => $value) { |
103
|
1 |
|
$poolConfig["php_admin_value[$key]"] = $value; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
foreach($this->environmentVariables as $name) { |
107
|
|
|
$poolConfig["env[$name]"] = "$$name"; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
if (!empty($poolConfig)) { |
111
|
1 |
|
$config[] = '[www]'; |
112
|
1 |
|
foreach ($poolConfig as $key => $value) { |
113
|
1 |
|
$config[] = "$key = $value"; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return \implode("\n", $config); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string A shell script that checks for existence of (non-empty) variables and runs php-fpm. |
122
|
|
|
*/ |
123
|
1 |
|
protected function createEntrypoint(): string |
124
|
|
|
{ |
125
|
1 |
|
$result = []; |
126
|
1 |
|
$result[] = '#!/bin/sh'; |
127
|
|
|
// Check for variables. |
128
|
1 |
|
foreach($this->environmentVariables as $name) { |
129
|
|
|
$result[] = \strtr('if [ -z "${name}" ]; then echo "Variable \${name} is required."; exit 1; fi', [ |
130
|
|
|
'{name}' => $name |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
if ($this->runMigrations) { |
135
|
|
|
$result[] = <<<SH |
136
|
|
|
ATTEMPTS=0 |
137
|
|
|
while [ \$ATTEMPTS -lt 10 ]; do |
138
|
|
|
# First run migrations. |
139
|
|
|
/project/protected/yiic migrate/up --interactive=0 |
140
|
|
|
if [ $? -eq 0 ]; then |
141
|
|
|
echo "Migrations done"; |
142
|
|
|
break; |
143
|
|
|
fi |
144
|
|
|
echo "Failed to run migrations, retrying in 10s."; |
145
|
|
|
sleep 10; |
146
|
|
|
let ATTEMPTS=ATTEMPTS+1 |
147
|
|
|
done |
148
|
|
|
|
149
|
|
|
if [ \$ATTEMPTS -gt 9 ]; then |
150
|
|
|
echo "Migrations failed.." |
151
|
|
|
exit 1; |
152
|
|
|
fi |
153
|
|
|
SH; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
$result[] = 'exec php-fpm7 --force-stderr --fpm-config /php-fpm.conf'; |
157
|
1 |
|
return \implode("\n", $result); |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
public function createBuildContext(): Context |
161
|
|
|
{ |
162
|
1 |
|
static $builder; |
163
|
1 |
|
$builder = new ContextBuilder(); |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* BEGIN COMPOSER |
167
|
|
|
*/ |
168
|
1 |
|
$builder->from('composer'); |
169
|
1 |
|
$builder->addFile('/build/composer.json', \Yii::getAlias($this->composerFilePath) .'/composer.json'); |
170
|
1 |
|
if (\file_exists(\Yii::getAlias($this->composerFilePath) . '/composer.lock')) { |
171
|
1 |
|
$builder->addFile('/build/composer.lock', \Yii::getAlias($this->composerFilePath) . '/composer.lock'); |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
$builder->run('cd /build && composer install --no-dev --no-autoloader --ignore-platform-reqs'); |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
// Add the actual source code. |
178
|
1 |
|
$root = \Yii::getAlias('@app'); |
179
|
1 |
|
$builder->addFile('/build/' . \basename($root), $root); |
|
|
|
|
180
|
1 |
|
$builder->run('cd /build && composer dumpautoload -o'); |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* END COMPOSER |
184
|
|
|
*/ |
185
|
|
|
|
186
|
|
|
|
187
|
1 |
|
$builder->from('alpine:edge'); |
188
|
|
|
$packages = [ |
189
|
1 |
|
'php7', |
190
|
|
|
'php7-fpm', |
191
|
|
|
'tini', |
192
|
|
|
'ca-certificates' |
193
|
|
|
]; |
194
|
1 |
|
foreach ($this->extensions as $extension) { |
195
|
1 |
|
$packages[] = "php7-$extension"; |
196
|
|
|
} |
197
|
1 |
|
$builder->run('apk add --update --no-cache ' . \implode(' ', $packages)); |
198
|
1 |
|
$builder->volume('/runtime'); |
199
|
1 |
|
$builder->copy('--from=0 /build', '/project'); |
200
|
1 |
|
$builder->add('/entrypoint.sh', $this->createEntrypoint()); |
201
|
1 |
|
$builder->run('chmod +x /entrypoint.sh'); |
202
|
1 |
|
$builder->add('/php-fpm.conf', $this->createFpmConfig()); |
203
|
1 |
|
$builder->run("php-fpm7 --force-stderr --fpm-config /php-fpm.conf -t"); |
204
|
1 |
|
$builder->entrypoint('["/sbin/tini", "--", "/entrypoint.sh"]'); |
205
|
|
|
|
206
|
|
|
|
207
|
1 |
|
$builder->run('find /project | wc -l'); |
208
|
1 |
|
return $builder->getContext(); |
209
|
|
|
} |
210
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.