feat(deploy): 优化 Docker 部署体验,新增一键部署脚本
## 新增功能 - 新增 docker-compose.local.yml:使用本地目录存储数据,便于迁移和备份 - 新增 docker-deploy.sh:一键部署脚本,自动生成安全密钥(JWT_SECRET、TOTP_ENCRYPTION_KEY、POSTGRES_PASSWORD) - 新增 deploy/.gitignore:忽略运行时数据目录 ## 优化改进 - docker-compose.local.yml 包含 PGDATA 环境变量修复,解决 PostgreSQL 18 Alpine 数据丢失问题 - 脚本自动设置 .env 文件权限为 600,增强安全性 - 脚本显示生成的凭证,方便用户记录 ## 文档更新 - 更新 README.md(英文版):新增"快速开始"章节,添加部署版本对比表 - 更新 README_CN.md(中文版):同步英文版更新 - 更新 deploy/README.md:详细说明两种部署方式和迁移方法 ## 使用方式 一键部署: ```bash curl -sSL https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy/docker-deploy.sh | bash docker-compose -f docker-compose.local.yml up -d ``` 轻松迁移: ```bash tar czf sub2api-complete.tar.gz deploy/ # 传输到新服务器后直接解压启动即可 ```
This commit is contained in:
19
deploy/.gitignore
vendored
Normal file
19
deploy/.gitignore
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
# =============================================================================
|
||||
# Sub2API Deploy Directory - Git Ignore
|
||||
# =============================================================================
|
||||
|
||||
# Data directories (generated at runtime when using docker-compose.local.yml)
|
||||
data/
|
||||
postgres_data/
|
||||
redis_data/
|
||||
|
||||
# Environment configuration (contains sensitive information)
|
||||
.env
|
||||
|
||||
# Backup files
|
||||
*.backup
|
||||
*.bak
|
||||
|
||||
# Temporary files
|
||||
*.tmp
|
||||
*.log
|
||||
147
deploy/README.md
147
deploy/README.md
@@ -13,7 +13,9 @@ This directory contains files for deploying Sub2API on Linux servers.
|
||||
|
||||
| File | Description |
|
||||
|------|-------------|
|
||||
| `docker-compose.yml` | Docker Compose configuration |
|
||||
| `docker-compose.yml` | Docker Compose configuration (named volumes) |
|
||||
| `docker-compose.local.yml` | Docker Compose configuration (local directories, easy migration) |
|
||||
| `docker-deploy.sh` | **One-click Docker deployment script (recommended)** |
|
||||
| `.env.example` | Docker environment variables template |
|
||||
| `DOCKER.md` | Docker Hub documentation |
|
||||
| `install.sh` | One-click binary installation script |
|
||||
@@ -24,7 +26,45 @@ This directory contains files for deploying Sub2API on Linux servers.
|
||||
|
||||
## Docker Deployment (Recommended)
|
||||
|
||||
### Quick Start
|
||||
### Method 1: One-Click Deployment (Recommended)
|
||||
|
||||
Use the automated preparation script for the easiest setup:
|
||||
|
||||
```bash
|
||||
# Download and run the preparation script
|
||||
curl -sSL https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy/docker-deploy.sh | bash
|
||||
|
||||
# Or download first, then run
|
||||
curl -sSL https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy/docker-deploy.sh -o docker-deploy.sh
|
||||
chmod +x docker-deploy.sh
|
||||
./docker-deploy.sh
|
||||
```
|
||||
|
||||
**What the script does:**
|
||||
- Downloads `docker-compose.local.yml` and `.env.example`
|
||||
- Automatically generates secure secrets (JWT_SECRET, TOTP_ENCRYPTION_KEY, POSTGRES_PASSWORD)
|
||||
- Creates `.env` file with generated secrets
|
||||
- Creates necessary data directories (data/, postgres_data/, redis_data/)
|
||||
- **Displays generated credentials** (POSTGRES_PASSWORD, JWT_SECRET, etc.)
|
||||
|
||||
**After running the script:**
|
||||
```bash
|
||||
# Start services
|
||||
docker-compose -f docker-compose.local.yml up -d
|
||||
|
||||
# View logs
|
||||
docker-compose -f docker-compose.local.yml logs -f sub2api
|
||||
|
||||
# If admin password was auto-generated, find it in logs:
|
||||
docker-compose -f docker-compose.local.yml logs sub2api | grep "admin password"
|
||||
|
||||
# Access Web UI
|
||||
# http://localhost:8080
|
||||
```
|
||||
|
||||
### Method 2: Manual Deployment
|
||||
|
||||
If you prefer manual control:
|
||||
|
||||
```bash
|
||||
# Clone repository
|
||||
@@ -33,18 +73,36 @@ cd sub2api/deploy
|
||||
|
||||
# Configure environment
|
||||
cp .env.example .env
|
||||
nano .env # Set POSTGRES_PASSWORD (required)
|
||||
nano .env # Set POSTGRES_PASSWORD and other required variables
|
||||
|
||||
# Start all services
|
||||
docker-compose up -d
|
||||
# Generate secure secrets (recommended)
|
||||
JWT_SECRET=$(openssl rand -hex 32)
|
||||
TOTP_ENCRYPTION_KEY=$(openssl rand -hex 32)
|
||||
echo "JWT_SECRET=${JWT_SECRET}" >> .env
|
||||
echo "TOTP_ENCRYPTION_KEY=${TOTP_ENCRYPTION_KEY}" >> .env
|
||||
|
||||
# Create data directories
|
||||
mkdir -p data postgres_data redis_data
|
||||
|
||||
# Start all services using local directory version
|
||||
docker-compose -f docker-compose.local.yml up -d
|
||||
|
||||
# View logs (check for auto-generated admin password)
|
||||
docker-compose logs -f sub2api
|
||||
docker-compose -f docker-compose.local.yml logs -f sub2api
|
||||
|
||||
# Access Web UI
|
||||
# http://localhost:8080
|
||||
```
|
||||
|
||||
### Deployment Version Comparison
|
||||
|
||||
| Version | Data Storage | Migration | Best For |
|
||||
|---------|-------------|-----------|----------|
|
||||
| **docker-compose.local.yml** | Local directories (./data, ./postgres_data, ./redis_data) | ✅ Easy (tar entire directory) | Production, need frequent backups/migration |
|
||||
| **docker-compose.yml** | Named volumes (/var/lib/docker/volumes/) | ⚠️ Requires docker commands | Simple setup, don't need migration |
|
||||
|
||||
**Recommendation:** Use `docker-compose.local.yml` (deployed by `docker-deploy.sh`) for easier data management and migration.
|
||||
|
||||
### How Auto-Setup Works
|
||||
|
||||
When using Docker Compose with `AUTO_SETUP=true`:
|
||||
@@ -89,6 +147,32 @@ SELECT
|
||||
|
||||
### Commands
|
||||
|
||||
For **local directory version** (docker-compose.local.yml):
|
||||
|
||||
```bash
|
||||
# Start services
|
||||
docker-compose -f docker-compose.local.yml up -d
|
||||
|
||||
# Stop services
|
||||
docker-compose -f docker-compose.local.yml down
|
||||
|
||||
# View logs
|
||||
docker-compose -f docker-compose.local.yml logs -f sub2api
|
||||
|
||||
# Restart Sub2API only
|
||||
docker-compose -f docker-compose.local.yml restart sub2api
|
||||
|
||||
# Update to latest version
|
||||
docker-compose -f docker-compose.local.yml pull
|
||||
docker-compose -f docker-compose.local.yml up -d
|
||||
|
||||
# Remove all data (caution!)
|
||||
docker-compose -f docker-compose.local.yml down
|
||||
rm -rf data/ postgres_data/ redis_data/
|
||||
```
|
||||
|
||||
For **named volumes version** (docker-compose.yml):
|
||||
|
||||
```bash
|
||||
# Start services
|
||||
docker-compose up -d
|
||||
@@ -115,10 +199,11 @@ docker-compose down -v
|
||||
| Variable | Required | Default | Description |
|
||||
|----------|----------|---------|-------------|
|
||||
| `POSTGRES_PASSWORD` | **Yes** | - | PostgreSQL password |
|
||||
| `JWT_SECRET` | **Recommended** | *(auto-generated)* | JWT secret (fixed for persistent sessions) |
|
||||
| `TOTP_ENCRYPTION_KEY` | **Recommended** | *(auto-generated)* | TOTP encryption key (fixed for persistent 2FA) |
|
||||
| `SERVER_PORT` | No | `8080` | Server port |
|
||||
| `ADMIN_EMAIL` | No | `admin@sub2api.local` | Admin email |
|
||||
| `ADMIN_PASSWORD` | No | *(auto-generated)* | Admin password |
|
||||
| `JWT_SECRET` | No | *(auto-generated)* | JWT secret |
|
||||
| `TZ` | No | `Asia/Shanghai` | Timezone |
|
||||
| `GEMINI_OAUTH_CLIENT_ID` | No | *(builtin)* | Google OAuth client ID (Gemini OAuth). Leave empty to use the built-in Gemini CLI client. |
|
||||
| `GEMINI_OAUTH_CLIENT_SECRET` | No | *(builtin)* | Google OAuth client secret (Gemini OAuth). Leave empty to use the built-in Gemini CLI client. |
|
||||
@@ -127,6 +212,30 @@ docker-compose down -v
|
||||
|
||||
See `.env.example` for all available options.
|
||||
|
||||
> **Note:** The `docker-deploy.sh` script automatically generates `JWT_SECRET`, `TOTP_ENCRYPTION_KEY`, and `POSTGRES_PASSWORD` for you.
|
||||
|
||||
### Easy Migration (Local Directory Version)
|
||||
|
||||
When using `docker-compose.local.yml`, all data is stored in local directories, making migration simple:
|
||||
|
||||
```bash
|
||||
# On source server: Stop services and create archive
|
||||
cd /path/to/deployment
|
||||
docker-compose -f docker-compose.local.yml down
|
||||
cd ..
|
||||
tar czf sub2api-complete.tar.gz deployment/
|
||||
|
||||
# Transfer to new server
|
||||
scp sub2api-complete.tar.gz user@new-server:/path/to/destination/
|
||||
|
||||
# On new server: Extract and start
|
||||
tar xzf sub2api-complete.tar.gz
|
||||
cd deployment/
|
||||
docker-compose -f docker-compose.local.yml up -d
|
||||
```
|
||||
|
||||
Your entire deployment (configuration + data) is migrated!
|
||||
|
||||
---
|
||||
|
||||
## Gemini OAuth Configuration
|
||||
@@ -359,6 +468,30 @@ The main config file is at `/etc/sub2api/config.yaml` (created by Setup Wizard).
|
||||
|
||||
### Docker
|
||||
|
||||
For **local directory version**:
|
||||
|
||||
```bash
|
||||
# Check container status
|
||||
docker-compose -f docker-compose.local.yml ps
|
||||
|
||||
# View detailed logs
|
||||
docker-compose -f docker-compose.local.yml logs --tail=100 sub2api
|
||||
|
||||
# Check database connection
|
||||
docker-compose -f docker-compose.local.yml exec postgres pg_isready
|
||||
|
||||
# Check Redis connection
|
||||
docker-compose -f docker-compose.local.yml exec redis redis-cli ping
|
||||
|
||||
# Restart all services
|
||||
docker-compose -f docker-compose.local.yml restart
|
||||
|
||||
# Check data directories
|
||||
ls -la data/ postgres_data/ redis_data/
|
||||
```
|
||||
|
||||
For **named volumes version**:
|
||||
|
||||
```bash
|
||||
# Check container status
|
||||
docker-compose ps
|
||||
|
||||
222
deploy/docker-compose.local.yml
Normal file
222
deploy/docker-compose.local.yml
Normal file
@@ -0,0 +1,222 @@
|
||||
# =============================================================================
|
||||
# Sub2API Docker Compose - Local Directory Version
|
||||
# =============================================================================
|
||||
# This configuration uses local directories for data storage instead of named
|
||||
# volumes, making it easy to migrate the entire deployment by simply copying
|
||||
# the deploy directory.
|
||||
#
|
||||
# Quick Start:
|
||||
# 1. Copy .env.example to .env and configure
|
||||
# 2. mkdir -p data postgres_data redis_data
|
||||
# 3. docker-compose -f docker-compose.local.yml up -d
|
||||
# 4. Check logs: docker-compose -f docker-compose.local.yml logs -f sub2api
|
||||
# 5. Access: http://localhost:8080
|
||||
#
|
||||
# Migration to New Server:
|
||||
# 1. docker-compose -f docker-compose.local.yml down
|
||||
# 2. tar czf sub2api-deploy.tar.gz deploy/
|
||||
# 3. Transfer to new server and extract
|
||||
# 4. docker-compose -f docker-compose.local.yml up -d
|
||||
# =============================================================================
|
||||
|
||||
services:
|
||||
# ===========================================================================
|
||||
# Sub2API Application
|
||||
# ===========================================================================
|
||||
sub2api:
|
||||
image: weishaw/sub2api:latest
|
||||
container_name: sub2api
|
||||
restart: unless-stopped
|
||||
ulimits:
|
||||
nofile:
|
||||
soft: 100000
|
||||
hard: 100000
|
||||
ports:
|
||||
- "${BIND_HOST:-0.0.0.0}:${SERVER_PORT:-8080}:8080"
|
||||
volumes:
|
||||
# Local directory mapping for easy migration
|
||||
- ./data:/app/data
|
||||
# Optional: Mount custom config.yaml (uncomment and create the file first)
|
||||
# Copy config.example.yaml to config.yaml, modify it, then uncomment:
|
||||
# - ./config.yaml:/app/data/config.yaml:ro
|
||||
environment:
|
||||
# =======================================================================
|
||||
# Auto Setup (REQUIRED for Docker deployment)
|
||||
# =======================================================================
|
||||
- AUTO_SETUP=true
|
||||
|
||||
# =======================================================================
|
||||
# Server Configuration
|
||||
# =======================================================================
|
||||
- SERVER_HOST=0.0.0.0
|
||||
- SERVER_PORT=8080
|
||||
- SERVER_MODE=${SERVER_MODE:-release}
|
||||
- RUN_MODE=${RUN_MODE:-standard}
|
||||
|
||||
# =======================================================================
|
||||
# Database Configuration (PostgreSQL)
|
||||
# =======================================================================
|
||||
- DATABASE_HOST=postgres
|
||||
- DATABASE_PORT=5432
|
||||
- DATABASE_USER=${POSTGRES_USER:-sub2api}
|
||||
- DATABASE_PASSWORD=${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
|
||||
- DATABASE_DBNAME=${POSTGRES_DB:-sub2api}
|
||||
- DATABASE_SSLMODE=disable
|
||||
|
||||
# =======================================================================
|
||||
# Redis Configuration
|
||||
# =======================================================================
|
||||
- REDIS_HOST=redis
|
||||
- REDIS_PORT=6379
|
||||
- REDIS_PASSWORD=${REDIS_PASSWORD:-}
|
||||
- REDIS_DB=${REDIS_DB:-0}
|
||||
- REDIS_ENABLE_TLS=${REDIS_ENABLE_TLS:-false}
|
||||
|
||||
# =======================================================================
|
||||
# Admin Account (auto-created on first run)
|
||||
# =======================================================================
|
||||
- ADMIN_EMAIL=${ADMIN_EMAIL:-admin@sub2api.local}
|
||||
- ADMIN_PASSWORD=${ADMIN_PASSWORD:-}
|
||||
|
||||
# =======================================================================
|
||||
# JWT Configuration
|
||||
# =======================================================================
|
||||
# IMPORTANT: Set a fixed JWT_SECRET to prevent login sessions from being
|
||||
# invalidated after container restarts. If left empty, a random secret
|
||||
# will be generated on each startup.
|
||||
# Generate a secure secret: openssl rand -hex 32
|
||||
- JWT_SECRET=${JWT_SECRET:-}
|
||||
- JWT_EXPIRE_HOUR=${JWT_EXPIRE_HOUR:-24}
|
||||
|
||||
# =======================================================================
|
||||
# TOTP (2FA) Configuration
|
||||
# =======================================================================
|
||||
# IMPORTANT: Set a fixed encryption key for TOTP secrets. If left empty,
|
||||
# a random key will be generated on each startup, causing all existing
|
||||
# TOTP configurations to become invalid (users won't be able to login
|
||||
# with 2FA).
|
||||
# Generate a secure key: openssl rand -hex 32
|
||||
- TOTP_ENCRYPTION_KEY=${TOTP_ENCRYPTION_KEY:-}
|
||||
|
||||
# =======================================================================
|
||||
# Timezone Configuration
|
||||
# This affects ALL time operations in the application:
|
||||
# - Database timestamps
|
||||
# - Usage statistics "today" boundary
|
||||
# - Subscription expiry times
|
||||
# - Log timestamps
|
||||
# Common values: Asia/Shanghai, America/New_York, Europe/London, UTC
|
||||
# =======================================================================
|
||||
- TZ=${TZ:-Asia/Shanghai}
|
||||
|
||||
# =======================================================================
|
||||
# Gemini OAuth Configuration (for Gemini accounts)
|
||||
# =======================================================================
|
||||
- GEMINI_OAUTH_CLIENT_ID=${GEMINI_OAUTH_CLIENT_ID:-}
|
||||
- GEMINI_OAUTH_CLIENT_SECRET=${GEMINI_OAUTH_CLIENT_SECRET:-}
|
||||
- GEMINI_OAUTH_SCOPES=${GEMINI_OAUTH_SCOPES:-}
|
||||
- GEMINI_QUOTA_POLICY=${GEMINI_QUOTA_POLICY:-}
|
||||
|
||||
# =======================================================================
|
||||
# Security Configuration (URL Allowlist)
|
||||
# =======================================================================
|
||||
# Enable URL allowlist validation (false to skip allowlist checks)
|
||||
- SECURITY_URL_ALLOWLIST_ENABLED=${SECURITY_URL_ALLOWLIST_ENABLED:-false}
|
||||
# Allow insecure HTTP URLs when allowlist is disabled (default: false, requires https)
|
||||
- SECURITY_URL_ALLOWLIST_ALLOW_INSECURE_HTTP=${SECURITY_URL_ALLOWLIST_ALLOW_INSECURE_HTTP:-false}
|
||||
# Allow private IP addresses for upstream/pricing/CRS (for internal deployments)
|
||||
- SECURITY_URL_ALLOWLIST_ALLOW_PRIVATE_HOSTS=${SECURITY_URL_ALLOWLIST_ALLOW_PRIVATE_HOSTS:-false}
|
||||
# Upstream hosts whitelist (comma-separated, only used when enabled=true)
|
||||
- SECURITY_URL_ALLOWLIST_UPSTREAM_HOSTS=${SECURITY_URL_ALLOWLIST_UPSTREAM_HOSTS:-}
|
||||
|
||||
# =======================================================================
|
||||
# Update Configuration (在线更新配置)
|
||||
# =======================================================================
|
||||
# Proxy for accessing GitHub (online updates + pricing data)
|
||||
# Examples: http://host:port, socks5://host:port
|
||||
- UPDATE_PROXY_URL=${UPDATE_PROXY_URL:-}
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
networks:
|
||||
- sub2api-network
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
|
||||
# ===========================================================================
|
||||
# PostgreSQL Database
|
||||
# ===========================================================================
|
||||
postgres:
|
||||
image: postgres:18-alpine
|
||||
container_name: sub2api-postgres
|
||||
restart: unless-stopped
|
||||
ulimits:
|
||||
nofile:
|
||||
soft: 100000
|
||||
hard: 100000
|
||||
volumes:
|
||||
# Local directory mapping for easy migration
|
||||
- ./postgres_data:/var/lib/postgresql/data
|
||||
environment:
|
||||
- POSTGRES_USER=${POSTGRES_USER:-sub2api}
|
||||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
|
||||
- POSTGRES_DB=${POSTGRES_DB:-sub2api}
|
||||
- PGDATA=/var/lib/postgresql/data
|
||||
- TZ=${TZ:-Asia/Shanghai}
|
||||
networks:
|
||||
- sub2api-network
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-sub2api} -d ${POSTGRES_DB:-sub2api}"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
start_period: 10s
|
||||
# 注意:不暴露端口到宿主机,应用通过内部网络连接
|
||||
# 如需调试,可临时添加:ports: ["127.0.0.1:5433:5432"]
|
||||
|
||||
# ===========================================================================
|
||||
# Redis Cache
|
||||
# ===========================================================================
|
||||
redis:
|
||||
image: redis:8-alpine
|
||||
container_name: sub2api-redis
|
||||
restart: unless-stopped
|
||||
ulimits:
|
||||
nofile:
|
||||
soft: 100000
|
||||
hard: 100000
|
||||
volumes:
|
||||
# Local directory mapping for easy migration
|
||||
- ./redis_data:/data
|
||||
command: >
|
||||
sh -c '
|
||||
redis-server
|
||||
--save 60 1
|
||||
--appendonly yes
|
||||
--appendfsync everysec
|
||||
${REDIS_PASSWORD:+--requirepass "$REDIS_PASSWORD"}'
|
||||
environment:
|
||||
- TZ=${TZ:-Asia/Shanghai}
|
||||
# REDISCLI_AUTH is used by redis-cli for authentication (safer than -a flag)
|
||||
- REDISCLI_AUTH=${REDIS_PASSWORD:-}
|
||||
networks:
|
||||
- sub2api-network
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
start_period: 5s
|
||||
|
||||
# =============================================================================
|
||||
# Networks
|
||||
# =============================================================================
|
||||
networks:
|
||||
sub2api-network:
|
||||
driver: bridge
|
||||
171
deploy/docker-deploy.sh
Normal file
171
deploy/docker-deploy.sh
Normal file
@@ -0,0 +1,171 @@
|
||||
#!/bin/bash
|
||||
# =============================================================================
|
||||
# Sub2API Docker Deployment Preparation Script
|
||||
# =============================================================================
|
||||
# This script prepares deployment files for Sub2API:
|
||||
# - Downloads docker-compose.local.yml and .env.example
|
||||
# - Generates secure secrets (JWT_SECRET, TOTP_ENCRYPTION_KEY, POSTGRES_PASSWORD)
|
||||
# - Creates necessary data directories
|
||||
#
|
||||
# After running this script, you can start services with:
|
||||
# docker-compose -f docker-compose.local.yml up -d
|
||||
# =============================================================================
|
||||
|
||||
set -e
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# GitHub raw content base URL
|
||||
GITHUB_RAW_URL="https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy"
|
||||
|
||||
# Print colored message
|
||||
print_info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# Generate random secret
|
||||
generate_secret() {
|
||||
openssl rand -hex 32
|
||||
}
|
||||
|
||||
# Check if command exists
|
||||
command_exists() {
|
||||
command -v "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# Main installation function
|
||||
main() {
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo " Sub2API Deployment Preparation"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
|
||||
# Check if openssl is available
|
||||
if ! command_exists openssl; then
|
||||
print_error "openssl is not installed. Please install openssl first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if deployment already exists
|
||||
if [ -f "docker-compose.local.yml" ] && [ -f ".env" ]; then
|
||||
print_warning "Deployment files already exist in current directory."
|
||||
read -p "Overwrite existing files? (y/N): " -r
|
||||
echo
|
||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||
print_info "Cancelled."
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Download docker-compose.local.yml
|
||||
print_info "Downloading docker-compose.local.yml..."
|
||||
if command_exists curl; then
|
||||
curl -sSL "${GITHUB_RAW_URL}/docker-compose.local.yml" -o docker-compose.local.yml
|
||||
elif command_exists wget; then
|
||||
wget -q "${GITHUB_RAW_URL}/docker-compose.local.yml" -O docker-compose.local.yml
|
||||
else
|
||||
print_error "Neither curl nor wget is installed. Please install one of them."
|
||||
exit 1
|
||||
fi
|
||||
print_success "Downloaded docker-compose.local.yml"
|
||||
|
||||
# Download .env.example
|
||||
print_info "Downloading .env.example..."
|
||||
if command_exists curl; then
|
||||
curl -sSL "${GITHUB_RAW_URL}/.env.example" -o .env.example
|
||||
else
|
||||
wget -q "${GITHUB_RAW_URL}/.env.example" -O .env.example
|
||||
fi
|
||||
print_success "Downloaded .env.example"
|
||||
|
||||
# Generate .env file with auto-generated secrets
|
||||
print_info "Generating secure secrets..."
|
||||
echo ""
|
||||
|
||||
# Generate secrets
|
||||
JWT_SECRET=$(generate_secret)
|
||||
TOTP_ENCRYPTION_KEY=$(generate_secret)
|
||||
POSTGRES_PASSWORD=$(generate_secret)
|
||||
|
||||
# Create .env from .env.example
|
||||
cp .env.example .env
|
||||
|
||||
# Update .env with generated secrets (cross-platform compatible)
|
||||
if sed --version >/dev/null 2>&1; then
|
||||
# GNU sed (Linux)
|
||||
sed -i "s/^JWT_SECRET=.*/JWT_SECRET=${JWT_SECRET}/" .env
|
||||
sed -i "s/^TOTP_ENCRYPTION_KEY=.*/TOTP_ENCRYPTION_KEY=${TOTP_ENCRYPTION_KEY}/" .env
|
||||
sed -i "s/^POSTGRES_PASSWORD=.*/POSTGRES_PASSWORD=${POSTGRES_PASSWORD}/" .env
|
||||
else
|
||||
# BSD sed (macOS)
|
||||
sed -i '' "s/^JWT_SECRET=.*/JWT_SECRET=${JWT_SECRET}/" .env
|
||||
sed -i '' "s/^TOTP_ENCRYPTION_KEY=.*/TOTP_ENCRYPTION_KEY=${TOTP_ENCRYPTION_KEY}/" .env
|
||||
sed -i '' "s/^POSTGRES_PASSWORD=.*/POSTGRES_PASSWORD=${POSTGRES_PASSWORD}/" .env
|
||||
fi
|
||||
|
||||
# Create data directories
|
||||
print_info "Creating data directories..."
|
||||
mkdir -p data postgres_data redis_data
|
||||
print_success "Created data directories"
|
||||
|
||||
# Set secure permissions for .env file (readable/writable only by owner)
|
||||
chmod 600 .env
|
||||
echo ""
|
||||
|
||||
# Display completion message
|
||||
echo "=========================================="
|
||||
echo " Preparation Complete!"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
echo "Generated secure credentials:"
|
||||
echo " POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}"
|
||||
echo " JWT_SECRET: ${JWT_SECRET}"
|
||||
echo " TOTP_ENCRYPTION_KEY: ${TOTP_ENCRYPTION_KEY}"
|
||||
echo ""
|
||||
print_warning "These credentials have been saved to .env file."
|
||||
print_warning "Please keep them secure and do not share publicly!"
|
||||
echo ""
|
||||
echo "Directory structure:"
|
||||
echo " docker-compose.local.yml - Docker Compose configuration"
|
||||
echo " .env - Environment variables (generated secrets)"
|
||||
echo " .env.example - Example template (for reference)"
|
||||
echo " data/ - Application data (will be created on first run)"
|
||||
echo " postgres_data/ - PostgreSQL data"
|
||||
echo " redis_data/ - Redis data"
|
||||
echo ""
|
||||
echo "Next steps:"
|
||||
echo " 1. (Optional) Edit .env to customize configuration"
|
||||
echo " 2. Start services:"
|
||||
echo " docker-compose -f docker-compose.local.yml up -d"
|
||||
echo ""
|
||||
echo " 3. View logs:"
|
||||
echo " docker-compose -f docker-compose.local.yml logs -f sub2api"
|
||||
echo ""
|
||||
echo " 4. Access Web UI:"
|
||||
echo " http://localhost:8080"
|
||||
echo ""
|
||||
print_info "If admin password is not set in .env, it will be auto-generated."
|
||||
print_info "Check logs for the generated admin password on first startup."
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user