-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
120 lines (105 loc) · 2.91 KB
/
docker-entrypoint.sh
File metadata and controls
120 lines (105 loc) · 2.91 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
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}CarBlockPy2 Docker Entrypoint${NC}"
echo -e "${GREEN}========================================${NC}"
# Function to wait for PostgreSQL
wait_for_db() {
local host="${DB_HOST:-db}"
local port="${DB_PORT:-5432}"
local max_attempts=30
local attempt=1
echo -e "${YELLOW}Waiting for PostgreSQL at ${host}:${port}...${NC}"
while [ $attempt -le $max_attempts ]; do
if python -c "
import psycopg2
import os
import sys
try:
conn = psycopg2.connect(
host='${host}',
port=${port},
database='${DB_NAME}',
user='${DB_USER}',
password='${DB_PASSWORD}'
)
conn.close()
sys.exit(0)
except Exception as e:
sys.exit(1)
" 2>/dev/null; then
echo -e "${GREEN}PostgreSQL is ready!${NC}"
return 0
fi
echo -e "${YELLOW}Attempt ${attempt}/${max_attempts}: PostgreSQL not ready yet...${NC}"
sleep 2
attempt=$((attempt + 1))
done
echo -e "${RED}ERROR: PostgreSQL is not available after ${max_attempts} attempts${NC}"
exit 1
}
# Function to check if tables exist
tables_exist() {
local host="${DB_HOST:-db}"
local port="${DB_PORT:-5432}"
python -c "
import psycopg2
import os
import sys
try:
conn = psycopg2.connect(
host='${host}',
port=${port},
database='${DB_NAME}',
user='${DB_USER}',
password='${DB_PASSWORD}'
)
cursor = conn.cursor()
cursor.execute(
\"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'users')\"
)
result = cursor.fetchone()[0]
cursor.close()
conn.close()
sys.exit(0 if result else 1)
except Exception as e:
sys.exit(1)
"
}
# Function to initialize database
init_database() {
echo -e "${YELLOW}Initializing database tables...${NC}"
if python scripts/init_db.py; then
echo -e "${GREEN}Database initialized successfully!${NC}"
else
echo -e "${RED}ERROR: Failed to initialize database${NC}"
exit 1
fi
}
# Main execution
main() {
# Wait for database to be ready
wait_for_db
# Check if we need to initialize the database
if [ "${SKIP_DB_INIT:-false}" != "true" ]; then
if tables_exist; then
echo -e "${GREEN}Database tables already exist. Skipping initialization.${NC}"
else
init_database
fi
else
echo -e "${YELLOW}Skipping database initialization (SKIP_DB_INIT=true)${NC}"
fi
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Starting CarBlockPy2 Bot...${NC}"
echo -e "${GREEN}========================================${NC}"
# Execute the main command
exec "$@"
}
# Run main function
main "$@"