From f19aa2081750cf8f29e04760794b193f5aef5d5d Mon Sep 17 00:00:00 2001 From: spxcds Date: Tue, 10 May 2016 20:25:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0initinstall=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/create_db.py | 5 +---- utils/management/commands/initinstall.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 utils/management/commands/initinstall.py diff --git a/tools/create_db.py b/tools/create_db.py index faf20e30..30e9a0ae 100644 --- a/tools/create_db.py +++ b/tools/create_db.py @@ -2,11 +2,9 @@ import os import time import MySQLdb - """ docker-compose启动的时候是并行启动的,可能执行本脚本的时候MySQL还没启动完 """ - i = 3 while i: try: @@ -21,6 +19,5 @@ while i: 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 +exit(1) diff --git a/utils/management/commands/initinstall.py b/utils/management/commands/initinstall.py new file mode 100644 index 00000000..9297f003 --- /dev/null +++ b/utils/management/commands/initinstall.py @@ -0,0 +1,21 @@ +# coding=utf-8 + +from django.core.management.base import BaseCommand +import os + + +class Command(BaseCommand): + def handle(self, *args, **options): + try: + if os.system("python manage.py migrate") != 0: + self.stdout.write(self.style.ERROR("Failed to execute command 'migrate'")) + exit(1) + if os.system("python manage.py migrate --database=submission") != 0: + self.stdout.write(self.style.ERROR("Failed to execute command 'migrate --database=submission'")) + exit(1) + if os.system("python manage.py initadmin") != 0: + self.stdout.write(self.style.ERROR("Failed to execute command 'initadmin'")) + exit(1) + self.stdout.write(self.style.SUCCESS("Done")) + except Exception as e: + self.stdout.write(self.style.ERROR("Failed to initialize, error: " + str(e)))