1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
| #!/bin/bash
set -euo pipefail
SERVER_HOST="127.0.0.1" SERVER_PORT="22" SERVER_USER="root" SERVER_PASSWORD="${SERVER_PASSWORD:-}" SERVER_DIR="/opt/project" PROJECT_NAME="project" BRANCH="main" FRONTEND_PORT="80" DB_PORT="8000"
RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m'
info() { echo -e "${BLUE}[INFO]${NC} $1"; } success() { echo -e "${GREEN}[OK]${NC} $1"; } warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } error() { echo -e "${RED}[ERROR]${NC} $1"; } section() { echo -e "\n${CYAN}========================================${NC}"; }
require_env() { if [ -z "${SERVER_PASSWORD}" ]; then error "未设置 SERVER_PASSWORD" exit 1 fi }
ssh_cmd() { local cmd="$1" require_env sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no -p "${SERVER_PORT}" \ "${SERVER_USER}@${SERVER_HOST}" "$cmd" }
check_local_dependencies() { section info "检查本地依赖..." command -v sshpass >/dev/null || { error "缺少 sshpass"; exit 1; } command -v scp >/dev/null || { error "缺少 scp"; exit 1; } command -v bun >/dev/null || { error "缺少 bun"; exit 1; } success "本地依赖检查通过" }
check_server_connection() { section info "检查服务器连接..." ssh_cmd "echo connected >/dev/null" success "服务器连接成功" }
install_server_dependencies() { section info "安装/检查服务器依赖..." ssh_cmd ' set -e sudo apt-get update sudo apt-get install -y docker.io docker-compose-plugin rsync curl sudo systemctl enable docker sudo systemctl start docker ' success "服务器依赖准备完成" }
setup_deploy_directory() { section info "创建部署目录..." ssh_cmd " mkdir -p ${SERVER_DIR} mkdir -p ${SERVER_DIR}/data mkdir -p ${SERVER_DIR}/logs " success "部署目录创建完成" }
build_frontend() { section info "本地构建前端..." bun run build:inventory success "构建完成" }
package_and_upload() { section info "上传部署产物..." local temp_dir temp_dir=$(mktemp -d) cp -r dist/inventory "${temp_dir}/" cp docker-compose.yml "${temp_dir}/" cp -r scripts "${temp_dir}/" sshpass -p "$SERVER_PASSWORD" scp -r -P "${SERVER_PORT}" \ "${temp_dir}/"* "${SERVER_USER}@${SERVER_HOST}:${SERVER_DIR}/" rm -rf "${temp_dir}" success "上传完成" }
setup_runtime_configs() { section info "生成远程运行配置..." ssh_cmd "cat > ${SERVER_DIR}/docker-compose.prod.yml << 'EOF' services: app: image: nginx:alpine container_name: ${PROJECT_NAME}-nginx ports: - \"${FRONTEND_PORT}:80\" volumes: - ${SERVER_DIR}/dist/inventory:/usr/share/nginx/html:ro restart: unless-stopped EOF" success "运行配置生成完成" }
start_services() { section info "启动服务..." ssh_cmd " cd ${SERVER_DIR} docker compose -f docker-compose.prod.yml up -d docker ps " success "服务启动完成" }
stop_services() { section info "停止服务..." ssh_cmd " cd ${SERVER_DIR} docker compose -f docker-compose.prod.yml down " success "服务已停止" }
restart_services() { stop_services sleep 1 start_services }
check_status() { section info "查看状态..." ssh_cmd "docker ps -a" }
view_logs() { section info "查看日志..." ssh_cmd "cd ${SERVER_DIR} && docker compose -f docker-compose.prod.yml logs --tail=100" }
health_check() { section info "健康检查..." ssh_cmd "curl -s -o /dev/null -w '%{http_code}' http://localhost:${FRONTEND_PORT}/" | \ grep -qE '200|304' success "健康检查通过" }
full_deploy() { section info "开始完整部署..." check_local_dependencies check_server_connection install_server_dependencies setup_deploy_directory build_frontend package_and_upload setup_runtime_configs start_services health_check success "部署完成" }
show_help() { echo "用法:$0 <command>" echo "command: deploy | start | stop | restart | status | logs | health | check" }
main() { local cmd="${1:-deploy}" case "$cmd" in deploy) full_deploy ;; start) check_server_connection; start_services ;; stop) check_server_connection; stop_services ;; restart) check_server_connection; restart_services ;; status) check_server_connection; check_status ;; logs) check_server_connection; view_logs ;; health) check_server_connection; health_check ;; check) check_server_connection ;; -h|--help) show_help ;; *) error "未知命令:$cmd"; show_help; exit 1 ;; esac }
main "$@"
|