-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
158 lines (132 loc) Β· 4.3 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
158 lines (132 loc) Β· 4.3 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
#!/bin/bash
set -e
# Docker entrypoint script for Python IMDB Bot
# Handles database migrations and startup
echo "π Starting Python IMDB Bot..."
# Function to check if Supabase is accessible
check_supabase() {
echo "π Checking Supabase connection..."
# Try to run a simple query to check if migrations have been applied
uv run python -c "
import sys
import os
sys.path.insert(0, '/app/src')
from python_imdb_bot.utils import supabase
try:
# Try a simple query to check if database is accessible
result = supabase.table('settings').select('*').limit(1).execute()
print('β
Supabase connection successful')
sys.exit(0)
except Exception as e:
print('β Supabase connection failed:', str(e))
print('β οΈ Please ensure:')
print(' 1. Database migrations are applied')
print(' 2. SUPABASE_URL and SUPABASE_KEY are set correctly')
print(' 3. Database is accessible from this container')
sys.exit(1)
"
return $?
}
# Function to run database migrations
run_migrations() {
echo "ποΈ Checking database migrations..."
# Check if we have npx available (for Supabase CLI)
if command -v npx &> /dev/null; then
echo "π¦ Running Supabase migrations..."
# Change to the supabase directory if it exists
if [ -d "/app/supabase" ]; then
cd /app/supabase
# Run migrations
if npx supabase db push; then
echo "β
Database migrations completed successfully"
return 0
else
echo "β Database migration failed"
return 1
fi
else
echo "β οΈ No supabase directory found, skipping migrations"
return 0
fi
else
echo "β οΈ npx not found, skipping automatic migrations"
echo " Run migrations manually: npx supabase db push"
return 0
fi
}
# Function to validate environment variables
validate_env() {
echo "π§ Validating environment variables..."
required_vars=("DISCORD_TOKEN" "SUPABASE_URL" "SUPABASE_KEY")
missing_vars=()
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
missing_vars+=("$var")
fi
done
if [ ${#missing_vars[@]} -gt 0 ]; then
echo "β Missing required environment variables:"
for var in "${missing_vars[@]}"; do
echo " - $var"
done
echo ""
echo " Please set these in your .env file or Docker environment"
return 1
fi
echo "β
Environment variables validated"
return 0
}
# Check if discord.py is installed
check_discord() {
echo "π Checking discord.py installation..."
if uv run python -c "import discord; print('β
discord.py is installed')" 2>/dev/null; then
return 0
else
echo "β discord.py is not installed"
echo "β οΈ Attempting to install discord.py..."
if uv add discord.py; then
echo "β
discord.py installed successfully"
return 0
else
echo "β Failed to install discord.py"
return 1
fi
fi
}
# Main execution flow
main() {
echo "π³ Docker container started"
echo "π Working directory: $(pwd)"
echo "π€ Running as user: $(whoami)"
# Validate environment
if ! validate_env; then
exit 1
fi
# Check discord.py installation
if ! check_discord; then
echo "β Cannot proceed without discord.py"
exit 1
fi
# Check Supabase connection
if ! check_supabase; then
echo "β οΈ Supabase connection issues detected"
echo " The bot will continue but may have limited functionality"
fi
# Run migrations if possible
if ! run_migrations; then
echo "β οΈ Migration issues detected"
echo " The bot will continue but may have database issues"
fi
# Start the bot
echo "π€ Starting Discord bot..."
echo "π Environment:"
echo " - Python: $(uv run python --version)"
echo " - Working directory: $(pwd)"
echo " - Log level: ${LOG_LEVEL:-INFO}"
# Execute the bot using main.py
exec python main.py
}
# Handle graceful shutdown
trap 'echo "π Received shutdown signal, exiting gracefully..."; exit 0' SIGTERM SIGINT
# Run main function
main "$@"