Repository navigation
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
147 lines (127 loc) · 4.25 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
147 lines (127 loc) · 4.25 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
#!/usr/bin/env sh
set -e # Exit on error
set -u # Treat undefined variables as errors
# Constants
MANAGE="python manage.py"
STUDLAN_USER="studlan"
STUDLAN_GROUP="studlan"
IMPORT_EXPORT_DIR="import_export"
IMPORT_FILE="$IMPORT_EXPORT_DIR/import.json.gz"
EXPORT_FILE="$IMPORT_EXPORT_DIR/export.json.gz"
# Optional env vars
STUDLAN_UID=${STUDLAN_UID:=}
STUDLAN_GID=${STUDLAN_GID:=}
SUPERUSER_USERNAME=${SUPERUSER_USERNAME:=}
SUPERUSER_EMAIL=${SUPERUSER_EMAIL:=}
SUPERUSER_PASSWORD=${SUPERUSER_PASSWORD:=}
SUPERUSER_INACTIVE=${SUPERUSER_INACTIVE:=}
FLUSH_DATABASE=${FLUSH_DATABASE:=}
IMPORT_DATABASE=${IMPORT_DATABASE:=}
EXPORT_DATABASE=${EXPORT_DATABASE:=}
NO_START=${NO_START:=}
echo "Starting studlan v$(cat VERSION)"
echo
# Check if settings exist
APP_SETTINGS_FILE=studlan/settings/local.py
if [[ ! -e $APP_SETTINGS_FILE ]]; then
echo "Django settings not found: $APP_SETTINGS_FILE" 1>&2
exit -1
fi
# Collect static files
echo "Collecting static files ..."
$MANAGE collectstatic --noinput --clear
# Optionally flush database
if [[ $FLUSH_DATABASE == "true" ]]; then
echo "Flushing the database ..."
$MANAGE flush --noinput
fi
# Run migration, but skip initial if matching table names already exist
echo "Migrating database ..."
$MANAGE migrate --fake-initial
echo
# Optionally import database
if [[ $IMPORT_DATABASE == "true" ]]; then
echo "Importing from $IMPORT_FILE ..."
if [[ -f $IMPORT_FILE ]]; then
$MANAGE loaddata $IMPORT_FILE
else
echo "Error: Import file not found: $IMPORT_FILE" 1>&2
fi
fi
# Clear expired sessions
$MANAGE clearsessions
# Optionally add superuser
# Warning: These should be trusted to avoid code injection
if [[ ! -z $SUPERUSER_USERNAME ]]; then
echo "Adding superuser ..."
# FIXME disable superiser
if [[ $SUPERUSER_INACTIVE == "true" ]]; then
SUPERUSER_ACTIVE="False"
else
SUPERUSER_ACTIVE="True"
fi
$MANAGE shell << END
# Python 2
from django.contrib.auth import get_user_model;
superuser_usernane = "${SUPERUSER_USERNAME}"
superuser_email = "${SUPERUSER_EMAIL}"
superuser_password = "${SUPERUSER_PASSWORD}"
superuser_active = ${SUPERUSER_ACTIVE}
if not superuser_usernane:
print "Error: Username not specified"
quit()
User = get_user_model();
if User.objects.filter(username=superuser_usernane).exists():
print "User with specified username already exists. Not adding superuser."
elif not superuser_email or not superuser_password:
print "User with specified username does not exist, but all credentials were not specified. Not adding superuser."
else:
print "User with specified username does not exist and all credentials were provided. Adding superuser with is_active=%s." % superuser_active
User.objects.create_superuser(username=superuser_usernane, email=superuser_email, password=superuser_password, is_active=superuser_active)
quit()
END
echo
echo "If a superuser was created, please change its password in the app"
fi
# Validate
echo "Checking validity ..."
$MANAGE check --deploy --fail-level=ERROR
echo
# Optionally export database
if [[ $EXPORT_DATABASE == "true" ]]; then
echo "Exporting to $EXPORT_FILE ..."
mkdir -p $IMPORT_EXPORT_DIR
touch $EXPORT_FILE
chmod 600 $EXPORT_FILE
# Exclude contenttypes and auth.Permission while using natural foreign keys to prevent IntegrityError on import
$MANAGE dumpdata --natural-foreign --exclude=contenttypes --exclude=auth.Permission --format=json --indent=2 | gzip > $EXPORT_FILE
fi
# Add group and user to run the app
if ! grep -q "^${STUDLAN_GROUP}:" /etc/group; then
if [[ ! -z $STUDLAN_GID ]]; then
groupadd -r -g "$STUDLAN_GID" $STUDLAN_GROUP
else
groupadd -r $STUDLAN_GROUP
fi
fi
if ! grep -q "^${STUDLAN_USER}:" /etc/passwd; then
if [[ ! -z $STUDLAN_UID ]]; then
useradd -r -g studlan -u "$STUDLAN_UID" $STUDLAN_USER
else
useradd -r -g $STUDLAN_GROUP $STUDLAN_USER
fi
echo "Added user: $(id studlan)"
fi
# Setup permissions and stuff
# Note: Volumes from vboxsf cannot be chowned
set +e
echo "Chowning all files ..."
chown -R $STUDLAN_USER:$STUDLAN_GROUP .
set -e
# Run uWSGI server
if [[ $NO_START == "true" ]]; then
echo "No-start enabled, stopping instead"
exit 0
fi
echo "Done. Starting server ..."
exec uwsgi --ini uwsgi.ini