docker启动的时候创建密钥和生成数据库

This commit is contained in:
virusdefender
2016-02-20 13:57:14 +08:00
parent cd123d2cdf
commit ac2b2445b5
3 changed files with 38 additions and 3 deletions

View File

@@ -1,5 +1,12 @@
#!/usr/bin/env bash
if [ ! -f "/code/oj/custom_settings.py" ]; then
cp /code/oj/custom_settings.example.py /code/oj/custom_settings.py
echo "SECRET_KEY=\"`cat /dev/urandom | head -1 | md5sum | head -c 32`\"" >> /code/oj/custom_settings.py
fi
find /code -name "*.pyc" -delete
python -m compileall /code
chown -R nobody:nogroup /code/log /code/test_case /code/upload
echo "Waiting MySQL and Redis to start"
sleep 10
python /code/tools/create_db.py
exec supervisord

View File

@@ -1,9 +1,6 @@
# coding=utf-8
import os
# please set your own SECRET_KEY to a long random string
SECRET_KEY = None
WEBSITE_INFO = {"website_name": u"example大学 OnlineJudge",
"website_name_shortcut": u"example oj",
@@ -15,3 +12,8 @@ SMTP_CONFIG = {"smtp_server": "smtp.xxx.com",
"email": "noreply@xxx.com",
"password": os.environ.get("smtp_password", "111111"),
"tls": False}
# please set your own SECRET_KEY to a long random string
# SECRET_KEY = ""

26
tools/create_db.py Normal file
View File

@@ -0,0 +1,26 @@
# coding=utf-8
import os
import time
import MySQLdb
"""
docker-compose启动的时候是并行启动的,可能执行本脚本的时候MySQL还没启动完
"""
i = 3
while i:
try:
conn = MySQLdb.connect(host=os.environ["MYSQL_PORT_3306_TCP_ADDR"],
user=os.environ["MYSQL_ENV_MYSQL_USER"],
passwd=os.environ["MYSQL_ENV_MYSQL_ROOT_PASSWORD"])
conn.cursor().execute("create database if not exists oj default character set utf8;")
conn.cursor().execute("create database if not exists oj_submission default character set utf8;")
print "Create database successfully"
exit(0)
except Exception as e:
print "Failed to create database, error: " + str(e) + ", will retry in 3 seconds"
i -= 1
time.sleep(3)
print "Failed to create database"
exit(1)