#!/usr/bin/env python3.9 # -*- coding: utf-8 -*- import os import subprocess import sys # 配置 MODULE = "LAS" GIT_URL = "http://43.137.10.199:3000/HFLN/LAS.git" GIT_USER = "nifangxu" GIT_PASS = "123456" LOCAL_PATH = f"/platform/{MODULE}/" def run_cmd(cmd, cwd=None): """执行命令行并实时输出""" print(f"[CMD] {cmd}") process = subprocess.Popen(cmd, cwd=cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) for line in process.stdout: print(line, end="") process.wait() if process.returncode != 0: sys.exit(process.returncode) def update_repo(): """拉取或更新代码""" if not os.path.exists(LOCAL_PATH): print("local codes does not exist, do clone...") auth_url = GIT_URL.replace("http://", f"http://{GIT_USER}:{GIT_PASS}@") run_cmd(f"git clone {auth_url} {LOCAL_PATH}") else: print("local codes already exist, do update...") run_cmd("git reset --hard", cwd=LOCAL_PATH) run_cmd("git pull", cwd=LOCAL_PATH) # 设置权限 print("get files permission") run_cmd("dos2unix *", cwd=LOCAL_PATH) run_cmd("chmod +x *.py", cwd=LOCAL_PATH) def check_repo(): """检查仓库目录是否存在""" if not os.path.exists(LOCAL_PATH): print(f"[error] local codes does not exist, run the following cmd first: python3.9 {MODULE}-deploy.py update") sys.exit(1) def start_service(): check_repo() print(f"starting {MODULE} ...") run_cmd("./start.py", cwd=LOCAL_PATH) def stop_service(): check_repo() print(f"stopping {MODULE}...") run_cmd("./stop.py", cwd=LOCAL_PATH) def restart_service(): check_repo() print(f"restart {MODULE}...") run_cmd("./stop.py || true", cwd=LOCAL_PATH) run_cmd("./start.py", cwd=LOCAL_PATH) def status_service(): check_repo() print(f"check {MODULE} status...") run_cmd("./status.py", cwd=LOCAL_PATH) def main(): if len(sys.argv) < 2: print(f"usage: python3.9 {MODULE}-deploy.py [update|start|stop|restart|status]") sys.exit(1) action = sys.argv[1].lower() if action == "update": update_repo() elif action == "start": start_service() elif action == "stop": stop_service() elif action == "restart": restart_service() elif action == "status": status_service() else: print(f"未知参数: {action}") print("可用参数: update | start | stop | restart | status") sys.exit(1) print(f"[ok] completed: {action}") if __name__ == "__main__": main()