run.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env bash
  2. set -e
  3. # 颜色定义
  4. RED='\033[0;31m' # 红色
  5. GREEN='\033[0;32m' # 绿色
  6. YELLOW='\033[0;33m' # 黄色
  7. BLUE='\033[0;34m' # 蓝色
  8. NC='\033[0m' # 无颜色
  9. # 获取当前脚本的目录
  10. export BASE_DIR=$(cd "$(dirname "$0")"; pwd)
  11. echo -e "${GREEN}📁 BASE_DIR: ${BASE_DIR}${NC}"
  12. # 检查 config.json 文件是否存在
  13. check_config_file() {
  14. if [ ! -f "${BASE_DIR}/config.json" ]; then
  15. echo -e "${RED}❌ 错误:未找到 config.json 文件。请确保 config.json 存在于当前目录。${NC}"
  16. exit 1
  17. fi
  18. }
  19. # 检查 Python 版本是否大于等于 3.7,并检查 pip 是否可用
  20. check_python_version() {
  21. if ! command -v python3 &> /dev/null; then
  22. echo -e "${RED}❌ 错误:未找到 Python3。请安装 Python 3.7 或以上版本。${NC}"
  23. exit 1
  24. fi
  25. PYTHON_VERSION=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
  26. PYTHON_MAJOR=$(echo "$PYTHON_VERSION" | cut -d'.' -f1)
  27. PYTHON_MINOR=$(echo "$PYTHON_VERSION" | cut -d'.' -f2)
  28. if (( PYTHON_MAJOR < 3 || (PYTHON_MAJOR == 3 && PYTHON_MINOR < 7) )); then
  29. echo -e "${RED}❌ 错误:Python 版本为 ${PYTHON_VERSION}。请安装 Python 3.7 或以上版本。${NC}"
  30. exit 1
  31. fi
  32. if ! python3 -m pip --version &> /dev/null; then
  33. echo -e "${RED}❌ 错误:未找到 pip。请安装 pip。${NC}"
  34. exit 1
  35. fi
  36. }
  37. # 检查并安装缺失的依赖
  38. install_dependencies() {
  39. echo -e "${YELLOW}⏳ 正在安装依赖...${NC}"
  40. if [ ! -f "${BASE_DIR}/requirements.txt" ]; then
  41. echo -e "${RED}❌ 错误:未找到 requirements.txt 文件。${NC}"
  42. exit 1
  43. fi
  44. # 安装 requirements.txt 中的依赖,使用清华大学的 PyPI 镜像
  45. pip3 install -r "${BASE_DIR}/requirements.txt" -i https://pypi.tuna.tsinghua.edu.cn/simple
  46. # 处理 requirements-optional.txt(如果存在)
  47. if [ -f "${BASE_DIR}/requirements-optional.txt" ]; then
  48. echo -e "${YELLOW}⏳ 正在安装可选的依赖...${NC}"
  49. pip3 install -r "${BASE_DIR}/requirements-optional.txt" -i https://pypi.tuna.tsinghua.edu.cn/simple
  50. fi
  51. }
  52. # 启动项目
  53. run_project() {
  54. echo -e "${GREEN}🚀 准备启动项目...${NC}"
  55. cd "${BASE_DIR}"
  56. sleep 2
  57. # 判断操作系统类型
  58. OS_TYPE=$(uname)
  59. if [[ "$OS_TYPE" == "Linux" ]]; then
  60. # 在 Linux 上使用 setsid
  61. setsid python3 "${BASE_DIR}/app.py" > "${BASE_DIR}/nohup.out" 2>&1 &
  62. echo -e "${GREEN}🚀 正在启动 ChatGPT-on-WeChat (Linux)...${NC}"
  63. elif [[ "$OS_TYPE" == "Darwin" ]]; then
  64. # 在 macOS 上直接运行
  65. python3 "${BASE_DIR}/app.py" > "${BASE_DIR}/nohup.out" 2>&1 &
  66. echo -e "${GREEN}🚀 正在启动 ChatGPT-on-WeChat (macOS)...${NC}"
  67. else
  68. echo -e "${RED}❌ 错误:不支持的操作系统 ${OS_TYPE}。${NC}"
  69. exit 1
  70. fi
  71. sleep 2
  72. # 显示日志输出,供用户扫码
  73. tail -n 30 -f "${BASE_DIR}/nohup.out"
  74. }
  75. # 更新项目
  76. update_project() {
  77. echo -e "${GREEN}🔄 准备更新项目,现在停止项目...${NC}"
  78. cd "${BASE_DIR}"
  79. # 停止项目
  80. stop_project
  81. echo -e "${GREEN}🔄 开始更新项目...${NC}"
  82. # 更新代码,从 git 仓库拉取最新代码
  83. if [ -d .git ]; then
  84. GIT_PULL_OUTPUT=$(git pull)
  85. if [ $? -eq 0 ]; then
  86. if [[ "$GIT_PULL_OUTPUT" == *"Already up to date."* ]]; then
  87. echo -e "${GREEN}✅ 代码已经是最新的。${NC}"
  88. else
  89. echo -e "${GREEN}✅ 代码更新完成。${NC}"
  90. fi
  91. else
  92. echo -e "${YELLOW}⚠️ 从 GitHub 更新失败,尝试切换到 Gitee 仓库...${NC}"
  93. # 更改远程仓库为 Gitee
  94. git remote set-url origin https://gitee.com/zhayujie/chatgpt-on-wechat.git
  95. GIT_PULL_OUTPUT=$(git pull)
  96. if [ $? -eq 0 ]; then
  97. if [[ "$GIT_PULL_OUTPUT" == *"Already up to date."* ]]; then
  98. echo -e "${GREEN}✅ 代码已经是最新的。${NC}"
  99. else
  100. echo -e "${GREEN}✅ 从 Gitee 更新成功。${NC}"
  101. fi
  102. else
  103. echo -e "${RED}❌ 错误:从 Gitee 更新仍然失败,请检查网络连接。${NC}"
  104. exit 1
  105. fi
  106. fi
  107. else
  108. echo -e "${RED}❌ 错误:当前目录不是 git 仓库,无法更新代码。${NC}"
  109. exit 1
  110. fi
  111. # 安装依赖
  112. install_dependencies
  113. # 启动项目
  114. run_project
  115. }
  116. # 停止项目
  117. stop_project() {
  118. echo -e "${GREEN}🛑 正在停止项目...${NC}"
  119. cd "${BASE_DIR}"
  120. pid=$(ps ax | grep -i app.py | grep "${BASE_DIR}" | grep python3 | grep -v grep | awk '{print $1}')
  121. if [ -z "$pid" ] ; then
  122. echo -e "${YELLOW}⚠️ 未找到正在运行的 ChatGPT-on-WeChat。${NC}"
  123. return
  124. fi
  125. echo -e "${GREEN}🛑 正在运行的 ChatGPT-on-WeChat (PID: ${pid})${NC}"
  126. kill ${pid}
  127. sleep 3
  128. if ps -p $pid > /dev/null; then
  129. echo -e "${YELLOW}⚠️ 进程未停止,尝试强制终止...${NC}"
  130. kill -9 ${pid}
  131. fi
  132. echo -e "${GREEN}✅ 已停止 ChatGPT-on-WeChat (PID: ${pid})${NC}"
  133. }
  134. # 主函数,根据用户参数执行操作
  135. case "$1" in
  136. start)
  137. check_config_file
  138. check_python_version
  139. run_project
  140. ;;
  141. stop)
  142. stop_project
  143. ;;
  144. restart)
  145. stop_project
  146. check_config_file
  147. check_python_version
  148. run_project
  149. ;;
  150. update)
  151. check_config_file
  152. check_python_version
  153. update_project
  154. ;;
  155. *)
  156. echo -e "${YELLOW}=========================================${NC}"
  157. echo -e "${YELLOW}用法:${GREEN}$0 ${BLUE}{start|stop|restart|update}${NC}"
  158. echo -e "${YELLOW}示例:${NC}"
  159. echo -e " ${GREEN}$0 ${BLUE}start${NC}"
  160. echo -e " ${GREEN}$0 ${BLUE}stop${NC}"
  161. echo -e " ${GREEN}$0 ${BLUE}restart${NC}"
  162. echo -e " ${GREEN}$0 ${BLUE}update${NC}"
  163. echo -e "${YELLOW}=========================================${NC}"
  164. exit 1
  165. ;;
  166. esac