-
Notifications
You must be signed in to change notification settings - Fork 155
/
compile
executable file
·522 lines (408 loc) · 15.5 KB
/
compile
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#!/bin/bash
set -e
set -o pipefail
shopt -s dotglob
basedir="$( cd -P "$( dirname "$0" )" && pwd )"
source "$basedir/../conf/buildpack.conf"
source $basedir/common.sh
if [ -n "$BUILDPACK_DEBUG" ]; then
set -x
fi
BUILD_DIR="$1"
CACHE_DIR="$2"
cd "$BUILD_DIR"
mkdir -p "$CACHE_DIR/package"
function fetch_engine_package() {
local engine="$1"
local version="$2"
local location="$3"
fetch_package "${engine}-${version}" "$location"
}
function fetch_package() {
local package="$1"
local location="$2"
mkdir -p "$location"
local checksum_url="http://${S3_BUCKET}.s3.amazonaws.com/package/${package}.md5"
local package_url="http://${S3_BUCKET}.s3.amazonaws.com/package/${package}.tgz"
local checksum=$(curl "$checksum_url" 2> /dev/null)
local cache_checksum=
if [ -f "$CACHE_DIR/package/${package}.md5" ]; then
local cache_checksum=$(cat "$CACHE_DIR/package/${package}.md5")
fi
mkdir -p "$CACHE_DIR/package/$(dirname "$package")"
if [ "$cache_checksum" != "$checksum" ]; then
curl "$package_url" -L -s > "$CACHE_DIR/package/${package}.tgz"
echo "$checksum" > "$CACHE_DIR/package/${package}.md5"
else
echo "Checksums match. Fetching from cache."
fi
tar xzf "$CACHE_DIR/package/${package}.tgz" -C "$location"
}
function log_error() {
local c='s/^/ ! /'
case $(uname) in
Darwin) echo "$*" | sed -l "$c";;
*) echo "$*" | sed -u "$c";;
esac
}
function indent() {
local c='s/^/ /'
case $(uname) in
Darwin) sed -l "$c";;
*) sed -u "$c";;
esac
}
function install_composer_deps() {
local cwd=$(pwd)
local target="$1"
if [ ! -f "$target/composer.json" ]; then
return 0
fi
if [ ! -f "$target/composer.lock" ]; then
log_error "Lockfile required. Please check it in."
return 1
fi
export COMPOSER_CACHE_DIR=$CACHE_DIR/composer
mkdir -p $COMPOSER_CACHE_DIR
mkdir -p "$target/vendor/composer/bin"
local checksum=$(curl --silent "http://${S3_BUCKET}.s3.amazonaws.com/composer/composer.phar.md5")
status "Vendoring Composer"
if [ ! -f "$CACHE_DIR/composer.phar.md5" ] || [ "$(cat $CACHE_DIR/composer.phar.md5)" != "$checksum" ]; then
echo "Updating Composer" | indent
curl --silent "http://${S3_BUCKET}.s3.amazonaws.com/composer/composer.phar" > "$CACHE_DIR/composer.phar" | indent
chmod a+x "$CACHE_DIR/composer.phar"
echo "$checksum" > $CACHE_DIR/composer.phar.md5
fi
cp "$CACHE_DIR/composer.phar" "$target/vendor/composer/bin/"
local required_extensions=$(jq --raw-output '.require | keys | .[]' < "$BUILD_DIR/composer.json" | grep '^ext-' | sed 's/^ext-//')
local ext_dir=/app/vendor/php/lib/php/extensions/no-debug-non-zts-$(php_api_version)
if [ -n "$required_extensions" ]; then
status "Bundling additional extensions"
for ext in $required_extensions; do
echo "$ext" | indent
local ext_ini="/app/vendor/php/etc/conf.d/$ext.ini"
if [ -f "$ext_ini" ]; then
echo "Already installed" | indent
elif [ -f "$ext_dir/$ext.so" ]; then
echo "Enabling bundled extension" | indent
echo "extension=$ext.so" > "$ext_ini"
else
local ext_dependency=${PHP_EXT_DEPENDENCIES["$ext"]}
if [ -n "$ext_dependency" ]; then
local ext_dependency_target=${DEPENDENCY_TARGET["$ext_dependency"]}
if [ -n "$ext_dependency_target" ]; then
fetch_package "$ext_dependency" "$ext_dependency_target" | indent
ADDITIONAL_INSTALLED_LIBS+=("$ext_dependency_target")
fi
fi
# TODO: Find a better way to ignore extensions which were not found in S3
fetch_package "ext/$(php_api_version)/php-${ext}" "/app/vendor/php" 2>/dev/null | indent || true
fi
done
fi
php "vendor/composer/bin/composer.phar" self-update
if [ -n "$COMPOSER_GITHUB_TOKEN" ]; then
status "Configuring the github authentication for Composer"
php "vendor/composer/bin/composer.phar" config -g github-oauth.github.com "$COMPOSER_GITHUB_TOKEN" --no-interaction
fi
status "Installing application dependencies with Composer"
{
cd "$target"
php "vendor/composer/bin/composer.phar" install \
--prefer-dist \
--optimize-autoloader \
--no-interaction \
--no-dev \
2>&1
cd "$cwd"
} | indent
}
function install_node_deps() {
local cwd=$(pwd)
local target="$1"
if [ ! -f "$target/package.json" ]; then
return 0
fi
status "NODE (package.json) app detected"
source ${basedir}/compile_node $BUILD_DIR $CACHE_DIR
local node_path='vendor/node'
# Symlink node to /app/vendor/node so node and its modules are runnable.
ln -s ${BUILD_DIR}/${node_path}/ /app/${node_path}
ln -s ${BUILD_DIR}/node_modules /app/node_modules
echo "Node " `node -v` | indent
}
export_env_dir() {
env_dir=$1
blacklist_regex=${3:-'^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH|LD_LIBRARY_PATH)$'}
if [ -d "$env_dir" ]; then
for e in $(ls $env_dir); do
echo "$e" | grep -qvE "$blacklist_regex" &&
export "$e=$(cat $env_dir/$e)"
:
done
fi
}
function mktmpdir() {
dir=$(mktemp -t php-$1-XXXX)
rm -rf $dir
mkdir -p $dir
echo $dir
}
function php_api_version() {
basename "$(php-config --extension-dir)" | tr '-' ' ' | cut -f 5 -d ' '
}
function install_newrelic() {
status "Enabling Newrelic support"
local version="$1"
local cwd=$(pwd)
local ext_dir=/app/vendor/php/lib/php/extensions/no-debug-non-zts-$(php_api_version)
local tempdir=$(mktmpdir "newrelic")
cd "$tempdir"
curl --silent -L "http://download.newrelic.com/php_agent/archive/${version}/newrelic-php5-${version}-linux.tar.gz" | tar xz
mkdir -p $BUILD_DIR/vendor/newrelic/{bin,etc}
cd "newrelic-php5-${version}-linux"
cp -f "daemon/newrelic-daemon.x64" $BUILD_DIR/vendor/newrelic/bin/newrelic-daemon
cp -f "agent/x64/newrelic-$(php_api_version).so" "${ext_dir}/newrelic.so"
cp -f "$basedir/../conf/php/newrelic.ini" "/app/vendor/php/etc/conf.d/newrelic.ini"
cd "$cwd"
}
function package_document_root() {
jq --raw-output '.extra.heroku["document-root"] // ""' < "$BUILD_DIR/composer.json"
}
function package_index_file() {
jq --raw-output '.extra.heroku["index-document"] // "index.php"' < "$BUILD_DIR/composer.json"
}
function package_framework() {
jq --raw-output '.extra.heroku.framework // ""' < "$BUILD_DIR/composer.json"
}
function package_nginx_version() {
jq --raw-output ".extra.heroku.engines.nginx // \"default\"" < "$BUILD_DIR/composer.json"
}
function package_php_version() {
jq --raw-output ".extra.heroku.engines.php // \"default\"" < "$BUILD_DIR/composer.json"
}
function package_php_config() {
jq --raw-output '.extra.heroku["php-config"] // [] | .[]' < "$BUILD_DIR/composer.json"
}
function package_php_includes() {
jq --raw-output '.extra.heroku["php-includes"] // [] | .[]' < "$BUILD_DIR/composer.json"
}
function package_nginx_includes() {
jq --raw-output '.extra.heroku["nginx-includes"] // [] | .[]' < "$BUILD_DIR/composer.json"
}
function package_log_files() {
jq --raw-output '.extra.heroku["log-files"] // [] | .[]' < "$BUILD_DIR/composer.json"
}
function package_compile_cmd() {
jq --raw-output '.extra.heroku["compile"] // [] | .[]' < "$BUILD_DIR/composer.json"
}
function package_newrelic_enabled() {
local val=$(jq --raw-output '.extra.heroku["newrelic"] // false' < "$BUILD_DIR/composer.json")
if [ "$val" = "true" ]; then
return 0
else
return 1
fi
}
export_env_dir "$3"
# Download jq binary for JSON processing
export PATH="$HOME/bin:$PATH"
curl "http://${S3_BUCKET}.s3.amazonaws.com/jq/jq" -L -s -o - > "$HOME/bin/jq"
chmod +x "$HOME/bin/jq"
DEFAULT_PHP="5.5.24"
DEFAULT_NGINX="1.4.4"
AVAILABLE_PHP_VERSIONS=$(curl "http://${S3_BUCKET}.s3.amazonaws.com/manifest.php" 2> /dev/null)
AVAILABLE_NGINX_VERSIONS=$(curl "http://${S3_BUCKET}.s3.amazonaws.com/manifest.nginx" 2> /dev/null)
MCRYPT_VERSION="2.5.8"
PHP_VERSION="default"
NGINX_VERSION="default"
DOCUMENT_ROOT=
INDEX_DOCUMENT="index.php"
FRAMEWORK=
PHP_EXTRA_CONFIG=
PHP_INCLUDES=
COMPILE_CMD=
NGINX_INCLUDES=
NEWRELIC_VERSION=4.4.5.35
LOG_FILES=( "/app/vendor/nginx/logs/access.log" "/app/vendor/nginx/logs/error.log" "/app/vendor/php/var/log/error.log" )
ADDITIONAL_INSTALLED_LIBS=()
# Read config variables from composer.json if it exists
if [ -f "$BUILD_DIR/composer.json" ]; then
PHP_VERSION=$(package_php_version)
NGINX_VERSION=$(package_nginx_version)
DOCUMENT_ROOT=$(package_document_root)
INDEX_DOCUMENT=$(package_index_file)
FRAMEWORK=$(package_framework)
PHP_EXTRA_CONFIG=$(package_php_config)
PHP_INCLUDES=$(package_php_includes)
COMPILE_CMD=$(package_compile_cmd)
NGINX_INCLUDES=$(package_nginx_includes)
USER_LOG_FILES=$(package_log_files)
fi
# Try reading PHP version from .php-version file in project
if [ -f "$BUILD_DIR/.php-version" ]; then
PHP_VERSION=$(cat "$BUILD_DIR/.php-version")
fi
if [ "$PHP_VERSION" = "default" ]; then
PHP_VERSION="$DEFAULT_PHP"
fi
if [ "$NGINX_VERSION" = "default" ]; then
NGINX_VERSION="$DEFAULT_NGINX"
fi
# Look for ".*" versions, match them against all available versions
# and select the latest version which was found.
if echo "$PHP_VERSION" | grep '\*' &>/dev/null; then
PHP_VERSION=$(echo "$AVAILABLE_PHP_VERSIONS" | grep "^$PHP_VERSION$" | sort -r -V | head -n1)
fi
if echo "$NGINX_VERSION" | grep '\*' &>/dev/null; then
NGINX_VERSION=$(echo "$AVAILABLE_NGINX_VERSIONS" | grep "^$NGINX_VERSION$" | sort -r -V | head -n1)
fi
VENDORED_NGINX=/app/vendor/nginx
VENDORED_PHP=/app/vendor/php
[ ! -d "$BUILD_DIR/vendor" ] && mkdir -p "$BUILD_DIR/vendor"
status "Bundling NGINX ${NGINX_VERSION}"
fetch_engine_package nginx "$NGINX_VERSION" /app/vendor/nginx | indent
status "Bundling PHP ${PHP_VERSION}"
# Install libraries also on build container, so PHP doesn't wayne about missing
# dynamic libraries when running it during the slug compilation.
fetch_package "libicu-51" /app/vendor/libicu > /dev/null
fetch_package "libmcrypt-${MCRYPT_VERSION}" /app/vendor/libmcrypt > /dev/null
fetch_engine_package php "$PHP_VERSION" /app/vendor/php | indent
test ! -d ".profile.d" && mkdir -p .profile.d || true
cat > ".profile.d/php.sh" <<SH
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/app/bin:/app/vendor/nginx/sbin:/app/vendor/php/sbin:/app/vendor/php/bin:/app/vendor/bin:\$PATH
export HEROKU_BUILD_TIME=$(date +%Y%m%d%H%M%S)
SH
source ".profile.d/php.sh"
# Fetch additional extensions
status "Bundling extensions"
echo " apcu"
fetch_package "ext/$(php_api_version)/php-apcu" "/app/vendor/php" | indent
echo " phpredis"
fetch_package "ext/$(php_api_version)/php-redis" "/app/vendor/php" | indent
echo " mongo"
fetch_package "ext/$(php_api_version)/php-mongo" "/app/vendor/php" | indent
if [ -f "$BUILD_DIR/composer.json" ] && package_newrelic_enabled; then
install_newrelic "$NEWRELIC_VERSION"
fi
if [ -n "$BUILDPACK_DEBUG" ]; then
ls -R /app/vendor/nginx
ls -R /app/vendor/php
fi
mkdir -p "conf"
cp "$basedir/util/autotune.php" "conf/autotune.php"
cp "$basedir/../conf/nginx/base.conf.erb" "conf/nginx.conf.erb"
cp "$basedir/../conf/php/php-fpm.conf" "/app/vendor/php/etc/php-fpm.conf"
cp "$basedir/../conf/php/php.ini" "/app/vendor/php/etc/php.ini"
pear config-set php_ini /app/vendor/php/etc/php.ini &> /dev/null
for conf in $PHP_EXTRA_CONFIG; do
echo "$conf" >> "/app/vendor/php/etc/php.ini"
done
for include in $PHP_INCLUDES; do
cp "$BUILD_DIR/$include" "/app/vendor/php/etc/conf.d/"
done
# Detect PHP framework
for f in "$basedir/../frameworks/"*; do
if "$f" detect "$BUILD_DIR"; then
FRAMEWORK="$f"
fi
done
# Fall back to classic mode
if [ -z "$FRAMEWORK" ]; then
FRAMEWORK="$basedir/../frameworks/default"
fi
# Try to load the framework from the "frameworks" directory if it's just a
# simple framework name like "symfony2"
if [ ! -f "$FRAMEWORK" ] && [ -f "$basedir/../frameworks/$FRAMEWORK" ]; then
FRAMEWORK="$basedir/../frameworks/$FRAMEWORK"
fi
if [ ! -f "$FRAMEWORK" ]; then
log_error "Framework \"$FRAMEWORK\" not found!"
exit 1
fi
"$FRAMEWORK" compile "$BUILD_DIR" "$CACHE_DIR"
install_node_deps "$BUILD_DIR"
install_composer_deps "$BUILD_DIR"
LOG_FILES=$(unique_array ${LOG_FILES[@]} ${USER_LOG_FILES[@]})
if [ -n "$FRAMEWORK" ] && [ -f "$FRAMEWORK" ]; then
FRAMEWORK_LOG_FILES=$("$FRAMEWORK" get-log-files)
LOG_FILES=$(unique_array ${LOG_FILES[@]} ${FRAMEWORK_LOG_FILES[@]})
fi
if [ -n "$COMPILE_CMD" ]; then
status "Running compile commands"
while read -r cmd; do
echo "Running '$cmd'" | indent
eval $cmd | indent
done <<< "$COMPILE_CMD"
fi
if [ -n "$FRAMEWORK" ] && [ -f "$FRAMEWORK" ]; then
"$FRAMEWORK" end "$BUILD_DIR" "$CACHE_DIR"
fi
"$FRAMEWORK" post-compile "$BUILD_DIR" "$CACHE_DIR" || true
status "Vendoring binaries into slug"
ADDITIONAL_INSTALLED_LIBS=$(unique_array ${ADDITIONAL_INSTALLED_LIBS[@]})
mv /app/vendor/libmcrypt vendor/libmcrypt
mv /app/vendor/libicu vendor/libicu
mv /app/vendor/nginx vendor/nginx
mv /app/vendor/php vendor/php
for lib in $ADDITIONAL_INSTALLED_LIBS; do
LIB_TARGET=$(basename "$lib")
mv "$lib" "vendor/$LIB_TARGET"
done
mkdir -p "bin"
cat > "bin/run" <<SH
#!/usr/bin/env bash
pmsgr=/tmp/pmsgr
rm -f \$pmsgr
mkfifo \$pmsgr
for var in \`env | cut -f1 -d=\`; do
echo "env[\$var] = \\$\${var}" >> /app/vendor/php/etc/php-fpm.conf
done
export DOCUMENT_ROOT="$DOCUMENT_ROOT"
export INDEX_DOCUMENT="$INDEX_DOCUMENT"
export NGINX_INCLUDES="$NGINX_INCLUDES"
if [ -n "\$NEW_RELIC_LICENSE_KEY" ]; then
echo "newrelic.license=\"\$NEW_RELIC_LICENSE_KEY\"" > /app/vendor/php/etc/conf.d/newrelic_license.ini
fi
erb conf/nginx.conf.erb > /app/vendor/nginx/conf/nginx.conf
erb conf/site.conf.erb > /app/vendor/nginx/conf/site.conf
if [[ -z \${WEB_CONCURRENCY:-} ]]; then
maxprocs=\$(ulimit -u)
ram="512M"
if [[ -n \${DYNO:-} && "\$maxprocs" == "32768" ]]; then
echo "Optimizing defaults for PX dyno...." >&2
ram="6G"
ram="2560M"
elif [[ -n ${DYNO:-} && "$maxprocs" == "16384" ]]; then
echo "Optimizing defaults for IX dyno...." >&2
elif [[ -n \${DYNO:-} && "\$maxprocs" == "512" ]]; then
echo "Optimizing defaults for 2X dyno..." >&2
ram="1G"
elif [[ -n \${DYNO:-} && "\$maxprocs" == "256" ]]; then
echo "Optimzing defaults for 1X dyno..." >&2
elif [[ -n \${DYNO:-} || \$verbose ]]; then
echo "No dyno detected; using defaults for 1X..." >&2
fi
# determine number of FPM processes to run
read WEB_CONCURRENCY php_memory_limit <<<\$(php -c "/app/vendor/php/etc/php.ini" /app/conf/autotune.php -y "/app/vendor/php/etc/php-fpm.conf" -t "\$DOCUMENT_ROOT" "\$ram")
[[ \$WEB_CONCURRENCY -lt 1 ]] && WEB_CONCURRENCY=1
export WEB_CONCURRENCY
echo "\${WEB_CONCURRENCY} processes at \${php_memory_limit}B memory limit." >&2
else
echo "Using WEB_CONCURRENCY=\${WEB_CONCURRENCY} processes." >&2
fi
`init_log_plex ${LOG_FILES}`
`tail_log_plex ${LOG_FILES} ${SYS_LOG_FILES}`
(
php-fpm -p "/app/vendor/php"
echo "php-fpm" > \$pmsgr
)&
(
nginx -p "/app/vendor/nginx" -c /app/vendor/nginx/conf/nginx.conf
echo "nginx" > \$pmsgr
)&
read exitproc <\$pmsgr
echo "Boot failed: \$exitproc"
exit 1
SH
chmod +x "bin/run"