-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·117 lines (99 loc) · 3.75 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
executable file
·117 lines (99 loc) · 3.75 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
#!/bin/bash
set -e
echo "Starting Open LPR application initialization..."
# Wait for database to be available (if using external database)
# For SQLite, this is not needed but keeping for future compatibility
# Check if the database directory exists
if [ ! -d "/app/data" ]; then
echo "Creating data directory for SQLite database..."
mkdir -p /app/data
fi
# Ensure proper permissions for data directory
# Run as root to set permissions, then switch back
if [ "$(id -u)" = "0" ]; then
chown -R django:django /app/data
chmod -R 755 /app/data
else
echo "Warning: Running as non-root, may not be able to set permissions"
fi
# Check if the media directory exists
if [ ! -d "/app/media" ]; then
echo "Creating media directory for uploaded files..."
mkdir -p /app/media
fi
# Ensure proper permissions for media directory
# Run as root to set permissions, then switch back
if [ "$(id -u)" = "0" ]; then
chown -R django:django /app/media
chmod -R 755 /app/media
else
echo "Warning: Running as non-root, may not be able to set media permissions"
fi
# Check if the metrics directory exists
if [ ! -d "/app/metrics" ]; then
echo "Creating metrics directory for persistent metrics storage..."
mkdir -p /app/metrics
fi
# Ensure proper permissions for metrics directory
# Run as root to set permissions, then switch back
if [ "$(id -u)" = "0" ]; then
chown -R django:django /app/metrics
chmod -R 755 /app/metrics
else
echo "Warning: Running as non-root, may not be able to set metrics permissions"
fi
# Change to app directory
cd /app
# Create a simple log file to ensure it exists and has right permissions
touch /app/data/django.log 2>/dev/null || true
if [ "$(id -u)" = "0" ]; then
chown django:django /app/data/django.log
chmod 644 /app/data/django.log
fi
# Create metrics state file to ensure it exists and has right permissions
touch /app/metrics/metrics_state.json 2>/dev/null || true
if [ "$(id -u)" = "0" ]; then
chown django:django /app/metrics/metrics_state.json
chmod 644 /app/metrics/metrics_state.json
fi
# Ensure database file has correct ownership if it exists
# Get the database path from Django settings or use default
DB_PATH="${DATABASE_PATH:-/app/db.sqlite3}"
if [ -f "$DB_PATH" ]; then
echo "Setting ownership for database file: $DB_PATH"
if [ "$(id -u)" = "0" ]; then
chown django:django "$DB_PATH"
chmod 644 "$DB_PATH"
fi
else
echo "Database file not found at $DB_PATH, will be created by Django migrations"
fi
# Run database migrations
echo "Running database migrations..."
python manage.py migrate --noinput
# Ensure database file has correct ownership after migrations
# This handles the case where the database was created during migrations
if [ -f "$DB_PATH" ]; then
echo "Setting ownership for database file after migrations: $DB_PATH"
if [ "$(id -u)" = "0" ]; then
chown django:django "$DB_PATH"
chmod 644 "$DB_PATH"
fi
fi
# Collect static files (in case they weren't collected during build)
echo "Collecting static files..."
python manage.py collectstatic --noinput
# Create superuser if environment variables are provided
if [ -n "$DJANGO_SUPERUSER_USERNAME" ] && [ -n "$DJANGO_SUPERUSER_EMAIL" ] && [ -n "$DJANGO_SUPERUSER_PASSWORD" ]; then
echo "Creating superuser..."
python manage.py createsuperuser --noinput --username "$DJANGO_SUPERUSER_USERNAME" --email "$DJANGO_SUPERUSER_EMAIL" || echo "Superuser already exists or creation failed"
fi
echo "Initialization complete. Starting application..."
# If running as root, switch to django user for the actual application
if [ "$(id -u)" = "0" ]; then
echo "Switching to django user for application startup..."
exec gosu django "$@"
else
# Execute the command passed to the script
exec "$@"
fi