-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.php
349 lines (292 loc) · 8.67 KB
/
bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
define('_ATOM_DIR', '/atom/src');
define('_ETC_DIR', '/etc/php/7.4');
define('_PHP_DIR', _ETC_DIR."/cli");
define('_FPM_DIR', _ETC_DIR."/fpm");
function getenv_default($name, $default)
{
$value = getenv($name);
if (false === $value) {
return $default;
}
return $value;
}
function getenv_or_fail($name)
{
$value = getenv($name);
if (false === $value) {
echo "Environment variable {$name} is not defined!";
exit(1);
}
return $value;
}
function get_host_and_port($value, $default_port)
{
$parts = explode(':', $value);
if (1 == count($parts)) {
$parts[1] = $default_port;
}
return ['host' => $parts[0], 'port' => $parts[1]];
}
$CONFIG = [
'atom.development_mode' => filter_var(getenv_default('ATOM_DEVELOPMENT_MODE', false), FILTER_VALIDATE_BOOLEAN),
'atom.coverage' => filter_var(getenv_default('ATOM_COVERAGE', false), FILTER_VALIDATE_BOOLEAN),
'atom.elasticsearch_host' => getenv_or_fail('ATOM_ELASTICSEARCH_HOST'),
'atom.memcached_host' => getenv_or_fail('ATOM_MEMCACHED_HOST'),
'atom.gearmand_host' => getenv_or_fail('ATOM_GEARMAND_HOST'),
'atom.mysql_dsn' => getenv_or_fail('ATOM_MYSQL_DSN'),
'atom.mysql_username' => getenv_or_fail('ATOM_MYSQL_USERNAME'),
'atom.mysql_password' => getenv_or_fail('ATOM_MYSQL_PASSWORD'),
'atom.debug_ip' => getenv_default('ATOM_DEBUG_IP', ''),
'php.max_execution_time' => getenv_default('ATOM_PHP_MAX_EXECUTION_TIME', '120'),
'php.max_input_time' => getenv_default('ATOM_PHP_MAX_INPUT_TIME', '120'),
'php.memory_limit' => getenv_default('ATOM_PHP_MEMORY_LIMIT', '1024'),
'php.post_max_size' => getenv_default('ATOM_PHP_POST_MAX_SIZE', '72M'),
'php.upload_max_filesize' => getenv_default('ATOM_PHP_UPLOAD_MAX_FILESIZE', '64M'),
'php.max_file_uploads' => getenv_default('ATOM_PHP_MAX_FILE_UPLOADS', '20'),
'php.date.timezone' => getenv_default('ATOM_PHP_DATE_TIMEZONE', 'Australia/Brisbane'),
];
//
// /apps/qubit/config/settings.yml
//
if (!file_exists(_ATOM_DIR.'/apps/qubit/config/settings.yml')) {
copy(_ATOM_DIR.'/apps/qubit/config/settings.yml.tmpl', _ATOM_DIR.'/apps/qubit/config/settings.yml');
}
//
// /config/propel.ini
//
@unlink(_ATOM_DIR.'/config/propel.ini');
copy(_ATOM_DIR.'/config/propel.ini.tmpl', _ATOM_DIR.'/config/propel.ini');
//
// /apps/qubit/config/gearman.yml
//
$gearman_yml = <<<EOT
all:
servers:
default: ${CONFIG['atom.gearmand_host']}
EOT;
@unlink(_ATOM_DIR.'/apps/qubit/config/gearman.yml');
file_put_contents(_ATOM_DIR.'/apps/qubit/config/gearman.yml', $gearman_yml);
//
// /apps/qubit/config/app.yml
//
if (!file_exists(_ATOM_DIR.'/apps/qubit/config/app.yml')) {
$parts = get_host_and_port($CONFIG['atom.memcached_host'], 11211);
$app_yml = <<<EOT
all:
upload_limit: -1
download_timeout: 10
cache_engine: sfMemcacheCache
cache_engine_param:
host: ${parts['host']}
port: ${parts['port']}
prefix: atom
storeCacheInfo: true
persistent: true
read_only: false
htmlpurifier_enabled: false
csp:
response_header: Content-Security-Policy-Report-Only
directives: "default-src 'self'; font-src 'self'; img-src 'self' https://www.gravatar.com/avatar/ https://*.google-analytics.com https://*.googletagmanager.com blob:; script-src 'self' https://*.googletagmanager.com 'nonce'; style-src 'self' 'nonce'; worker-src 'self' blob:; connect-src https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com; frame-ancestors 'self';"
EOT;
file_put_contents(_ATOM_DIR.'/apps/qubit/config/app.yml', $app_yml);
}
//
// /apps/qubit/config/factories.yml
//
if (!file_exists(_ATOM_DIR.'/apps/qubit/config/factories.yml')) {
$parts = get_host_and_port($CONFIG['atom.memcached_host'], 11211);
$factories_yml = <<<EOT
prod:
storage:
class: QubitCacheSessionStorage
param:
session_name: symfony
session_cookie_httponly: true
session_cookie_secure: true
cache:
class: sfMemcacheCache
param:
host: ${parts['host']}
port: ${parts['port']}
prefix: atom
storeCacheInfo: true
persistent: true
dev:
storage:
class: QubitCacheSessionStorage
param:
session_name: symfony
session_cookie_httponly: true
session_cookie_secure: true
cache:
class: sfMemcacheCache
param:
host: ${parts['host']}
port: ${parts['port']}
prefix: atom
storeCacheInfo: true
persistent: true
EOT;
file_put_contents(_ATOM_DIR.'/apps/qubit/config/factories.yml', $factories_yml);
}
//
// /config/search.yml
//
$parts = get_host_and_port($CONFIG['atom.elasticsearch_host'], 9200);
$search_yml = <<<EOT
all:
server:
host: ${parts['host']}
post: ${parts['port']}
EOT;
@unlink(_ATOM_DIR.'/config/search.yml');
file_put_contents(_ATOM_DIR.'/config/search.yml', $search_yml);
//
// /config/config.php
//
$config_php = <<<EOT
<?php
return [
'all' => [
'propel' => [
'class' => 'sfPropelDatabase',
'param' => [
'encoding' => 'utf8mb4',
'persistent' => true,
'pooling' => true,
'dsn' => '${CONFIG['atom.mysql_dsn']}',
'username' => '${CONFIG['atom.mysql_username']}',
'password' => '${CONFIG['atom.mysql_password']}',
],
],
],
'dev' => [
'propel' => [
'param' => [
'classname' => 'DebugPDO',
'debug' => [
'realmemoryusage' => true,
'details' => [
'time' => [
'enabled' => true,
],
'slow' => [
'enabled' => true,
'threshold' => 0.1,
],
'mem' => [
'enabled' => true,
],
'mempeak' => [
'enabled' => true,
],
'memdelta' => [
'enabled' => true,
],
],
],
],
],
],
'test' => [
'propel' => [
'param' => [
'classname' => 'DebugPDO',
],
],
],
];
EOT;
@unlink(_ATOM_DIR.'/config/config.php');
file_put_contents(_ATOM_DIR.'/config/config.php', $config_php);
//
// php ini
//
$php_ini = <<<EOT
[PHP]
output_buffering = 4096
expose_php = off
log_errors = on
error_reporting = E_ALL
display_errors = stderr
display_startup_errors = on
max_execution_time = ${CONFIG['php.max_execution_time']}
max_input_time = ${CONFIG['php.max_input_time']}
memory_limit = ${CONFIG['php.memory_limit']}
log_errors = on
post_max_size = ${CONFIG['php.post_max_size']}
default_charset = UTF-8
cgi.fix_pathinfo = off
upload_max_filesize = ${CONFIG['php.upload_max_filesize']}
max_file_uploads = ${CONFIG['php.max_file_uploads']}
date.timezone = ${CONFIG['php.date.timezone']}
session.use_only_cookies = off
opcache.fast_shutdown = on
opcache.max_accelerated_files = 10000
opcache.validate_timestamps = off
pcov.enabled = 0
EOT;
if ($CONFIG['atom.development_mode']) {
$php_ini .= <<<EOT
\n
# Development-specific configuration
expose_php = on
opcache.validate_timestamps = on
EOT;
}
if ($CONFIG['atom.coverage']) {
$php_ini .= <<<'EOT'
pcov.enabled = 1
EOT;
}
@unlink(_PHP_DIR.'/php.ini');
file_put_contents(_PHP_DIR.'/php.ini', $php_ini);
@unlink(_FPM_DIR.'/php.ini');
file_put_contents(_FPM_DIR.'/php.ini', $php_ini);
//
// fpm ini
//
$fpm_ini = <<<'EOT'
[global]
error_log = /proc/self/fd/2
daemonize = no
[atom]
access.log = /proc/self/fd/2
clear_env = no
catch_workers_output = yes
user = root
group = root
listen = [::]:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
EOT;
// Get debug IPs:
// - Always add the env. var. if it's defined.
// - In dev. mode, find and append the container gateway
// if it's not the same, to allow access from the host.
$debugIps = [];
if ($CONFIG['atom.debug_ip']) {
$debugIps[] = $CONFIG['atom.debug_ip'];
}
if ($CONFIG['atom.development_mode']) {
$ret = exec('ip -4 route show default | cut -d" " -f3');
if ($ret && $ret != $CONFIG['atom.debug_ip']) {
$debugIps[] = $ret;
}
}
if ($debugIps) {
$debugIps = implode(',', $debugIps);
$fpm_ini .= <<<EOT
env[ATOM_DEBUG_IP] = {$debugIps}
EOT;
}
@unlink(_FPM_DIR.'/pool.d/atom.conf');
file_put_contents(_FPM_DIR.'/pool.d/atom.conf', $fpm_ini);
//
// sf symlink
//
@symlink(_ATOM_DIR.'/vendor/symfony/data/web/sf', _ATOM_DIR.'/sf');