Files
jiangmingzhao-daily-report/backend/deploy.py

89 lines
2.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
部署脚本 - 自动化部署Django应用
"""
import os
import sys
import subprocess
import django
# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
django.setup()
def run_command(command, description):
"""运行命令并处理错误"""
print(f"\n{'='*50}")
print(f"执行: {description}")
print(f"命令: {command}")
print(f"{'='*50}")
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode == 0:
print(f"{description} - 成功")
if result.stdout:
print(result.stdout)
else:
print(f"{description} - 失败")
if result.stderr:
print(result.stderr)
return False
return True
def deploy():
"""执行部署流程"""
print("🚀 开始部署企业级日报系统后端...")
# 1. 检查Python版本
if not run_command("python --version", "检查Python版本"):
return False
# 2. 安装依赖
if not run_command("pip install -r requirements.txt", "安装Python依赖"):
return False
# 3. 数据库迁移
if not run_command("python manage.py makemigrations", "生成数据库迁移文件"):
return False
if not run_command("python manage.py migrate", "执行数据库迁移"):
return False
# 4. 创建超级用户
print("\n📝 创建超级用户和测试用户...")
try:
exec(open('create_superuser.py').read())
print("✅ 用户创建完成")
except Exception as e:
print(f"❌ 创建用户失败: {e}")
# 5. 收集静态文件
if not run_command("python manage.py collectstatic --noinput", "收集静态文件"):
return False
# 6. 运行测试
if not run_command("python manage.py check", "检查系统配置"):
return False
print("\n🎉 后端部署完成!")
print("\n📋 部署信息:")
print("- 管理员账号: admin / admin123456")
print("- 测试用户: zhangsan / test123456")
print("- API地址: http://localhost:8000/api/")
print("- 管理后台: http://localhost:8000/admin/")
print("\n🚀 启动服务: python manage.py runserver")
return True
if __name__ == '__main__':
try:
success = deploy()
sys.exit(0 if success else 1)
except KeyboardInterrupt:
print("\n\n⚠️ 部署被用户中断")
sys.exit(1)
except Exception as e:
print(f"\n\n❌ 部署失败: {e}")
sys.exit(1)