forked from praekeltfoundation/docker-django-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
django-entrypoint.sh
executable file
·79 lines (65 loc) · 2.56 KB
/
django-entrypoint.sh
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
#!/usr/bin/env sh
set -e
# No args or looks like options or the APP_MODULE for Gunicorn
if [ "$#" = 0 ] || \
[ "${1#-}" != "$1" ] || \
echo "$1" | grep -Eq '^([_A-Za-z]\w*\.)*[_A-Za-z]\w*:[_A-Za-z]\w*$'; then
set -- gunicorn "$@"
fi
# Looks like a Celery command, let's run that with Celery's entrypoint script
if [ "$1" = 'celery' ]; then
set -- celery-entrypoint.sh "$@"
fi
if [ "$1" = 'gunicorn' ]; then
# Do a chown of the /app/media & /app/mediafiles directories (if they exist)
# at runtime in case the directory was mounted as a root-owned volume.
for media in /app/media /app/mediafiles; do
if [ -d $media ] && [ "$(stat -c %U $media)" != 'django' ]; then
chown -R django:django $media
fi
done
# Run the migration as the django user so that if it creates a local DB
# (e.g. when using sqlite in development), that DB is still writable.
# Ultimately, the user shouldn't really be using a local DB and it's difficult
# to offer support for all the cases in which a local DB might be created --
# but here we do the minimum.
if [ -z "$SKIP_MIGRATIONS" ]; then
su-exec django django-admin migrate --noinput
fi
# Allow running of collectstatic command because it might require env vars
if [ -n "$RUN_COLLECTSTATIC" ]; then
su-exec django django-admin collectstatic --noinput
fi
if [ -n "$SUPERUSER_PASSWORD" ]; then
echo "from django.contrib.auth.models import User
if not User.objects.filter(username='admin').exists():
User.objects.create_superuser('admin', '[email protected]', '$SUPERUSER_PASSWORD')
" | su-exec django django-admin shell
echo "Created superuser with username 'admin' and password '$SUPERUSER_PASSWORD'"
fi
nginx -g 'daemon off;' &
# Celery
ensure_celery_app() {
[ -n "$CELERY_APP" ] || \
{ echo 'If $CELERY_WORKER or $CELERY_BEAT are set then $CELERY_APP must be provided'; exit 1; }
}
if [ -n "$CELERY_WORKER" ]; then
ensure_celery_app
celery-entrypoint.sh worker --pool=solo --pidfile worker.pid &
fi
if [ -n "$CELERY_BEAT" ]; then
ensure_celery_app
celery-entrypoint.sh beat --pidfile beat.pid &
fi
if [ -n "$APP_MODULE" ]; then
echo 'DEPRECATED: Providing APP_MODULE via an environment variable is deprecated.
Please provide it using the container command rather.' 1>&2
set -- "$@" "$APP_MODULE"
fi
# Create the Gunicorn runtime directory at runtime in case /run is a tmpfs
if mkdir /run/gunicorn 2> /dev/null; then
chown django:django /run/gunicorn
fi
set -- su-exec django "$@" --config /etc/gunicorn/config.py
fi
exec "$@"