66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
"""部署脚本。
|
|
|
|
用法:
|
|
python scripts/deploy.py --check # 预检(依赖/配置/数据库连通性)
|
|
python scripts/deploy.py --health # 服务健康检查
|
|
python scripts/deploy.py --docker # docker compose 构建并启动
|
|
"""
|
|
import argparse
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
def pre_check() -> None:
|
|
print("== 依赖检查 ==")
|
|
try:
|
|
import tornado
|
|
import sqlalchemy
|
|
import pydantic
|
|
print(" 核心依赖已安装")
|
|
except ImportError as e:
|
|
print(f" 缺少依赖: {e}")
|
|
sys.exit(1)
|
|
|
|
print("== 配置文件检查 ==")
|
|
if (ROOT / ".env").exists():
|
|
print(" .env 存在")
|
|
else:
|
|
print(" 警告: .env 不存在,将从 .env.example 复制")
|
|
(ROOT / ".env.example").read_text()
|
|
(ROOT / ".env").write_text((ROOT / ".env.example").read_text())
|
|
print(" 已复制 .env.example -> .env")
|
|
|
|
print("== 预检完成 ==")
|
|
|
|
|
|
def health() -> None:
|
|
subprocess.run([sys.executable, str(ROOT / "scripts" / "health_check.py")])
|
|
|
|
|
|
def docker_deploy() -> None:
|
|
subprocess.run(["docker", "compose", "up", "-d", "--build"], cwd=str(ROOT), check=True)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="部署脚本")
|
|
parser.add_argument("--check", action="store_true", help="部署预检")
|
|
parser.add_argument("--health", action="store_true", help="健康检查")
|
|
parser.add_argument("--docker", action="store_true", help="Docker 部署")
|
|
args = parser.parse_args()
|
|
|
|
if args.check:
|
|
pre_check()
|
|
elif args.health:
|
|
health()
|
|
elif args.docker:
|
|
docker_deploy()
|
|
else:
|
|
parser.print_help()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|