| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- #!/bin/bash
- # ============================================
- # 灵越智报 2.0 - 服务器一键部署脚本
- # 适用于: Ubuntu 22.04 LTS
- # 服务器: lanaipc
- # ============================================
- set -e
- # 颜色定义
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- BLUE='\033[0;34m'
- NC='\033[0m'
- # 配置
- PROJECT_DIR="/mnt/win_home/lingyue-zhibao"
- LOG_DIR="/var/log/lingyue"
- DATA_DIR="/mnt/win_home/lingyue-data"
- # 数据库配置
- DB_NAME="lingyue_zhibao"
- DB_USER="lingyue"
- DB_PASS="123123"
- # RabbitMQ 配置
- MQ_USER="admin"
- MQ_PASS="admin123"
- log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
- log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
- log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
- log_title() { echo -e "\n${BLUE}========== $1 ==========${NC}\n"; }
- # 检查 root 权限
- check_root() {
- if [ "$EUID" -ne 0 ]; then
- log_error "请使用 root 用户运行此脚本"
- exit 1
- fi
- }
- # 安装基础依赖
- install_dependencies() {
- log_title "安装基础依赖"
-
- apt update
- apt install -y openjdk-17-jdk maven git curl wget
-
- log_info "Java 版本: $(java -version 2>&1 | head -1)"
- log_info "Maven 版本: $(mvn -version 2>&1 | head -1)"
- }
- # 安装 PostgreSQL + pgvector
- install_postgresql() {
- log_title "安装 PostgreSQL + pgvector"
-
- # 安装 PostgreSQL
- apt install -y postgresql postgresql-contrib postgresql-server-dev-all build-essential
-
- # 编译安装 pgvector
- if [ ! -d "/tmp/pgvector" ]; then
- cd /tmp
- git clone https://github.com/pgvector/pgvector.git
- fi
- cd /tmp/pgvector
- make clean || true
- make
- make install
-
- # 启动服务
- systemctl enable postgresql
- systemctl restart postgresql
-
- log_info "PostgreSQL 已安装并启动"
- }
- # 配置数据库
- setup_database() {
- log_title "配置数据库"
-
- # 创建用户和数据库
- sudo -u postgres psql <<EOF
- DO \$\$
- BEGIN
- IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '${DB_USER}') THEN
- CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}';
- END IF;
- END
- \$\$;
- SELECT 'CREATE DATABASE ${DB_NAME} OWNER ${DB_USER}'
- WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${DB_NAME}')\gexec
- GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};
- EOF
- # 创建扩展
- sudo -u postgres psql -d ${DB_NAME} <<EOF
- CREATE EXTENSION IF NOT EXISTS vector;
- CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
- EOF
-
- log_info "数据库 ${DB_NAME} 配置完成"
- }
- # 安装 Redis
- install_redis() {
- log_title "安装 Redis"
-
- apt install -y redis-server
- systemctl enable redis-server
- systemctl restart redis-server
-
- log_info "Redis 已安装: $(redis-cli ping)"
- }
- # 安装 RabbitMQ
- install_rabbitmq() {
- log_title "安装 RabbitMQ"
-
- apt install -y rabbitmq-server
- systemctl enable rabbitmq-server
- systemctl restart rabbitmq-server
-
- # 启用管理插件
- rabbitmq-plugins enable rabbitmq_management
-
- # 创建用户
- rabbitmqctl add_user ${MQ_USER} ${MQ_PASS} 2>/dev/null || true
- rabbitmqctl set_user_tags ${MQ_USER} administrator
- rabbitmqctl set_permissions -p / ${MQ_USER} ".*" ".*" ".*"
-
- log_info "RabbitMQ 已安装,管理界面: http://localhost:15672"
- }
- # 安装 Ollama
- install_ollama() {
- log_title "安装 Ollama"
-
- if ! command -v ollama &> /dev/null; then
- curl -fsSL https://ollama.ai/install.sh | sh
- fi
-
- # 启动 Ollama
- nohup ollama serve > /var/log/ollama.log 2>&1 &
- sleep 5
-
- # 下载 Embedding 模型
- if ! ollama list | grep -q "nomic-embed-text"; then
- log_info "下载 nomic-embed-text 模型..."
- ollama pull nomic-embed-text
- fi
-
- log_info "Ollama 已安装"
- }
- # 部署项目
- deploy_project() {
- log_title "部署项目"
-
- # 创建目录
- mkdir -p ${LOG_DIR}
- mkdir -p ${DATA_DIR}
-
- # 进入项目目录
- if [ ! -d "${PROJECT_DIR}" ]; then
- log_error "项目目录不存在: ${PROJECT_DIR}"
- log_info "请先克隆项目到 ${PROJECT_DIR}"
- exit 1
- fi
-
- cd ${PROJECT_DIR}
-
- # 初始化数据库表
- log_info "初始化数据库表..."
- PGPASSWORD=${DB_PASS} psql -U ${DB_USER} -d ${DB_NAME} -h localhost -f backend/sql/init.sql
- PGPASSWORD=${DB_PASS} psql -U ${DB_USER} -d ${DB_NAME} -h localhost -f backend/sql/rag_tables.sql 2>/dev/null || true
-
- # 编译项目
- log_info "编译项目..."
- cd backend
- mvn clean package -DskipTests -q
-
- log_info "项目编译完成"
- }
- # 创建 Systemd 服务
- create_systemd_service() {
- log_title "创建 Systemd 服务"
-
- cat > /etc/systemd/system/lingyue.service <<EOF
- [Unit]
- Description=Lingyue Zhibao Application
- After=network.target postgresql.service redis-server.service rabbitmq-server.service
- [Service]
- Type=simple
- User=root
- WorkingDirectory=${PROJECT_DIR}/backend
- ExecStart=/usr/bin/java -Xms1g -Xmx2g -XX:+UseG1GC -jar lingyue-starter/target/lingyue-starter.jar
- Restart=always
- RestartSec=10
- StandardOutput=append:${LOG_DIR}/lingyue.log
- StandardError=append:${LOG_DIR}/lingyue-error.log
- Environment=JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
- Environment=SPRING_PROFILES_ACTIVE=prod
- [Install]
- WantedBy=multi-user.target
- EOF
- systemctl daemon-reload
- log_info "Systemd 服务已创建"
- }
- # 启动应用
- start_app() {
- log_title "启动应用"
-
- systemctl enable lingyue
- systemctl restart lingyue
-
- sleep 10
-
- if systemctl is-active --quiet lingyue; then
- log_info "应用启动成功"
- log_info "访问地址: http://$(hostname -I | awk '{print $1}'):8000"
- else
- log_error "应用启动失败,查看日志: journalctl -u lingyue -f"
- fi
- }
- # 停止应用
- stop_app() {
- log_title "停止应用"
- systemctl stop lingyue
- log_info "应用已停止"
- }
- # 查看状态
- show_status() {
- log_title "服务状态"
-
- echo "PostgreSQL: $(systemctl is-active postgresql)"
- echo "Redis: $(systemctl is-active redis-server)"
- echo "RabbitMQ: $(systemctl is-active rabbitmq-server)"
- echo "Lingyue: $(systemctl is-active lingyue)"
-
- if command -v ollama &> /dev/null && curl -s http://localhost:11434/api/tags > /dev/null 2>&1; then
- echo "Ollama: running"
- else
- echo "Ollama: stopped"
- fi
-
- echo ""
- if systemctl is-active --quiet lingyue; then
- log_info "应用访问地址: http://$(hostname -I | awk '{print $1}'):8000"
- fi
- }
- # 查看日志
- show_logs() {
- journalctl -u lingyue -f
- }
- # 健康检查
- health_check() {
- log_title "健康检查"
-
- local health=$(curl -s http://localhost:8000/actuator/health 2>/dev/null)
- if [ -n "$health" ]; then
- echo "$health" | python3 -m json.tool 2>/dev/null || echo "$health"
- else
- log_error "应用未响应"
- fi
- }
- # 完整安装
- full_install() {
- check_root
- install_dependencies
- install_postgresql
- setup_database
- install_redis
- install_rabbitmq
- install_ollama
- deploy_project
- create_systemd_service
- start_app
-
- log_title "部署完成"
- show_status
- }
- # 仅更新代码
- update_only() {
- log_title "更新代码"
-
- cd ${PROJECT_DIR}
- git pull origin main || git pull origin master || true
-
- cd backend
- mvn clean package -DskipTests -q
-
- systemctl restart lingyue
-
- sleep 10
- health_check
- }
- # 显示帮助
- show_help() {
- cat <<EOF
- 灵越智报 2.0 - 服务器部署脚本
- 用法: ./server-deploy.sh [命令]
- 命令:
- install 完整安装(首次部署)
- update 仅更新代码并重启
- start 启动应用
- stop 停止应用
- restart 重启应用
- status 查看服务状态
- logs 查看应用日志
- health 健康检查
- help 显示此帮助
- 首次部署:
- ./server-deploy.sh install
- 更新部署:
- ./server-deploy.sh update
- EOF
- }
- # 主函数
- main() {
- case "${1:-help}" in
- install)
- full_install
- ;;
- update)
- update_only
- ;;
- start)
- start_app
- ;;
- stop)
- stop_app
- ;;
- restart)
- systemctl restart lingyue
- sleep 5
- health_check
- ;;
- status)
- show_status
- ;;
- logs)
- show_logs
- ;;
- health)
- health_check
- ;;
- help|--help|-h)
- show_help
- ;;
- *)
- log_error "未知命令: $1"
- show_help
- exit 1
- ;;
- esac
- }
- main "$@"
|