-
Notifications
You must be signed in to change notification settings - Fork 75
/
entrypoint.sh
executable file
·79 lines (73 loc) · 2.39 KB
/
entrypoint.sh
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
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[32;1m'
YELLOW='\033[33;1m'
NC='\033[0m' # No Color
function database_empty() {
# Check if database is empty
if [ "${DB_TYPE}" == "mysql" ]; then
tables=$(mysql -h"${MYSQL_HOST:-127.0.0.1}" -P"${MYSQL_PORT:-3306}" -u"${MYSQL_USER}" -p"${MYSQL_PASS}" -D"${MYSQL_DB}" -e "SHOW TABLES;" 2>/dev/null | wc -l)
if [ "$tables" -le 1 ]; then
return 0 # Database is empty
fi
return 1 # Database is not empty
else
tables=$(sqlite3 /app/data/db.sqlite3 "SELECT count(*) FROM sqlite_master WHERE type='table';")
if [ "$tables" -eq 0 ]; then
return 0 # Database is empty
fi
return 1 # Database is not empty
fi
}
function init_database() {
# Wait for database to be ready
if [ "${DB_TYPE}" == "mysql" ]; then
while true; do
if mysql -h"${MYSQL_HOST:-127.0.0.1}" -P"${MYSQL_PORT:-3306}" -u"${MYSQL_USER}" -p"${MYSQL_PASS}" -e "use ${MYSQL_DB};" >/dev/null; then
break
else
echo -e "${RED}ERROR: Database not ready, will waiting${NC}"
sleep 3
fi
done
else
sqlite3 /app/data/db.sqlite3 "PRAGMA journal_mode = WAL;"
fi
# Initialize database schema
echo -e "${GREEN}INFO: Start initializing database${NC}"
if ! (python manage.py makemigrations www 2>/dev/null); then
echo -e "${RED}ERROR: failed to makemigrations www${NC}"
exit 1
fi
if ! (python manage.py makemigrations console 2>/dev/null); then
echo -e "${RED}ERROR: failed to makemigrations console${NC}"
exit 1
fi
if ! (python manage.py migrate >/dev/null); then
echo -e "${RED}ERROR: failed to migrate${NC}"
exit 1
fi
echo -e "${GREEN}INFO: Database initialization completed${NC}"
# Initialize default region data
if [ "${DB_TYPE}" == "mysql" ]; then
if ! (python default_region.py 2> /dev/null); then
echo -e "${RED}ERROR: failed to default_region${NC}"
exit 1
fi
else
if ! (python default_region_sqlite.py 2> /dev/null); then
echo -e "${YELLOW}WARN: failed to default_region${NC}"
fi
fi
}
if [ "$1" = "debug" -o "$1" = "bash" ]; then
exec /bin/bash
elif [ "$1" = "version" ]; then
echo "${RELEASE_DESC}"
else
if (database_empty); then
init_database
fi
# python upgrade.py
exec gunicorn goodrain_web.wsgi -b 0.0.0.0:${PORT:-7070} --max-requests=5000 -k gevent --reload --workers=2 --timeout=75 --log-file - --access-logfile - --error-logfile -
fi