run_LAS.py 2.7 KB

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