diff --git a/dockerfiles/oj_web_server/run.sh b/dockerfiles/oj_web_server/run.sh index 5e5ff4d..7a612db 100644 --- a/dockerfiles/oj_web_server/run.sh +++ b/dockerfiles/oj_web_server/run.sh @@ -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 \ No newline at end of file diff --git a/oj/custom_settings.example.py b/oj/custom_settings.example.py index 15448ce..4274a19 100644 --- a/oj/custom_settings.example.py +++ b/oj/custom_settings.example.py @@ -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 = "" + + diff --git a/tools/create_db.py b/tools/create_db.py new file mode 100644 index 0000000..faf20e3 --- /dev/null +++ b/tools/create_db.py @@ -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) \ No newline at end of file