-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·126 lines (111 loc) · 3.81 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
executable file
·126 lines (111 loc) · 3.81 KB
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
#!/usr/bin/env sh
set -eu
# Which of the three long-running processes this container is.
#
# One image, three roles: the web server, the Horizon master, and the
# scheduler. They can be selected two ways, because the platforms that run
# this disagree about which they offer:
#
# docker run bilis horizon — an argument, when the platform lets
# you override the command.
# BILIS_ROLE=horizon — an environment variable, for platforms
# (Coolify's Dockerfile build pack among
# them) whose only per-resource knob is
# the environment.
#
# An explicit argument wins, so `docker run bilis artisan migrate` still works
# on a host whose environment says `horizon`. With neither, it is the web role.
#
# Flags ride with the argument form (`horizon --environment=production`).
# `BILIS_ROLE` carries the role and nothing else: bare argv already means "run
# this command verbatim", and it cannot also mean "flags for the role in the
# environment".
KNOWN_ROLES="web horizon scheduler artisan"
is_known_role() {
for known in $KNOWN_ROLES; do
[ "$1" = "$known" ] && return 0
done
return 1
}
resolve_role() {
if [ "$#" -gt 0 ] && is_known_role "$1"; then
echo "$1"
return 0
fi
# Anything else in argv is a command to run verbatim, not a role, and the
# environment must not hijack it.
if [ "$#" -gt 0 ]; then
echo ""
return 0
fi
role="${BILIS_ROLE:-web}"
# A typo here is the worst kind of failure: `BILIS_ROLE=horzion` would
# otherwise start a second web server, and the only symptom would be a
# queue that never drains, hours later, with nothing in any log to say why.
if ! is_known_role "$role"; then
echo "bilis-entrypoint: unknown BILIS_ROLE '$role' (expected one of: $KNOWN_ROLES)" >&2
exit 64
fi
echo "$role"
}
prepare_storage() {
mkdir -p \
storage/app/private \
storage/app/public \
storage/framework/cache/data \
storage/framework/sessions \
storage/framework/testing \
storage/framework/views \
storage/logs \
bootstrap/cache
if [ "${DB_CONNECTION:-sqlite}" = "sqlite" ]; then
database_path="${DB_DATABASE:-/app/database/database.sqlite}"
mkdir -p "$(dirname "$database_path")"
touch "$database_path"
fi
}
prepare_laravel() {
if [ "${BILIS_OPTIMIZE_ON_STARTUP:-true}" = "true" ]; then
php artisan optimize
fi
if [ "${BILIS_MIGRATE_ON_STARTUP:-false}" = "true" ]; then
php artisan migrate --force
php artisan clickhouse:migrate --no-interaction
fi
}
role="$(resolve_role "$@")"
# The healthcheck runs in its own process with no view of this container's
# argv, so record the role it resolved to. Best effort: a read-only or
# unwritable path must not stop the service from starting — the healthcheck
# falls back to BILIS_ROLE.
role_file="${BILIS_ROLE_FILE:-/tmp/bilis-role}"
printf '%s' "$role" > "$role_file" 2>/dev/null || true
# Only consume an argument when it was one: with the role coming from the
# environment, argv is already the extra flags to pass through.
if [ "$#" -gt 0 ] && [ "$role" != "" ] && [ "$1" = "$role" ]; then
shift
fi
case "$role" in
web)
prepare_storage
prepare_laravel
exec frankenphp run --config /etc/caddy/Caddyfile "$@"
;;
horizon)
prepare_storage
prepare_laravel
exec php artisan horizon "$@"
;;
scheduler)
prepare_storage
prepare_laravel
exec php artisan schedule:work "$@"
;;
artisan)
prepare_storage
exec php artisan "$@"
;;
*)
exec "$@"
;;
esac