-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·225 lines (188 loc) · 10.5 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
executable file
·225 lines (188 loc) · 10.5 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
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
#!/bin/bash
# This docker-entrypoint script is based on https://stackoverflow.com/a/33993532/388117
echo "*************** docker-entrypoint.sh ***************************"
echo "Running 'docker-entrypoint.sh', which executes three things"
echo "1. Collects static files"
echo "2. Applies database migrations"
echo "3. Starts server"
echo "******************************************"
echo "But first, let's print out some environment-related contexts to ensure we're all on the same page:"
echo ""
echo "Python info:"
which python
python --version
echo ""
echo "Django version:"
python3 -m django --version
echo ""
echo "pip3 list:"
pip3 list
echo ""
echo "User (whoami output):"
whoami
echo ""
echo "pwd":
pwd
echo ""
echo "Directory (la -al) info:"
ls -al
echo ""
echo "Directory info for media (la -al media):"
ls -al media
echo ""
# This is related to the permissions issues I'm having with WSL2 and Docker
# https://stackoverflow.com/q/69575151/388117
# setfacl -m u:48:rwx /code
# chown -R apache /code
# Capture build/version info for the /version/ endpoint (#1366).
# The servers deploy via git, so .git is present and `git rev-parse` works; we
# capture the short SHA + a timestamp ONCE here (not per request) into a small
# build-info.json that website/views/version.py reads. Falls back to "unknown"
# if git isn't available (the view also tolerates a missing file).
#
# On the servers /code is a bind-mount whose .git is owned by apache:makelab
# while this script runs as root, so git's "dubious ownership" guard would
# otherwise refuse to run -- mark /code safe first (-c is process-local, no
# global config side effects). git is installed in the Dockerfile.
echo "****************** STEP -1/5: docker-entrypoint.sh ************************"
echo "-1. Writing build-info.json (git sha + build timestamp) for /version/"
echo "******************************************"
GIT_SHA=$(git -c safe.directory=/code -C /code rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILT_AT=$(date --iso-8601=seconds 2>/dev/null || echo "unknown")
printf '{"git_sha": "%s", "built_at": "%s"}\n' "$GIT_SHA" "$BUILT_AT" > build-info.json
cat build-info.json
# Collect static files
echo "****************** STEP 0/5: docker-entrypoint.sh ************************"
echo "0. Printing environment variables visible to Django"
echo "******************************************"
python manage.py print_environment_vars
# Collect static files
echo "****************** STEP 1/5: docker-entrypoint.sh ************************"
echo "1. Collecting static files"
echo "******************************************"
python manage.py collectstatic --noinput
#python manage.py datetodatetime
# Apply database migrations
# TODO: explore doing migration in compose yml file: https://stackoverflow.com/a/44283611
echo "****************** STEP 2/5: docker-entrypoint.sh ************************"
echo "2. Running makemigrations and migrate"
echo "******************************************"
python manage.py makemigrations
python manage.py migrate
echo "****************** STEP 3/5: docker-entrypoint.sh ************************"
echo "3. Running makemigrations and migrate explicitly to website (often fixes some first-time run issues)"
echo "******************************************"
python manage.py makemigrations website
python manage.py migrate website
echo "****************** STEP 4/5: docker-entrypoint.sh ************************"
echo "4.0 Running 'python manage.py delete_unused_files' to delete unused files in file system"
echo "******************************************"
python manage.py delete_unused_files
echo "****************** STEP 4.1/5: docker-entrypoint.sh ************************"
echo "4.1 Running 'python manage.py thumbnail_cleanup' to delete unused thumbnails"
echo "******************************************"
python manage.py thumbnail_cleanup
echo "****************** STEP 4.2/5: docker-entrypoint.sh ************************"
echo "4.2 Running 'python manage.py generate_slugs_for_old_news_items' to generate slugs for old news items"
echo "******************************************"
python manage.py generate_slugs_for_old_news_items
echo "****************** STEP 4.2b/5: docker-entrypoint.sh ************************"
echo "4.2b Running 'python manage.py normalize_news_image_styles' to make legacy news images responsive (#1269)"
echo "******************************************"
python manage.py normalize_news_image_styles
echo "****************** STEP 4.3/5: docker-entrypoint.sh ************************"
echo "4.3 Running 'python manage.py auto_close_project_roles' to auto-close project roles"
echo "******************************************"
python manage.py auto_close_project_roles
echo "****************** STEP 4.4/5: docker-entrypoint.sh ************************"
echo "4.4 Running 'python manage.py remove_year_from_forum_name' to strip trailing years from Talk/Poster/Publication forum names (#1390)"
echo "******************************************"
python manage.py remove_year_from_forum_name
echo "****************** STEP 4.5/5: docker-entrypoint.sh ************************"
echo "3.1 Running 'python manage.py fix_sortedm2m_columns' to fix any missing sort_value columns"
echo "******************************************"
python manage.py fix_sortedm2m_columns
echo "****************** STEP 4.6/5: docker-entrypoint.sh ************************"
echo "4.6 Running 'python manage.py backfill_num_pages' to fill missing publication page counts"
echo "******************************************"
python manage.py backfill_num_pages
echo "****************** STEP 4.7/5: docker-entrypoint.sh ************************"
echo "4.7 Running 'python manage.py backfill_project_visibility' to resolve is_visible for legacy projects"
echo "******************************************"
python manage.py backfill_project_visibility
echo "****************** STEP 4.7b/5: docker-entrypoint.sh ************************"
echo "4.7b Running 'python manage.py backfill_original_filenames' to recover original upload filenames for never-renamed artifacts (#1391)"
echo "******************************************"
python manage.py backfill_original_filenames
echo "****************** STEP 4.8/5: docker-entrypoint.sh ************************"
echo "4.8 Running 'python manage.py recompute_url_names' to de-collide historical url_names (#1206)"
echo "******************************************"
python manage.py recompute_url_names
echo "****************** STEP 4.8b/5: docker-entrypoint.sh ************************"
echo "4.8b Running 'python manage.py seed_project_aliases' to redirect renamed project slugs (#944)"
echo "******************************************"
python manage.py seed_project_aliases
echo "****************** STEP 4.9/5: docker-entrypoint.sh ************************"
echo "4.9 Running 'python manage.py propagate_publication_projects' to link talks/videos/posters to their publication's projects (#649)"
echo "******************************************"
python manage.py propagate_publication_projects
echo "****************** STEP 4.10/5: docker-entrypoint.sh ************************"
echo "4.10 Running 'python manage.py setup_admin_groups' to create/refresh the Editors and Contributors admin groups (#1125)"
echo "******************************************"
python manage.py setup_admin_groups
echo "****************** STEP 4.10b/5: docker-entrypoint.sh ************************"
echo "4.10b Running 'python manage.py restandardize_artifact_filenames' to rename legacy talk/poster/pub files to the standardized scheme (#1401)"
echo "******************************************"
python manage.py restandardize_artifact_filenames
echo "****************** STEP 4.10c/5: docker-entrypoint.sh ************************"
echo "4.10c Running 'python manage.py repair_diverged_artifact_filenames' to recover artifacts whose files were renamed on disk but not in the DB (#1390 dotted-name bug)"
echo "******************************************"
# TEMPORARY (#1390): --dry-run logs which diverged artifacts WOULD be repaired,
# touching nothing on disk or in the DB, so we can review on prod before doing it
# for real. REMOVE --dry-run and redeploy to perform the recovery.
python manage.py repair_diverged_artifact_filenames --dry-run
# echo "****************** STEP 4.3/5: docker-entrypoint.sh ************************"
# echo "4.3 Running 'python manage.py rename_person_images' to rename person images"
# echo "******************************************"
# python manage.py rename_person_images
# echo "****************** STEP 4.4/5: docker-entrypoint.sh ************************"
# echo "4.4 Running 'python manage.py rename_talk_files' to rename talk files"
# echo "******************************************"
# python manage.py rename_talk_files
# Start server
#
# Production-grade environments (TEST, PROD) run Gunicorn, the recommended WSGI
# server. Local development (DJANGO_ENV=DEBUG) keeps Django's `runserver` for
# its auto-reload on code edits, friendlier tracebacks, debug toolbar, and
# static-file serving under DEBUG=True. See issue #1034.
#
# This swap is entirely inside the container -- UW CSE's Apache still reverse-
# proxies dynamic requests to 127.0.0.1:8571 (-> container :8000) and serves
# /static/ and /media/ directly, exactly as before -- so it ships via the
# normal push-to-deploy path with no Apache/IT changes.
#
# Gunicorn tuning (overridable via env vars in the compose file):
# GUNICORN_WORKERS number of worker processes. The (2*cores)+1 rule of thumb
# would be ~49 on the 24-core host, but that box is SHARED
# with all Project Sidewalk instances (see #959), so we
# default to a modest 3.
# GUNICORN_TIMEOUT per-request worker timeout in seconds. Gunicorn's default
# of 30s can kill slow admin operations (ImageMagick/PDF
# thumbnail generation), so we default to 120.
echo "****************** STEP 5/5: docker-entrypoint.sh ************************"
if [ "$DJANGO_ENV" = "TEST" ] || [ "$DJANGO_ENV" = "PROD" ]; then
GUNICORN_WORKERS="${GUNICORN_WORKERS:-3}"
GUNICORN_TIMEOUT="${GUNICORN_TIMEOUT:-120}"
echo "5. Starting Gunicorn (DJANGO_ENV=$DJANGO_ENV, workers=$GUNICORN_WORKERS, timeout=${GUNICORN_TIMEOUT}s)"
echo "******************************************"
exec gunicorn makeabilitylab.wsgi:application \
--bind 0.0.0.0:8000 \
--workers "$GUNICORN_WORKERS" \
--timeout "$GUNICORN_TIMEOUT" \
--access-logfile - \
--error-logfile -
else
echo "5. Starting dev server with 'python manage.py runserver 0.0.0.0:8000' (DJANGO_ENV=$DJANGO_ENV)"
echo "******************************************"
exec python manage.py runserver 0.0.0.0:8000
fi