mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-01 17:00:21 +08:00
deploy: restore local build and sqlite compose
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
# Build artifacts
|
||||
build/
|
||||
target/
|
||||
*.so
|
||||
*.egg
|
||||
*.egg-info/
|
||||
|
||||
# Frontend
|
||||
frontend/node_modules/
|
||||
frontend/dist/
|
||||
frontend/.vite/
|
||||
|
||||
# Development
|
||||
|
||||
135
Dockerfile.app.local
Normal file
135
Dockerfile.app.local
Normal file
@@ -0,0 +1,135 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
# Aether 运行镜像:Rust gateway 直接服务 API + 前端静态文件(国内镜像源版本)
|
||||
# 构建命令: docker build -f Dockerfile.app.local -t aether-app:latest .
|
||||
|
||||
ARG RUST_VERSION=1.95.0
|
||||
|
||||
# ==================== 前端构建 ====================
|
||||
FROM node:22-slim AS frontend-builder
|
||||
WORKDIR /app/frontend
|
||||
COPY frontend/package*.json ./
|
||||
RUN --mount=type=cache,id=aether-npm-cache,target=/root/.npm,sharing=locked \
|
||||
npm config set registry https://registry.npmmirror.com && \
|
||||
npm ci --no-audit --no-fund
|
||||
COPY frontend/ ./
|
||||
RUN npm run build
|
||||
|
||||
# ==================== Rust gateway 构建 ====================
|
||||
FROM rust:${RUST_VERSION}-slim AS gateway-base
|
||||
WORKDIR /build
|
||||
|
||||
# 本地镜像优先缩短构建时间,保留 release 语义,但改用更快的 thin LTO。
|
||||
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse \
|
||||
CARGO_PROFILE_RELEASE_LTO=thin \
|
||||
CARGO_PROFILE_RELEASE_CODEGEN_UNITS=16
|
||||
|
||||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
|
||||
--mount=type=cache,target=/var/lib/apt,sharing=locked \
|
||||
sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \
|
||||
apt-get update && apt-get install -y --no-install-recommends \
|
||||
build-essential \
|
||||
ca-certificates \
|
||||
libssl-dev \
|
||||
pkg-config \
|
||||
perl
|
||||
|
||||
RUN --mount=type=cache,id=aether-cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
|
||||
--mount=type=cache,id=aether-cargo-git,target=/usr/local/cargo/git,sharing=locked \
|
||||
cargo install cargo-chef --locked
|
||||
|
||||
FROM gateway-base AS gateway-planner
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
COPY apps/ ./apps/
|
||||
COPY crates/ ./crates/
|
||||
RUN cargo chef prepare --recipe-path recipe.json
|
||||
|
||||
FROM gateway-base AS gateway-builder
|
||||
COPY --from=gateway-planner /build/recipe.json ./recipe.json
|
||||
RUN --mount=type=cache,id=aether-cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
|
||||
--mount=type=cache,id=aether-cargo-git,target=/usr/local/cargo/git,sharing=locked \
|
||||
--mount=type=cache,id=aether-cargo-target-local,target=/build/target,sharing=locked \
|
||||
cargo chef cook --release --locked --package aether-gateway --bin aether-gateway --recipe-path recipe.json
|
||||
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
COPY apps/ ./apps/
|
||||
COPY crates/ ./crates/
|
||||
RUN --mount=type=cache,id=aether-cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
|
||||
--mount=type=cache,id=aether-cargo-git,target=/usr/local/cargo/git,sharing=locked \
|
||||
--mount=type=cache,id=aether-cargo-target-local,target=/build/target,sharing=locked \
|
||||
cargo build --release --locked -p aether-gateway && \
|
||||
cp target/release/aether-gateway /tmp/aether-gateway
|
||||
|
||||
# ==================== 最小运行时打包 ====================
|
||||
FROM gateway-builder AS runtime-prep
|
||||
RUN set -eux; \
|
||||
mkdir -p \
|
||||
/runtime-root/app/data \
|
||||
/runtime-root/app/logs \
|
||||
/runtime-root/etc \
|
||||
/runtime-root/etc/ssl \
|
||||
/runtime-root/lib \
|
||||
/runtime-root/lib64 \
|
||||
/runtime-root/usr/local/bin; \
|
||||
cp /tmp/aether-gateway /runtime-root/usr/local/bin/aether-gateway; \
|
||||
: > /tmp/runtime-libs.txt; \
|
||||
: > /tmp/runtime-scan-queue.txt; \
|
||||
printf '%s\n' /tmp/aether-gateway >> /tmp/runtime-scan-queue.txt; \
|
||||
while [ -s /tmp/runtime-scan-queue.txt ]; do \
|
||||
current="$(head -n1 /tmp/runtime-scan-queue.txt)"; \
|
||||
sed -i '1d' /tmp/runtime-scan-queue.txt; \
|
||||
ldd "$current" | awk '/=>/ { print $3 } $1 ~ /^\// { print $1 }' | while read -r lib; do \
|
||||
[ -n "$lib" ]; \
|
||||
if ! grep -Fxq "$lib" /tmp/runtime-libs.txt; then \
|
||||
printf '%s\n' "$lib" >> /tmp/runtime-libs.txt; \
|
||||
printf '%s\n' "$lib" >> /tmp/runtime-scan-queue.txt; \
|
||||
fi; \
|
||||
done; \
|
||||
done; \
|
||||
sort -u /tmp/runtime-libs.txt -o /tmp/runtime-libs.txt; \
|
||||
while read -r lib; do \
|
||||
[ -n "$lib" ]; \
|
||||
dest="/runtime-root$(dirname "$lib")"; \
|
||||
mkdir -p "$dest"; \
|
||||
cp -L "$lib" "$dest/"; \
|
||||
done < /tmp/runtime-libs.txt; \
|
||||
for lib in \
|
||||
/lib/x86_64-linux-gnu/libnss_dns.so.2 \
|
||||
/lib/x86_64-linux-gnu/libnss_files.so.2 \
|
||||
/lib/x86_64-linux-gnu/libresolv.so.2; do \
|
||||
if [ -f "$lib" ]; then \
|
||||
dest="/runtime-root$(dirname "$lib")"; \
|
||||
mkdir -p "$dest"; \
|
||||
cp -L "$lib" "$dest/"; \
|
||||
fi; \
|
||||
done; \
|
||||
cp -a /usr/lib/ssl /runtime-root/usr/lib/; \
|
||||
cp -a /etc/ssl/certs /runtime-root/etc/ssl/; \
|
||||
if [ -f /etc/ssl/openssl.cnf ]; then \
|
||||
cp /etc/ssl/openssl.cnf /runtime-root/etc/ssl/openssl.cnf; \
|
||||
fi; \
|
||||
if [ -f /etc/nsswitch.conf ]; then \
|
||||
cp /etc/nsswitch.conf /runtime-root/etc/nsswitch.conf; \
|
||||
fi
|
||||
|
||||
# ==================== 运行时镜像 ====================
|
||||
FROM scratch
|
||||
|
||||
# 复制 gateway 二进制
|
||||
COPY --from=runtime-prep /runtime-root/ /
|
||||
|
||||
# 复制前端构建产物
|
||||
COPY --from=frontend-builder /app/frontend/dist /srv/frontend
|
||||
WORKDIR /app
|
||||
|
||||
ENV LANG=C.UTF-8 \
|
||||
LC_ALL=C.UTF-8 \
|
||||
RUST_LOG=aether_gateway=info \
|
||||
APP_PORT=8084 \
|
||||
AETHER_GATEWAY_STATIC_DIR=/srv/frontend
|
||||
|
||||
EXPOSE 8084
|
||||
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
||||
CMD ["/usr/local/bin/aether-gateway", "--healthcheck"]
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/aether-gateway"]
|
||||
207
deploy.sh
Executable file
207
deploy.sh
Executable file
@@ -0,0 +1,207 @@
|
||||
#!/bin/bash
|
||||
# 智能部署脚本 - 自动检测代码变化并重建
|
||||
#
|
||||
# 用法:
|
||||
# 部署/更新: ./deploy.sh
|
||||
# 强制全部重建: ./deploy.sh --force
|
||||
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
LOCAL_APP_IMAGE="${LOCAL_APP_IMAGE:-aether-app:latest}"
|
||||
export LOCAL_APP_IMAGE
|
||||
|
||||
# 兼容 docker-compose 和 docker compose
|
||||
if command -v docker-compose &> /dev/null; then
|
||||
DC=(docker-compose -f docker-compose.yml -f docker-compose.local.yml)
|
||||
USE_LEGACY_COMPOSE=true
|
||||
else
|
||||
DC=(docker compose -f docker-compose.yml -f docker-compose.local.yml)
|
||||
USE_LEGACY_COMPOSE=false
|
||||
fi
|
||||
|
||||
compose_up() {
|
||||
if [ "$USE_LEGACY_COMPOSE" = true ]; then
|
||||
"${DC[@]}" up -d --no-build "$@"
|
||||
else
|
||||
"${DC[@]}" up -d --no-build --pull never "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
# 缓存文件
|
||||
CODE_HASH_FILE=".code-hash"
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: ./deploy.sh [options]
|
||||
|
||||
Options:
|
||||
--force, -f 强制重建并重启
|
||||
-h, --help 显示帮助
|
||||
|
||||
Environment:
|
||||
LOCAL_APP_IMAGE 本地构建镜像名,默认 aether-app:latest
|
||||
EOF
|
||||
}
|
||||
|
||||
FORCE_REBUILD_ALL=false
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--force|-f)
|
||||
FORCE_REBUILD_ALL=true
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Unknown argument: $1"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
require_file() {
|
||||
if [ ! -f "$1" ]; then
|
||||
echo "Required file not found: $1"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
hash_stream() {
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum | cut -d' ' -f1
|
||||
else
|
||||
shasum -a 256 | cut -d' ' -f1
|
||||
fi
|
||||
}
|
||||
|
||||
emit_file_for_hash() {
|
||||
local path="$1"
|
||||
[ -f "$path" ] || return 0
|
||||
printf '\n>>> %s\n' "$path"
|
||||
cat "$path"
|
||||
}
|
||||
|
||||
emit_tree_for_hash() {
|
||||
local root="$1"
|
||||
[ -d "$root" ] || return 0
|
||||
find "$root" -type f \
|
||||
! -path '*/node_modules/*' \
|
||||
! -path '*/target/*' \
|
||||
! -path '*/dist/*' \
|
||||
! -path '*/.mypy_cache/*' \
|
||||
! -path '*/.vite/*' \
|
||||
2>/dev/null | sort | while IFS= read -r path; do
|
||||
emit_file_for_hash "$path"
|
||||
done
|
||||
}
|
||||
|
||||
# 计算代码文件的哈希值
|
||||
calc_code_hash() {
|
||||
{
|
||||
for file in \
|
||||
Dockerfile.app.local \
|
||||
docker-compose.yml \
|
||||
docker-compose.local.yml \
|
||||
.dockerignore \
|
||||
Cargo.toml \
|
||||
Cargo.lock \
|
||||
rust-toolchain.toml \
|
||||
frontend/package.json \
|
||||
frontend/package-lock.json \
|
||||
frontend/index.html \
|
||||
frontend/vite.config.ts \
|
||||
frontend/tsconfig.json \
|
||||
frontend/tsconfig.app.json \
|
||||
frontend/tsconfig.node.json \
|
||||
frontend/postcss.config.js \
|
||||
frontend/tailwind.config.js; do
|
||||
emit_file_for_hash "$file"
|
||||
done
|
||||
|
||||
for dir in frontend/src frontend/public apps crates; do
|
||||
emit_tree_for_hash "$dir"
|
||||
done
|
||||
} | hash_stream
|
||||
}
|
||||
|
||||
# 检查代码是否变化
|
||||
check_code_changed() {
|
||||
local current_hash
|
||||
current_hash=$(calc_code_hash)
|
||||
if [ -f "$CODE_HASH_FILE" ]; then
|
||||
local saved_hash
|
||||
saved_hash=$(cat "$CODE_HASH_FILE")
|
||||
if [ "$current_hash" = "$saved_hash" ]; then
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
save_code_hash() { calc_code_hash > "$CODE_HASH_FILE"; }
|
||||
|
||||
# 构建应用镜像
|
||||
build_app() {
|
||||
require_file Dockerfile.app.local
|
||||
echo ">>> Building app image: $LOCAL_APP_IMAGE"
|
||||
DOCKER_BUILDKIT="${DOCKER_BUILDKIT:-1}" docker build --pull=false -f Dockerfile.app.local -t "$LOCAL_APP_IMAGE" .
|
||||
save_code_hash
|
||||
}
|
||||
|
||||
# 强制全部重建
|
||||
if [ "$FORCE_REBUILD_ALL" = true ]; then
|
||||
echo ">>> Force rebuilding everything..."
|
||||
build_app
|
||||
compose_up --force-recreate
|
||||
docker image prune -f
|
||||
echo ">>> Done!"
|
||||
"${DC[@]}" ps
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 标记是否需要重启
|
||||
NEED_RESTART=false
|
||||
|
||||
# 检查代码是否变化
|
||||
if ! docker image inspect "$LOCAL_APP_IMAGE" >/dev/null 2>&1; then
|
||||
echo ">>> App image not found, building..."
|
||||
build_app
|
||||
NEED_RESTART=true
|
||||
elif check_code_changed; then
|
||||
echo ">>> Code changed, rebuilding app image..."
|
||||
build_app
|
||||
NEED_RESTART=true
|
||||
else
|
||||
echo ">>> Code unchanged."
|
||||
fi
|
||||
|
||||
# 检查容器是否在运行
|
||||
CONTAINERS_RUNNING=true
|
||||
if [ -z "$("${DC[@]}" ps -q 2>/dev/null)" ]; then
|
||||
CONTAINERS_RUNNING=false
|
||||
fi
|
||||
|
||||
# 有变化时重启,或容器未运行时启动
|
||||
if [ "$NEED_RESTART" = true ]; then
|
||||
echo ">>> Restarting services..."
|
||||
compose_up
|
||||
elif [ "$CONTAINERS_RUNNING" = false ]; then
|
||||
echo ">>> Containers not running, starting services..."
|
||||
compose_up
|
||||
else
|
||||
echo ">>> No changes detected, skipping restart."
|
||||
fi
|
||||
|
||||
# 清理
|
||||
docker image prune -f >/dev/null 2>&1 || true
|
||||
|
||||
echo ">>> Done!"
|
||||
echo ">>> Note: empty databases auto-bootstrap on first start."
|
||||
echo ">>> Note: docker compose now defaults to auto-running pending migrations/backfills on app startup."
|
||||
echo ">>> Note: set AETHER_GATEWAY_AUTO_PREPARE_DATABASE=false if you want to keep manual rollout."
|
||||
"${DC[@]}" ps
|
||||
11
docker-compose.local.yml
Normal file
11
docker-compose.local.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
# Aether 本地构建覆盖配置
|
||||
# 使用方法:
|
||||
# 启动服务: docker compose -f docker-compose.yml -f docker-compose.local.yml up -d --build
|
||||
# 或使用: ./deploy.sh
|
||||
|
||||
services:
|
||||
app:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.app.local
|
||||
image: ${LOCAL_APP_IMAGE:-aether-app:latest}
|
||||
@@ -6,6 +6,11 @@ services:
|
||||
- .env
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
AETHER_DATABASE_DRIVER: sqlite
|
||||
AETHER_DATABASE_URL: sqlite://./data/aether.db
|
||||
AETHER_RUNTIME_BACKEND: memory
|
||||
AETHER_GATEWAY_DEPLOYMENT_TOPOLOGY: single-node
|
||||
AETHER_GATEWAY_NODE_ROLE: all
|
||||
AETHER_LOG_DESTINATION: ${AETHER_LOG_DESTINATION:-both}
|
||||
AETHER_LOG_FORMAT: ${AETHER_LOG_FORMAT:-pretty}
|
||||
AETHER_LOG_DIR: ${AETHER_LOG_DIR:-/app/logs}
|
||||
|
||||
Reference in New Issue
Block a user