91 lines
3.0 KiB
Bash
91 lines
3.0 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
|
|
echo "=========================================="
|
|
echo "IoT PreTester V3 - Backend Initialization"
|
|
echo "=========================================="
|
|
|
|
# Check if running as correct user
|
|
CURRENT_UID=$(id -u)
|
|
CURRENT_GID=$(id -g)
|
|
echo "Running as UID:GID = $CURRENT_UID:$CURRENT_GID"
|
|
|
|
if [ "$CURRENT_UID" != "1000" ] || [ "$CURRENT_GID" != "1000" ]; then
|
|
echo "WARNING: Not running as UID:GID 1000:1000"
|
|
echo "This may cause permission issues with shared volumes"
|
|
fi
|
|
|
|
# Wait for database to be ready (if using external DB)
|
|
echo "Waiting for database..."
|
|
sleep 2
|
|
|
|
# Create required directories
|
|
echo "Creating required directories..."
|
|
mkdir -p /app/files/firmware 2>/dev/null || true
|
|
mkdir -p /app/files/pcap 2>/dev/null || true
|
|
mkdir -p /app/files/wireshark_capture 2>/dev/null || true
|
|
mkdir -p /app/data 2>/dev/null || true
|
|
mkdir -p /app/logs 2>/dev/null || true
|
|
echo "✓ Directories created"
|
|
|
|
# Test write permissions on shared volume
|
|
echo "Testing write permissions..."
|
|
if touch /app/files/.write_test 2>/dev/null; then
|
|
rm -f /app/files/.write_test
|
|
echo "✓ Write access to /app/files confirmed"
|
|
else
|
|
echo "⚠ WARNING: Cannot write to /app/files directory"
|
|
echo " This may cause firmware/pcap analysis issues"
|
|
fi
|
|
|
|
# Change to django_project directory
|
|
cd /app/django_project
|
|
|
|
# Run migrations
|
|
# The repo ships no migration files for IoTAnalyzer, so generate them first —
|
|
# otherwise migrate only covers Django's built-in apps and the iot_* tables
|
|
# are never created.
|
|
echo "Running database migrations..."
|
|
python manage.py makemigrations IoTAnalyzer --noinput
|
|
python manage.py migrate --noinput
|
|
echo "✓ Migrations completed"
|
|
|
|
# Collect static files
|
|
echo "Collecting static files..."
|
|
python manage.py collectstatic --noinput --clear || echo "⚠ Static files collection skipped"
|
|
|
|
# Create superuser if it doesn't exist
|
|
echo "Checking for superuser..."
|
|
python manage.py shell -c "
|
|
from django.contrib.auth import get_user_model;
|
|
User = get_user_model();
|
|
if not User.objects.filter(username='${DJANGO_SUPERUSER_USERNAME:-admin}').exists():
|
|
User.objects.create_superuser(
|
|
'${DJANGO_SUPERUSER_USERNAME:-admin}',
|
|
'${DJANGO_SUPERUSER_EMAIL:-admin@example.com}',
|
|
'${DJANGO_SUPERUSER_PASSWORD:-admin}'
|
|
);
|
|
print('✓ Superuser created');
|
|
else:
|
|
print('✓ Superuser already exists');
|
|
" || echo "⚠ Superuser check failed"
|
|
|
|
# Initialize database with default data
|
|
if [ "$INIT_DATABASE" = "true" ]; then
|
|
echo "Initializing database with default data..."
|
|
python create_models.py || echo "⚠ Database initialization skipped"
|
|
fi
|
|
|
|
echo "=========================================="
|
|
echo "Starting Django application..."
|
|
echo "=========================================="
|
|
|
|
# Start Gunicorn server
|
|
exec gunicorn IoTPreTester.wsgi:application \
|
|
--bind 0.0.0.0:8000 \
|
|
--workers ${GUNICORN_WORKERS:-4} \
|
|
--timeout ${GUNICORN_TIMEOUT:-120} \
|
|
--access-logfile /app/logs/access.log \
|
|
--error-logfile /app/logs/error.log \
|
|
--log-level ${LOG_LEVEL:-info}
|