You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 line
6.2KB

  1. #!/bin/bash
  2. # 福泉WebAPI 安装脚本
  3. # 作者: Your Name
  4. # 版本: 1.0.0
  5. # 颜色定义
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m'
  11. print_message() {
  12. local color=$1
  13. local message=$2
  14. echo -e "${color}[$(date '+%Y-%m-%d %H:%M:%S')] $message${NC}"
  15. }
  16. # 检查是否为root用户
  17. check_root() {
  18. if [ "$EUID" -eq 0 ]; then
  19. print_message $YELLOW "警告: 不建议使用root用户运行此脚本"
  20. read -p "是否继续? (y/N): " -n 1 -r
  21. echo
  22. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  23. exit 1
  24. fi
  25. fi
  26. }
  27. # 检查系统类型
  28. check_system() {
  29. if [[ "$OSTYPE" == "linux-gnu"* ]]; then
  30. print_message $GREEN "检测到Linux系统"
  31. else
  32. print_message $RED "此脚本仅支持Linux系统"
  33. exit 1
  34. fi
  35. }
  36. # 检查Java环境
  37. check_java() {
  38. if command -v java &> /dev/null; then
  39. JAVA_VERSION=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
  40. print_message $GREEN "检测到Java版本: $JAVA_VERSION"
  41. return 0
  42. else
  43. print_message $RED "未找到Java环境"
  44. return 1
  45. fi
  46. }
  47. # 安装Java
  48. install_java() {
  49. print_message $BLUE "正在安装Java环境..."
  50. # 检测包管理器
  51. if command -v apt-get &> /dev/null; then
  52. # Ubuntu/Debian
  53. sudo apt-get update
  54. sudo apt-get install -y openjdk-8-jdk
  55. elif command -v yum &> /dev/null; then
  56. # CentOS/RHEL
  57. sudo yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
  58. elif command -v dnf &> /dev/null; then
  59. # Fedora
  60. sudo dnf install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
  61. else
  62. print_message $RED "不支持的包管理器,请手动安装JDK 1.8+"
  63. exit 1
  64. fi
  65. # 验证安装
  66. if check_java; then
  67. print_message $GREEN "Java安装成功"
  68. else
  69. print_message $RED "Java安装失败"
  70. exit 1
  71. fi
  72. }
  73. # 创建应用目录
  74. create_app_dir() {
  75. APP_DIR="/opt/fuquan-webapi"
  76. if [ ! -d "$APP_DIR" ]; then
  77. sudo mkdir -p "$APP_DIR"
  78. print_message $BLUE "创建应用目录: $APP_DIR"
  79. fi
  80. # 设置权限
  81. sudo chown -R $USER:$USER "$APP_DIR"
  82. print_message $GREEN "应用目录创建完成"
  83. }
  84. # 复制文件
  85. copy_files() {
  86. APP_DIR="/opt/fuquan-webapi"
  87. # 复制JAR文件
  88. if [ -f "webapi-0.0.1-SNAPSHOT.jar" ]; then
  89. cp webapi-0.0.1-SNAPSHOT.jar "$APP_DIR/"
  90. print_message $GREEN "JAR文件复制完成"
  91. else
  92. print_message $RED "未找到JAR文件,请先编译项目"
  93. exit 1
  94. fi
  95. # 复制启动脚本
  96. if [ -f "start.sh" ]; then
  97. cp start.sh "$APP_DIR/"
  98. chmod +x "$APP_DIR/start.sh"
  99. print_message $GREEN "启动脚本复制完成"
  100. fi
  101. # 创建配置文件
  102. if [ ! -f "$APP_DIR/application.yml" ]; then
  103. cat > "$APP_DIR/application.yml" << EOF
  104. spring:
  105. datasource:
  106. url: jdbc:sqlserver://localhost:1433;databaseName=fuquan_db;encrypt=false;trustServerCertificate=true
  107. username: your_username
  108. password: your_password
  109. driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
  110. jpa:
  111. hibernate:
  112. ddl-auto: update
  113. show-sql: false
  114. properties:
  115. hibernate:
  116. dialect: org.hibernate.dialect.SQLServer2012Dialect
  117. server:
  118. port: 8806
  119. servlet:
  120. context-path: /fuquanapi
  121. api:
  122. client:
  123. ak: your_access_key_here
  124. sk: your_secret_key_here
  125. logging:
  126. level:
  127. com.example.webapi: INFO
  128. file:
  129. name: logs/app.log
  130. EOF
  131. print_message $GREEN "配置文件创建完成"
  132. fi
  133. }
  134. # 创建系统服务
  135. create_service() {
  136. SERVICE_FILE="/etc/systemd/system/fuquan-webapi.service"
  137. if [ ! -f "$SERVICE_FILE" ]; then
  138. sudo tee "$SERVICE_FILE" > /dev/null << EOF
  139. [Unit]
  140. Description=福泉WebAPI服务
  141. After=network.target
  142. [Service]
  143. Type=simple
  144. User=$USER
  145. WorkingDirectory=/opt/fuquan-webapi
  146. ExecStart=/opt/fuquan-webapi/start.sh start
  147. ExecStop=/opt/fuquan-webapi/start.sh stop
  148. Restart=always
  149. RestartSec=10
  150. [Install]
  151. WantedBy=multi-user.target
  152. EOF
  153. print_message $GREEN "系统服务创建完成"
  154. # 重新加载systemd
  155. sudo systemctl daemon-reload
  156. print_message $BLUE "系统服务已注册"
  157. else
  158. print_message $YELLOW "系统服务已存在"
  159. fi
  160. }
  161. # 设置防火墙
  162. setup_firewall() {
  163. print_message $BLUE "配置防火墙规则..."
  164. # 检测防火墙类型
  165. if command -v ufw &> /dev/null; then
  166. # Ubuntu UFW
  167. sudo ufw allow 8806/tcp
  168. print_message $GREEN "UFW防火墙规则已添加"
  169. elif command -v firewall-cmd &> /dev/null; then
  170. # CentOS/RHEL firewalld
  171. sudo firewall-cmd --permanent --add-port=8806/tcp
  172. sudo firewall-cmd --reload
  173. print_message $GREEN "firewalld防火墙规则已添加"
  174. else
  175. print_message $YELLOW "未检测到防火墙,请手动开放8806端口"
  176. fi
  177. }
  178. # 显示安装完成信息
  179. show_completion() {
  180. print_message $GREEN "=========================================="
  181. print_message $GREEN "福泉WebAPI 安装完成!"
  182. print_message $GREEN "=========================================="
  183. echo ""
  184. print_message $BLUE "应用目录: /opt/fuquan-webapi"
  185. print_message $BLUE "配置文件: /opt/fuquan-webapi/application.yml"
  186. print_message $BLUE "启动脚本: /opt/fuquan-webapi/start.sh"
  187. echo ""
  188. print_message $YELLOW "请编辑配置文件设置数据库连接信息:"
  189. print_message $YELLOW "sudo nano /opt/fuquan-webapi/application.yml"
  190. echo ""
  191. print_message $BLUE "启动服务:"
  192. print_message $BLUE "sudo systemctl start fuquan-webapi"
  193. print_message $BLUE "sudo systemctl enable fuquan-webapi"
  194. echo ""
  195. print_message $BLUE "查看服务状态:"
  196. print_message $BLUE "sudo systemctl status fuquan-webapi"
  197. echo ""
  198. print_message $GREEN "访问地址: http://localhost:8806/fuquanapi"
  199. print_message $GREEN "API文档: http://localhost:8806/fuquanapi/swagger-ui.html"
  200. }
  201. # 主函数
  202. main() {
  203. print_message $BLUE "开始安装福泉WebAPI..."
  204. check_root
  205. check_system
  206. if ! check_java; then
  207. install_java
  208. fi
  209. create_app_dir
  210. copy_files
  211. create_service
  212. setup_firewall
  213. show_completion
  214. }
  215. # 执行主函数
  216. main "$@"