run_LAS.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python3.9
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import subprocess
  5. import sys
  6. # 配置
  7. GIT_URL = "http://43.137.10.199:3000/HFLN/LAS.git"
  8. LOCAL_PATH = "/platform/LAS/"
  9. GIT_USER = "nifangxu"
  10. GIT_PASS = "123456"
  11. def run_cmd(cmd, cwd=None):
  12. """执行命令行并实时输出"""
  13. print(f"[CMD] {cmd}")
  14. process = subprocess.Popen(cmd, cwd=cwd, shell=True,
  15. stdout=subprocess.PIPE,
  16. stderr=subprocess.STDOUT,
  17. text=True)
  18. for line in process.stdout:
  19. print(line, end="")
  20. process.wait()
  21. if process.returncode != 0:
  22. sys.exit(process.returncode)
  23. def update_repo():
  24. """拉取或更新代码"""
  25. if not os.path.exists(LOCAL_PATH):
  26. print("本地仓库不存在,开始克隆...")
  27. auth_url = GIT_URL.replace("http://", f"http://{GIT_USER}:{GIT_PASS}@")
  28. run_cmd(f"git clone {auth_url} {LOCAL_PATH}")
  29. else:
  30. print("本地仓库已存在,开始更新...")
  31. run_cmd("git reset --hard", cwd=LOCAL_PATH)
  32. run_cmd("git pull", cwd=LOCAL_PATH)
  33. # 设置权限
  34. print("设置文件权限...")
  35. run_cmd("dos2unix *", cwd=LOCAL_PATH)
  36. run_cmd("chmod +x *.py", cwd=LOCAL_PATH)
  37. def check_repo():
  38. """检查仓库目录是否存在"""
  39. if not os.path.exists(LOCAL_PATH):
  40. print("❌ 本地代码目录不存在,请先执行: python3 LAS-deploy.py update")
  41. sys.exit(1)
  42. def start_service():
  43. check_repo()
  44. print("启动 LAS 服务...")
  45. run_cmd("./start.py", cwd=LOCAL_PATH)
  46. def stop_service():
  47. check_repo()
  48. print("停止 LAS 服务...")
  49. run_cmd("./stop.py", cwd=LOCAL_PATH)
  50. def restart_service():
  51. check_repo()
  52. print("重启 LAS 服务...")
  53. run_cmd("./stop.py || true", cwd=LOCAL_PATH)
  54. run_cmd("./start.py", cwd=LOCAL_PATH)
  55. def status_service():
  56. check_repo()
  57. print("查看 LAS 服务状态...")
  58. run_cmd("./status.py", cwd=LOCAL_PATH)
  59. def main():
  60. if len(sys.argv) < 2:
  61. print("用法: python3 LAS-deploy.py [update|start|stop|restart|status]")
  62. sys.exit(1)
  63. action = sys.argv[1].lower()
  64. if action == "update":
  65. update_repo()
  66. elif action == "start":
  67. start_service()
  68. elif action == "stop":
  69. stop_service()
  70. elif action == "restart":
  71. restart_service()
  72. elif action == "status":
  73. status_service()
  74. else:
  75. print(f"未知参数: {action}")
  76. print("可用参数: update | start | stop | restart | status")
  77. sys.exit(1)
  78. print(f"✅ 操作完成: {action}")
  79. if __name__ == "__main__":
  80. main()