Repository navigation
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
131 lines (109 loc) · 4.52 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
131 lines (109 loc) · 4.52 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
#!/bin/bash
set -e
# Docker entrypoint script for kiosk-show-replacement
# Handles database initialization/migration and starts the application
echo "=== Kiosk Show Replacement - Docker Entrypoint ==="
# Construct DATABASE_URL from individual MYSQL_* variables if not already set
if [ -z "${DATABASE_URL:-}" ] && [ -n "${MYSQL_HOST:-}" ]; then
MYSQL_PORT="${MYSQL_PORT:-3306}"
MYSQL_USER="${MYSQL_USER:?MYSQL_HOST is set but MYSQL_USER is not}"
MYSQL_PASSWORD="${MYSQL_PASSWORD:?MYSQL_HOST is set but MYSQL_PASSWORD is not}"
MYSQL_DATABASE="${MYSQL_DATABASE:?MYSQL_HOST is set but MYSQL_DATABASE is not}"
ENCODED_USER=$(python -c "from urllib.parse import quote_plus; import os; print(quote_plus(os.environ['MYSQL_USER']))")
ENCODED_PASSWORD=$(python -c "from urllib.parse import quote_plus; import os; print(quote_plus(os.environ['MYSQL_PASSWORD']))")
export DATABASE_URL="mysql+pymysql://${ENCODED_USER}:${ENCODED_PASSWORD}@${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}"
echo "Constructed DATABASE_URL from MYSQL_* environment variables"
fi
# Set default environment variables if not provided
export FLASK_ENV="${FLASK_ENV:-production}"
export FLASK_APP="kiosk_show_replacement.app:create_app('${FLASK_ENV}')"
# Wait for database if using external database (MariaDB/PostgreSQL)
if [[ "$DATABASE_URL" == mysql* ]] || [[ "$DATABASE_URL" == postgresql* ]]; then
echo "Waiting for database to be ready..."
# Extract host and port from DATABASE_URL for health check
# Format: mysql+pymysql://user:pass@host:port/db or postgresql://user:pass@host:port/db
DB_HOST=$(echo "$DATABASE_URL" | sed -E 's|.*@([^:/]+).*|\1|')
DB_PORT=$(echo "$DATABASE_URL" | sed -E 's|.*:([0-9]+)/.*|\1|')
# Default ports if not specified
if [[ "$DATABASE_URL" == mysql* ]]; then
DB_PORT="${DB_PORT:-3306}"
else
DB_PORT="${DB_PORT:-5432}"
fi
# Wait up to 60 seconds for database
RETRIES=30
until nc -z "$DB_HOST" "$DB_PORT" 2>/dev/null || [ $RETRIES -eq 0 ]; do
echo "Waiting for database at $DB_HOST:$DB_PORT... ($RETRIES retries left)"
RETRIES=$((RETRIES-1))
sleep 2
done
if [ $RETRIES -eq 0 ]; then
echo "ERROR: Database not available after 60 seconds"
exit 1
fi
echo "Database is available!"
fi
# Run database migrations
echo "Running database migrations..."
python -c "
from kiosk_show_replacement.app import create_app
from flask_migrate import upgrade
app = create_app('${FLASK_ENV}')
with app.app_context():
upgrade()
print('Migrations completed successfully')
"
# Check if this is a fresh database (no admin user exists)
echo "Checking database state..."
python -c "
import sys
from kiosk_show_replacement.app import create_app
from kiosk_show_replacement.models import User
app = create_app('${FLASK_ENV}')
with app.app_context():
admin = User.query.filter_by(is_admin=True).first()
if admin is None:
print('No admin user found - database needs initialization')
sys.exit(1)
else:
print('Admin user exists - database is ready')
sys.exit(0)
" || {
echo "Initializing database with default admin user..."
# Get admin credentials from environment or use defaults
ADMIN_USER="${KIOSK_ADMIN_USERNAME:-admin}"
ADMIN_PASS="${KIOSK_ADMIN_PASSWORD:-admin}"
ADMIN_EMAIL="${KIOSK_ADMIN_EMAIL:-}"
python -c "
from kiosk_show_replacement.app import create_app
from kiosk_show_replacement.database_utils import DatabaseUtils
app = create_app('${FLASK_ENV}')
with app.app_context():
# Create tables if they don't exist
DatabaseUtils.create_all_tables()
# Create admin user
admin = DatabaseUtils.create_admin_user(
username='${ADMIN_USER}',
password='${ADMIN_PASS}',
email='${ADMIN_EMAIL}' if '${ADMIN_EMAIL}' else None
)
if admin:
print(f'Created admin user: ${ADMIN_USER}')
else:
print('Admin user may already exist')
"
echo "Database initialization completed"
}
echo "Starting application..."
# Check if NewRelic is enabled (NEW_RELIC_LICENSE_KEY is set and non-empty)
if [ -n "${NEW_RELIC_LICENSE_KEY:-}" ]; then
echo "NewRelic monitoring enabled"
# Set default app name if not provided
export NEW_RELIC_APP_NAME="${NEW_RELIC_APP_NAME:-kiosk-show-replacement}"
# Wrap the command with newrelic-admin for automatic instrumentation
exec newrelic-admin run-program "$@"
else
echo "NewRelic monitoring disabled (NEW_RELIC_LICENSE_KEY not set)"
# Execute the main command directly (usually gunicorn)
exec "$@"
fi